| 1 | package RWDE::LoginUser; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use RWDE::Configuration; | ||
| 7 | |||
| 8 | use Error qw(:try); | ||
| 9 | use RWDE::Exceptions; | ||
| 10 | use RWDE::RObject; | ||
| 11 | |||
| 12 | use vars qw($VERSION); | ||
| 13 | $VERSION = sprintf "%d", q$Revision: 507 $ =~ /(\d+)/; | ||
| 14 | |||
| 15 | =pod | ||
| 16 | |||
| 17 | =head1 RWDE::LoginUser | ||
| 18 | |||
| 19 | Authentication class to facilitate logging in users. It is assumed that there is a primary field that a user | ||
| 20 | can be retrieved by, and a single password field upon which they authenticate with. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | =head2 Authenticate() | ||
| 25 | |||
| 26 | Given the appropriate parameters (lookup field,lookup value) the desired record | ||
| 27 | is fetched from the database and the authentication method is executed. | ||
| 28 | |||
| 29 | Currently this method only really works when you are looking up by the class id, | ||
| 30 | although there are plans to make this more flexible. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | sub Authenticate { | ||
| 35 | my ($self, $params) = @_; | ||
| 36 | |||
| 37 | # If the method is not called in the instance context | ||
| 38 | # the system has to do a lookup for the instance first | ||
| 39 | RWDE::RObject->check_params({ required => ['lookup_value', 'password'], supplied => $params }); | ||
| 40 | |||
| 41 | my $lookup = $$params{lookup} || $self->get_lookup_field(); | ||
| 42 | |||
| 43 | my $term; | ||
| 44 | |||
| 45 | #TODO replace these with one single call to a collective method that just knows what to do | ||
| 46 | if ($lookup eq 'admin_login') { | ||
| 47 | $term = $self->fetch_by_admin_login({ $lookup => $$params{lookup_value} }); | ||
| 48 | } | ||
| 49 | elsif ($lookup eq 'login_email') { | ||
| 50 | $term = $self->fetch_by_login_email({ $lookup => $$params{lookup_value}}); | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | $term = $self->fetch_by_id({ $lookup => $$params{lookup_value}}); | ||
| 54 | } | ||
| 55 | |||
| 56 | $term->authenticate($params); | ||
| 57 | |||
| 58 | return $term; | ||
| 59 | } | ||
| 60 | |||
| 61 | =head2 authenticate() | ||
| 62 | |||
| 63 | Given a login capable object, attempt to authenticate the user represented by the object. | ||
| 64 | |||
| 65 | -Verify that the object is an instance | ||
| 66 | -Check the password and ensure it is correct | ||
| 67 | -Check the status and make sure the user is permitted to login | ||
| 68 | |||
| 69 | If any criteria fail the appropriate exception is thrown and should be caught and handled properly | ||
| 70 | |||
| 71 | If all of these criteria are met the lastlogin field is updated to represent this successful login | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub authenticate { | ||
| 76 | my ($self, $params) = @_; | ||
| 77 | |||
| 78 | $self->check_object(); | ||
| 79 | |||
| 80 | $self->check_password({ password => $$params{password} }); | ||
| 81 | |||
| 82 | $self->check_status(); | ||
| 83 | |||
| 84 | $self->lastlogin(RWDE::Time->now()); | ||
| 85 | |||
| 86 | $self->update_record(); | ||
| 87 | |||
| 88 | return; | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 check_password | ||
| 92 | |||
| 93 | Returns true if the 'password' stored in the params hash | ||
| 94 | matches that of the current record, false for failed match | ||
| 95 | or throws an exception otherwise. | ||
| 96 | |||
| 97 | BadPasswordException is thrown if the password is invalid | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub check_password { | ||
| 102 | my ($self, $params) = @_; | ||
| 103 | |||
| 104 | if (!defined($$params{password})) { | ||
| 105 | throw RWDE::BadPasswordException({ info => "$self supplied incorrect password" }); | ||
| 106 | } | ||
| 107 | |||
| 108 | elsif ($self->get_password() ne $$params{password}) { | ||
| 109 | throw RWDE::BadPasswordException({ info => "$self supplied incorrect password" }); | ||
| 110 | } | ||
| 111 | |||
| 112 | return (); | ||
| 113 | } | ||
| 114 | |||
| 115 | =head2 generate_randpass | ||
| 116 | |||
| 117 | Generate a random string that is 8 characters long. This is useful for assigning | ||
| 118 | random passwords to new users, after which you require them to change it after first | ||
| 119 | login. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub generate_randpass { | ||
| 124 | my ($self, $params) = @_; | ||
| 125 | |||
| 126 | my $passwordsize = 8; | ||
| 127 | my @alphanumeric = ('a' .. 'z', 'A' .. 'Z', 0 .. 9); | ||
| 128 | my $randpassword = join '', map { $alphanumeric[ rand @alphanumeric ] } 0 .. $passwordsize; | ||
| 129 | |||
| 130 | return $randpassword; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 |