Annotation of loncom/interface/createaccount.pm, revision 1.31
1.1 raeburn 1: # The LearningOnline Network
2: # Allow visitors to create a user account with the username being either an
3: # institutional log-in ID (institutional authentication required - localauth
4: # or kerberos) or an e-mail address.
5: #
1.31 ! bisitz 6: # $Id: createaccount.pm,v 1.30 2009/03/12 13:59:49 raeburn Exp $
1.1 raeburn 7: #
8: # Copyright Michigan State University Board of Trustees
9: #
10: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
11: #
12: # LON-CAPA is free software; you can redistribute it and/or modify
13: # it under the terms of the GNU General Public License as published by
14: # the Free Software Foundation; either version 2 of the License, or
15: # (at your option) any later version.
16: #
17: # LON-CAPA is distributed in the hope that it will be useful,
18: # but WITHOUT ANY WARRANTY; without even the implied warranty of
19: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20: # GNU General Public License for more details.
21: #
22: # You should have received a copy of the GNU General Public License
23: # along with LON-CAPA; if not, write to the Free Software
24: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25: #
26: # /home/httpd/html/adm/gpl.txt
27: #
28: # http://www.lon-capa.org/
29: #
30: #
31: package Apache::createaccount;
32:
33: use strict;
34: use Apache::Constants qw(:common);
35: use Apache::lonacc;
36: use Apache::lonnet;
37: use Apache::loncommon;
1.12 raeburn 38: use Apache::lonhtmlcommon;
1.1 raeburn 39: use Apache::lonlocal;
1.3 raeburn 40: use Apache::lonauth;
1.1 raeburn 41: use Apache::resetpw;
42: use Authen::Captcha;
43: use DynaLoader; # for Crypt::DES version
44: use Crypt::DES;
1.3 raeburn 45: use LONCAPA qw(:DEFAULT :match);
1.8 raeburn 46: use HTML::Entities;
1.1 raeburn 47:
48: sub handler {
49: my $r = shift;
50: &Apache::loncommon::content_type($r,'text/html');
51: $r->send_http_header;
52: if ($r->header_only) {
53: return OK;
54: }
1.22 raeburn 55:
1.5 raeburn 56: my $domain;
1.3 raeburn 57:
1.5 raeburn 58: my $sso_username = $r->subprocess_env->get('REDIRECT_SSOUserUnknown');
59: my $sso_domain = $r->subprocess_env->get('REDIRECT_SSOUserDomain');
60:
1.27 raeburn 61: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token','courseid']);
62: &Apache::lonacc::get_posted_cgi($r);
63: &Apache::lonlocal::get_language_handle($r);
64:
1.5 raeburn 65: if ($sso_username ne '' && $sso_domain ne '') {
66: $domain = $sso_domain;
1.27 raeburn 67: } else {
1.5 raeburn 68: $domain = &Apache::lonnet::default_login_domain();
1.27 raeburn 69: if (defined($env{'form.courseid'})) {
70: if (&validate_course($env{'form.courseid'})) {
71: if ($env{'form.courseid'} =~ /^($match_domain)_($match_courseid)$/) {
72: $domain = $1;
73: }
74: }
75: }
1.5 raeburn 76: }
1.1 raeburn 77: my $domdesc = &Apache::lonnet::domain($domain,'description');
78: my $contact_name = &mt('LON-CAPA helpdesk');
1.15 raeburn 79: my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
80: my $contacts =
81: &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
82: $domain,$origmail);
83: my ($contact_email) = split(',',$contacts);
1.1 raeburn 84: my $lonhost = $r->dir_config('lonHostID');
85: my $include = $r->dir_config('lonIncludes');
1.4 raeburn 86: my $start_page;
1.3 raeburn 87:
88: my $handle = &Apache::lonnet::check_for_valid_session($r);
1.30 raeburn 89: if (($handle ne '') && ($handle !~ /^publicuser_\d+$/)) {
1.4 raeburn 90: $start_page =
1.3 raeburn 91: &Apache::loncommon::start_page('Already logged in');
92: my $end_page =
93: &Apache::loncommon::end_page();
94: $r->print($start_page."\n".'<h2>'.&mt('You are already logged in').'</h2>'.
95: '<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]logout[_4].','<a href="/adm/roles">','</a>','<a href="/adm/logout">','</a>').
1.6 bisitz 96: '</p><p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'.$end_page);
1.20 raeburn 97: return OK;
98: }
99:
1.22 raeburn 100: my ($js,$courseid,$title);
1.20 raeburn 101: if (defined($env{'form.courseid'})) {
102: $courseid = &validate_course($env{'form.courseid'});
1.3 raeburn 103: }
1.22 raeburn 104: if ($courseid ne '') {
105: $js = &catreturn_js();
106: $title = 'Self-enroll in a LON-CAPA course';
107: } else {
108: $title = 'Create a user account in LON-CAPA';
109: }
1.20 raeburn 110: if ($env{'form.phase'} eq 'selfenroll_login') {
1.22 raeburn 111: $title = 'Self-enroll in a LON-CAPA course';
1.4 raeburn 112: if ($env{'form.udom'} ne '') {
113: $domain = $env{'form.udom'};
114: }
1.20 raeburn 115: my ($result,$output) =
116: &username_validation($r,$env{'form.uname'},$domain,$domdesc,
117: $contact_name,$contact_email,$courseid,
118: $lonhost);
119: if ($result eq 'existingaccount') {
120: $r->print($output);
1.22 raeburn 121: &print_footer($r);
1.20 raeburn 122: return OK;
123: } else {
124: $start_page =
1.22 raeburn 125: &Apache::loncommon::start_page($title,$js,
1.20 raeburn 126: {'no_inline_link' => 1,});
1.22 raeburn 127: &print_header($r,$start_page,$courseid);
128: $r->print($output);
129: &print_footer($r);
1.20 raeburn 130: return OK;
131: }
1.4 raeburn 132: }
1.20 raeburn 133: $start_page =
1.22 raeburn 134: &Apache::loncommon::start_page($title,$js,
1.20 raeburn 135: {'no_inline_link' => 1,});
1.5 raeburn 136: my @cancreate;
1.1 raeburn 137: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
138: if (ref($domconfig{'usercreation'}) eq 'HASH') {
1.3 raeburn 139: if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
1.5 raeburn 140: if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
141: @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
142: } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') &&
143: ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
144: @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
1.1 raeburn 145: }
146: }
147: }
1.3 raeburn 148:
1.5 raeburn 149: if (@cancreate == 0) {
1.22 raeburn 150: &print_header($r,$start_page,$courseid);
1.14 raeburn 151: my $output = '<h3>'.&mt('Account creation unavailable').'</h3>'.
152: '<span class="LC_warning">'.
1.16 raeburn 153: &mt('Creation of a new user account using an e-mail address or an institutional log-in ID as username is not permitted at this institution ([_1]).',$domdesc).'</span><br /><br />';
1.3 raeburn 154: $r->print($output);
1.22 raeburn 155: &print_footer($r);
1.3 raeburn 156: return OK;
157: }
158:
1.5 raeburn 159: if ($sso_username ne '') {
1.22 raeburn 160: &print_header($r,$start_page,$courseid);
1.18 raeburn 161: my ($msg,$sso_logout);
162: $sso_logout = &sso_logout_frag($r,$domain);
1.5 raeburn 163: if (grep(/^sso$/,@cancreate)) {
1.14 raeburn 164: $msg = '<h3>'.&mt('Account creation').'</h3>'.
1.17 raeburn 165: &mt("Although your username and password were authenticated by your institution's Single Sign On system, you do not currently have a LON-CAPA account at this institution.").'<br />';
166:
1.18 raeburn 167: $msg .= &username_check($sso_username,$domain,$domdesc,$courseid,
168: $lonhost,$contact_email,$contact_name,$sso_logout);
1.5 raeburn 169: } else {
1.17 raeburn 170: $msg = '<h3>'.&mt('Account creation unavailable').'</h3>'.
171: '<span class="LC_warning">'.&mt("Although your username and password were authenticated by your institution's Single Sign On system, you do not currently have a LON-CAPA account at this institution, and you are not permitted to create one.").'</span><br /><br />'.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email).'<hr />'.
1.18 raeburn 172: $sso_logout;
1.5 raeburn 173: }
1.17 raeburn 174: $r->print($msg);
1.22 raeburn 175: &print_footer($r);
1.5 raeburn 176: return OK;
177: }
178:
1.4 raeburn 179: my ($output,$nostart,$noend);
1.3 raeburn 180: my $token = $env{'form.token'};
181: if ($token) {
182: ($output,$nostart,$noend) =
183: &process_mailtoken($r,$token,$contact_name,$contact_email,$domain,
1.4 raeburn 184: $domdesc,$lonhost,$include,$start_page);
1.3 raeburn 185: if ($nostart) {
186: if ($noend) {
187: return OK;
188: } else {
189: $r->print($output);
1.22 raeburn 190: &print_footer($r);
1.3 raeburn 191: return OK;
192: }
193: } else {
1.22 raeburn 194: &print_header($r,$start_page,$courseid);
1.3 raeburn 195: $r->print($output);
1.22 raeburn 196: &print_footer($r);
1.3 raeburn 197: return OK;
198: }
199: }
200:
201: if ($env{'form.phase'} eq 'username_activation') {
202: (my $result,$output,$nostart) =
203: &username_activation($r,$env{'form.uname'},$domain,$domdesc,
204: $lonhost,$courseid);
205: if ($result eq 'ok') {
206: if ($nostart) {
207: return OK;
208: }
209: }
1.22 raeburn 210: &print_header($r,$start_page,$courseid);
1.3 raeburn 211: $r->print($output);
1.22 raeburn 212: &print_footer($r);
1.3 raeburn 213: return OK;
1.19 raeburn 214: } elsif ($env{'form.phase'} eq 'username_validation') {
215: (my $result,$output) =
216: &username_validation($r,$env{'form.uname'},$domain,$domdesc,
217: $contact_name,$contact_email,$courseid,
218: $lonhost);
219: if ($result eq 'existingaccount') {
220: $r->print($output);
1.22 raeburn 221: &print_footer($r);
1.19 raeburn 222: return OK;
223: } else {
1.22 raeburn 224: &print_header($r,$start_page,$courseid);
1.19 raeburn 225: }
226: } elsif ($env{'form.create_with_email'}) {
1.22 raeburn 227: &print_header($r,$start_page,$courseid);
1.1 raeburn 228: $output = &process_email_request($env{'form.useremail'},$domain,$domdesc,
1.5 raeburn 229: $contact_name,$contact_email,\@cancreate,
1.3 raeburn 230: $lonhost,$domconfig{'usercreation'},
231: $courseid);
232: } elsif (!$token) {
1.22 raeburn 233: &print_header($r,$start_page,$courseid);
1.1 raeburn 234: my $now=time;
1.5 raeburn 235: if (grep(/^login$/,@cancreate)) {
1.1 raeburn 236: my $jsh=Apache::File->new($include."/londes.js");
237: $r->print(<$jsh>);
238: $r->print(&javascript_setforms($now));
239: }
1.12 raeburn 240: if (grep(/^email$/,@cancreate)) {
241: $r->print(&javascript_validmail());
242: }
1.5 raeburn 243: $output = &print_username_form($domain,$domdesc,\@cancreate,$now,$lonhost,
1.22 raeburn 244: $courseid);
1.1 raeburn 245: }
246: $r->print($output);
1.22 raeburn 247: &print_footer($r);
1.1 raeburn 248: return OK;
249: }
250:
1.3 raeburn 251: sub print_header {
1.22 raeburn 252: my ($r,$start_page,$courseid) = @_;
1.3 raeburn 253: $r->print($start_page);
254: &Apache::lonhtmlcommon::clear_breadcrumbs();
1.22 raeburn 255: if ($courseid ne '') {
256: my %coursehash = &Apache::lonnet::coursedescription($courseid);
257: &selfenroll_crumbs($r,$courseid,$coursehash{'description'});
258: }
1.3 raeburn 259: &Apache::lonhtmlcommon::add_breadcrumb
260: ({href=>"/adm/createuser",
261: text=>"New username"});
262: $r->print(&Apache::lonhtmlcommon::breadcrumbs('Create account'));
263: return;
264: }
265:
1.22 raeburn 266: sub print_footer {
267: my ($r) = @_;
268: if ($env{'form.courseid'} ne '') {
269: $r->print('<form name="backupcrumbs" method="post" action="">'.
270: &Apache::lonhtmlcommon::echo_form_input(['backto','logtoken',
271: 'token','serverid','uname','upass','phase','create_with_email',
272: 'code','useremail','crypt','cfirstname','clastname',
1.23 raeburn 273: 'cmiddlename','cgeneration','cpermanentemail','cid']).
1.22 raeburn 274: '</form>');
275: }
276: $r->print(&Apache::loncommon::end_page());
277: }
278:
279: sub selfenroll_crumbs {
280: my ($r,$courseid,$desc) = @_;
281: &Apache::lonhtmlcommon::add_breadcrumb
282: ({href=>"javascript:ToCatalog('backupcrumbs','')",
283: text=>"Course Catalog"});
284: if ($env{'form.coursenum'} ne '') {
285: &Apache::lonhtmlcommon::add_breadcrumb
286: ({href=>"javascript:ToCatalog('backupcrumbs','details')",
287: text=>"Course details"});
288: }
289: my $last_crumb;
290: if ($desc ne '') {
291: $last_crumb = &mt('Self-enroll in [_1]','<span class="LC_cusr_emph">'.$desc.'</span>');
292: } else {
293: $last_crumb = &mt('Self-enroll');
294: }
295: &Apache::lonhtmlcommon::add_breadcrumb
296: ({href=>"javascript:ToSelfenroll('backupcrumbs')",
297: text=>$last_crumb,
298: no_mt=>"1"});
299: return;
300: }
301:
1.3 raeburn 302: sub validate_course {
303: my ($courseid) = @_;
304: my ($cdom,$cnum) = ($courseid =~ /^($match_domain)_($match_courseid)$/);
305: if (($cdom ne '') && ($cnum ne '')) {
306: if (&Apache::lonnet::is_course($cdom,$cnum)) {
307: return ($courseid);
308: }
309: }
310: return;
311: }
312:
1.1 raeburn 313: sub javascript_setforms {
314: my ($now) = @_;
315: my $js = <<ENDSCRIPT;
316: <script language="JavaScript">
317: function send() {
318: this.document.server.elements.uname.value = this.document.client.elements.uname.value;
319: uextkey=this.document.client.elements.uextkey.value;
320: lextkey=this.document.client.elements.lextkey.value;
321: initkeys();
322:
323: this.document.server.elements.upass.value
324: = crypted(this.document.client.elements.upass$now.value);
325:
326: this.document.client.elements.uname.value='';
327: this.document.client.elements.upass$now.value='';
328:
329: this.document.server.submit();
330: return false;
331: }
332: </script>
333: ENDSCRIPT
334: return $js;
335: }
336:
337: sub javascript_checkpass {
338: my ($now) = @_;
1.7 bisitz 339: my $nopass = &mt('You must enter a password.');
1.1 raeburn 340: my $mismatchpass = &mt('The passwords you entered did not match.').'\\n'.
341: &mt('Please try again.');
342: my $js = <<"ENDSCRIPT";
343: <script type="text/javascript" language="JavaScript">
344: function checkpass() {
345: var upass = this.document.client.elements.upass$now.value;
346: var upasscheck = this.document.client.elements.upasscheck$now.value;
347: if (upass == '') {
348: alert("$nopass");
349: return;
350: }
351: if (upass == upasscheck) {
352: this.document.client.elements.upasscheck$now.value='';
353: send();
354: return;
355: } else {
356: alert("$mismatchpass");
357: return;
358: }
359: }
360: </script>
361: ENDSCRIPT
362: return $js;
363: }
364:
1.12 raeburn 365: sub javascript_validmail {
366: my %lt = &Apache::lonlocal::texthash (
367: email => 'The e-mail address you entered',
368: notv => 'is not a valid e-mail address',
369: );
370: my $output = "\n".'<script type="text/javascript">'."\n".
371: &Apache::lonhtmlcommon::javascript_valid_email()."\n";
372: $output .= <<"ENDSCRIPT";
373: function validate_email() {
374: field = document.createaccount.useremail;
375: if (validmail(field) == false) {
376: alert("$lt{'email'}: "+field.value+" $lt{'notv'}.");
377: return false;
378: }
379: return true;
380: }
381: ENDSCRIPT
382: $output .= "\n".'</script>'."\n";
383: return $output;
384: }
385:
1.1 raeburn 386: sub print_username_form {
1.3 raeburn 387: my ($domain,$domdesc,$cancreate,$now,$lonhost,$courseid) = @_;
1.1 raeburn 388: my %lt = &Apache::lonlocal::texthash(
389: unam => 'username',
390: udom => 'domain',
1.26 schafran 391: uemail => 'E-mail address in LON-CAPA',
1.1 raeburn 392: proc => 'Proceed');
393: my $output;
1.5 raeburn 394: if (ref($cancreate) eq 'ARRAY') {
395: if (grep(/^login$/,@{$cancreate})) {
396: my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
397: if ((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
1.16 raeburn 398: $output = '<div class="LC_left_float"><h3>'.&mt('Create account with a username provided by this institution').'</h3>';
1.20 raeburn 399: my $submit_text = &mt('Create LON-CAPA account');
1.16 raeburn 400: $output .= &mt('If you already have a log-in ID at this institution,[_1] you may be able to use it for LON-CAPA.','<br />').'<br /><br />'.&mt('Type in your log-in ID and password to find out.').'<br /><br />';
1.20 raeburn 401: $output .= &login_box($now,$lonhost,$courseid,$submit_text,
1.22 raeburn 402: $domain,'createaccount').'</div>';
1.5 raeburn 403: }
404: }
405: if (grep(/^email$/,@{$cancreate})) {
406: $output .= '<div class="LC_left_float"><h3>'.&mt('Create account with an e-mail address as your username').'</h3>';
1.28 raeburn 407: my $captchaform = &create_captcha();
408: if ($captchaform) {
409: my $submit_text = &mt('Request LON-CAPA account');
410: my $emailform = '<input type="text" name="useremail" size="25" value="" />';
411: if (grep(/^login$/,@{$cancreate})) {
412: $output .= &mt('Provide your e-mail address to request a LON-CAPA account,[_1] if you do not have a log-in ID at your institution.','<br />').'<br /><br />';
413: } else {
414: $output .= '<br />';
415: }
416: $output .= '<form name="createaccount" method="post" onSubmit="return validate_email()" action="/adm/createaccount">'.
417: &Apache::lonhtmlcommon::start_pick_box()."\n".
418: &Apache::lonhtmlcommon::row_title(&mt('E-mail address'),
419: 'LC_pick_box_title')."\n".
420: $emailform."\n".
421: &Apache::lonhtmlcommon::row_closure(1).
422: &Apache::lonhtmlcommon::row_title(&mt('Validation'),
423: 'LC_pick_box_title')."\n".
424: $captchaform."\n".'<br /><br />';
425: if ($courseid ne '') {
426: $output .= '<input type="hidden" name="courseid" value="'.$courseid.'"/>'."\n";
427: }
428: $output .= '<input type="submit" name="create_with_email" value="'.
429: $submit_text.'" />'.
430: &Apache::lonhtmlcommon::row_closure(1).
431: &Apache::lonhtmlcommon::end_pick_box().'<br /><br />';
432: if ($courseid ne '') {
433: $output .= &Apache::lonhtmlcommon::echo_form_input(['courseid']);
434: }
435: $output .= '</form>';
1.5 raeburn 436: } else {
1.28 raeburn 437: my $helpdesk = '/adm/helpdesk?origurl=%2fadm%2fcreateaccount';
438: if ($courseid ne '') {
439: $helpdesk .= '&courseid='.$courseid;
440: }
441: $output .= '<span class="LC_error">'.&mt('An error occurred generating the validation code[_1] required for an e-mail address to be used as username.','<br />').'</span><br /><br />'.&mt('[_1]Contact the helpdesk[_2] or [_3]reload[_2] the page and try again.','<a href="'.$helpdesk.'">','</a>','<a href="javascript:window.location.reload()">');
1.5 raeburn 442: }
1.28 raeburn 443: $output .= '</div>';
1.3 raeburn 444: }
1.1 raeburn 445: }
446: if ($output eq '') {
1.16 raeburn 447: $output = &mt('Creation of a new LON-CAPA user account using an e-mail address or an institutional log-in ID as your username is not permitted at [_1].',$domdesc);
1.1 raeburn 448: } else {
449: $output .= '<div class="LC_clear_float_footer"></div>';
450: }
451: return $output;
452: }
453:
1.20 raeburn 454: sub login_box {
455: my ($now,$lonhost,$courseid,$submit_text,$domain,$context) = @_;
456: my $output;
457: my %titles = &Apache::lonlocal::texthash(
458: createaccount => 'Log-in ID',
459: selfenroll => 'Username',
460: );
461: my ($lkey,$ukey) = &Apache::lonpreferences::des_keys();
462: my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
463: my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount',
464: $lonhost);
465: $output = &serverform($logtoken,$lonhost,undef,$courseid,$context);
466: my $unameform = '<input type="text" name="uname" size="10" value="" />';
467: my $upassform = '<input type="password" name="upass'.$now.'" size="10" />';
468: $output .= '<form name="client" method="post" action="/adm/createaccount">'."\n".
469: &Apache::lonhtmlcommon::start_pick_box()."\n";
1.31 ! bisitz 470: $output .= &Apache::lonhtmlcommon::row_title($titles{$context},
! 471: 'LC_pick_box_title')."\n".
! 472: $unameform."\n".
! 473: &Apache::lonhtmlcommon::row_closure(1)."\n".
! 474: &Apache::lonhtmlcommon::row_title(&mt('Password'),
! 475: 'LC_pick_box_title')."\n".
! 476: $upassform;
1.20 raeburn 477: if ($context eq 'selfenroll') {
478: my $udomform = '<input type="text" name="udom" size="10" value="'.
479: $domain.'" />';
1.31 ! bisitz 480: $output .= &Apache::lonhtmlcommon::row_closure(1)."\n".
! 481: &Apache::lonhtmlcommon::row_title(&mt('Domain'),
1.20 raeburn 482: 'LC_pick_box_title')."\n".
1.31 ! bisitz 483: $udomform."\n";
1.20 raeburn 484: }
485:
1.31 ! bisitz 486: $output .= &Apache::lonhtmlcommon::row_closure(0).
! 487: &Apache::lonhtmlcommon::row_title().
1.20 raeburn 488: '<input type="button" name="username_validation" value="'.
489: $submit_text.'" onclick="javascript:send()" />'."\n".
490: &Apache::lonhtmlcommon::row_closure(1)."\n".
1.29 bisitz 491: &Apache::lonhtmlcommon::end_pick_box();
492: $output .= '<p>'.
493: '<a href="/adm/resetpw">'.&mt('Forgot password?').'</a>'.
494: '</p>'."\n";
495: $output .= '<input type="hidden" name="lextkey" value="'.$lextkey.'">'."\n".
1.20 raeburn 496: '<input type="hidden" name="uextkey" value="'.$uextkey.'">'."\n".
1.22 raeburn 497: '</form>';
1.20 raeburn 498: return $output;
499: }
500:
1.1 raeburn 501: sub process_email_request {
502: my ($useremail,$domain,$domdesc,$contact_name,$contact_email,$cancreate,
1.3 raeburn 503: $server,$settings,$courseid) = @_;
1.21 raeburn 504: $useremail = $env{'form.useremail'};
1.1 raeburn 505: my $output;
1.5 raeburn 506: if (ref($cancreate) eq 'ARRAY') {
507: if (!grep(/^email$/,@{$cancreate})) {
508: $output = &invalid_state('noemails',$domdesc,
509: $contact_name,$contact_email);
510: return $output;
511: } elsif ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
512: $output = &invalid_state('baduseremail',$domdesc,
1.1 raeburn 513: $contact_name,$contact_email);
514: return $output;
515: } else {
1.5 raeburn 516: my $uhome = &Apache::lonnet::homeserver($useremail,$domain);
517: if ($uhome ne 'no_host') {
518: $output = &invalid_state('existinguser',$domdesc,
519: $contact_name,$contact_email);
1.1 raeburn 520: return $output;
1.5 raeburn 521: } else {
522: my $code = $env{'form.code'};
523: my $md5sum = $env{'form.crypt'};
524: my %captcha_params = &captcha_settings();
525: my $captcha = Authen::Captcha->new(
526: output_folder => $captcha_params{'output_dir'},
527: data_folder => $captcha_params{'db_dir'},
528: );
529: my $captcha_chk = $captcha->check_code($code,$md5sum);
530: my %captcha_hash = (
531: 0 => 'Code not checked (file error)',
532: -1 => 'Failed: code expired',
533: -2 => 'Failed: invalid code (not in database)',
534: -3 => 'Failed: invalid code (code does not match crypt)',
535: );
536: if ($captcha_chk != 1) {
537: $output = &invalid_state('captcha',$domdesc,$contact_name,
538: $contact_email,$captcha_hash{$captcha_chk});
539: return $output;
540: }
541: my $uhome=&Apache::lonnet::homeserver($useremail,$domain);
542: if ($uhome eq 'no_host') {
1.23 raeburn 543: my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts);
544: &call_rulecheck($useremail,$domain,\%alerts,\%rulematch,
545: \%inst_results,\%curr_rules,%got_rules,'username');
546: if (ref($alerts{'username'}) eq 'HASH') {
547: if (ref($alerts{'username'}{$domain}) eq 'HASH') {
1.5 raeburn 548: if ($alerts{'username'}{$domain}{$useremail}) {
549: $output = &invalid_state('userrules',$domdesc,
550: $contact_name,$contact_email);
551: return $output;
552: }
1.1 raeburn 553: }
554: }
1.5 raeburn 555: my $format_msg =
556: &guest_format_check($useremail,$domain,$cancreate,
557: $settings);
558: if ($format_msg) {
559: $output = &invalid_state('userformat',$domdesc,$contact_name,
560: $contact_email,$format_msg);
561: return $output;
562: }
1.1 raeburn 563: }
564: }
565: }
1.5 raeburn 566: $output = &send_token($domain,$useremail,$server,$domdesc,$contact_name,
567: $contact_email,$courseid);
1.1 raeburn 568: }
569: return $output;
570: }
571:
1.23 raeburn 572: sub call_rulecheck {
573: my ($uname,$udom,$alerts,$rulematch,$inst_results,$curr_rules,
574: $got_rules,$tocheck) = @_;
575: my ($checkhash,$checks);
576: $checkhash->{$uname.':'.$udom} = { 'newuser' => 1, };
577: if ($tocheck eq 'username') {
578: $checks = { 'username' => 1 };
579: }
580: &Apache::loncommon::user_rule_check($checkhash,$checks,
581: $alerts,$rulematch,$inst_results,$curr_rules,
582: $got_rules);
583: return;
584: }
585:
1.1 raeburn 586: sub send_token {
1.3 raeburn 587: my ($domain,$email,$server,$domdesc,$contact_name,$contact_email,$courseid) = @_;
1.14 raeburn 588: my $msg = '<h3>'.&mt('Account creation status').'</h3>'.
589: &mt('Thank you for your request to create a new LON-CAPA account.').
590: '<br /><br />';
1.1 raeburn 591: my $now = time;
592: my %info = ('ip' => $ENV{'REMOTE_ADDR'},
593: 'time' => $now,
594: 'domain' => $domain,
1.3 raeburn 595: 'username' => $email,
596: 'courseid' => $courseid);
1.1 raeburn 597: my $token = &Apache::lonnet::tmpput(\%info,$server);
598: if ($token !~ /^error/ && $token ne 'no_such_host') {
599: my $esc_token = &escape($token);
1.28 raeburn 600: my $showtime = localtime(time);
601: my $mailmsg = &mt('A request was submitted on [_1] for creation of a LON-CAPA account at the following institution: [_2].',$showtime,$domdesc).' '.
602: &mt('To complete this process please open a web browser and enter the following URL in the address/location box: [_1]',
603: &Apache::lonnet::absolute_url().'/adm/createaccount?token='.$esc_token);
1.1 raeburn 604: my $result = &Apache::resetpw::send_mail($domdesc,$email,$mailmsg,$contact_name,
605: $contact_email);
606: if ($result eq 'ok') {
607: $msg .= &mt('A message has been sent to the e-mail address you provided.').'<br />'.&mt('The message includes the web address for the link you will use to complete the account creation process.').'<br />'.&mt("The link included in the message will be valid for the next [_1]two[_2] hours.",'<b>','</b>');
608: } else {
1.14 raeburn 609: $msg .= '<span class="LC_error">'.
610: &mt('An error occurred when sending a message to the e-mail address you provided.').'</span><br />'.
611: ' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1 raeburn 612: }
613: } else {
1.14 raeburn 614: $msg .= '<span class="LC_error">'.
615: &mt('An error occurred creating a token required for the account creation process.').'</span><br />'.
616: ' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1 raeburn 617: }
618: return $msg;
619: }
620:
621: sub process_mailtoken {
1.3 raeburn 622: my ($r,$token,$contact_name,$contact_email,$domain,$domdesc,$lonhost,
623: $include,$start_page) = @_;
624: my ($msg,$nostart,$noend);
1.1 raeburn 625: my %data = &Apache::lonnet::tmpget($token);
626: my $now = time;
627: if (keys(%data) == 0) {
1.9 raeburn 628: $msg = &mt('Sorry, the URL you provided to complete creation of a new LON-CAPA account was invalid.')
629: .' '.&mt('Either the token included in the URL has been deleted or the URL you provided was invalid.')
630: .' '.&mt('Please submit a [_1]new request[_2] for account creation and follow the new link page included in the e-mail that will be sent to you.','<a href="/adm/createaccount">','</a>');
1.1 raeburn 631: return $msg;
632: }
633: if (($data{'time'} =~ /^\d+$/) &&
634: ($data{'domain'} ne '') &&
635: ($data{'username'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/)) {
636: if ($now - $data{'time'} < 7200) {
637: if ($env{'form.phase'} eq 'createaccount') {
1.3 raeburn 638: my ($result,$output) = &create_account($r,$domain,$lonhost,
1.1 raeburn 639: $data{'username'},$domdesc);
640: if ($result eq 'ok') {
641: $msg = $output;
1.17 raeburn 642: my $shownow = &Apache::lonlocal::locallocaltime($now);
1.16 raeburn 643: my $mailmsg = &mt('A LON-CAPA account for the institution: [_1] has been created [_2] from IP address: [_3]. If you did not perform this action or authorize it, please contact the [_4] ([_5]).',$domdesc,$shownow,$ENV{'REMOTE_ADDR'},$contact_name,$contact_email)."\n";
1.1 raeburn 644: my $mailresult = &Apache::resetpw::send_mail($domdesc,$data{'email'},
645: $mailmsg,$contact_name,
646: $contact_email);
647: if ($mailresult eq 'ok') {
648: $msg .= &mt('An e-mail confirming creation of your new LON-CAPA account has been sent to [_1].',$data{'username'});
649: } else {
650: $msg .= &mt('An error occurred when sending e-mail to [_1] confirming creation of your LON-CAPA account.',$data{'username'});
651: }
1.3 raeburn 652: my %form = &start_session($r,$data{'username'},$domain,
653: $lonhost,$data{'courseid'},
654: $token);
655: $nostart = 1;
656: $noend = 1;
1.1 raeburn 657: } else {
1.7 bisitz 658: $msg .= &mt('A problem occurred when attempting to create your new LON-CAPA account.')
659: .'<br />'.$output
660: # .&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>');
661: .&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1 raeburn 662: }
1.3 raeburn 663: my $delete = &Apache::lonnet::tmpdel($token);
1.1 raeburn 664: } else {
1.3 raeburn 665: $msg .= &mt('Please provide user information and a password for your new account.').'<br />'.&mt('Your password, which must contain at least seven characters, will be sent to the LON-CAPA server in an encrypted form.').'<br />';
666: $msg .= &print_dataentry_form($r,$domain,$lonhost,$include,$token,$now,$data{'username'},$start_page);
667: $nostart = 1;
1.1 raeburn 668: }
669: } else {
1.7 bisitz 670: $msg = &mt('Sorry, the token generated when you requested creation of an account has expired.')
671: .' '.&mt('Please submit a [_1]new request[_2] for account creation and follow the new link included in the e-mail that will be sent to you.','<a href="/adm/createaccount">','</a>');
1.4 raeburn 672: }
1.1 raeburn 673: } else {
1.7 bisitz 674: $msg .= &mt('Sorry, the URL generated when you requested creation of an account contained incomplete information.')
675: .' '.&mt('Please submit a [_1]new request[_2] for account creation and follow the new link included in the e-mail that will be sent to you.','<a href="/adm/createaccount">','</a>');
1.1 raeburn 676: }
1.3 raeburn 677: return ($msg,$nostart,$noend);
678: }
679:
680: sub start_session {
681: my ($r,$username,$domain,$lonhost,$courseid,$token) = @_;
682: my %form = (
683: uname => $username,
684: udom => $domain,
685: );
686: my $firsturl = '/adm/roles';
687: if (defined($courseid)) {
688: $courseid = &validate_course($courseid);
689: if ($courseid ne '') {
690: $form{'courseid'} = $courseid;
1.22 raeburn 691: $firsturl = '/adm/selfenroll?courseid='.$courseid;
1.3 raeburn 692: }
693: }
694: if ($r->dir_config('lonBalancer') eq 'yes') {
695: &Apache::lonauth::success($r,$form{'uname'},$form{'udom'},
696: $lonhost,'noredirect',undef,\%form);
1.19 raeburn 697: if ($token ne '') {
698: my $delete = &Apache::lonnet::tmpdel($token);
699: }
1.3 raeburn 700: $r->internal_redirect('/adm/switchserver');
701: } else {
702: &Apache::lonauth::success($r,$form{'uname'},$form{'udom'},
703: $lonhost,$firsturl,undef,\%form);
704: }
705: return %form;
1.1 raeburn 706: }
707:
1.3 raeburn 708:
1.1 raeburn 709: sub print_dataentry_form {
1.3 raeburn 710: my ($r,$domain,$lonhost,$include,$mailtoken,$now,$username,$start_page) = @_;
1.1 raeburn 711: my ($error,$output);
1.3 raeburn 712: &print_header($r,$start_page);
1.1 raeburn 713: if (open(my $jsh,"<$include/londes.js")) {
714: while(my $line = <$jsh>) {
715: $r->print($line);
716: }
717: close($jsh);
1.3 raeburn 718: $output .= &javascript_setforms($now)."\n".&javascript_checkpass($now);
1.1 raeburn 719: my ($lkey,$ukey) = &Apache::lonpreferences::des_keys();
720: my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
721: my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount',
722: $lonhost);
723: my @userinfo = ('firstname','middlename','lastname','generation','id',
724: 'permanentemail');
725: my %lt=&Apache::lonlocal::texthash(
726: 'pd' => "Personal Data",
727: 'firstname' => "First Name",
728: 'middlename' => "Middle Name",
729: 'lastname' => "Last Name",
730: 'generation' => "Generation",
731: 'permanentemail' => "Permanent e-mail address",
1.24 bisitz 732: 'id' => "Student/Employee ID",
1.1 raeburn 733: 'lg' => "Login Data"
734: );
735: my %textboxsize = (
736: firstname => '15',
737: middlename => '15',
738: lastname => '15',
739: generation => '5',
740: id => '15',
741: );
742: my $genhelp=&Apache::loncommon::help_open_topic('Generation');
1.3 raeburn 743: $output .= '<div class="LC_left_float"><h3>'.$lt{'pd'}.'</h3>'.
744: '<form name="server" method="post" target="_top" action="/adm/createaccount">'.
1.1 raeburn 745: &Apache::lonhtmlcommon::start_pick_box();
746: foreach my $item (@userinfo) {
747: my $rowtitle = $lt{$item};
748: if ($item eq 'generation') {
749: $rowtitle = $genhelp.$rowtitle;
750: }
751: $output .= &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
752: if ($item eq 'permanentemail') {
753: $output .= $username;
754: } else {
755: $output .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
756: }
757: $output .= &Apache::lonhtmlcommon::row_closure(1);
758: }
759: $output .= &Apache::lonhtmlcommon::end_pick_box();
760: $output .= <<"ENDSERVERFORM";
761: <input type="hidden" name="logtoken" value="$logtoken" />
762: <input type="hidden" name="token" value="$mailtoken" />
763: <input type="hidden" name="serverid" value="$lonhost" />
764: <input type="hidden" name="uname" value="" />
765: <input type="hidden" name="upass" value="" />
766: <input type="hidden" name="phase" value="createaccount" />
767: </form></div>
768: ENDSERVERFORM
769: my $upassone = '<input type="password" name="upass'.$now.'" size="10" />';
770: my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="10" />';
771: my $submit_text = &mt('Create LON-CAPA account');
772: $output .= '<div class="LC_left_float"><h3>'.$lt{'lg'}.'</h3>'."\n".
773: '<form name="client" method="post" />'."\n".
774: &Apache::lonhtmlcommon::start_pick_box()."\n".
775: &Apache::lonhtmlcommon::row_title(&mt('Username'),
776: 'LC_pick_box_title')."\n".
777: $username."\n".
778: &Apache::lonhtmlcommon::row_closure(1)."\n".
779: &Apache::lonhtmlcommon::row_title(&mt('Password'),
780: 'LC_pick_box_title')."\n".
781: $upassone."\n".
782: &Apache::lonhtmlcommon::row_closure(1)."\n".
783: &Apache::lonhtmlcommon::row_title(&mt('Confirm password'),
784: 'LC_pick_box_title')."\n".
785: $upasstwo."\n".
786: &Apache::lonhtmlcommon::row_closure(1)."\n".
787: &Apache::lonhtmlcommon::end_pick_box()."\n".
788: '<input type="hidden" name="uname" value="'.$username.'">'."\n".
789: '<input type="hidden" name="lextkey" value="'.$lextkey.'">'."\n".
790: '<input type="hidden" name="uextkey" value="'.$uextkey.'">'."\n".
791: '</form></div>'."\n".
792: '<div class="LC_clear_float_footer"><br /><br />'."\n".
793: '<form name="buttonform">'."\n".
794: '<input type="button" name="createaccount" value="'.
795: $submit_text.'" onclick="javascript:checkpass();" /></form></div>';
796: } else {
1.7 bisitz 797: $output = &mt('Could not load javascript file [_1]','<tt>londes.js</tt>');
1.1 raeburn 798: }
1.3 raeburn 799: return $output;
1.1 raeburn 800: }
801:
802: sub create_account {
1.3 raeburn 803: my ($r,$domain,$lonhost,$username,$domdesc) = @_;
1.1 raeburn 804: my ($retrieved,$output,$upass) = &process_credentials($env{'form.logtoken'},
805: $env{'form.serverid'});
806: # Error messages
1.7 bisitz 807: my $error = '<span class="LC_error">'.&mt('Error:').' ';
1.1 raeburn 808: my $end = '</span><br /><br />';
809: my $rtnlink = '<a href="javascript:history.back();" />'.
810: &mt('Return to previous page').'</a>'.
811: &Apache::loncommon::end_page();
812: if ($retrieved eq 'ok') {
1.22 raeburn 813: if ($env{'form.courseid'} ne '') {
1.1 raeburn 814: my ($result,$userchkmsg) = &check_id($username,$domain,$domdesc);
815: if ($result eq 'fail') {
816: $output = $error.&mt('Invalid ID format').$end.
817: $userchkmsg.$rtnlink;
818: return ('fail',$output);
819: }
820: }
821: } else {
822: return ('fail',$error.$output.$end.$rtnlink);
823: }
824: # Call modifyuser
825: my $result =
826: &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
827: 'internal',$upass,$env{'form.cfirstname'},
828: $env{'form.cmiddlename'},$env{'form.clastname'},
829: $env{'form.cgeneration'},undef,undef,$username);
1.7 bisitz 830: $output = &mt('Generating user: [_1]',$result);
1.1 raeburn 831: my $uhome = &Apache::lonnet::homeserver($username,$domain);
1.7 bisitz 832: $output .= '<br />'.&mt('Home server: [_1]',$uhome).' '.
1.1 raeburn 833: &Apache::lonnet::hostname($uhome).'<br /><br />';
834: return ('ok',$output);
835: }
836:
837: sub username_validation {
1.19 raeburn 838: my ($r,$username,$domain,$domdesc,$contact_name,$contact_email,$courseid,
839: $lonhost) = @_;
1.1 raeburn 840: my ($retrieved,$output,$upass);
841:
842: $username= &LONCAPA::clean_username($username);
843: $domain = &LONCAPA::clean_domain($domain);
844: my $uhome = &Apache::lonnet::homeserver($username,$domain);
845:
846: ($retrieved,$output,$upass) = &process_credentials($env{'form.logtoken'},
847: $env{'form.serverid'});
1.19 raeburn 848: if ($retrieved ne 'ok') {
849: return ('fail',$output);
850: }
851: if ($uhome ne 'no_host') {
852: my $result = &Apache::lonnet::authenticate($username,$upass,$domain);
853: if ($result ne 'no_host') {
854: my %form = &start_session($r,$username,$domain,$lonhost,$courseid);
855: $output = '<br /><br />'.&mt('A LON-CAPA account already exists for username [_1] at this institution ([_2]).','<tt>'.$username.'</tt>',$domdesc).'<br />'.&mt('The password entered was also correct so you have been logged in.');
856: return ('existingaccount',$output);
857: } else {
1.22 raeburn 858: $output = &login_failure_msg($courseid);
1.19 raeburn 859: }
860: } else {
1.1 raeburn 861: my $primlibserv = &Apache::lonnet::domain($domain,'primary');
862: my $authok;
863: my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
864: if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
865: my $checkdefauth = 1;
866: $authok =
867: &Apache::lonnet::reply("encrypt:auth:$domain:$username:$upass:$checkdefauth",$primlibserv);
868: } else {
869: $authok = 'non_authorized';
870: }
871: if ($authok eq 'authorized') {
1.18 raeburn 872: $output = &username_check($username,$domain,$domdesc,$courseid,$lonhost,
873: $contact_email,$contact_name);
1.4 raeburn 874: } else {
1.22 raeburn 875: $output = &login_failure_msg($courseid);
1.4 raeburn 876: }
877: }
1.19 raeburn 878: return ('ok',$output);
1.4 raeburn 879: }
880:
1.22 raeburn 881: sub login_failure_msg {
882: my ($courseid) = @_;
883: my $url;
884: if ($courseid ne '') {
885: $url = "/adm/selfenroll?courseid=".$courseid;
886: } else {
887: $url = "/adm/createaccount";
888: }
889: my $output = '<h4>'.&mt('Authentication failed').'</h4><div class="LC_warning">'.
890: &mt('Username and/or password could not be authenticated.').
891: '</div>'.
892: &mt('Please check the username and password.').'<br /><br />';
893: '<a href="'.$url.'">'.&mt('Try again').'</a>';
894: return $output;
895: }
896:
1.4 raeburn 897: sub username_check {
1.18 raeburn 898: my ($username,$domain,$domdesc,$courseid,$lonhost,$contact_email,$contact_name,
899: $sso_logout) = @_;
1.23 raeburn 900: my (%rulematch,%inst_results,$checkfail,$rowcount,$editable,$output,$msg,
1.18 raeburn 901: %alerts,%curr_rules,%got_rules);
1.23 raeburn 902: &call_rulecheck($username,$domain,\%alerts,\%rulematch,
903: \%inst_results,\%curr_rules,%got_rules,'username');
1.4 raeburn 904: if (ref($alerts{'username'}) eq 'HASH') {
905: if (ref($alerts{'username'}{$domain}) eq 'HASH') {
906: if ($alerts{'username'}{$domain}{$username}) {
907: if (ref($curr_rules{$domain}) eq 'HASH') {
1.18 raeburn 908: $output =
1.17 raeburn 909: &Apache::loncommon::instrule_disallow_msg('username',$domdesc,1,
910: 'selfcreate').
1.4 raeburn 911: &Apache::loncommon::user_rule_formats($domain,$domdesc,
912: $curr_rules{$domain}{'username'},'username');
1.1 raeburn 913: }
1.18 raeburn 914: $checkfail = 'username';
1.1 raeburn 915: }
1.4 raeburn 916: }
917: }
1.18 raeburn 918: if (!$checkfail) {
919: $output = '<form method="post" action="/adm/createaccount">';
920: (my $datatable,$rowcount,$editable) =
921: &Apache::loncreateuser::personal_data_display($username,$domain,1,'selfcreate',
922: $inst_results{$username.':'.$domain});
923: if ($rowcount > 0) {
924: $output .= $datatable;
925: }
926: $output .= '<br /><br /><input type="hidden" name="uname" value="'.$username.'" />'."\n".
927: '<input type="hidden" name="udom" value="'.$domain.'" />'."\n".
928: '<input type="hidden" name="phase" value="username_activation" />';
929: my $now = time;
930: my %info = ('ip' => $ENV{'REMOTE_ADDR'},
931: 'time' => $now,
932: 'domain' => $domain,
933: 'username' => $username);
934: my $authtoken = &Apache::lonnet::tmpput(\%info,$lonhost);
935: if ($authtoken !~ /^error/ && $authtoken ne 'no_such_host') {
936: $output .= '<input type="hidden" name="authtoken" value="'.&HTML::Entities::encode($authtoken,'&<>"').'" />';
937: } else {
938: $output = &mt('An error occurred when storing a token').'<br />'.
939: &mt('You will not be able to proceed to the next stage of account creation').
940: &linkto_email_help($contact_email,$domdesc);
941: $checkfail = 'authtoken';
942: }
943: }
944: if ($checkfail) {
945: $msg = '<h4>'.&mt('Account creation unavailable').'</h4>';
946: if ($checkfail eq 'username') {
947: $msg .= '<span class="LC_warning">'.
948: &mt('A LON-CAPA account may not be created with the username you use.').
949: '</span><br /><br />'.$output;
950: } elsif ($checkfail eq 'authtoken') {
951: $msg .= '<span class="LC_error">'.&mt('Error creating token.').'</span>'.
952: '<br />'.$output;
953: }
954: $msg .= &mt('Please contact the [_1] ([_2]) for assistance.',
955: $contact_name,$contact_email).'<br /><hr />'.
956: $sso_logout;
957: &Apache::lonnet::logthis("ERROR: failure type of '$checkfail' when performing username check to create account for authenticated user: $username, in domain $domain");
1.8 raeburn 958: } else {
1.18 raeburn 959: if ($courseid ne '') {
960: $output .= '<input type="hidden" name="courseid" value="'.$courseid.'" />';
961: }
962: $output .= '<input type="submit" name="newaccount" value="'.
963: &mt('Create LON-CAPA account').'" /></form>';
964: if ($rowcount) {
965: if ($editable) {
1.22 raeburn 966: if ($courseid ne '') {
967: $msg = '<h4>'.&mt('User information').'</h4>';
968: }
969: $msg .= &mt('To create one, use the table below to provide information about yourself, then click the [_1]Create LON-CAPA account[_2] button.','<span class="LC_cusr_emph">','</span>').'<br />';
1.18 raeburn 970: } else {
1.22 raeburn 971: if ($courseid ne '') {
972: $msg = '<h4>'.&mt('Review user information').'</h4>';
973: }
974: $msg .= &mt('A user account will be created with information displayed in the table below, when you click the [_1]Create LON-CAPA account[_2] button.','<span class="LC_cusr_emph">','</span>').'<br />';
1.18 raeburn 975: }
976: } else {
1.22 raeburn 977: if ($courseid ne '') {
978: $msg = '<h4>'.&mt('Confirmation').'</h4>';
979: }
980: $msg .= &mt('Confirm that you wish to create an account.');
1.18 raeburn 981: }
982: $msg .= $output;
983: }
984: return $msg;
1.1 raeburn 985: }
986:
987: sub username_activation {
1.3 raeburn 988: my ($r,$username,$domain,$domdesc,$lonhost,$courseid) = @_;
1.1 raeburn 989: my $output;
1.7 bisitz 990: my $error = '<span class="LC_error">'.&mt('Error:').' ';
1.1 raeburn 991: my $end = '</span><br /><br />';
992: my $rtnlink = '<a href="javascript:history.back();" />'.
993: &mt('Return to previous page').'</a>'.
994: &Apache::loncommon::end_page();
995: my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
1.8 raeburn 996: my %data = &Apache::lonnet::tmpget($env{'form.authtoken'});
997: my $now = time;
998: my $earlyout;
999: my $timeout = 300;
1000: if (keys(%data) == 0) {
1001: $output = &mt('Sorry, your authentication has expired.');
1002: $earlyout = 'fail';
1003: }
1004: if (($data{'time'} !~ /^\d+$/) ||
1005: ($data{'domain'} ne $domain) ||
1006: ($data{'username'} ne $username)) {
1007: $earlyout = 'fail';
1008: $output = &mt('The credentials you provided could not be verified.');
1009: } elsif ($now - $data{'time'} > $timeout) {
1010: $earlyout = 'fail';
1011: $output = &mt('Sorry, your authentication has expired.');
1012: }
1013: if ($earlyout ne '') {
1014: $output .= '<br />'.&mt('Please [_1]start again[_2].','<a href="/adm/createaccount">','</a>');
1015: return($earlyout,$output);
1016: }
1.3 raeburn 1017: if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) &&
1018: ($domdefaults{'auth_arg_def'} ne '')) ||
1019: ($domdefaults{'auth_def'} eq 'localauth')) {
1.22 raeburn 1020: if ($env{'form.courseid'} ne '') {
1.1 raeburn 1021: my ($result,$userchkmsg) = &check_id($username,$domain,$domdesc);
1022: if ($result eq 'fail') {
1023: $output = $error.&mt('Invalid ID format').$end.
1024: $userchkmsg.$rtnlink;
1025: return ('fail',$output);
1026: }
1027: }
1028: # Call modifyuser
1.23 raeburn 1029: my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts,%info);
1030: &call_rulecheck($username,$domain,\%alerts,\%rulematch,
1031: \%inst_results,\%curr_rules,%got_rules);
1032: my @userinfo = ('firstname','middlename','lastname','generation',
1033: 'permanentemail','id');
1034: my %canmodify =
1035: &Apache::loncreateuser::selfcreate_canmodify('selfcreate',$domain,
1036: \@userinfo,\%inst_results);
1037: foreach my $item (@userinfo) {
1038: if ($canmodify{$item}) {
1039: $info{$item} = $env{'form.c'.$item};
1040: } else {
1041: $info{$item} = $inst_results{$username.':'.$domain}{$item};
1042: }
1043: }
1044: if (ref($inst_results{$username.':'.$domain}{'inststatus'}) eq 'ARRAY') {
1045: my @inststatuses = @{$inst_results{$username.':'.$domain}{'inststatus'}};
1046: $info{'inststatus'} = join(':',map { &escape($_); } @inststatuses);
1047: }
1.1 raeburn 1048: my $result =
1.23 raeburn 1049: &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
1.1 raeburn 1050: $domdefaults{'auth_def'},
1.23 raeburn 1051: $domdefaults{'auth_arg_def'},$info{'firstname'},
1052: $info{'middlename'},$info{'lastname'},
1053: $info{'generation'},undef,undef,
1054: $info{'permanentemail'},$info{'inststatus'});
1.3 raeburn 1055: if ($result eq 'ok') {
1.8 raeburn 1056: my $delete = &Apache::lonnet::tmpdel($env{'form.authtoken'});
1.3 raeburn 1057: $output = &mt('A LON-CAPA account has been created for username: [_1] in domain: [_2].',$username,$domain);
1058: my %form = &start_session($r,$username,$domain,$lonhost,$courseid);
1059: my $nostart = 1;
1060: return ('ok',$output,$nostart);
1061: } else {
1062: $output = &mt('Account creation failed for username: [_1] in domain: [_2].',$username,$domain).'<br /><span class="LC_error">'.&mt('Error: [_1]',$result).'</span>';
1063: return ('fail',$output);
1064: }
1.1 raeburn 1065: } else {
1.7 bisitz 1066: $output = &mt('User account creation is not available for the current default authentication type.')."\n";
1.1 raeburn 1067: return('fail',$output);
1068: }
1069: }
1070:
1071: sub check_id {
1072: my ($username,$domain,$domdesc) = @_;
1073: # Check ID format
1074: my (%alerts,%rulematch,%inst_results,%curr_rules,%checkhash);
1075: my %checks = ('id' => 1);
1076: %{$checkhash{$username.':'.$domain}} = (
1077: 'newuser' => 1,
1078: 'id' => $env{'form.cid'},
1079: );
1080: &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
1081: \%rulematch,\%inst_results,\%curr_rules);
1082: if (ref($alerts{'id'}) eq 'HASH') {
1083: if (ref($alerts{'id'}{$domain}) eq 'HASH') {
1084: if ($alerts{'id'}{$domain}{$env{'form.cid'}}) {
1085: my $userchkmsg;
1086: if (ref($curr_rules{$domain}) eq 'HASH') {
1087: $userchkmsg =
1088: &Apache::loncommon::instrule_disallow_msg('id',
1089: $domdesc,1).
1090: &Apache::loncommon::user_rule_formats($domain,
1091: $domdesc,$curr_rules{$domain}{'id'},'id');
1092: }
1093: return ('fail',$userchkmsg);
1094: }
1095: }
1096: }
1097: return;
1098: }
1099:
1100: sub invalid_state {
1101: my ($error,$domdesc,$contact_name,$contact_email,$msgtext) = @_;
1.14 raeburn 1102: my $msg = '<h3>'.&mt('Account creation unavailable').'</h3><span class="LC_error">';
1.1 raeburn 1103: if ($error eq 'baduseremail') {
1104: $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
1105: } elsif ($error eq 'existinguser') {
1.16 raeburn 1106: $msg = &mt('The e-mail address you provided is already in use as a username in LON-CAPA at this institution.');
1.1 raeburn 1107: } elsif ($error eq 'userrules') {
1.25 bisitz 1108: $msg = &mt('Username rules at this institution do not allow the e-mail address you provided to be used as a username.');
1.1 raeburn 1109: } elsif ($error eq 'userformat') {
1.16 raeburn 1110: $msg = &mt('The e-mail address you provided may not be used as a username at this LON-CAPA institution.');
1.1 raeburn 1111: } elsif ($error eq 'captcha') {
1112: $msg = &mt('Validation of the code your entered failed.');
1113: } elsif ($error eq 'noemails') {
1.16 raeburn 1114: $msg = &mt('Creation of a new user account using an e-mail address as username is not permitted at this LON-CAPA institution.');
1.1 raeburn 1115: }
1.14 raeburn 1116: $msg .= '</span>';
1.1 raeburn 1117: if ($msgtext) {
1118: $msg .= '<br />'.$msgtext;
1119: }
1.8 raeburn 1120: $msg .= &linkto_email_help($contact_email,$domdesc);
1121: return $msg;
1122: }
1123:
1124: sub linkto_email_help {
1125: my ($contact_email,$domdesc) = @_;
1126: my $msg;
1.1 raeburn 1127: if ($contact_email ne '') {
1128: my $escuri = &HTML::Entities::encode('/adm/createaccount','&<>"');
1.17 raeburn 1129: $msg .= '<br />'.&mt('You may wish to contact the [_1]LON-CAPA helpdesk[_2] for [_3].','<a href="/adm/helpdesk?origurl='.$escuri.'">','</a>',$domdesc).'<br />';
1.1 raeburn 1130: } else {
1.17 raeburn 1131: $msg .= '<br />'.&mt('You may wish to send an e-mail to the server administrator: [_1] for [_2].',$Apache::lonnet::perlvar{'AdminEmail'},$domdesc).'<br />';
1.1 raeburn 1132: }
1133: return $msg;
1134: }
1135:
1136: sub create_captcha {
1137: my ($output_dir,$db_dir) = @_;
1138: my %captcha_params = &captcha_settings();
1.28 raeburn 1139: my ($output,$maxtries,$tries) = ('',10,0);
1140: while ($tries < $maxtries) {
1141: $tries ++;
1142: my $captcha = Authen::Captcha->new (
1143: output_folder => $captcha_params{'output_dir'},
1144: data_folder => $captcha_params{'db_dir'},
1145: );
1146: my $md5sum = $captcha->generate_code($captcha_params{'numchars'});
1147:
1148: if (-e $Apache::lonnet::perlvar{'lonCaptchaDir'}.'/'.$md5sum.'.png') {
1149: $output = '<input type="hidden" name="crypt" value="'.$md5sum.'" />'."\n".
1150: &mt('Type in the letters/numbers shown below').' '.
1151: '<input type="text" size="5" name="code" value="" /><br />'.
1152: '<img src="'.$captcha_params{'www_output_dir'}.'/'.$md5sum.'.png">';
1153: last;
1154: }
1155: }
1.1 raeburn 1156: return $output;
1157: }
1158:
1159: sub captcha_settings {
1160: my %captcha_params = (
1.13 raeburn 1161: output_dir => $Apache::lonnet::perlvar{'lonCaptchaDir'},
1162: www_output_dir => "/captchaspool",
1163: db_dir => $Apache::lonnet::perlvar{'lonCaptchaDb'},
1.1 raeburn 1164: numchars => '5',
1165: );
1166: return %captcha_params;
1167: }
1168:
1169: sub getkeys {
1170: my ($lkey,$ukey) = @_;
1171: my $lextkey=hex($lkey);
1172: if ($lextkey>2147483647) { $lextkey-=4294967296; }
1173:
1174: my $uextkey=hex($ukey);
1175: if ($uextkey>2147483647) { $uextkey-=4294967296; }
1176: return ($lextkey,$uextkey);
1177: }
1178:
1179: sub serverform {
1.20 raeburn 1180: my ($logtoken,$lonhost,$mailtoken,$courseid,$context) = @_;
1.22 raeburn 1181: my $phase = 'username_validation';
1182: my $catalog_elements;
1.20 raeburn 1183: if ($context eq 'selfenroll') {
1184: $phase = 'selfenroll_login';
1185: }
1.22 raeburn 1186: if ($courseid ne '') {
1187: $catalog_elements = &Apache::lonhtmlcommon::echo_form_input(['courseid','phase']);
1188: }
1189: my $output = <<ENDSERVERFORM;
1.20 raeburn 1190: <form name="server" method="post" action="/adm/createaccount">
1.1 raeburn 1191: <input type="hidden" name="logtoken" value="$logtoken" />
1192: <input type="hidden" name="token" value="$mailtoken" />
1193: <input type="hidden" name="serverid" value="$lonhost" />
1194: <input type="hidden" name="uname" value="" />
1195: <input type="hidden" name="upass" value="" />
1.20 raeburn 1196: <input type="hidden" name="phase" value="$phase" />
1.3 raeburn 1197: <input type="hidden" name="courseid" value="$courseid" />
1.22 raeburn 1198: $catalog_elements
1.1 raeburn 1199: </form>
1200: ENDSERVERFORM
1201: return $output;
1202: }
1203:
1204: sub process_credentials {
1205: my ($logtoken,$lonhost) = @_;
1206: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$logtoken,$lonhost);
1207: my ($retrieved,$output,$upass);
1208: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
1.7 bisitz 1209: $output = &mt('Information needed to verify your login information is missing, inaccessible or expired.')
1210: .'<br />'.&mt('You may need to reload the previous page to obtain a new token.');
1.1 raeburn 1211: return ($retrieved,$output,$upass);
1212: } else {
1213: my $reply = &Apache::lonnet::reply('tmpdel:'.$logtoken,$lonhost);
1214: if ($reply eq 'ok') {
1215: $retrieved = 'ok';
1216: } else {
1217: $output = &mt('Session could not be opened.');
1218: }
1219: }
1220: my ($key,$caller)=split(/&/,$tmpinfo);
1221: if ($caller eq 'createaccount') {
1222: $upass = &Apache::lonpreferences::des_decrypt($key,$env{'form.upass'});
1223: } else {
1224: $output = &mt('Unable to retrieve your log-in information - unexpected context');
1225: }
1226: return ($retrieved,$output,$upass);
1227: }
1228:
1229: sub guest_format_check {
1230: my ($useremail,$domain,$cancreate,$settings) = @_;
1231: my ($login,$format_match,$format_msg,@user_rules);
1232: if (ref($settings) eq 'HASH') {
1233: if (ref($settings->{'email_rule'}) eq 'ARRAY') {
1234: push(@user_rules,@{$settings->{'email_rule'}});
1235: }
1236: }
1237: if (@user_rules > 0) {
1238: my %rule_check =
1239: &Apache::lonnet::inst_rulecheck($domain,$useremail,undef,
1.2 raeburn 1240: 'selfcreate',\@user_rules);
1.1 raeburn 1241: if (keys(%rule_check) > 0) {
1242: foreach my $item (keys(%rule_check)) {
1243: if ($rule_check{$item}) {
1244: $format_match = 1;
1245: last;
1246: }
1247: }
1248: }
1249: }
1250: if ($format_match) {
1251: ($login) = ($useremail =~ /^([^\@]+)\@/);
1252: $format_msg = '<br />'.&mt("Your e-mail address uses the same internet domain as your institution's LON-CAPA service.").'<br />'.&mt('Creation of a LON-CAPA account with this type of e-mail address as username is not permitted.').'<br />';
1.5 raeburn 1253: if (ref($cancreate) eq 'ARRAY') {
1254: if (grep(/^login$/,@{$cancreate})) {
1.7 bisitz 1255: $format_msg .= &mt('You should request creation of a LON-CAPA account for a log-in ID of "[_1]" at your institution instead.',$login).'<br />';
1.5 raeburn 1256: }
1.1 raeburn 1257: }
1258: }
1259: return $format_msg;
1260: }
1261:
1.17 raeburn 1262: sub sso_logout_frag {
1263: my ($r,$domain) = @_;
1264: my $endsessionmsg;
1265: if (defined($r->dir_config('lonSSOUserLogoutMessageFile_'.$domain))) {
1266: my $msgfile = $r->dir_config('lonSSOUserLogoutMessageFile_'.$domain);
1267: if (-e $msgfile) {
1268: open(my $fh,"<$msgfile");
1269: $endsessionmsg = join('',<$fh>);
1270: close($fh);
1271: }
1272: } elsif (defined($r->dir_config('lonSSOUserLogoutMessageFile'))) {
1273: my $msgfile = $r->dir_config('lonSSOUserLogoutMessageFile');
1274: if (-e $msgfile) {
1275: open(my $fh,"<$msgfile");
1276: $endsessionmsg = join('',<$fh>);
1277: close($fh);
1278: }
1279: }
1280: return $endsessionmsg;
1281: }
1282:
1.22 raeburn 1283: sub catreturn_js {
1284: return <<"ENDSCRIPT";
1285: <script type="text/javascript">
1286:
1287: function ToSelfenroll(formname) {
1288: var formidx = getFormByName(formname);
1289: if (formidx > -1) {
1290: document.forms[formidx].action = '/adm/selfenroll';
1291: numidx = getIndexByName(formidx,'phase');
1292: if (numidx > -1) {
1293: document.forms[formidx].elements[numidx].value = '';
1294: }
1295: numidx = getIndexByName(formidx,'context');
1296: if (numidx > -1) {
1297: document.forms[formidx].elements[numidx].value = '';
1298: }
1299: }
1300: document.forms[formidx].submit();
1301: }
1302:
1303: function ToCatalog(formname,caller) {
1304: var formidx = getFormByName(formname);
1305: if (formidx > -1) {
1306: document.forms[formidx].action = '/adm/coursecatalog';
1307: numidx = getIndexByName(formidx,'coursenum');
1308: if (numidx > -1) {
1309: if (caller != 'details') {
1310: document.forms[formidx].elements[numidx].value = '';
1311: }
1312: }
1313: }
1314: document.forms[formidx].submit();
1315: }
1316:
1317: function getIndexByName(formidx,item) {
1318: for (var i=0;i<document.forms[formidx].elements.length;i++) {
1319: if (document.forms[formidx].elements[i].name == item) {
1320: return i;
1321: }
1322: }
1323: return -1;
1324: }
1325:
1326: function getFormByName(item) {
1327: for (var i=0; i<document.forms.length; i++) {
1328: if (document.forms[i].name == item) {
1329: return i;
1330: }
1331: }
1332: return -1;
1333: }
1334:
1335: </script>
1336: ENDSCRIPT
1337:
1338: }
1339:
1.1 raeburn 1340: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>