メインコンテンツにスキップ

RBAC API

RBACに対するよりフレンドリーなAPI。 このAPIはManagement APIの一部です。 RBACのユーザーはこのAPIを使用してコードを簡略化できます。

参照

グローバル変数 e はEnforcerインスタンスです。

e, err := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")

GetRolesForUser()

GetRolesForUserはユーザーが持っている役割を取得します。

例えば:

res := e.GetRolesForUser("alice")

GetUsersForRole()

GetUsersForRoleは役割を持っているユーザーを取得します。

例えば:

res := e.GetUsersForRole("data1_admin")

HasRoleForUser()

HasRoleForUserはユーザーが役割を持っているかどうかを判断します。

例えば:

res := e.HasRoleForUser("alice", "data1_admin")

AddRoleForUser()

AddRoleForUserはユーザーに役割を追加します。 ユーザーがすでにその役割を持っている場合はfalseを返します(つまり、影響を受けません)。

例えば:

e.AddRoleForUser("alice", "data2_admin")

AddRolesForUser()

AddRolesForUserはユーザーに複数の役割を追加します。 ユーザーがすでにこれらの役割のいずれかを持っている場合はfalseを返します(つまり、影響を受けません)。

例えば:

var roles = []string{"data2_admin", "data1_admin"}
e.AddRolesForUser("alice", roles)

DeleteRoleForUser()

DeleteRoleForUserはユーザーの役割を削除します。 ユーザーがその役割を持っていない場合はfalseを返します(つまり、影響を受けません)。

例えば:

e.DeleteRoleForUser("alice", "data1_admin")

DeleteRolesForUser()

DeleteRolesForUserはユーザーのすべての役割を削除します。 ユーザーが役割を一つも持っていない場合はfalseを返します(つまり、影響を受けません)。

例えば:

e.DeleteRolesForUser("alice")

DeleteUser()

DeleteUserはユーザーを削除します。 ユーザーが存在しない場合はfalseを返します(つまり、影響を受けません)。

例えば:

e.DeleteUser("alice")

DeleteRole()

DeleteRoleは役割を削除します。

例えば:

e.DeleteRole("data2_admin")

DeletePermission()

DeletePermissionは権限を削除します。 権限が存在しない場合(つまり、影響を受けない場合)はfalseを返します。

例えば:

e.DeletePermission("read")

AddPermissionForUser()

AddPermissionForUserは、ユーザーまたはロールに対する権限を追加します。 ユーザーまたはロールがすでにその権限を持っている場合(つまり、影響を受けない場合)はfalseを返します。

例えば:

e.AddPermissionForUser("bob", "read")

AddPermissionsForUser()

AddPermissionsForUserは、ユーザーまたはロールに対して複数の権限を追加します。 ユーザーまたはロールがすでにその権限のうちの1つを持っている場合(つまり、影響を受けない場合)はfalseを返します。

例えば:

var permissions = [][]string{{"data1", "read"},{"data2","write"}}
for i := 0; i < len(permissions); i++ {
e.AddPermissionsForUser("alice", permissions[i])
}

DeletePermissionForUser()

DeletePermissionForUserは、ユーザーまたはロールの権限を削除します。 ユーザーまたはロールがその権限を持っていない場合(つまり、影響を受けない場合)はfalseを返します。

例えば:

e.DeletePermissionForUser("bob", "read")

DeletePermissionsForUser()

DeletePermissionsForUserは、ユーザーまたはロールの権限を削除します。 ユーザーまたはロールが何の権限も持っていない場合(つまり、影響を受けない場合)はfalseを返します。

例えば:

e.DeletePermissionsForUser("bob")

GetPermissionsForUser()

GetPermissionsForUserは、ユーザーまたはロールの権限を取得します。

例えば:

e.GetPermissionsForUser("bob")

HasPermissionForUser()

HasPermissionForUserは、ユーザーが権限を持っているかどうかを判断します。

例えば:

e.HasPermissionForUser("alice", []string{"read"})

GetImplicitRolesForUser()

GetImplicitRolesForUserは、ユーザーが持っている暗黙的なロールを取得します。 GetRolesForUser()と比較して、この関数は直接的なロールに加えて間接的なロールを取得します。

例えば:

g, alice, role:admin  
g, role:admin, role:user

GetRolesForUser("alice")は、["role:admin"]のみを取得できます。\ しかし、GetImplicitRolesForUser("alice")は、["role:admin", "role:user"]を取得します。

例えば:

e.GetImplicitRolesForUser("alice")

GetImplicitUsersForRole()

GetImplicitUsersForRoleは、そのロールを継承するすべてのユーザーを取得します。 GetUsersForRole()と比較して、この関数は間接的なユーザーを取得します。

例えば:

g, alice, role:admin  
g, role:admin, role:user

GetUsersForRole("role:user")は、["role:admin"]のみを取得できます。\ But GetImplicitUesrsForRole("role:user") will get: ["role:admin", "alice"].

For example:

users := e.GetImplicitUsersForRole("role:user")

GetImplicitPermissionsForUser()

GetImplicitPermissionsForUser gets implicit permissions for a user or role.\ Compared to GetPermissionsForUser(), this function retrieves permissions for inherited roles.

For example:

p, admin, data1, read  
p, alice, data2, read
g, alice, admin

GetPermissionsForUser("alice") can only get: [["alice", "data2", "read"]].\ But GetImplicitPermissionsForUser("alice") will get: [["admin", "data1", "read"], ["alice", "data2", "read"]].

For example:

e.GetImplicitPermissionsForUser("alice")

GetNamedImplicitPermissionsForUser()

GetNamedImplicitPermissionsForUser gets implicit permissions for a user or role by named policy Compared to GetImplicitPermissionsForUser(), this function allow you to specify the policy name.

For example:

p, admin, data1, read
p2, admin, create
g, alice, admin

GetImplicitPermissionsForUser("alice") only get: [["admin", "data1", "read"]], whose policy is default "p"

But you can specify the policy as "p2" to get: [["admin", "create"]] by GetNamedImplicitPermissionsForUser("p2","alice")

For example:

e.GetNamedImplicitPermissionsForUser("p2","alice")

GetDomainsForUser()

GetDomainsForUser gets all domains which a user has.

For example:

p, admin, domain1, data1, read
p, admin, domain2, data2, read
p, admin, domain2, data2, write
g, alice, admin, domain1
g, alice, admin, domain2

GetDomainsForUser("alice") could get ["domain1", "domain2"]

For example:

result, err := e.GetDomainsForUser("alice")

GetImplicitResourcesForUser()

GetImplicitResourcesForUser returns all policies that should be true for user.

For example:

p, alice, data1, read
p, bob, data2, write
p, data2_admin, data2, read
p, data2_admin, data2, write

g, alice, data2_admin

GetImplicitResourcesForUser("alice") will return [[alice data1 read] [alice data2 read] [alice data2 write]]

resources, err := e.GetImplicitResourcesForUser("alice")

GetImplicitUsersForPermission()

GetImplicitUsersForPermission gets implicit users for a permission.

For example:

p, admin, data1, read
p, bob, data1, read
g, alice, admin

GetImplicitUsersForPermission("data1", "read") will return: ["alice", "bob"].

Note: only users will be returned, roles (2nd arg in "g") will be excluded.

users, err := e.GetImplicitUsersForPermission("data1", "read")

GetAllowedObjectConditions()

GetAllowedObjectConditions returns a string array of object conditions that the user can access.

For example:

p, alice, r.obj.price < 25, read
p, admin, r.obj.category_id = 2, read
p, bob, r.obj.author = bob, write

g, alice, admin

e.GetAllowedObjectConditions("alice", "read", "r.obj.") will return ["price < 25", "category_id = 2"], nil

Note:

  1. prefix: オブジェクト条件のプレフィックスをカスタマイズでき、一般的に「r.obj.」がプレフィックスとして使用されます。 プレフィックスを削除した残りの部分はオブジェクトの条件です。 プレフィックスの要件を満たさないobjポリシーがある場合、errors.ERR_OBJ_CONDITIONが返されます。

  2. 'objectConditions'配列が空の場合、errors.ERR_EMPTY_CONDITIONを返します。このエラーは、一部のデータアダプタのORMが空の条件を受け取るとデフォルトで全テーブルデータを返すため、期待とは逆の動作をする傾向があるため返されます(例:GORM)。このような動作をしないアダプタを使用している場合、このエラーを無視することを選択できます。

conditions, err := e.GetAllowedObjectConditions("alice", "read", "r.obj.")

GetImplicitUsersForResource()

GetImplicitUsersForResourceはリソースに基づいて暗黙的なユーザーを返します。

例えば:

p, alice, data1, read
p, bob, data2, write
p, data2_admin, data2, read
p, data2_admin, data2, write
g, alice, data2_admin

GetImplicitUsersForResource("data2")は[["bob", "data2", "write"], ["alice", "data2", "read"] ["alice", "data2", "write"]], nilを返します。

GetImplicitUsersForResource("data1")は[["alice", "data1", "read"]], nilを返します。

ImplicitUsers, err := e.GetImplicitUsersForResource("data2")
メモ

ユーザーのみが返され、ロール("g"の2番目の引数)は除外されます。