Permalink
Browse files

Adding many_to_many mapping for user roles. Adding has_role function …

…which can be called to check roles in the db
  • Loading branch information...
1 parent cdd6143 commit 523eacfa99f2302dfff7cd59846653a6faca704a @idnorton idnorton committed Dec 7, 2011
Showing with 22 additions and 0 deletions.
  1. +22 −0 lib/PresentingPerl/Schema/Result/User.pm
View
22 lib/PresentingPerl/Schema/Result/User.pm
@@ -101,12 +101,34 @@ __PACKAGE__->has_many(
# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-12-03 14:51:10
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:LdcXqKuNwSPcZp4qrNP8mw
+# many_to_many():
+# args:
+# 1) Name of relationship, DBIC will create accessor with this name
+# 2) Name of has_many() relationship this many_to_many() is shortcut for
+# 3) Name of belongs_to() relationship in model class of has_many() above
+# You must already have the has_many() defined to use a many_to_many().
+__PACKAGE__->many_to_many(roles => 'user_roles', 'role');
+
sub check_password {
my ($self, $passp) = @_;
return $self->password->match($passp);
}
+=head2 has_role
+
+Check if a user has the specified role
+
+=cut
+
+use Perl6::Junction qw/any/;
+sub has_role {
+ my ($self, $role) = @_;
+
+ # Does this user posses the required role?
+ return any(map { $_->role } $self->roles) eq $role;
+}
+
# You can replace this text with custom code or comments, and it will be preserved on regeneration
__PACKAGE__->meta->make_immutable;
1;

0 comments on commit 523eacf

Please sign in to comment.