Re: Controller filter parameters

Manfred Stienstra
Tue, 03 Nov 2009 11:35:43 -0000

Hi Rodrigo,

Here is an example of what you probably want:

  class User
    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 < ApplicationController
    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 you restart this discussion on the Rails Talk list and CC Eloy and
me? This list is meant for discussing Rails core development.

Thanks,
 Manfred

On Nov 3, 11:16 am, Rodrigo Rosenfeld Rosas <rr.ro...@gmail.com>
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.