Hi Adam,

have you tried using the roles method on the other users?

        my $user = $c->stash->{users_rs}->find({ uniqid => $uniqid });
        die "No such user: $uniqid\n" if (!$user);
        my @roles = $user->roles;

do you have a many_to_many defined in your user table class? eg

__PACKAGE__->has_many(user2role_maps => 'MyApp::DB::Result::User2Role', 'user');
__PACKAGE__->many_to_many(roles => '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 <[email protected]> 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/Manual/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 make
> 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 = 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 ) = @_;
>       
>       $c->stash( users_rs => $c->model('DB::User'));
>       $c->stash( role_rs => $c->model('DB::Role'));
>       $c->stash( usertorole_rs => $c->model('DB::Userstorole'));
> }
>
> sub user : Chained('base'): CaptureArgs(1) {
>       my ( $self, $c, $uniqid ) = @_;
>
>       if ( $uniqid == m/[^0-9]/ ) {
>               die "The ID number is not numeric\n";
>       }
>       my $user = $c->stash->{users_rs}->find({ uniqid => $uniqid });
>       die "No such user: $uniqid\n" if (!$user);
>       my $roles = $c->stash->{usertorole_rs}->search(
>               undef,
>               {
>                       where => { 'userid', $uniqid }
>               },
>       );
>       warn "No such role: $uniqid\n" if (!$roles);
>       $c->stash(user => $user,
>               roles => $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: [email protected]
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/[email protected]/
> Dev site: http://dev.catalyst.perl.org/
>
>

_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/

Reply via email to