how do you all store the roles? #246
-
currently I have an array field on my model and using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Usually, I design the authorization model on top of permissions and roles. Permissions are granular and describe business logic restrictions, e.g., "manage_projects", "view_projects", "manage_users", "view_users", etc. Roles act as permission sets and only used to assign typical permissions for users. Permissions can be stored in an array or JSONB field (I prefer the latter): class User < ApplicationRecord
attribute :permissions, Permissions::Type.new
end
User.new.permissions #=> Permissions::Set
# Roles only used to synchronize permissions
user.permissions = role.permissions In policies, we check for permissions, not roles: class ApplicationPolicy < ActionPolicy::Base
# add permissions accessor
delegate :permissions, to: :user
# allow everything to adnins by default
pre_check :allow_admins!
private
def allow_admins!
allow! if admin?
end
end
class UserPolicy < ApplicationPolicy
pre_check :ensure_same_account!, only: :show?
def show?
permissions.view_users?
end
private
def ensure_same_account!
deny! unless record.account_id == user.account_id
end
end |
Beta Was this translation helpful? Give feedback.
-
any example of controller codes for roles modification that using jsonb? I
also wonder how the strong parameters and the views looks like
…On Thu, May 18, 2023, 6:52 AM Vladimir Dementyev ***@***.***> wrote:
Usually, I design the authorization model on top of permissions and roles.
Permissions are granular and describe business logic restrictions, e.g.,
"manage_projects", "view_projects", "manage_users", "view_users", etc.
Roles act as permission sets and only used to assign typical permissions
for users.
Permissions can be stored in an array or JSONB field (I prefer the latter):
class User < ApplicationRecord
attribute :permissions, Permissions::Type.newend
User.new.permissions #=> Permissions::Set
# Roles only used to synchronize permissionsuser.permissions = role.permissions
In policies, we check for permissions, not roles:
class ApplicationPolicy < ActionPolicy::Base
# add permissions accessor
delegate :permissions, to: :user
# allow everything to adnins by default
pre_check :allow_admins!
private
def allow_admins!
allow! if admin?
endend
class UserPolicy < ApplicationPolicy
pre_check :ensure_same_account!, only: :show?
def show?
permissions.view_users?
end
private
def ensure_same_account!
deny! unless record.account_id == user.account_id
endend
—
Reply to this email directly, view it on GitHub
<#246 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKKFOTN4ICJ3WHAVIH5SVZLXGVQDLANCNFSM6AAAAAAX4PA3DU>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
Usually, I design the authorization model on top of permissions and roles. Permissions are granular and describe business logic restrictions, e.g., "manage_projects", "view_projects", "manage_users", "view_users", etc. Roles act as permission sets and only used to assign typical permissions for users.
Permissions can be stored in an array or JSONB field (I prefer the latter):
In policies, we check for permissions, not roles: