« blib/lib/Lemonldap/NG/Portal/Password/AD.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/UserDB/LDAP.pm »
| 1 | package Lemonldap::NG::Portal::Password::LDAP; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 6 | PE_ERROR | ||
| 7 | PE_LDAPERROR | ||
| 8 | PE_PASSWORD_OK | ||
| 9 | PE_LDAPCONNECTFAILED | ||
| 10 | ); | ||
| 11 | |||
| 12 | extends qw( | ||
| 13 | Lemonldap::NG::Portal::Lib::LDAP | ||
| 14 | Lemonldap::NG::Portal::Password::Base | ||
| 15 | ); | ||
| 16 | |||
| 17 | our $VERSION = '2.0.16'; | ||
| 18 | |||
| 19 | sub init { | ||
| 20 | my ($self) = @_; | ||
| 21 | return ( $self->Lemonldap::NG::Portal::Password::Base::init | ||
| 22 | and $self->Lemonldap::NG::Portal::Lib::LDAP::init ); | ||
| 23 | } | ||
| 24 | |||
| 25 | # Confirmation is done by Lib::Net::LDAP::userModifyPassword | ||
| 26 | sub confirm { | ||
| 27 | return 1; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub modifyPassword { | ||
| 31 | my ( $self, $req, $pwd, %args ) = @_; | ||
| 32 | my ( $dn, $requireOldPassword ); | ||
| 33 | |||
| 34 | # If the password change is done in a different backend, | ||
| 35 | # we need to reload the correct DN | ||
| 36 | $self->getUser( $req, useMail => $args{useMail} ) | ||
| 37 | if $self->conf->{ldapGetUserBeforePasswordChange}; | ||
| 38 | |||
| 39 | if ( $req->data->{dn} ) { | ||
| 40 | $dn = $req->data->{dn}; | ||
| 41 | $requireOldPassword = | ||
| 42 | $self->requireOldPwdRule->( $req, $req->userData ); | ||
| 43 | $self->logger->debug("Get DN from request data: $dn"); | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | $dn = $req->sessionInfo->{_dn}; | ||
| 47 | $requireOldPassword = | ||
| 48 | $self->requireOldPwdRule->( $req, $req->sessionInfo ); | ||
| 49 | $self->logger->debug("Get DN from session data: $dn"); | ||
| 50 | } | ||
| 51 | unless ($dn) { | ||
| 52 | $self->logger->error('"dn" is not set, aborting password modification'); | ||
| 53 | return PE_ERROR; | ||
| 54 | } | ||
| 55 | $requireOldPassword = 0 if $args{passwordReset}; | ||
| 56 | |||
| 57 | # Ensure connection is valid | ||
| 58 | $self->bind; | ||
| 59 | return PE_LDAPCONNECTFAILED unless $self->ldap; | ||
| 60 | |||
| 61 | # Call the modify password method | ||
| 62 | my $code = | ||
| 63 | $self->ldap->userModifyPassword( $dn, $pwd, $req->data->{oldpassword}, | ||
| 64 | 0, $requireOldPassword ); | ||
| 65 | return $code unless ( $code == PE_PASSWORD_OK ); | ||
| 66 | |||
| 67 | # If password policy and force reset, set reset flag | ||
| 68 | if ( $self->conf->{ldapPpolicyControl} | ||
| 69 | and $req->data->{forceReset} | ||
| 70 | and $self->conf->{ldapUsePasswordResetAttribute} ) | ||
| 71 | { | ||
| 72 | my $result = $self->ldap->modify( | ||
| 73 | $dn, | ||
| 74 | replace => { | ||
| 75 | $self->conf->{ldapPasswordResetAttribute} => | ||
| 76 | $self->conf->{ldapPasswordResetAttributeValue} | ||
| 77 | } | ||
| 78 | ); | ||
| 79 | |||
| 80 | unless ( $result->code == 0 ) { | ||
| 81 | $self->logger->error( "LDAP modify " | ||
| 82 | . $self->conf->{ldapPasswordResetAttribute} | ||
| 83 | . " error " | ||
| 84 | . $result->code . ": " | ||
| 85 | . $result->error ); | ||
| 86 | return PE_LDAPERROR; | ||
| 87 | } | ||
| 88 | |||
| 89 | $self->logger->debug( $self->conf->{ldapPasswordResetAttribute} | ||
| 90 | . " set to " | ||
| 91 | . $self->conf->{ldapPasswordResetAttributeValue} ); | ||
| 92 | } | ||
| 93 | |||
| 94 | return $code; | ||
| 95 | } | ||
| 96 | |||
| 97 | 1; |