| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Realm::GenericPrincipal; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::Realm::GenericPrincipal - generic security principal class | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Generic implementation of B<Servlet::Util::Principal> that is | ||
| 16 | available for use by B<Wombat::Realm> implementations. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | use base qw(Servlet::Util::Principal); | ||
| 21 | use fields qw(name password realm roles); | ||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | |||
| 25 | =pod | ||
| 26 | |||
| 27 | =head1 CONSTRUCTOR | ||
| 28 | |||
| 29 | =over | ||
| 30 | |||
| 31 | =item new() | ||
| 32 | |||
| 33 | Construct and return a B<Wombat::Realm::GenericPrincipal> instance, | ||
| 34 | initializing fields appropriately. If subclasses override the | ||
| 35 | constructor, they must be sure to call | ||
| 36 | |||
| 37 | $self->SUPER::new(); | ||
| 38 | |||
| 39 | =back | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub new { | ||
| 44 | my $self = shift; | ||
| 45 | my $realm = shift; | ||
| 46 | my $name = shift; | ||
| 47 | my $password = shift; | ||
| 48 | my $roles = shift; | ||
| 49 | |||
| 50 | $self = fields::new($self) unless ref $self; | ||
| 51 | |||
| 52 | $self->{name} = $name; | ||
| 53 | $self->{password} = $password; | ||
| 54 | $self->{realm} = $realm; | ||
| 55 | $self->{roles} = $roles && ref $roles eq 'ARRAY' ? | ||
| 56 | { map { $_ => 1 } @$roles } : | ||
| 57 | {}; | ||
| 58 | |||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | =pod | ||
| 63 | |||
| 64 | =head1 ACCESSOR METHODS | ||
| 65 | |||
| 66 | =over | ||
| 67 | |||
| 68 | =item getName() | ||
| 69 | |||
| 70 | Return the username of the user represented by this Principal. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub getName { | ||
| 75 | my $self = shift; | ||
| 76 | |||
| 77 | return $self->{name}; | ||
| 78 | } | ||
| 79 | |||
| 80 | =pod | ||
| 81 | |||
| 82 | =item getPassword() | ||
| 83 | |||
| 84 | Return the authentication credentials for the user represented by this | ||
| 85 | Principal. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub getPassword { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | return $self->{password}; | ||
| 93 | } | ||
| 94 | |||
| 95 | =pod | ||
| 96 | |||
| 97 | =item getRealm() | ||
| 98 | |||
| 99 | Return the Realm with which this Principal is associated. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub getRealm { | ||
| 104 | my $self = shift; | ||
| 105 | |||
| 106 | return $self->{realm}; | ||
| 107 | } | ||
| 108 | |||
| 109 | =pod | ||
| 110 | |||
| 111 | =item getRoles() | ||
| 112 | |||
| 113 | Return the array of roles associated with this user. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub getRoles { | ||
| 118 | my $self = shift; | ||
| 119 | |||
| 120 | my $roles = sort keys %{ $self->{roles} }; | ||
| 121 | |||
| 122 | return wantarray ? @$roles : $roles; | ||
| 123 | } | ||
| 124 | |||
| 125 | =pod | ||
| 126 | |||
| 127 | =back | ||
| 128 | |||
| 129 | =head1 PUBLIC METHODS | ||
| 130 | |||
| 131 | =over | ||
| 132 | |||
| 133 | =item hasRole($role) | ||
| 134 | |||
| 135 | Return true if this principal possesses the specified role, or false | ||
| 136 | otherwise. | ||
| 137 | |||
| 138 | B<Parameters:> | ||
| 139 | |||
| 140 | =over | ||
| 141 | |||
| 142 | =item $role | ||
| 143 | |||
| 144 | the name of the security role to be checked | ||
| 145 | |||
| 146 | =back | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub hasRole { | ||
| 151 | my $self = shift; | ||
| 152 | my $role = shift; | ||
| 153 | |||
| 154 | return $self->{roles}->{$role}; | ||
| 155 | } | ||
| 156 | |||
| 157 | =pod | ||
| 158 | |||
| 159 | =back | ||
| 160 | |||
| 161 | =head1 SEE ALSO | ||
| 162 | |||
| 163 | B<Servlet::Util::Principal>, | ||
| 164 | B<Wombat::Realm> | ||
| 165 | |||
| 166 | =head1 AUTHOR | ||
| 167 | |||
| 168 | Brian Moseley, bcm@maz.org | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | 1; | ||
| 173 | __END__ |