ruby on rails - dynamic cancan from database with complex conditions -


i'm trying define user created roles select permissions list of permissions. want permission this:

def initialize(user)   user.projects_users.each |project_user|     project_user.role.privileges |privilege|       can :create, projectsuser, :project_id => project_user.project_id     end   end end 

but i'm trying save privileges in database in such way can outcome above

def initialize(user)   user.projects_users.each |project_user|     project_user.role.privileges |privilege|       can privilege.action.to_sym, privilege.subject_class.constantize, privilege.conditions     end   end end 

the problem lies in 'privilege.conditions' part. cannot store condition must executed in ability.rb file. if try store:

{ :project_id => project_user.project_id } 

it there no variable named 'project_user'. save string , in ability file eval(privilege.condition), need on values. tried this:

def initialize(user)   user.projects_users.each |project_user|     project_user.role.privileges |privilege|       can privilege.action.to_sym, privilege.subject_class.constantize, privilege.conditions.each |subject, id|         subject => eval(id)       end     end   end end 

the error i'm getting 'syntax error, unexpected =>, expecting keyword_end' 'subject =>' piece.

not sure how exactly...

i'm using line of commands test it:

@user_id = 4 @role = role.create(name: "tester", project_id: 4) @priv = privilege.create(:action => :create, :subject_class => 'projectsuser', :conditions => { :project_id => 'project_user.project_id' }) @role.privileges << @priv @project_user = projectsuser.create(:user_id => @user_id, :role_id => @role.id, :project_id => @role.project_id) @a = ability.new(user.find(@user_id)) @a.can?(:create, projectsuser.new(:user_id => @user_id + 1, :role_id => @role.id, :project_id => @role.project_id)) 

any advice?

ok found easy work around. block on conditions not being evaluated correctly. here's working code:

user.projects_users.each |project_user|   project_user.role.privileges.each |privilege|     can privilege.action.to_sym, privilege.subject_class.constantize, hash[privilege.conditions.map {|subject, condition| [subject, eval(condition)] }]   end end 

notice hash[privilege.conditions.map {|subject, condition| [subject, eval(condition)] }]

what doing taking symbol key in conditions such :subject_id , mapping evaluated condition, evaluated particular id.

in model have

class privilege < activerecord::base     has_and_belongs_to_many :roles    serialize :conditions, hash end 

and example model is:

privilege.create(   :action => :create,    :subject_class => 'projectsuser',    :conditions => { :project_id => 'project_user.project_id' } ) 

update

this method works conditions 1 level deep. condition not work. a: typeerror: no implicit conversion of hash string

:conditions => {    :project => {      :location_id => 'project_user.project.location_id'   } } 

this not best solution, work around is

:conditions => {    :project => "{      :location_id => eval(\"project_user.project.location_id'\")   }" } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -