FAQ
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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20110516/04918962/attachment.htm

Search Discussions

  • Jason Galea at May 17, 2011 at 2:05 pm
    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 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: Catalyst@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/
  • Adam Jimerson at May 17, 2011 at 3:45 pm
    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 wrote:

    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 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: Catalyst@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@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/9223ee6c/attachment.htm
  • Jason Galea at May 17, 2011 at 9:54 pm

    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?
    just stash the user and call roles on it as you did with the "current" user.
    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 wrote:

    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 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: Catalyst@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@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@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/


    --
    "don't treat 'em like they'll always be around.." me, 5/4/11

    Jason Galea
    Web Developer

    Ph 07 40556926
    Mob 04 12345 534
    www.eightdegrees.com.au

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedMay 17, '11 at 1:10a
activeMay 17, '11 at 9:54p
posts4
users2
websitecatalystframework.org
irc#catalyst

2 users in discussion

Jason Galea: 2 posts Adam Jimerson: 2 posts

People

Translate

site design / logo © 2022 Grokbase