[Catalyst] Question on user role management

Adam Jimerson vendion at gmail.com
Tue May 17 01:10:53 GMT 2011


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


More information about the Catalyst mailing list