File:
[LON-CAPA] /
loncom /
interface /
createaccount.pm
Revision
1.74:
download - view:
text,
annotated -
select for diffs
Thu Aug 3 16:22:08 2017 UTC (6 years, 11 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Self-creation of user accounts using e-mail verification.
- Options set by domain coordinator for account requests verified by e-mail
for: processing, restrictions on permitted e-mail addresses, and choice
of username can be set to different values, depending on requester's
(self-reported) institutional status.
- Setting which status types can be self-reported by requesters using
e-mail verification moved from
"Default authentication/language/timezone/portal/types" panel to
"Users self-creating accounts" panel.
- Web form used to request account prompts user for appropriate responses
depending on self-reported status, and e-mail and username constraints.
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: # kerberos, or SSO) or an e-mail address. Requests to use an e-mail address as
5: # username may be processed automatically, or may be queued for approval.
6: #
7: # $Id: createaccount.pm,v 1.74 2017/08/03 16:22:08 raeburn Exp $
8: #
9: # Copyright Michigan State University Board of Trustees
10: #
11: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
12: #
13: # LON-CAPA is free software; you can redistribute it and/or modify
14: # it under the terms of the GNU General Public License as published by
15: # the Free Software Foundation; either version 2 of the License, or
16: # (at your option) any later version.
17: #
18: # LON-CAPA is distributed in the hope that it will be useful,
19: # but WITHOUT ANY WARRANTY; without even the implied warranty of
20: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21: # GNU General Public License for more details.
22: #
23: # You should have received a copy of the GNU General Public License
24: # along with LON-CAPA; if not, write to the Free Software
25: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26: #
27: # /home/httpd/html/adm/gpl.txt
28: #
29: # http://www.lon-capa.org/
30: #
31: #
32: package Apache::createaccount;
33:
34: use strict;
35: use Apache::Constants qw(:common);
36: use Apache::lonacc;
37: use Apache::lonnet;
38: use Apache::loncommon;
39: use Apache::lonhtmlcommon;
40: use Apache::lonlocal;
41: use Apache::lonauth;
42: use Apache::resetpw;
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('SSOUserUnknown');
59: my $sso_domain = $r->subprocess_env->get('SSOUserDomain');
60:
61: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
62: ['token','courseid','domain','type']);
63: &Apache::lonacc::get_posted_cgi($r);
64: &Apache::lonlocal::get_language_handle($r);
65:
66: if ($sso_username ne '' && $sso_domain ne '') {
67: $domain = $sso_domain;
68: } else {
69: ($domain, undef) = Apache::lonnet::is_course($env{'form.courseid'});
70: unless ($domain) {
71: if ($env{'form.phase'} =~ /^username_(activation|validation)$/) {
72: if (($env{'form.udom'} =~ /^$match_domain$/) &&
73: (&Apache::lonnet::domain($env{'form.udom'}) ne '')) {
74: $domain = $env{'form.udom'};
75: } else {
76: $domain = &Apache::lonnet::default_login_domain();
77: }
78: } elsif (($env{'form.phase'} eq '') &&
79: ($env{'form.domain'} =~ /^$match_domain$/) &&
80: (&Apache::lonnet::domain($env{'form.domain'}) ne '')) {
81: $domain = $env{'form.domain'};
82: } else {
83: $domain = &Apache::lonnet::default_login_domain();
84: }
85: }
86: }
87: my $domdesc = &Apache::lonnet::domain($domain,'description');
88: my $contact_name = &mt('LON-CAPA helpdesk');
89: my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
90: my $contacts =
91: &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
92: $domain,$origmail);
93: my ($contact_email) = split(',',$contacts);
94: my $lonhost = $r->dir_config('lonHostID');
95: my $include = $r->dir_config('lonIncludes');
96: my $start_page;
97:
98: my $handle = &Apache::lonnet::check_for_valid_session($r);
99: if (($handle ne '') && ($handle !~ /^publicuser_\d+$/)) {
100: $start_page =
101: &Apache::loncommon::start_page('Already logged in');
102: my $end_page =
103: &Apache::loncommon::end_page();
104: $r->print($start_page."\n".'<h2>'.&mt('You are already logged in').'</h2>'.
105: '<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].',
106: '<a href="/adm/roles">','</a>','<a href="/adm/logout">','</a>').
107: '</p><p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'.$end_page);
108: return OK;
109: }
110:
111: my ($js,$courseid,$title);
112: $courseid = Apache::lonnet::is_course($env{'form.courseid'});
113: if ($courseid ne '') {
114: $js = &catreturn_js();
115: $title = 'Self-enroll in a LON-CAPA course';
116: } else {
117: $title = 'Create a user account in LON-CAPA';
118: }
119: if ($env{'form.phase'} eq 'selfenroll_login') {
120: $title = 'Self-enroll in a LON-CAPA course';
121: if ($env{'form.udom'} ne '') {
122: $domain = $env{'form.udom'};
123: }
124:
125: my %domconfig =
126: &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
127: my ($cancreate,$statustocreate) =
128: &get_creation_controls($domain,$domconfig{'usercreation'});
129:
130: my ($result,$output) =
131: &username_validation($r,$env{'form.uname'},$domain,$domdesc,
132: $contact_name,$contact_email,$courseid,
133: $lonhost,$statustocreate);
134: if ($result eq 'redirect') {
135: $r->internal_redirect('/adm/switchserver');
136: return OK;
137: } elsif ($result eq 'existingaccount') {
138: $r->print($output);
139: &print_footer($r);
140: return OK;
141: } else {
142: $start_page = &Apache::loncommon::start_page($title,$js);
143: &print_header($r,$start_page,$courseid);
144: $r->print($output);
145: &print_footer($r);
146: return OK;
147: }
148: }
149:
150: my %domconfig =
151: &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
152: my ($cancreate,$statustocreate,$statusforemail,$emailusername,
153: $emailoptions,$verification,$emaildomain,$types,$usertypes,$othertitle) =
154: &get_creation_controls($domain,$domconfig{'usercreation'});
155: my ($additems,$pagetitle);
156: if (ref($cancreate) eq 'ARRAY') {
157: unless (($env{'form.token'}) || ($sso_username ne '') || ($env{'form.phase'}) ||
158: ($env{'form.create_with_email'})) {
159: if ((grep(/^email$/,@{$cancreate})) && (ref($statusforemail) eq 'ARRAY')) {
160: my $usertype = &get_usertype($domain);
161: if ((($usertype eq '') || (!grep(/^\Q$usertype\E$/,@{$statusforemail}))) &&
162: (@{$statusforemail} > 0)) {
163: $js .= &setelements_js($statusforemail,$types,$usertypes,$othertitle);
164: $additems = {'add_entries' => { 'onload' => "setElements();"} };
165: if ((@{$cancreate} == 1) && (@{$statusforemail} > 0)) {
166: $pagetitle = 'Select affiliation';
167: }
168: } else {
169: $js .= &username_js();
170: }
171: }
172: }
173: }
174: $start_page = &Apache::loncommon::start_page($title,$js,$additems);
175: if (@{$cancreate} == 0) {
176: &print_header($r,$start_page,$courseid,$pagetitle);
177: my $output = '<h3>'.&mt('Account creation unavailable').'</h3>'.
178: '<span class="LC_warning">'.
179: &mt('Creation of a new user account using an institutional log-in ID or e-mail verification is not permitted for: [_1].',$domdesc).
180: '</span><br /><br />';
181: $r->print($output);
182: &print_footer($r);
183: return OK;
184: }
185:
186: if ($sso_username ne '') {
187: &print_header($r,$start_page,$courseid);
188: my ($msg,$sso_logout);
189: $sso_logout = &sso_logout_frag($r,$domain);
190: if (grep(/^sso$/,@{$cancreate})) {
191: $msg = '<h3>'.&mt('Account creation').'</h3>'.
192: &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 />';
193: my $shibenv;
194: if (($r->dir_config('lonOtherAuthen') eq 'yes') &&
195: ($r->dir_config('lonOtherAuthenType') eq 'Shibboleth')) {
196: if (ref($domconfig{'usercreation'}) eq 'HASH') {
197: if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
198: if (ref($domconfig{'usercreation'}{'cancreate'}{'shibenv'}) eq 'HASH') {
199: my @possfields = ('firstname','middlename','lastname','generation',
200: 'permanentemail','id');
201: $shibenv= {};
202: foreach my $key (keys(%{$domconfig{'usercreation'}{'cancreate'}{'shibenv'}})) {
203: if ($key eq 'inststatus') {
204: if (ref($usertypes) eq 'HASH') {
205: if ($domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key} ne '') {
206: if (exists($usertypes->{$domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key}})) {
207: $shibenv->{$key} = $domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key};
208: }
209: }
210: }
211: } elsif (grep(/^\Q$key\E/,@possfields)) {
212: if ($domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key} ne '') {
213: $shibenv->{$key} = $domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key};
214: }
215: }
216: }
217: }
218: }
219: }
220: }
221: $msg .= &username_check($sso_username,$domain,$domdesc,$courseid,
222: $lonhost,$contact_email,$contact_name,
223: $sso_logout,$statustocreate,$shibenv);
224: } else {
225: $msg = '<h3>'.&mt('Account creation unavailable').'</h3>'.
226: '<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 />'.
227: $sso_logout;
228: }
229: $r->print($msg);
230: &print_footer($r);
231: return OK;
232: }
233:
234: my ($output,$nostart,$noend,$redirect);
235: my $token = $env{'form.token'};
236: if ($token) {
237: ($output,$nostart,$noend,$redirect) =
238: &process_mailtoken($r,$token,$contact_name,$contact_email,$domain,
239: $domdesc,$lonhost,$include,$start_page,$cancreate,
240: $domconfig{'usercreation'},$types);
241: if ($redirect) {
242: $r->internal_redirect('/adm/switchserver');
243: return OK;
244: } elsif ($nostart) {
245: if ($noend) {
246: return OK;
247: } else {
248: $r->print($output);
249: &print_footer($r);
250: return OK;
251: }
252: } else {
253: &print_header($r,$start_page,$courseid);
254: $r->print($output);
255: &print_footer($r);
256: return OK;
257: }
258: }
259: my ($usernameset,$condition,$excluded,$hascustom);
260: if ((grep(/^email$/,@{$cancreate})) && (($env{'form.create_with_email'}) ||
261: ((!$token) && ($env{'form.phase'} eq '')))) {
262: my $usertype = &get_usertype($domain);
263: if ($usertype eq '') {
264: $usertype = 'default';
265: }
266: if (ref($verification) eq 'HASH') {
267: if ($verification->{$usertype} =~ /^(free|first)$/) {
268: $usernameset = $verification->{$usertype};
269: }
270: }
271: if (ref($emailoptions) eq 'HASH') {
272: if ($emailoptions->{$usertype} =~ /^(inst|noninst)$/) {
273: my $chosen = $1;
274: if (ref($emaildomain) eq 'HASH') {
275: if (ref($emaildomain->{$usertype}) eq 'HASH') {
276: if ($chosen eq 'inst') {
277: $condition = $emaildomain->{$usertype}->{$chosen};
278: } else {
279: $excluded = $emaildomain->{$usertype}->{$chosen};
280: }
281: }
282: }
283: } elsif ($emailoptions->{$usertype} eq 'custom') {
284: $hascustom = 1;
285: }
286: }
287: }
288: if ($env{'form.phase'} eq 'username_activation') {
289: (my $result,$output,$nostart) =
290: &username_activation($r,$env{'form.uname'},$domain,$domdesc,
291: $courseid);
292: if ($result eq 'redirect') {
293: $r->internal_redirect('/adm/switchserver');
294: return OK;
295: } elsif ($result eq 'ok') {
296: if ($nostart) {
297: return OK;
298: }
299: }
300: &print_header($r,$start_page,$courseid);
301: $r->print($output);
302: &print_footer($r);
303: return OK;
304: } elsif ($env{'form.phase'} eq 'username_validation') {
305: (my $result,$output) =
306: &username_validation($r,$env{'form.uname'},$domain,$domdesc,
307: $contact_name,$contact_email,$courseid,
308: $lonhost,$statustocreate);
309: if ($result eq 'existingaccount') {
310: $r->print($output);
311: &print_footer($r);
312: return OK;
313: } else {
314: &print_header($r,$start_page,$courseid);
315: }
316: } elsif ($env{'form.create_with_email'}) {
317: &print_header($r,$start_page,$courseid);
318: my $usertype = &get_usertype($domain);
319: if ($usertype eq '') {
320: $usertype = 'default';
321: }
322: $output = &process_email_request($env{'form.uname'},$domain,$domdesc,
323: $contact_name,$contact_email,$cancreate,
324: $lonhost,$domconfig{'usercreation'},
325: $emailusername,$courseid,$usertype,
326: $usernameset,$condition,$excluded,$hascustom);
327: } elsif (!$token) {
328: &print_header($r,$start_page,$courseid,$pagetitle);
329: my $now=time;
330: if ((grep(/^login$/,@{$cancreate})) && (!grep(/^email$/,@{$cancreate}))) {
331: if (open(my $jsh,"<$include/londes.js")) {
332: while(my $line = <$jsh>) {
333: $r->print($line);
334: }
335: close($jsh);
336: $r->print(&javascript_setforms($now));
337: }
338: }
339: if (grep(/^email$/,@{$cancreate})) {
340: $r->print(&javascript_validmail($condition));
341: }
342: my $usertype = &get_usertype($domain);
343: $output = &print_username_form($r,$domain,$domdesc,$cancreate,$now,$lonhost,
344: $include,$courseid,$emailusername,
345: $statusforemail,$usernameset,$condition,
346: $excluded,$usertype,$types,$usertypes,$othertitle);
347: }
348: $r->print($output);
349: &print_footer($r);
350: return OK;
351: }
352:
353: sub print_header {
354: my ($r,$start_page,$courseid,$pagetitle) = @_;
355: $r->print($start_page);
356: &Apache::lonhtmlcommon::clear_breadcrumbs();
357: my $url = '/adm/createaccount';
358: if ($pagetitle eq '') {
359: $pagetitle = 'New username';
360: }
361: if ($courseid ne '') {
362: my %coursehash = &Apache::lonnet::coursedescription($courseid);
363: &selfenroll_crumbs($r,$courseid,$coursehash{'description'});
364: }
365: if ($env{'form.reportedtype'}) {
366: &Apache::lonhtmlcommon::add_breadcrumb
367: ({href=>$url,
368: text=>"Select affiliation"});
369: }
370: &Apache::lonhtmlcommon::add_breadcrumb
371: ({href=>$url,
372: text=>$pagetitle});
373: $r->print(&Apache::lonhtmlcommon::breadcrumbs('Create account'));
374: return;
375: }
376:
377: sub print_footer {
378: my ($r) = @_;
379: if ($env{'form.courseid'} ne '') {
380: $r->print('<form name="backupcrumbs" method="post" action="">'.
381: &Apache::lonhtmlcommon::echo_form_input(['backto','logtoken',
382: 'token','serverid','uname','upass','phase','create_with_email',
383: 'code','crypt','cfirstname','clastname','g-recaptcha-response',
384: 'recaptcha_challenge_field','recaptcha_response_field',
385: 'cmiddlename','cgeneration','cpermanentemail','cid']).
386: '</form>');
387: }
388: $r->print(&Apache::loncommon::end_page());
389: }
390:
391: sub get_usertype {
392: my ($domain) = @_;
393: my $usertype;
394: my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($domain);
395: if (ref($types) eq 'ARRAY') {
396: push(@{$types},'default');
397: my $posstype = $env{'form.type'};
398: $posstype =~ s/^\s+|\s$//g;
399: if (grep(/^\Q$posstype\E$/,@{$types})) {
400: $usertype = $posstype;
401: }
402: }
403: return $usertype;
404: }
405:
406: sub selfenroll_crumbs {
407: my ($r,$courseid,$desc) = @_;
408: &Apache::lonhtmlcommon::add_breadcrumb
409: ({href=>"javascript:ToCatalog('backupcrumbs','')",
410: text=>"Course/Community Catalog"});
411: if ($env{'form.coursenum'} ne '') {
412: &Apache::lonhtmlcommon::add_breadcrumb
413: ({href=>"javascript:ToCatalog('backupcrumbs','details')",
414: text=>"Course details"});
415: }
416: my $last_crumb;
417: if ($desc ne '') {
418: $last_crumb = &mt("Self-enroll in [_1]","'$desc'");
419: } else {
420: $last_crumb = &mt('Self-enroll');
421: }
422: &Apache::lonhtmlcommon::add_breadcrumb
423: ({href=>"javascript:ToSelfenroll('backupcrumbs')",
424: text=>$last_crumb,
425: no_mt=>"1"});
426: return;
427: }
428:
429: sub javascript_setforms {
430: my ($now,$emailusername,$captcha,$usertype,$recaptchaversion,$usernameset,$condition,$excluded) = @_;
431: my ($setuserinfo,@required,$requiredchk);
432: if (ref($emailusername) eq 'HASH') {
433: if (ref($emailusername->{$usertype}) eq 'HASH') {
434: foreach my $key (sort(keys(%{$emailusername->{$usertype}}))) {
435: if ($emailusername->{$usertype}{$key} eq 'required') {
436: push(@required,$key);
437: }
438: $setuserinfo .= ' server.elements.'.$key.'.value=client.elements.'.$key.'.value;'."\n";
439: }
440: $setuserinfo .= ' server.elements.type.value=client.elements.type.value;'."\n";
441: }
442: if ($captcha eq 'original') {
443: $setuserinfo .= ' server.elements.code.value=client.elements.code.value;'."\n".
444: ' server.elements.crypt.value=client.elements.crypt.value;'."\n";
445: } elsif ($captcha eq 'recaptcha') {
446: if ($recaptchaversion ne '2') {
447: $setuserinfo .=
448: ' server.elements.recaptcha_challenge_field.value=client.elements.recaptcha_challenge_field.value;'."\n".
449: ' server.elements.recaptcha_response_field.value=client.elements.recaptcha_response_field.value;'."\n";
450: }
451: }
452: if ($usernameset eq 'free') {
453: $setuserinfo .=
454: ' server.elements.username.value=client.elements.username.value;'."\n";
455: }
456: }
457: if (@required) {
458: my $missprompt = &mt('One or more required fields are currently blank.');
459: &js_escape(\$missprompt);
460: my $reqstr = join("','",@required);
461: $requiredchk = <<"ENDCHK";
462: var requiredfields = new Array('$reqstr');
463: missing = 0;
464: for (var i=0; i<requiredfields.length; i++) {
465: try {
466: eval("client.elements."+requiredfields[i]+".value");
467: }
468: catch(err) {
469: continue;
470: }
471: if (eval("client.elements."+requiredfields[i]+".value") == '') {
472: missing ++;
473: }
474: }
475: if (missing > 0) {
476: alert("$missprompt");
477: return false;
478: }
479:
480: ENDCHK
481: }
482: my $js = <<ENDSCRIPT;
483: <script type="text/javascript">
484: // <![CDATA[
485: function send(one,two,context) {
486: var server;
487: var client;
488: if (document.forms[one]) {
489: server = document.forms[one];
490: if (document.forms[two]) {
491: client = document.forms[two];
492: $requiredchk
493: server.elements.uname.value = client.elements.uname.value;
494: server.elements.udom.value = client.elements.udom.value;
495:
496: uextkey=client.elements.uextkey.value;
497: lextkey=client.elements.lextkey.value;
498: initkeys();
499: server.elements.upass.value
500: = getCrypted(client.elements.upass$now.value);
501: client.elements.uname.value='';
502: client.elements.upass$now.value='';
503: if (context == 'email') {
504: $setuserinfo
505: client.elements.upasscheck$now.value='';
506: }
507: server.submit();
508: }
509: }
510: return false;
511: }
512:
513: // ]]>
514: </script>
515: ENDSCRIPT
516: if (($captcha eq 'recaptcha') && ($recaptchaversion eq '2')) {
517: $js .= "\n".'<script src="https://www.google.com/recaptcha/api.js"></script>'."\n";
518: }
519: return $js;
520: }
521:
522: sub javascript_checkpass {
523: my ($now,$context) = @_;
524: my $nopass = &mt('You must enter a password.');
525: my $mismatchpass = &mt('The passwords you entered did not match.')."\n".
526: &mt('Please try again.');
527: &js_escape(\$nopass);
528: &js_escape(\$mismatchpass);
529: my $js = <<"ENDSCRIPT";
530: <script type="text/javascript">
531: // <![CDATA[
532: function checkpass(one,two) {
533: var client;
534: if (document.forms[two]) {
535: client = document.forms[two];
536: var upass = client.elements.upass$now.value;
537: var upasscheck = client.elements.upasscheck$now.value;
538: if (upass == '') {
539: alert("$nopass");
540: return false;
541: }
542: if (upass == upasscheck) {
543: client.elements.upasscheck$now.value='';
544: if (validate_email(client)) {
545: send(one,two,'$context');
546: }
547: return false;
548: } else {
549: alert("$mismatchpass");
550: return false;
551: }
552: }
553: return false;
554: }
555: // ]]>
556: </script>
557: ENDSCRIPT
558: return $js;
559: }
560:
561: sub javascript_validmail {
562: my ($condition) = @_;
563: my %js_lt = &Apache::lonlocal::texthash (
564: email => 'The e-mail address you entered',
565: notv => 'is not a valid e-mail address',
566: avae => 'A valid e-mail address is not formed when the value you entered',
567: isco => 'is combined with the required domain',
568: );
569: my $output = "\n".'<script type="text/javascript">'."\n".
570: '// <![CDATA['."\n".
571: &Apache::lonhtmlcommon::javascript_valid_email()."\n";
572: &js_escape(\%js_lt);
573: $output .= <<"ENDSCRIPT";
574: function validate_email(client) {
575: field = client.uname;
576: var condition = '$condition';
577: if (validmail(field,condition) == false) {
578: if ((condition != undefined) && (condition != '')) {
579: alert("$js_lt{'avae'}: "+field.value+" $js_lt{'isco'}: "+condition);
580: } else {
581: alert("$js_lt{'email'}: "+field.value+" $js_lt{'notv'}.");
582: }
583: return false;
584: }
585: return true;
586: }
587: ENDSCRIPT
588: $output .= "\n".'// ]]>'."\n".'</script>'."\n";
589: return $output;
590: }
591:
592: sub print_username_form {
593: my ($r,$domain,$domdesc,$cancreate,$now,$lonhost,$include,$courseid,$emailusername,
594: $statusforemail,$usernameset,$condition,$excluded,$usertype,$types,$usertypes,
595: $othertitle) = @_;
596: my %lt = &Apache::lonlocal::texthash (
597: crac => 'Create account with a username provided by this institution',
598: clca => 'Create LON-CAPA account',
599: type => 'Type in your log-in ID and password to find out.',
600: plse => 'Please provide a password for your new account.',
601: info => 'Please provide user information and a password for your new account.',
602: yopw => 'Your password will be encrypted when sent (and stored).',
603: crae => 'Create account using e-mail address verification',
604: );
605: my $output;
606: if (ref($cancreate) eq 'ARRAY') {
607: if (grep(/^login$/,@{$cancreate})) {
608: my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
609: if ((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
610: $output = '<div class="LC_left_float"><h3>'.$lt{'crac'}.'</h3>';
611: $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 />').
612: '<br /><br />'.
613: $lt{'type'}.
614: '<br /><br />';
615: $output .= &login_box($now,$lonhost,$courseid,$lt{'clca'},
616: $domain,'createaccount').'</div>';
617: }
618: }
619: if (grep(/^email$/,@{$cancreate})) {
620: $output .= '<div class="LC_left_float"><h3>'.$lt{'crae'}.'</h3>';
621: if ($usertype ne '') {
622: if ((ref($statusforemail) eq 'ARRAY') && (@{$statusforemail} > 0)) {
623: unless (grep(/^\Q$usertype\E$/,@{$statusforemail})) {
624: undef($usertype);
625: }
626: } elsif ($usertype ne 'default') {
627: undef($usertype);
628: }
629: }
630: if (($usertype eq '') && (ref($statusforemail) eq 'ARRAY') &&
631: (@{$statusforemail} > 0) && (ref($types) eq 'ARRAY') && (@{$types} > 0)) {
632: my @posstypes = @{$types};
633: unless (grep(/^default$/,@posstypes)) {
634: push(@posstypes,'default');
635: }
636: $output .= '<p>'.&mt('Choose your affiliation at [_1]',$domdesc).'</p>'."\n".
637: '<form name="reportstatus" id="LC_reportstatus" action="" method="post" '.
638: 'onsubmit="return checkVerification();"><p>';
639: foreach my $type (@posstypes) {
640: my $name;
641: if ($type eq 'default') {
642: $name = $othertitle;
643: } else {
644: $name = $type;
645: if (ref($usertypes) eq 'HASH') {
646: if (exists($usertypes->{$type})) {
647: $name = $usertypes->{$type};
648: }
649: }
650: }
651: $output .= '<label><input type="radio" name="type" value="'.$type.'" />'.
652: $name.'</label>'.(' 'x2);
653: }
654: if ($env{'form.courseid'} =~ /^$match_domain\_$match_courseid$/) {
655: $output .= "\n".'<input type="hidden" name="courseid" value="'.$env{'form.courseid'}.'" />';
656: }
657: $output .= '</p>'."\n".'<p><input type="submit" name="reportedtype" value="'.&mt('Submit').'" /></p></form>';
658: } else {
659: my ($captchaform,$error,$captcha,$recaptchaversion) =
660: &Apache::loncommon::captcha_display('usercreation',$lonhost);
661: if ($error) {
662: my $helpdesk = '/adm/helpdesk?origurl=%2fadm%2fcreateaccount';
663: if ($courseid ne '') {
664: $helpdesk .= '&courseid='.$courseid;
665: }
666: $output .= '<span class="LC_error">'.
667: &mt('An error occurred generating the validation code[_1] required for use of an e-mail address to request a LON-CAPA account.','<br />').
668: '</span><br /><br />'.
669: &mt('[_1]Contact the helpdesk[_2] or [_3]reload[_2] the page and try again.',
670: '<a href="'.$helpdesk.'">','</a>','<a href="javascript:window.location.reload()">');
671: } else {
672: if (grep(/^login$/,@{$cancreate})) {
673: $output .= &mt('If you do not have a log-in ID at your institution, [_1]provide your e-mail address to request a LON-CAPA account.','<br />').'<br /><br />'.
674: $lt{'plse'}.'<br />'.
675: $lt{'yopw'}.'<br />';
676: } else {
677: my $prompt = $lt{'plse'};
678: if (ref($emailusername) eq 'HASH') {
679: if (ref($emailusername->{$usertype}) eq 'HASH') {
680: if (keys(%{$emailusername->{$usertype}}) > 0) {
681: $prompt = $lt{'info'};
682: }
683: }
684: }
685: $output .= $prompt.'<br />'.
686: $lt{'yopw'}.'<br />';
687: }
688: if ($usertype eq '') {
689: $usertype = 'default';
690: }
691: $output .= &print_dataentry_form($r,$domain,$lonhost,$include,$now,$captchaform,
692: $courseid,$emailusername,$captcha,$usertype,
693: $recaptchaversion,$usernameset,$condition,$excluded);
694: }
695: }
696: $output .= '</div>';
697: }
698: }
699: if ($output eq '') {
700: $output = &mt('Creation of a new LON-CAPA user account using an institutional log-in ID or verification by e-mail is not permitted at [_1].',$domdesc);
701: } else {
702: $output .= '<div class="LC_clear_float_footer"></div>';
703: }
704: return $output;
705: }
706:
707: sub login_box {
708: my ($now,$lonhost,$courseid,$submit_text,$domain,$context) = @_;
709: my $output;
710: my %titles = &Apache::lonlocal::texthash(
711: createaccount => 'Log-in ID',
712: selfenroll => 'Username',
713: );
714: my ($lkey,$ukey) = &Apache::loncommon::des_keys();
715: my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
716: my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount:createaccount',
717: $lonhost);
718: $output = &serverform($logtoken,$lonhost,undef,$courseid,$context);
719: my $unameform = '<input type="text" name="uname" size="20" value="" autocomplete="off" />';
720: my $upassform = '<input type="password" name="upass'.$now.'" size="20" autocomplete="off" />';
721: $output .= '<form name="client" method="post" action="" onsubmit="return(send('."'server','client'".'));">'."\n".
722: &Apache::lonhtmlcommon::start_pick_box()."\n".
723: &Apache::lonhtmlcommon::row_title($titles{$context},
724: 'LC_pick_box_title')."\n".
725: $unameform."\n".
726: &Apache::lonhtmlcommon::row_closure(1)."\n".
727: &Apache::lonhtmlcommon::row_title(&mt('Password'),
728: 'LC_pick_box_title')."\n".
729: $upassform;
730: if ($context eq 'selfenroll') {
731: my $udomform = '<input type="text" name="udom" size="10" value="'.
732: $domain.'" />';
733: $output .= &Apache::lonhtmlcommon::row_closure(1)."\n".
734: &Apache::lonhtmlcommon::row_title(&mt('Domain'),
735: 'LC_pick_box_title')."\n".
736: $udomform."\n";
737: } else {
738: $output .= '<input type="hidden" name="udom" value="'.$domain.'" />';
739: }
740: $output .= &Apache::lonhtmlcommon::row_closure(1).
741: &Apache::lonhtmlcommon::row_title().
742: '<br /><input type="submit" name="username_validation" value="'.
743: $submit_text.'" />'."\n";
744: if ($context eq 'selfenroll') {
745: $output .= '<br /><br /><table width="100%"><tr><td align="right">'.
746: '<span class="LC_fontsize_medium">'.
747: '<a href="/adm/resetpw">'.&mt('Forgot password?').'</a>'.
748: '</span></td></tr></table>'."\n";
749: }
750: $output .= &Apache::lonhtmlcommon::row_closure(1)."\n".
751: &Apache::lonhtmlcommon::end_pick_box().'<br />'."\n";
752: $output .= '<input type="hidden" name="lextkey" value="'.$lextkey.'" />'."\n".
753: '<input type="hidden" name="uextkey" value="'.$uextkey.'" />'."\n".
754: '</form>';
755: return $output;
756: }
757:
758: sub process_email_request {
759: my ($useremail,$domain,$domdesc,$contact_name,$contact_email,$cancreate,
760: $server,$settings,$emailusername,$courseid,$usertype,$usernameset,
761: $condition,$excluded,$hascustom) = @_;
762: my ($output,$uname);
763: if (ref($cancreate) eq 'ARRAY') {
764: if (!grep(/^email$/,@{$cancreate})) {
765: $output = &invalid_state('noemails',$domdesc,
766: $contact_name,$contact_email);
767: return $output;
768: } elsif ((($condition ne '') && ($useremail !~ /^[^\@]+$/)) ||
769: (($condition eq '') && ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/))) {
770: $output = &invalid_state('baduseremail',$domdesc,
771: $contact_name,$contact_email);
772: return $output;
773: } else {
774: $useremail =~ s/^\s+|\s+$//g;
775: my $possuname;
776: if ($condition ne '') {
777: if ($usernameset eq 'first') {
778: $possuname = $useremail;
779: }
780: $useremail .= $condition;
781: } elsif ($excluded ne '') {
782: if ($useremail =~ /^[^\@]+\Q$excluded\E$/) {
783: $output = &invalid_state('userrules',$domdesc,
784: $contact_name,$contact_email);
785: return $output;
786: }
787: }
788: if (($usernameset eq 'free') && ($env{'form.username'} ne '')) {
789: $possuname = $env{'form.username'};
790: } elsif (($usernameset eq 'first') && ($condition eq '')) {
791: if ($condition eq '') {
792: ($possuname) = ($useremail =~ /^([^\@]+)\@/);
793: }
794: }
795: if ($possuname ne '') {
796: $possuname =~ s/^\s+|\s+$//g;
797: if ($possuname ne '') {
798: $uname=&LONCAPA::clean_username($possuname);
799: if ($uname ne $possuname) {
800: $output = &invalid_state('badusername',$domdesc,
801: $contact_name,$contact_email);
802: return $output;
803: }
804: }
805: }
806: if ($possuname eq '') {
807: $uname=&LONCAPA::clean_username($useremail);
808: if ($useremail ne $uname) {
809: $output = &invalid_state('badusername',$domdesc,
810: $contact_name,$contact_email);
811: return $output;
812: }
813: }
814: my $uhome = &Apache::lonnet::homeserver($uname,$domain);
815: if ($uhome ne 'no_host') {
816: $output = &invalid_state('existinguser',$domdesc,
817: $contact_name,$contact_email);
818: return $output;
819: } else {
820: my ($captcha_chk,$captcha_error) = &Apache::loncommon::captcha_response('usercreation',$server);
821: if ($captcha_chk != 1) {
822: $output = &invalid_state('captcha',$domdesc,$contact_name,
823: $contact_email,$captcha_error);
824: return $output;
825: }
826: my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts);
827: &call_rulecheck($uname,$domain,\%alerts,\%rulematch,
828: \%inst_results,\%curr_rules,\%got_rules,'username');
829: if (ref($alerts{'username'}) eq 'HASH') {
830: if (ref($alerts{'username'}{$domain}) eq 'HASH') {
831: if ($alerts{'username'}{$domain}{$uname}) {
832: $output = &invalid_state('userrules',$domdesc,
833: $contact_name,$contact_email);
834: return $output;
835: }
836: }
837: }
838: if ($hascustom) {
839: my $format_msg =
840: &guest_format_check($useremail,$domain,$cancreate,
841: $settings,$usertype);
842: if ($format_msg) {
843: $output = &invalid_state('userformat',$domdesc,$contact_name,
844: $contact_email,$format_msg);
845: return $output;
846: }
847: }
848: }
849: }
850: $output = &send_token($domain,$useremail,$server,$domdesc,$contact_name,
851: $contact_email,$courseid,$emailusername,$usertype,
852: $uname);
853: }
854: return $output;
855: }
856:
857: sub call_rulecheck {
858: my ($uname,$udom,$alerts,$rulematch,$inst_results,$curr_rules,
859: $got_rules,$tocheck) = @_;
860: my ($checkhash,$checks);
861: $checkhash->{$uname.':'.$udom} = { 'newuser' => 1, };
862: if ($tocheck eq 'username') {
863: $checks = { 'username' => 1 };
864: }
865: &Apache::loncommon::user_rule_check($checkhash,$checks,
866: $alerts,$rulematch,$inst_results,$curr_rules,
867: $got_rules);
868: return;
869: }
870:
871: sub send_token {
872: my ($domain,$email,$server,$domdesc,$contact_name,$contact_email,$courseid,$emailusername,
873: $usertype,$uname) = @_;
874: my $msg = '<h3>'.&mt('Account creation status').'</h3>'.
875: &mt('Thank you for your request to create a new LON-CAPA account.').
876: '<br /><br />';
877: my $now = time;
878: $env{'form.logtoken'} =~ s/(`)//g;
879: if ($env{'form.logtoken'}) {
880: my $logtoken = $env{'form.logtoken'};
881: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$logtoken,$server);
882: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
883: $msg = &mt('Information needed to process your request is missing, inaccessible or expired.')
884: .'<br />'.&mt('Return to the previous page to try again.');
885: } else {
886: my $reply = &Apache::lonnet::reply('tmpdel:'.$logtoken,$server);
887: unless ($reply eq 'ok') {
888: $msg .= &mt('Request could not be processed.');
889: }
890: }
891: my %info = ('ip' => $ENV{'REMOTE_ADDR'},
892: 'time' => $now,
893: 'domain' => $domain,
894: 'username' => $email,
895: 'courseid' => $courseid,
896: 'upass' => $env{'form.upass'},
897: 'serverid' => $env{'form.serverid'},
898: 'tmpinfo' => $tmpinfo);
899: if ($uname ne '') {
900: $info{'username'} = $uname;
901: $info{'email'} = $email;
902: }
903: if (ref($emailusername) eq 'HASH') {
904: if (ref($emailusername->{$usertype}) eq 'HASH') {
905: foreach my $item (keys(%{$emailusername->{$usertype}})) {
906: $info{$item} = $env{'form.'.$item};
907: $info{$item} =~ s/(`)//g;
908: }
909: }
910: }
911: if ($usertype ne '') {
912: $info{'usertype'} = $usertype;
913: }
914: my $token = &Apache::lonnet::tmpput(\%info,$server,'createaccount');
915: if ($token !~ /^error/ && $token ne 'no_such_host') {
916: my $esc_token = &escape($token);
917: my $showtime = localtime(time);
918: my $mailmsg = &mt('A request was submitted on [_1] for creation of a LON-CAPA account at the following institution: [_2].',$showtime,$domdesc).' '.
919: &mt('To complete this process please open a web browser and enter the following URL in the address/location box: [_1]',
920: &Apache::lonnet::absolute_url().'/adm/createaccount?token='.$esc_token);
921: my $result = &Apache::resetpw::send_mail($domdesc,$email,$mailmsg,$contact_name,
922: $contact_email);
923: if ($result eq 'ok') {
924: $msg .= &mt('A message has been sent to the e-mail address you provided.').'<br />'.
925: &mt('The message includes the web address for the link you will use to complete the account creation process.').'<br />'.
926: &mt("The link included in the message will be valid for the next [_1]two[_2] hours.",'<b>','</b>');
927: } else {
928: $msg .= '<span class="LC_error">'.
929: &mt('An error occurred when sending a message to the e-mail address you provided.').'</span><br />'.
930: ' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
931: }
932: } else {
933: $msg .= '<span class="LC_error">'.
934: &mt('An error occurred creating a token required for the account creation process.').'</span><br />'.
935: ' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
936: }
937: } else {
938: $msg .= $msg = &mt('Information needed to process your request is missing, inaccessible or expired.')
939: .'<br />'.&mt('Return to the previous page to try again.');
940:
941: }
942: return $msg;
943: }
944:
945: sub process_mailtoken {
946: my ($r,$token,$contact_name,$contact_email,$domain,$domdesc,$lonhost,
947: $include,$start_page,$cancreate,$settings,$types) = @_;
948: my ($msg,$nostart,$noend,$redirect);
949: my %data = &Apache::lonnet::tmpget($token);
950: my $now = time;
951: if (keys(%data) == 0) {
952: $msg = &mt('Sorry, the URL you provided to complete creation of a new LON-CAPA account was invalid.')
953: .' '.&mt('Either the token included in the URL has been deleted or the URL you provided was invalid.')
954: .' '.&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.',
955: '<a href="/adm/createaccount">','</a>');
956: return $msg;
957: }
958: if (($data{'time'} =~ /^\d+$/) &&
959: ($data{'domain'} ne '') &&
960: ((($data{'email'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && ($data{'username'} =~ /^$match_username$/)) ||
961: ($data{'username'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/))) {
962: if ($now - $data{'time'} < 7200) {
963: # Check if request should be queued.
964: if (ref($cancreate) eq 'ARRAY') {
965: my ($disposition,$usertype);
966: if (grep(/^email$/,@{$cancreate})) {
967: if (exists($data{'usertype'})) {
968: $usertype = $data{'usertype'};
969: my @posstypes;
970: if (ref($types) eq 'ARRAY') {
971: @posstypes = @{$types};
972: if (@posstypes) {
973: unless (grep(/^default$/,@posstypes)) {
974: push(@posstypes,'default');
975: }
976: }
977: if (grep(/\Q$usertype\E/,@posstypes)) {
978: unless ($usertype eq 'default') {
979: $data{'inststatus'} = $usertype;
980: }
981: } else {
982: $disposition = 'approval';
983: }
984: }
985: delete($data{'usertype'});
986: }
987: if (ref($settings) eq 'HASH') {
988: if (ref($settings->{'cancreate'}) eq 'HASH') {
989: if (ref($settings->{'cancreate'}{'selfcreateprocessing'}) eq 'HASH') {
990: if ($usertype ne '') {
991: $disposition = $settings->{'cancreate'}{'selfcreateprocessing'}{$usertype};
992: unless ($disposition =~ /^(approval|automatic)$/) {
993: $disposition = 'approval';
994: }
995: }
996: }
997: }
998: }
999: if ($disposition eq 'approval') {
1000: $msg = &store_request($domain,$data{'username'},'approval',\%data,$settings);
1001: my $delete = &Apache::lonnet::tmpdel($token);
1002: } else {
1003: my ($result,$output,$uhome) =
1004: &create_account($r,$domain,$domdesc,\%data);
1005: if ($result eq 'ok') {
1006: $msg = $output;
1007: my $shownow = &Apache::lonlocal::locallocaltime($now);
1008: 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";
1009: my $mailresult = &Apache::resetpw::send_mail($domdesc,$data{'email'},
1010: $mailmsg,$contact_name,
1011: $contact_email);
1012: if ($mailresult eq 'ok') {
1013: $msg .= &mt('An e-mail confirming creation of your new LON-CAPA account has been sent to [_1].',$data{'username'});
1014: } else {
1015: $msg .= &mt('An error occurred when sending e-mail to [_1] confirming creation of your LON-CAPA account.',$data{'username'});
1016: }
1017: $redirect = &start_session($r,$data{'username'},$domain,$uhome,
1018: $data{'courseid'},$token);
1019: $nostart = 1;
1020: $noend = 1;
1021: } else {
1022: $msg .= &mt('A problem occurred when attempting to create your new LON-CAPA account.')
1023: .'<br />'.$output;
1024: if (($contact_name ne '') && ($contact_email ne '')) {
1025: $msg .= &mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1026: }
1027: }
1028: my $delete = &Apache::lonnet::tmpdel($token);
1029: }
1030: } else {
1031: $msg = &invalid_state('noemails',$domdesc,$contact_name,$contact_email);
1032: }
1033: } else {
1034: $msg = &invalid_state('noemails',$domdesc,$contact_name,$contact_email);
1035: }
1036: } else {
1037: $msg = &mt('Sorry, the token generated when you requested creation of an account has expired.')
1038: .' '.&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>');
1039: }
1040: } else {
1041: $msg .= &mt('Sorry, the URL generated when you requested creation of an account contained incomplete information.')
1042: .' '.&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>');
1043: }
1044: return ($msg,$nostart,$noend,$redirect);
1045: }
1046:
1047: sub start_session {
1048: my ($r,$username,$domain,$uhome,$courseid,$token) = @_;
1049: my ($is_balancer) = &Apache::lonnet::check_loadbalancing($username,$domain);
1050: if ($is_balancer) {
1051: Apache::lonauth::success($r, $username, $domain, $uhome,
1052: 'noredirect', undef, {});
1053:
1054: Apache::lonnet::tmpdel($token) if $token;
1055:
1056: return 'redirect';
1057: } else {
1058: $courseid = Apache::lonnet::is_course($courseid);
1059:
1060: Apache::lonauth::success($r, $username, $domain, $uhome,
1061: ($courseid ? "/adm/selfenroll?courseid=$courseid" : '/adm/roles'),
1062: undef, {});
1063: }
1064: return;
1065: }
1066:
1067: #
1068: # The screen that the user gets to create his or her account
1069: # Desired username, desired password, etc
1070: # Stores token to store DES-key and stage during creation session
1071: #
1072: sub print_dataentry_form {
1073: my ($r,$domain,$lonhost,$include,$now,$captchaform,$courseid,$emailusername,$captcha,
1074: $usertype,$recaptchaversion,$usernameset,$condition,$excluded) = @_;
1075: my ($error,$output);
1076: if (open(my $jsh,"<$include/londes.js")) {
1077: while(my $line = <$jsh>) {
1078: $r->print($line);
1079: }
1080: close($jsh);
1081: $output = &javascript_setforms($now,$emailusername,$captcha,$usertype,$recaptchaversion,
1082: $usernameset,$condition,$excluded).
1083: "\n".&javascript_checkpass($now,'email');
1084: my ($lkey,$ukey) = &Apache::loncommon::des_keys();
1085: my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
1086: my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount:createaccount',
1087: $lonhost);
1088: $output .=
1089: '<form name="createaccount" method="post" target="_top" action="/adm/createaccount">';
1090: if ($courseid ne '') {
1091: $output .= '<input type="hidden" name="courseid" value="'.$courseid.'"/>'."\n";
1092: }
1093: if (ref($emailusername) eq 'HASH') {
1094: if (ref($emailusername->{$usertype}) eq 'HASH') {
1095: foreach my $field (sort(keys(%{$emailusername->{$usertype}}))) {
1096: $output .= '<input type="hidden" name="'.$field.'" value="" />'."\n";
1097: }
1098: }
1099: }
1100: if ($captcha eq 'original') {
1101: $output .= '
1102: <input type="hidden" name="crypt" value="" />
1103: <input type="hidden" name="code" value="" />
1104: ';
1105: } elsif ($captcha eq 'recaptcha') {
1106: if ($recaptchaversion eq '2') {
1107: $output .= "$captchaform\n";
1108: undef($captchaform);
1109: } else {
1110: $output .= '
1111: <input type="hidden" name="recaptcha_challenge_field" value="" />
1112: <input type="hidden" name="recaptcha_response_field" value="" />
1113: ';
1114: }
1115: }
1116: if ($usertype ne '') {
1117: $output .= '<input type="hidden" name="type" value="'.
1118: &HTML::Entities::encode($usertype,'\'<>"&').'" />'."\n";
1119: }
1120: if ($usernameset eq 'free') {
1121: $output .= '<input type="hidden" name="username" value="" />'."\n";
1122: }
1123: $output .= <<"ENDSERVERFORM";
1124: <input type="hidden" name="logtoken" value="$logtoken" />
1125: <input type="hidden" name="serverid" value="$lonhost" />
1126: <input type="hidden" name="uname" value="" />
1127: <input type="hidden" name="upass" value="" />
1128: <input type="hidden" name="udom" value="" />
1129: <input type="hidden" name="phase" value="createaccount" />
1130: <input type="hidden" name="create_with_email" value="1" />
1131: </form>
1132: ENDSERVERFORM
1133: my $beginclientform = '<form name="newemail" method="post" action="" '.
1134: 'onsubmit="return checkpass('."'createaccount','newemail'".');">'."\n";
1135: my $endclientform = '<input type="hidden" name="udom" value="'.$domain.'" />'."\n".
1136: '<input type="hidden" name="lextkey" value="'.$lextkey.'" />'."\n".
1137: '<input type="hidden" name="uextkey" value="'.$uextkey.'" />'."\n".
1138: '</form>'."\n".
1139: '<p class="LC_info">'.&mt('Fields marked [_1]*[_2] are required.','<b>','</b>').'</p>';
1140: my ($datatable,$rowcount) =
1141: &Apache::loncreateuser::personal_data_display('',$domain,'email','selfcreate',
1142: '','',$now,$captchaform,
1143: $emailusername,$usertype,
1144: $usernameset,$condition,$excluded);
1145: if ($rowcount) {
1146: $output .= '<div class="LC_left_float">'.$beginclientform.$datatable.$endclientform;
1147: } else {
1148: $output .= $beginclientform.$endclientform;
1149: }
1150: if ($rowcount) {
1151: $output .= '</div>'."\n".
1152: '<div class="LC_clear_float_footer"></div>'."\n";
1153: }
1154: } else {
1155: $output = &mt('Could not load javascript file [_1]','<tt>londes.js</tt>');
1156: }
1157: return $output;
1158: }
1159:
1160: #
1161: # Retrieve rules for generating accounts from domain configuration
1162: # Can the user make a new account or just self-enroll?
1163:
1164: sub get_creation_controls {
1165: my ($domain,$usercreation) = @_;
1166: my (@cancreate,@statustocreate,@statusforemail,$emailusername,$processing,
1167: $emailoptions,$verification,$emaildomain,$othertitle,$usertypes,$types);
1168: if (ref($usercreation) eq 'HASH') {
1169: if (ref($usercreation->{'cancreate'}) eq 'HASH') {
1170: ($othertitle,$usertypes,$types) =
1171: &Apache::loncommon::sorted_inst_types($domain);
1172: if (ref($usercreation->{'cancreate'}{'statustocreate'}) eq 'ARRAY') {
1173: @statustocreate = @{$usercreation->{'cancreate'}{'statustocreate'}};
1174: if (@statustocreate == 0) {
1175: if (ref($types) eq 'ARRAY') {
1176: if (@{$types} == 0) {
1177: @statustocreate = ('default');
1178: }
1179: } else {
1180: @statustocreate = ('default');
1181: }
1182: }
1183: } else {
1184: @statustocreate = ('default');
1185: if (ref($types) eq 'ARRAY') {
1186: push(@statustocreate,@{$types});
1187: }
1188: }
1189: if (ref($usercreation->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
1190: @cancreate = @{$usercreation->{'cancreate'}{'selfcreate'}};
1191: } elsif (($usercreation->{'cancreate'}{'selfcreate'} ne 'none') &&
1192: ($usercreation->{'cancreate'}{'selfcreate'} ne '')) {
1193: @cancreate = ($usercreation->{'cancreate'}{'selfcreate'});
1194: }
1195: if (grep(/^email$/,@cancreate)) {
1196: if (ref($usercreation->{'cancreate'}{'selfcreateprocessing'}) eq 'HASH') {
1197: $processing = $usercreation->{'cancreate'}{'selfcreateprocessing'};
1198: }
1199: if (ref($usercreation->{'cancreate'}{'emailoptions'}) eq 'HASH') {
1200: $emailoptions = $usercreation->{'cancreate'}{'emailoptions'};
1201: }
1202: if (ref($usercreation->{'cancreate'}{'emailverified'}) eq 'HASH') {
1203: $verification = $usercreation->{'cancreate'}{'emailverified'};
1204: }
1205: if (ref($usercreation->{'cancreate'}{'emaildomain'}) eq 'HASH') {
1206: $emaildomain = $usercreation->{'cancreate'}{'emaildomain'};
1207: }
1208: if (ref($processing)) {
1209: my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
1210: my @emailtypes;
1211: if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
1212: @statusforemail = @{$domdefaults{'inststatusguest'}};
1213: unless (@statusforemail) {
1214: my @okcreate;
1215: foreach my $poss (@cancreate) {
1216: unless ($poss eq 'email') {
1217: push(@okcreate,$poss);
1218: }
1219: }
1220: @cancreate = @okcreate;
1221: }
1222: }
1223: }
1224: }
1225: if (ref($usercreation->{'cancreate'}{'emailusername'}) eq 'HASH') {
1226: $emailusername = $usercreation->{'cancreate'}{'emailusername'};
1227: } else {
1228: $emailusername = {
1229: default => {
1230: 'lastname' => '1',
1231: 'firstname' => 1,
1232: },
1233: };
1234: }
1235: }
1236: }
1237: return (\@cancreate,\@statustocreate,\@statusforemail,$emailusername,
1238: $emailoptions,$verification,$emaildomain,$types,$usertypes,$othertitle);
1239: }
1240:
1241: sub create_account {
1242: my ($r,$domain,$domdesc,$dataref) = @_;
1243: my $error = '<span class="LC_error">'.&mt('Error:').' ';
1244: my $end = '</span><br /><br />';
1245: my $rtnlink = '<a href="javascript:history.back();">'.
1246: &mt('Return to previous page').'</a>'.
1247: &Apache::loncommon::end_page();
1248: my $output;
1249: if (ref($dataref) eq 'HASH') {
1250: my ($username,$encpass,$serverid,$courseid,$id,$firstname,$middlename,$lastname,
1251: $generation,$inststatus,$permanentemail);
1252: $username = $dataref->{'username'};
1253: $encpass = $dataref->{'upass'};
1254: $serverid = $dataref->{'serverid'};
1255: $courseid = $dataref->{'courseid'};
1256: $id = $dataref->{'id'};
1257: $firstname = $dataref->{'firstname'};
1258: $middlename = $dataref->{'middlename'};
1259: $lastname = $dataref->{'lastname'};
1260: $generation = $dataref->{'generation'};
1261: $inststatus = $dataref->{'inststatus'};
1262:
1263: if ($dataref->{'email'} ne '') {
1264: $permanentemail = $dataref->{'email'};
1265: } else {
1266: $permanentemail = $dataref->{'username'};
1267: }
1268: my $currhome = &Apache::lonnet::homeserver($username,$domain);
1269: unless ($currhome eq 'no_host') {
1270: $output = &mt('User account requested for username: [_1] in domain: [_2] already exists.',$username,$domain);
1271: return ('fail',$error.$output.$end.$rtnlink);
1272: }
1273:
1274: # Split the logtoken to retrieve the DES key and decrypt the encypted password
1275:
1276: my ($key,$caller)=split(/&/,$dataref->{'tmpinfo'});
1277: if ($caller eq 'createaccount') {
1278: my $upass;
1279: if ($encpass eq '') {
1280: $output = &mt('Password retrieved was blank.');
1281: return ('fail',$error.$output.$end.$rtnlink);
1282: } else {
1283: $upass = &Apache::loncommon::des_decrypt($key,$encpass);
1284: }
1285:
1286: # See if we are allowed to use the proposed student/employee ID,
1287: # as per domain rules; if not, student/employee will be left blank.
1288:
1289: if ($id ne '') {
1290: my ($result,$userchkmsg) = &check_id($username,$domain,$id,$domdesc,'email');
1291: if ($result eq 'fail') {
1292: $output = $error.&mt('Invalid ID format').$end.
1293: $userchkmsg;
1294: undef($id);
1295: }
1296: }
1297:
1298: # Create an internally authenticated account with password $upass
1299: # if the user account does not already exist.
1300: # Assign student/employee id, first name, last name, etc.
1301:
1302: my $result =
1303: &Apache::lonnet::modifyuser($domain,$username,$id,
1304: 'internal',$upass,$firstname,$middlename,
1305: $lastname,$generation,undef,undef,$permanentemail);
1306: $output = &mt('Generating user: [_1]',$result);
1307:
1308: # Now that the user account exists, retrieve the homeserver, and include it in the output.
1309:
1310: my $uhome = &Apache::lonnet::homeserver($username,$domain);
1311: unless (($inststatus eq 'default') || ($inststatus eq '')) {
1312: &Apache::lonnet::put('environment',{inststatus => $inststatus},$domain,$username);
1313: }
1314: $output .= '<br />'.&mt('Home server: [_1]',$uhome).' '.
1315: &Apache::lonnet::hostname($uhome).'<br /><br />';
1316: return ('ok',$output,$uhome);
1317: } else {
1318: $output = &mt('Unable to retrieve your account creation information - unexpected context');
1319: undef($encpass);
1320: return ('fail',$error.$output.$end.$rtnlink);
1321: }
1322: } else {
1323: $output = &mt('Unable to retrieve information for your account request.');
1324: return ('fail',$error.$output.$end.$rtnlink);
1325: }
1326: }
1327:
1328: sub username_validation {
1329: my ($r,$username,$domain,$domdesc,$contact_name,$contact_email,$courseid,
1330: $lonhost,$statustocreate) = @_;
1331: # $r: request object
1332: # $username,$domain: for the user who needs to be validated
1333: # $domdesc: full name of the domain (for error messages)
1334: # $contact_name, $contact_email: name and email for user assistance (for error messages in &username_check)
1335: # $courseid: ID of the course if user came to username_validation via self-enroll link,
1336: # passed to start_session()
1337: # $lonhost: LON-CAPA lonHostID
1338: # $statustocreate: -> inststatus in username_check ('faculty', 'staff', 'student', ...)
1339:
1340: #
1341: # Sanitize incoming username and domain
1342: #
1343: $username= &LONCAPA::clean_username($username);
1344: $domain = &LONCAPA::clean_domain($domain);
1345:
1346: #
1347: # Check if LON-CAPA account already exists for $username:$domain
1348: #
1349: my $uhome = &Apache::lonnet::homeserver($username,$domain);
1350:
1351: my $output;
1352:
1353: # Retrieve DES key from server using logtoken
1354:
1355: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$env{'form.logtoken'},$env{'form.serverid'});
1356: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
1357: $output = &mt('Information needed to verify your login information is missing, inaccessible or expired.')
1358: .'<br />'.&mt('You may need to reload the previous page to obtain a new token.');
1359: return ('fail',$output);
1360: } else {
1361: my $reply = &Apache::lonnet::reply('tmpdel:'.$env{'form.logtoken'},$env{'form.serverid'});
1362: unless ($reply eq 'ok') {
1363: $output = &mt('Session could not be opened.');
1364: return ('fail',$output);
1365: }
1366: }
1367:
1368: # Split the logtoken to retrieve the DES key and decrypt the encypted password
1369:
1370: my ($key,$caller)=split(/&/,$tmpinfo);
1371: my $upass;
1372: if ($caller eq 'createaccount') {
1373: $upass = &Apache::loncommon::des_decrypt($key,$env{'form.upass'});
1374: } else {
1375: $output = &mt('Unable to retrieve your log-in information - unexpected context');
1376: return ('fail',$output);
1377: }
1378: if ($uhome ne 'no_host') {
1379: my $result = &Apache::lonnet::authenticate($username,$upass,$domain);
1380: if ($result ne 'no_host') {
1381: my $redirect = &start_session($r,$username,$domain,$uhome,$courseid);
1382: if ($redirect) {
1383: return ($redirect);
1384: }
1385: $output = '<br /><br />'.
1386: &mt('A LON-CAPA account already exists for username [_1] at this institution ([_2]).',
1387: '<tt>'.$username.'</tt>',$domdesc).'<br />'.
1388: &mt('The password entered was also correct so you have been logged in.');
1389: return ('existingaccount',$output);
1390: } else {
1391: $output = &login_failure_msg($courseid);
1392: }
1393: } else {
1394: my $primlibserv = &Apache::lonnet::domain($domain,'primary');
1395: my $authok;
1396: my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
1397: if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) ||
1398: ($domdefaults{'auth_def'} eq 'localauth')) {
1399: my $checkdefauth = 1;
1400: $authok =
1401: &Apache::lonnet::reply("encrypt:auth:$domain:$username:$upass:$checkdefauth",$primlibserv);
1402: } else {
1403: $authok = 'non_authorized';
1404: }
1405: if ($authok eq 'authorized') {
1406: $output = &username_check($username,$domain,$domdesc,$courseid,$lonhost,
1407: $contact_email,$contact_name,undef,
1408: $statustocreate);
1409: } else {
1410: $output = &login_failure_msg($courseid);
1411: }
1412: }
1413: return ('ok',$output);
1414: }
1415:
1416: sub login_failure_msg {
1417: my ($courseid) = @_;
1418: my $url;
1419: if ($courseid ne '') {
1420: $url = "/adm/selfenroll?courseid=".$courseid;
1421: } else {
1422: $url = "/adm/createaccount";
1423: }
1424: my $output = '<h4>'.&mt('Authentication failed').'</h4><div class="LC_warning">'.
1425: &mt('Username and/or password could not be authenticated.').
1426: '</div>'.
1427: &mt('Please check the username and password.').'<br /><br />';
1428: '<a href="'.$url.'">'.&mt('Try again').'</a>';
1429: return $output;
1430: }
1431:
1432: sub username_check {
1433: my ($username,$domain,$domdesc,$courseid,$lonhost,$contact_email,
1434: $contact_name,$sso_logout,$statustocreate,$shibenv) = @_;
1435: my (%rulematch,%inst_results,$checkfail,$rowcount,$editable,$output,$msg,
1436: %alerts,%curr_rules,%got_rules);
1437: &call_rulecheck($username,$domain,\%alerts,\%rulematch,
1438: \%inst_results,\%curr_rules,\%got_rules,'username');
1439: if (ref($alerts{'username'}) eq 'HASH') {
1440: if (ref($alerts{'username'}{$domain}) eq 'HASH') {
1441: if ($alerts{'username'}{$domain}{$username}) {
1442: if (ref($curr_rules{$domain}) eq 'HASH') {
1443: $output =
1444: &Apache::loncommon::instrule_disallow_msg('username',$domdesc,1,
1445: 'selfcreate').
1446: &Apache::loncommon::user_rule_formats($domain,$domdesc,
1447: $curr_rules{$domain}{'username'},'username');
1448: }
1449: $checkfail = 'username';
1450: }
1451: }
1452: }
1453: if (!$checkfail) {
1454: if (ref($statustocreate) eq 'ARRAY') {
1455: $checkfail = 'inststatus';
1456: if (ref($inst_results{$username.':'.$domain}{inststatus}) eq 'ARRAY') {
1457: foreach my $inststatus (@{$inst_results{$username.':'.$domain}{inststatus}}) {
1458: if (grep(/^\Q$inststatus\E$/,@{$statustocreate})) {
1459: undef($checkfail);
1460: last;
1461: }
1462: }
1463: } elsif (grep(/^default$/,@{$statustocreate})) {
1464: undef($checkfail);
1465: }
1466: }
1467: }
1468: if (!$checkfail) {
1469: $output = '<form method="post" action="/adm/createaccount">';
1470: if (ref($shibenv) eq 'HASH') {
1471: foreach my $key (keys(%{$shibenv})) {
1472: if ($ENV{$shibenv->{$key}} ne '') {
1473: $inst_results{$username.':'.$domain}{$key} = $ENV{$shibenv->{$key}};
1474: }
1475: }
1476: }
1477: (my $datatable,$rowcount,$editable) =
1478: &Apache::loncreateuser::personal_data_display($username,$domain,1,'selfcreate',
1479: $inst_results{$username.':'.$domain});
1480: if ($rowcount > 0) {
1481: $output .= $datatable;
1482: }
1483: $output .= '<br /><br /><input type="hidden" name="uname" value="'.$username.'" />'."\n".
1484: '<input type="hidden" name="udom" value="'.$domain.'" />'."\n".
1485: '<input type="hidden" name="phase" value="username_activation" />';
1486: my $now = time;
1487: my %info = ('ip' => $ENV{'REMOTE_ADDR'},
1488: 'time' => $now,
1489: 'domain' => $domain,
1490: 'username' => $username);
1491: my $authtoken = &Apache::lonnet::tmpput(\%info,$lonhost,'createaccount');
1492: if ($authtoken !~ /^error/ && $authtoken ne 'no_such_host') {
1493: $output .= '<input type="hidden" name="authtoken" value="'.&HTML::Entities::encode($authtoken,'&<>"').'" />';
1494: } else {
1495: $output = &mt('An error occurred when storing a token').'<br />'.
1496: &mt('You will not be able to proceed to the next stage of account creation').
1497: &linkto_email_help($contact_email,$domdesc);
1498: $checkfail = 'authtoken';
1499: }
1500: }
1501: if ($checkfail) {
1502: $msg = '<br /><h4>'.&mt('Account creation unavailable').'</h4>';
1503: if ($checkfail eq 'username') {
1504: $msg .= '<span class="LC_warning">'.
1505: &mt('A LON-CAPA account may not be created with the username you use.').
1506: '</span><br /><br />'.$output;
1507: } elsif ($checkfail eq 'authtoken') {
1508: $msg .= '<span class="LC_error">'.&mt('Error creating token.').'</span>'.
1509: '<br />'.$output;
1510: } elsif ($checkfail eq 'inststatus') {
1511: $msg .= '<span class="LC_warning">'.
1512: &mt('You are not permitted to create a LON-CAPA account.').
1513: '</span><br /><br />'.$output;
1514: }
1515: $msg .= &mt('Please contact the [_1] ([_2]) for assistance.',
1516: $contact_name,$contact_email).'<br /><hr />'.
1517: $sso_logout;
1518: &Apache::lonnet::logthis("ERROR: failure type of '$checkfail' when performing username check to create account for authenticated user: $username, in domain $domain");
1519: } else {
1520: if ($courseid ne '') {
1521: $output .= '<input type="hidden" name="courseid" value="'.$courseid.'" />';
1522: }
1523: $output .= '<input type="submit" name="newaccount" value="'.
1524: &mt('Create LON-CAPA account').'" /></form>';
1525: if ($rowcount) {
1526: if ($editable) {
1527: if ($courseid ne '') {
1528: $msg = '<br /><h4>'.&mt('User information').'</h4>';
1529: }
1530: $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 />';
1531: } else {
1532: if ($courseid ne '') {
1533: $msg = '<h4>'.&mt('Review user information').'</h4>';
1534: }
1535: $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 />';
1536: }
1537: } else {
1538: if ($courseid ne '') {
1539: $msg = '<h4>'.&mt('Confirmation').'</h4>';
1540: }
1541: $msg .= &mt('Confirm that you wish to create an account.');
1542: }
1543: $msg .= $output;
1544: }
1545: return $msg;
1546: }
1547:
1548: sub username_activation {
1549: my ($r,$username,$domain,$domdesc,$courseid) = @_;
1550: my $output;
1551: my $error = '<span class="LC_error">'.&mt('Error:').' ';
1552: my $end = '</span><br /><br />';
1553: my $rtnlink = '<a href="javascript:history.back();">'.
1554: &mt('Return to previous page').'</a>'.
1555: &Apache::loncommon::end_page();
1556: my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
1557: my %data = &Apache::lonnet::tmpget($env{'form.authtoken'});
1558: my $now = time;
1559: my $earlyout;
1560: my $timeout = 300;
1561: if (keys(%data) == 0) {
1562: $output = &mt('Sorry, your authentication has expired.');
1563: $earlyout = 'fail';
1564: }
1565: if (($data{'time'} !~ /^\d+$/) ||
1566: ($data{'domain'} ne $domain) ||
1567: ($data{'username'} ne $username)) {
1568: $earlyout = 'fail';
1569: $output = &mt('The credentials you provided could not be verified.');
1570: } elsif ($now - $data{'time'} > $timeout) {
1571: $earlyout = 'fail';
1572: $output = &mt('Sorry, your authentication has expired.');
1573: }
1574: if ($earlyout ne '') {
1575: my $link = '/adm/createaccount';
1576: if (&Apache::lonnet::domain($domain) ne '') {
1577: $link .= "?domain=$domain";
1578: }
1579: $output .= '<br />'.&mt('Please [_1]start again[_2].',
1580: '<a href="'.$link.'">','</a>');
1581: return($earlyout,$output);
1582: }
1583: if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) &&
1584: ($domdefaults{'auth_arg_def'} ne '')) ||
1585: ($domdefaults{'auth_def'} eq 'localauth')) {
1586: if ($env{'form.courseid'} ne '') {
1587: my $id = $env{'form.cid'};
1588: my ($result,$userchkmsg) = &check_id($username,$domain,$id,$domdesc,'institutional');
1589: if ($result eq 'fail') {
1590: $output = $error.&mt('Invalid ID format').$end.
1591: $userchkmsg.$rtnlink;
1592: return ('fail',$output);
1593: }
1594: }
1595: # Call modifyuser
1596: my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts,%info);
1597: &call_rulecheck($username,$domain,\%alerts,\%rulematch,
1598: \%inst_results,\%curr_rules,\%got_rules);
1599: my @userinfo = ('firstname','middlename','lastname','generation',
1600: 'permanentemail','id');
1601: my %canmodify =
1602: &Apache::loncreateuser::selfcreate_canmodify('selfcreate',$domain,
1603: \@userinfo,\%inst_results);
1604: foreach my $item (@userinfo) {
1605: if ($canmodify{$item}) {
1606: $info{$item} = $env{'form.c'.$item};
1607: } else {
1608: $info{$item} = $inst_results{$username.':'.$domain}{$item};
1609: }
1610: }
1611: if (ref($inst_results{$username.':'.$domain}{'inststatus'}) eq 'ARRAY') {
1612: my @inststatuses = @{$inst_results{$username.':'.$domain}{'inststatus'}};
1613: $info{'inststatus'} = join(':',map { &escape($_); } @inststatuses);
1614: }
1615: my $result =
1616: &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
1617: $domdefaults{'auth_def'},
1618: $domdefaults{'auth_arg_def'},$info{'firstname'},
1619: $info{'middlename'},$info{'lastname'},
1620: $info{'generation'},undef,undef,
1621: $info{'permanentemail'},$info{'inststatus'});
1622: if ($result eq 'ok') {
1623: my $delete = &Apache::lonnet::tmpdel($env{'form.authtoken'});
1624: $output = &mt('A LON-CAPA account has been created for username: [_1] in domain: [_2].',$username,$domain);
1625: my $uhome=&Apache::lonnet::homeserver($username,$domain,'true');
1626: my $nostart = 1;
1627: my $response = 'ok';
1628: my $redirect = &start_session($r,$username,$domain,$uhome,$courseid);
1629: if ($redirect) {
1630: $response = $redirect;
1631: }
1632: return ($response,$output,$nostart);
1633: } else {
1634: $output = &mt('Account creation failed for username: [_1] in domain: [_2].',$username,$domain).'<br /><span class="LC_error">'.&mt('Error: [_1]',$result).'</span>';
1635: return ('fail',$output);
1636: }
1637: } else {
1638: $output = &mt('User account creation is not available for the current default authentication type.')."\n";
1639: return('fail',$output);
1640: }
1641: }
1642:
1643: sub check_id {
1644: my ($username,$domain,$id,$domdesc,$usernametype) = @_;
1645: # Check student/employee ID format
1646: # Is proposed student/employee ID acceptable according to domain's rules.
1647: # $domdesc is just used for user error messages
1648: my (%alerts,%rulematch,%inst_results,%curr_rules,%checkhash);
1649: my %checks = ('id' => 1);
1650: %{$checkhash{$username.':'.$domain}} = (
1651: 'newuser' => 1,
1652: 'id' => $id,
1653: );
1654: &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
1655: \%rulematch,\%inst_results,\%curr_rules);
1656: if (ref($alerts{'id'}) eq 'HASH') {
1657: if (ref($alerts{'id'}{$domain}) eq 'HASH') {
1658: if ($alerts{'id'}{$domain}{$env{'form.cid'}}) {
1659: my $userchkmsg;
1660: if (ref($curr_rules{$domain}) eq 'HASH') {
1661: if ($usernametype eq 'email') {
1662: $userchkmsg = &mt('A student/employee ID has not been set because the value suggested matched the format used for institutional users in the domain, and you are using an e-mail address as username, not an institutional username.');
1663: } else {
1664: $userchkmsg =
1665: &Apache::loncommon::instrule_disallow_msg('id',
1666: $domdesc,1).
1667: &Apache::loncommon::user_rule_formats($domain,
1668: $domdesc,$curr_rules{$domain}{'id'},'id');
1669: }
1670: }
1671: return ('fail',$userchkmsg);
1672: }
1673: }
1674: }
1675: return;
1676: }
1677:
1678: sub invalid_state {
1679: my ($error,$domdesc,$contact_name,$contact_email,$msgtext) = @_;
1680: my $msg = '<h3>'.&mt('Account creation unavailable').'</h3><span class="LC_error">';
1681: if ($error eq 'baduseremail') {
1682: $msg .= &mt('The e-mail address you provided does not appear to be a valid address.');
1683: } elsif ($error eq 'badusername') {
1684: $msg .= &mt('The e-mail address you provided contains characters which prevent its use as a username in LON-CAPA.');
1685: } elsif ($error eq 'existinguser') {
1686: $msg .= &mt('The e-mail address you provided is already in use as a username in LON-CAPA at this institution.');
1687: } elsif ($error eq 'userrules') {
1688: $msg .= &mt('Username rules at this institution do not allow the e-mail address you provided to be used as a username.');
1689: } elsif ($error eq 'userformat') {
1690: $msg .= &mt('The e-mail address you provided may not be used as a username at this LON-CAPA institution.');
1691: } elsif ($error eq 'captcha') {
1692: $msg .= &mt('Validation of the code you entered failed.');
1693: } elsif ($error eq 'noemails') {
1694: $msg .= &mt('Creation of a new user account using an e-mail address as username is not permitted at this LON-CAPA institution.');
1695: } elsif ($error eq 'emailfail') {
1696: $msg .= &mt('Creation of a new user account with verification by e-mail is not permitted with the e-mail address you provided');
1697: }
1698: $msg .= '</span>';
1699: if ($msgtext) {
1700: $msg .= '<br />'.$msgtext;
1701: }
1702: $msg .= &linkto_email_help($contact_email,$domdesc,$error);
1703: return $msg;
1704: }
1705:
1706: sub linkto_email_help {
1707: my ($contact_email,$domdesc,$error) = @_;
1708: my $msg;
1709: my $href = '/adm/helpdesk';
1710: if ($contact_email ne '') {
1711: my $escuri = &HTML::Entities::encode('/adm/createaccount','&<>"');
1712: $href .= '?origurl='.$escuri;
1713: if ($error eq 'existinguser') {
1714: my $escemail = &HTML::Entities::encode($env{'form.useremail'});
1715: $href .= '&useremail='.$escemail.'&useraccount='.$escemail;
1716: }
1717: $msg .= '<br />'.&mt('You may wish to contact the [_1]LON-CAPA helpdesk[_2] for [_3].','<a href="'.$href.'">','</a>',$domdesc).'<br />';
1718: } else {
1719: $msg .= '<br />'.&mt('You may wish to send an e-mail to the server administrator: [_1] for [_2].',$Apache::lonnet::perlvar{'AdmEMail'},$domdesc).'<br />';
1720: }
1721: return $msg;
1722: }
1723:
1724: sub getkeys {
1725: my ($lkey,$ukey) = @_;
1726: my $lextkey=hex($lkey);
1727: if ($lextkey>2147483647) { $lextkey-=4294967296; }
1728:
1729: my $uextkey=hex($ukey);
1730: if ($uextkey>2147483647) { $uextkey-=4294967296; }
1731: return ($lextkey,$uextkey);
1732: }
1733:
1734: sub serverform {
1735: my ($logtoken,$lonhost,$mailtoken,$courseid,$context) = @_;
1736: my $phase = 'username_validation';
1737: my $catalog_elements;
1738: if ($context eq 'selfenroll') {
1739: $phase = 'selfenroll_login';
1740: }
1741: if ($courseid ne '') {
1742: $catalog_elements = &Apache::lonhtmlcommon::echo_form_input(['courseid','phase']);
1743: }
1744: my $output = <<ENDSERVERFORM;
1745: <form name="server" method="post" action="/adm/createaccount">
1746: <input type="hidden" name="logtoken" value="$logtoken" />
1747: <input type="hidden" name="token" value="$mailtoken" />
1748: <input type="hidden" name="serverid" value="$lonhost" />
1749: <input type="hidden" name="uname" value="" />
1750: <input type="hidden" name="upass" value="" />
1751: <input type="hidden" name="udom" value="" />
1752: <input type="hidden" name="phase" value="$phase" />
1753: <input type="hidden" name="courseid" value="$courseid" />
1754: $catalog_elements
1755: </form>
1756: ENDSERVERFORM
1757: return $output;
1758: }
1759:
1760: sub store_request {
1761: my ($dom,$username,$val,$dataref,$settings) = @_;
1762: my $output;
1763: my $domconfiguser = &Apache::lonnet::get_domainconfiguser($dom);
1764: my $key = &escape($username);
1765: my $now = time();
1766: if (&Apache::lonnet::put('usernamequeue', { $key.'_'.$val => $now },
1767: $dom,$domconfiguser) eq 'ok') {
1768: if (ref($dataref) eq 'HASH') {
1769: my $logtoken = $dataref->{'tmpinfo'};
1770: my $serverid = $dataref->{'serverid'};
1771: if ($logtoken && $serverid) {
1772: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$logtoken,$serverid);
1773: unless (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
1774: my $reply = &Apache::lonnet::reply('tmpdel:'.$logtoken,$serverid);
1775: if ($reply eq 'ok') {
1776: my ($key,$caller)=split(/&/,$tmpinfo);
1777: $dataref->{'key'} = $key;
1778: undef($dataref->{'tmpinfo'});
1779: undef($dataref->{'serverid'});
1780: }
1781: }
1782: }
1783: }
1784: my %userrequest = ( $username => $dataref );
1785: $userrequest{$username}{timestamp} = $now;
1786: $userrequest{$username}{status} = $val;
1787: my $notifylist;
1788: if (ref($settings) eq 'HASH') {
1789: if (ref($settings->{'cancreate'}) eq 'HASH') {
1790: if (ref($settings->{'cancreate'}{'notify'}) eq 'HASH') {
1791: my $notifylist = $settings->{'cancreate'}{'notify'}{'approval'};
1792: if ($notifylist) {
1793: my $sender = $domconfiguser.':'.$dom;
1794: my $domdesc = &Apache::lonnet::domain($dom,'description');
1795: my $fullname;
1796: if (ref($dataref) eq 'HASH') {
1797: if ($dataref->{'firstname'}) {
1798: $fullname = $dataref->{'firstname'};
1799: }
1800: if ($dataref->{'lastname'}) {
1801: $fullname .= ' '.$dataref->{'lastname'};
1802: }
1803: $fullname =~ s/^\s+|\s+$//g;
1804: }
1805: &Apache::loncoursequeueadmin::send_selfserve_notification($notifylist,
1806: "$fullname ($username)",
1807: undef,$domdesc,$now,'usernamereq',$sender);
1808: }
1809: }
1810: }
1811: }
1812: my $userresult =
1813: &Apache::lonnet::put('nohist_requestedusernames',\%userrequest,$dom,$domconfiguser);
1814: $output = '<p class="LC_info">'.
1815: &mt('Your request for a LON-CAPA account has been submitted for approval.').
1816: '</p>'.
1817: '<p class="LC_info">'.
1818: &mt('An e-mail will be sent to [_1] when your request has been reviewed by an administrator and action has been taken.',$username).
1819: '</p>';
1820: } else {
1821: $output = '<span class="LC_error">'.
1822: &mt('An error occurred when attempting to save your request for a LON-CAPA account.');
1823: '</span>';
1824: }
1825: return $output;
1826: }
1827:
1828: sub guest_format_check {
1829: my ($useremail,$domain,$cancreate,$settings,$usertype) = @_;
1830: my ($login,$format_match,$format_msg,@user_rules);
1831: if (ref($settings) eq 'HASH') {
1832: if (ref($settings->{'email_rule'}) eq 'ARRAY') {
1833: push(@user_rules,@{$settings->{'email_rule'}});
1834: } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
1835: if (ref($settings->{'email_rule'}->{$usertype}) eq 'ARRAY') {
1836: push(@user_rules,@{$settings->{'email_rule'}->{$usertype}});
1837: }
1838: }
1839: }
1840: if (@user_rules > 0) {
1841: my %rule_check =
1842: &Apache::lonnet::inst_rulecheck($domain,$useremail,undef,
1843: 'selfcreate',\@user_rules);
1844: if (keys(%rule_check) > 0) {
1845: foreach my $item (keys(%rule_check)) {
1846: if ($rule_check{$item}) {
1847: $format_match = 1;
1848: last;
1849: }
1850: }
1851: }
1852: }
1853: if ($format_match) {
1854: ($login) = ($useremail =~ /^([^\@]+)\@/);
1855: $format_msg = '<br />'.
1856: &mt("Your e-mail address uses the same internet domain as your institution's LON-CAPA service.").'<br />'.
1857: &mt('Creation of a LON-CAPA account with this type of e-mail address as username is not permitted.').'<br />';
1858: if (ref($cancreate) eq 'ARRAY') {
1859: if (grep(/^login$/,@{$cancreate})) {
1860: $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 />';
1861: }
1862: }
1863: }
1864: return $format_msg;
1865: }
1866:
1867: sub sso_logout_frag {
1868: my ($r,$domain) = @_;
1869: my $endsessionmsg;
1870: if (defined($r->dir_config('lonSSOUserLogoutMessageFile_'.$domain))) {
1871: my $msgfile = $r->dir_config('lonSSOUserLogoutMessageFile_'.$domain);
1872: if (-e $msgfile) {
1873: open(my $fh,"<$msgfile");
1874: $endsessionmsg = join('',<$fh>);
1875: close($fh);
1876: }
1877: } elsif (defined($r->dir_config('lonSSOUserLogoutMessageFile'))) {
1878: my $msgfile = $r->dir_config('lonSSOUserLogoutMessageFile');
1879: if (-e $msgfile) {
1880: open(my $fh,"<$msgfile");
1881: $endsessionmsg = join('',<$fh>);
1882: close($fh);
1883: }
1884: }
1885: return $endsessionmsg;
1886: }
1887:
1888: sub catreturn_js {
1889: return <<"ENDSCRIPT";
1890: <script type="text/javascript">
1891: // <![CDATA[
1892: function ToSelfenroll(formname) {
1893: var formidx = getFormByName(formname);
1894: if (formidx > -1) {
1895: document.forms[formidx].action = '/adm/selfenroll';
1896: numidx = getIndexByName(formidx,'phase');
1897: if (numidx > -1) {
1898: document.forms[formidx].elements[numidx].value = '';
1899: }
1900: numidx = getIndexByName(formidx,'context');
1901: if (numidx > -1) {
1902: document.forms[formidx].elements[numidx].value = '';
1903: }
1904: }
1905: document.forms[formidx].submit();
1906: }
1907:
1908: function ToCatalog(formname,caller) {
1909: var formidx = getFormByName(formname);
1910: if (formidx > -1) {
1911: document.forms[formidx].action = '/adm/coursecatalog';
1912: numidx = getIndexByName(formidx,'coursenum');
1913: if (numidx > -1) {
1914: if (caller != 'details') {
1915: document.forms[formidx].elements[numidx].value = '';
1916: }
1917: }
1918: }
1919: document.forms[formidx].submit();
1920: }
1921:
1922: function getIndexByName(formidx,item) {
1923: for (var i=0;i<document.forms[formidx].elements.length;i++) {
1924: if (document.forms[formidx].elements[i].name == item) {
1925: return i;
1926: }
1927: }
1928: return -1;
1929: }
1930:
1931: function getFormByName(item) {
1932: for (var i=0; i<document.forms.length; i++) {
1933: if (document.forms[i].name == item) {
1934: return i;
1935: }
1936: }
1937: return -1;
1938: }
1939: // ]]>
1940: </script>
1941: ENDSCRIPT
1942:
1943: }
1944:
1945: sub setelements_js {
1946: my ($statusforemail,$types,$usertypes,$othertitle) = @_;
1947: my ($posstypes,$posstypesnames,$availabletypes);
1948: if ((ref($statusforemail) eq 'ARRAY') && (ref($types) eq 'ARRAY') &&
1949: (ref($usertypes) eq 'HASH')) {
1950: $posstypes = join("','",@{$types},'default');
1951: $posstypesnames = join("','",(map {$usertypes->{$_};} @{$types}),$othertitle);
1952: $availabletypes = join("','", @{$statusforemail});
1953: }
1954: return <<"ENDSCRIPT";
1955: <script type="text/javascript">
1956: // <![CDATA[
1957:
1958: function setElements() {
1959: if (document.getElementById('LC_reportstatus')) {
1960: var reportnum = document.reportstatus.type.length;
1961: if ((reportnum != 'undefined') && (typeof(document.reportstatus.type) != 'undefined')) {
1962: for (var i=0; i<reportnum; i++) {
1963: document.reportstatus.type[i].checked = false;
1964: }
1965: }
1966: }
1967: }
1968:
1969: function checkVerification() {
1970: var curr;
1971: var cancreate = false;
1972: var reportnum = document.reportstatus.type.length;
1973: if ((reportnum == 'undefined') && (typeof(document.reportstatus.type) != 'undefined')) {
1974: curr = document.reportstatus.type.value;
1975: } else if (document.reportstatus.type.length) {
1976: for (var i=0; i<document.reportstatus.type.length; i++) {
1977: if (document.reportstatus.type[i].checked) {
1978: curr = document.reportstatus.type[i].value;
1979: break;
1980: }
1981: }
1982: }
1983: var types = Array('$posstypes');
1984: var names = Array('$posstypesnames');
1985: var available = Array('$availabletypes');
1986: if (available.length) {
1987: for (var i=0; i<available.length; i++) {
1988: if (curr == available[i]) {
1989: cancreate = true;
1990: break;
1991: }
1992: }
1993: }
1994: if (types.length > 0) {
1995: for (var j=0; j<types.length; j++) {
1996: if (curr == types[j]) {
1997: if (!cancreate) {
1998: alert('Creation of an account via verification by e-mail unavailable for user type: "'+names[j]+'"');
1999: }
2000: break;
2001: }
2002: }
2003: }
2004: if (cancreate) {
2005: return true;
2006: } else {
2007: return false;
2008: }
2009: }
2010:
2011: // ]]>
2012: </script>
2013: ENDSCRIPT
2014:
2015: }
2016:
2017: sub username_js {
2018: return <<"ENDSCRIPT";
2019: <script type="text/javascript">
2020: // <![CDATA[
2021:
2022: function toggleUsernameDisp(caller,divid) {
2023: if (document.getElementById(divid)) {
2024: if (caller.checked) {
2025: if (caller.value == '1') {
2026: document.getElementById(divid).style.display = 'none';
2027: } else {
2028: document.getElementById(divid).style.display = 'inline';
2029: }
2030: }
2031: }
2032: }
2033: // ]]>
2034: </script>
2035: ENDSCRIPT
2036:
2037: }
2038:
2039: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>