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