Added LDAP-lookup for the forgotten password field.
[usit-rt.git] / local / html / Callbacks / UiOCallbacks / Elements / Login / AfterForm
CommitLineData
84fb5b46
MKG
1%# Add template named 'Password Change', with description
2%#
3%# Automatically generate password for external users who have forgotten their password
4%#
5%# and content
6%#
7%#
8%# Hei {$RealName} !
9%#
10%# Denne meldingen er automatisk generert som svar på
11%# din henvendelse, som ekstern bruker av Request Tracker
12%# ved UiO, om bytte av passord på følgende hjemmeside:
13%#
14%# https://hjelp.uio.no
15%#
16%# For å logge deg på RT bruker du:
17%# Brukernavn (Username): {$Username}
18%# Passord (Password): {$Password}
19%#
20%# Har du ellers problemer med din innlogging så sender
21%# du en e-post til:
22%#
23%# general@hjelp.uio.no
24%#
25%# mvh
26%# RT drift
27
28<DIV ALIGN="CENTER">
29<P align="CENTER">
30<U><B>Glemt passord?/Forgot Your password?</B></U><br>
31<B>
32%print $customMessage2;
33</B><br>
34%print $customMessage;
35<form method="post" action="<% RT->Config->Get('WebPath') %>/NoAuth/Login.html">
36<input type="text" name="email">
37<input type="submit">
38</form>
39</P>
40</DIV>
41
42<%INIT>
ceb44510
MKG
43use Net::LDAP qw(LDAP_SUCCESS LDAP_PARTIAL_RESULTS);
44use Net::LDAP::Util qw(ldap_error_name);
45
84fb5b46
MKG
46my $customMessage;
47my $customMessage2;
48
ceb44510 49#my $mailfrom = 'RT reminder <general@hjelp.uio.no>';
84fb5b46
MKG
50
51if ($email) {
ceb44510
MKG
52my $targetType = (GetEmailTypeFromLdap($email) || "none" );
53
54
55
56if ($targetType !~ /(Sympa|none)/) {
84fb5b46
MKG
57 $customMessage = "[ You can not change or request a password for an UiO account here. See: <a href=https://brukerinfo.uio.no/>UiO user administration services</a>. ]";
58 $customMessage2 = "[ ERROR: Use your UiO username and password to log in above. ]";
59 }
60 else {
61 my $UserObj = RT::User->new($RT::SystemUser);
62 $UserObj->LoadByEmail($email);
63 if (defined($UserObj->Id)) {
64 my ($val, $str) = ResetPassword($UserObj);
65 $customMessage2 = $str;
66 $customMessage = "";
67 }
68 else {
69 $customMessage2 = "[ ERROR: No account matches that email address ]";
70 $customMessage = "[ Please enter your email address again ]";
71 }
72 }
73} else {
74 $customMessage = "[ Please enter your email address ]";
75}
76
77sub ResetPassword {
78 my $self = shift;
79
80 unless ( $self->CurrentUserCanModify('Password') ) {
81 return ( 0, $self->loc("Permission Denied") );
82 }
83 my ( $status, $pass ) = $self->SetRandomPassword();
84
85 unless ($status) {
86 return ( 0, "$pass" );
87 }
88
89 my $ret = RT::Interface::Email::SendEmailUsingTemplate(
90 To => $self->EmailAddress,
91 Template => 'Password Change',
92 Arguments => {
93 Password => $pass,
94 RealName => $self->RealName,
95 Username => $self->Name
96 },
97 );
98
99 if ($ret) {
100 return ( 1, $self->loc('[ SUCCESS: New password notification sent to Your email address ]') );
101 }
102 else {
103 return ( 0, $self->loc('[ ERROR: Notification could not be sent ]') );
104 }
ceb44510
MKG
105}
106
107sub GetEmailTypeFromLdap {
108 my $email = shift;
109 my $ldap = LdapConnect($RT::LdapServer);
110 my $targetType;
111 my $mesg = $ldap->search (
112 base => $RT::LdapMailBase,
113 scope => "one",
114 filter => "(&(&(objectClass=mailAddr))(mail=$email))",
115 attributes => ["targetType"],
116 );
117
118 if ( ($mesg->code != LDAP_SUCCESS) and
119 ($mesg->code != LDAP_PARTIAL_RESULTS) ) {
120 $RT::Logger->critical("Login-After-Form: Search failed: ",
121 "retval=", $mesg->code, " ",
122 ldap_error_name($mesg->code));
123 LdapDisconnect($ldap);
124 return undef;
125 }
84fb5b46 126
ceb44510
MKG
127 while (my $entry = $mesg->shift_entry) {
128 $targetType = $entry->get_value("targetType");
129 }
130 LdapDisconnect($ldap);
131 return $targetType;
132}
133
134sub LdapConnect {
135 my $LdapServer = shift;
136 my $mesg;
137 my $ldap = Net::LDAP->new($LdapServer,
138 version => 3);
139 unless ($ldap) {
140 $RT::Logger->critical("Login-After-Form: Cannot connect to",
141 "LDAP server ", $LdapServer);
142 return undef;
143 }
144 $mesg = $ldap->bind;
145 if ($mesg->code != LDAP_SUCCESS) {
146 $RT::Logger->critical("Login-After-Form: Cannot bind to LDAP: ",
147 "retval=", $mesg->code, " ",
148 ldap_error_name($mesg->code));
149 return undef;
150 }
151 return $ldap;
152}
153
154sub LdapDisconnect {
155 my $ldap = shift;
156 my $mesg = $ldap->unbind();
157 if ($mesg->code != LDAP_SUCCESS) {
158 $RT::Logger->critical("Login-After-Form: unbind failed: ",
159 "retval=", $mesg->code, " ",
160 ldap_error_name($mesg->code));
161 }
84fb5b46
MKG
162}
163
84fb5b46
MKG
164</%INIT>
165
166<%ARGS>
167$email => undef
168</%ARGS>