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