i am working through with a light weight app that i want to implement a super simple role structure. currently i am working with enum to set the role, but i want to implement some sort of hierarchy.
enum role: [:registered_user, :active_registered_user, :admin, :account_admin, :vip]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :registered_user
end
This is how i define the roles on the users model, what i would like to do is have it so that registered_user < active_registered_user < admin < account_admin < vip.
So pretty much they have access to all the roles beneath them.
So if something said if current_user.admin? if i was an account_admin or vip it would return true.
I was thinking of doing some thing like this
def has_access?(user, access_role)
access_hash = {
"vip" => ['vip', 'account_admin', 'admin', 'active_registered_user', 'registered_user'],
"account_admin" => ['account_admin', 'admin', 'active_registered_user', 'registered_user'],
"admin" => ['admin', 'active_registered_user', 'registered_user'],
"active_registered_user" => ['active_registered_user', 'registered_user'],
"registered_user" => ['registered_user']
}
access_hash.[user.role].include?(access_role)
end
But then i have to run this method everywhere! is there a more ruby way of doing this?
Any help or design insights will be much appreciated.
Note:
enum allows for some cool active record calls:
user.admin! # sets the role to "admin"
user.admin? # => true
user.role # => "admin"
just if you wanted to see how i called user.role.
Aucun commentaire:
Enregistrer un commentaire