[Catalyst] Question on user role management

Adam Jimerson vendion at gmail.com
Tue May 17 15:45:08 GMT 2011


Hi Jason,

No I didn't even think that I could get that information that way, still
very new to Catalyst/MVC/OO programing.  I will give that a try, because it
is being stored into an array in your example I would still need to put it
in a stash and go through it with a FOR or FOREACH loop in my view correct?

Also yes I do have a many_to_many relationship in my user.pm for roles.

On Tue, May 17, 2011 at 10:05 AM, Jason Galea <lists at eightdegrees.com.au>wr=
ote:

> Hi Adam,
>
> have you tried using the roles method on the other users?
>
>        my $user =3D $c->stash->{users_rs}->find({ uniqid =3D> $uniqid });
>        die "No such user: $uniqid\n" if (!$user);
>         my @roles =3D $user->roles;
>
> do you have a many_to_many defined in your user table class? eg
>
> __PACKAGE__->has_many(user2role_maps =3D> 'MyApp::DB::Result::User2Role',
> 'user');
> __PACKAGE__->many_to_many(roles =3D> 'user2role_maps', 'role');
>
> I'm guessing you do as c.user.roles works..
>
> cheers,
>
> J
>
> On Tue, May 17, 2011 at 11:10 AM, Adam Jimerson <vendion at gmail.com> wrote:
> > I am trying to come up with a way to manage roles for users in my
> Catalyst
> > app, I have a database structure much like what is used in Chapter 5 of
> the
> > Catalyst::Manual::Tutorial
> > <
> http://search.cpan.org/~bobtfish/Catalyst-Manual-5.8007/lib/Catalyst/Manu=
al/Tutorial/05_Authentication.pod
> >
> > where I have a user table, a role table, and and a usertorole table.  I
> am
> > trying to find a way to get a list of roles for a user to be able to ma=
ke
> > changes, add new roles and/or remove roles from the user.  I have both
> > authentication and authorization working in my app and I can fetch the
> roles
> > for the user currently logged in by
> > <ul>
> > [% FOR role =3D c.user.roles %]<li>[% role %]</li>[% END %]
> > </ul>
> > But when I try to get a list from a different user it doesn't work as
> > expected, here is what I am currently doing
> >
> > sub base : Chained('/'): PathPart('admin') :CaptureArgs(0) {
> >       my ( $self, $c ) =3D @_;
> >
> >       $c->stash( users_rs =3D> $c->model('DB::User'));
> >       $c->stash( role_rs =3D> $c->model('DB::Role'));
> >       $c->stash( usertorole_rs =3D> $c->model('DB::Userstorole'));
> > }
> >
> > sub user : Chained('base'): CaptureArgs(1) {
> >       my ( $self, $c, $uniqid ) =3D @_;
> >
> >       if ( $uniqid =3D=3D m/[^0-9]/ ) {
> >               die "The ID number is not numeric\n";
> >       }
> >       my $user =3D $c->stash->{users_rs}->find({ uniqid =3D> $uniqid });
> >       die "No such user: $uniqid\n" if (!$user);
> >       my $roles =3D $c->stash->{usertorole_rs}->search(
> >               undef,
> >               {
> >                       where =3D> { 'userid', $uniqid }
> >               },
> >       );
> >       warn "No such role: $uniqid\n" if (!$roles);
> >       $c->stash(user =3D> $user,
> >               roles =3D> $roles);
> > }
> >
> > [% FOR role IN roles %]
> >               <tr><td>Role #:</td><td>Role [% role.role %] Role ID [%
> role.roleid %]
> > User id [% role.userid %]</td></tr>
> > [% END %]
> >
> > My database schema is so
> >
> > CREATE TABLE roles (
> >     uniqid integer NOT NULL,
> >     role character varying(32) NOT NULL
> > );
> >
> > CREATE TABLE users (
> >     uniqid integer NOT NULL,
> >     username character varying(20) NOT NULL,
> >     password character varying(40) NOT NULL,
> >     firstname character varying(20) NOT NULL,
> >     lastname character varying(20) NOT NULL,
> >     email character varying(20) NOT NULL,
> >     active boolean DEFAULT true NOT NULL,
> >     created timestamp without time zone DEFAULT now() NOT NULL
> > );
> >
> > CREATE TABLE userstoroles (
> >     userid integer NOT NULL,
> >     role integer NOT NULL
> > );
> >
> > Am I going about this the wrong way or is there something that I am over
> > looking?
> >
> > _______________________________________________
> > List: Catalyst at lists.scsys.co.uk
> > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> > Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> > Dev site: http://dev.catalyst.perl.org/
> >
> >
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20110517/9223e=
e6c/attachment.htm


More information about the Catalyst mailing list