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