Re: [off-topic] Re: Re: Controller filter parameters

Eloy Duran
Tue, 03 Nov 2009 14:14:21 -0000

Hi Rodrigo,

The boolean accessors on the User model could probably be refactored,
but you get the idea.

class User < ActiveRecord::Base
   has_many :roles

   def institution_admin?
     roles.any? { |r| r.label == ''institution_admin'' }
   end

   def system_admin?
     roles.any? { |r| r.label == ''system_admin'' }
   end
end

class InstitutionController
   allow_accesss(:system_admin)
   allow_accesss(:institution_admin) do
     @authenticated.institution == @institution
   end

   prepend_before_filter :find_institution

   private

   def find_institution
     @institution = Institution.find(params[:id])
   end
end

Can we continue this discussion on Ruby on Rails: Talk? This list is
meant for discussions about Ruby on Rails core development.

Manfred

Sent from my colleagues'' Mac pro.

On Nov 3, 2009, at 11:16 AM, Rodrigo Rosenfeld Rosas wrote:

>
> Em 02-11-2009 15:05, Eloy Duran escreveu:
>> Hey Rodrigo,
>>
>>
>>> Hi Eloy, thank you for your suggestion, but I don''t think
it would
>>> satisfy my needs...
>>>
>>> In my case, a user has a role that can be attached to some
>>> conditions.
>>> For instance, the user ''manager'' has a role
''institution_admin'' only
>>> for
>>> institution ''manager_institution''...
>>>
>>
>> I don''t completely follow the explanation of the example, but
that
>> would probably be easy with authorization-san. It already supports
>> the
>> idea of ''role'' on an object.
>> In all the projects we have used it, we haven''t found one
scenario
>> that we couldn''t solve.
>>
>> class InstitutionsController<  ActionController::Base
>>    allow_access :institution_admin do
>>      # perform any checks and return truthy or falsy value
>>    end
>> end
>>
> I still can''t figure out how would be the complete use case with
> authorization-san.
>
> Let me put the examples in more detail. In my project, users have
> roles,
> which have permissions, as usual.
>
> But some roles are attached to some condition. In a role
> ''institution_admin'', a user should be attached to some
specific
> existent
> institution.
>
> But if a user belongs to ''system_admin'' role, for
instance, it
> shouldn''t
> be attached to any conditions.
>
> I have in User:
>
> has_many :roles, :through => :assignments
>
> And in Assignment, there is ''user_id'',
''role_id'' and an integer
> ''condition'' that could be null. The roles are fixed and I
check that
> condition is filled in correctly depending on the role.
>
> There is a hash that maps the expected condition class to each role.
>
> If you think I could do the same with authorization-san, I would be
> glad
> to see a more in-depth example.
>
> Thank you,
>
> Rodrigo.
>
>
> >