'.$end_page);
return OK;
}
my ($js,$courseid,$title);
- if (defined($env{'form.courseid'})) {
- $courseid = &validate_course($env{'form.courseid'});
- }
+ $courseid = Apache::lonnet::is_course($env{'form.courseid'});
if ($courseid ne '') {
$js = &catreturn_js();
$title = 'Self-enroll in a LON-CAPA course';
@@ -112,41 +105,34 @@ sub handler {
if ($env{'form.udom'} ne '') {
$domain = $env{'form.udom'};
}
+
+ my %domconfig =
+ &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
+ my ($cancreate,$statustocreate) =
+ &get_creation_controls($domain,$domconfig{'usercreation'});
+
my ($result,$output) =
&username_validation($r,$env{'form.uname'},$domain,$domdesc,
$contact_name,$contact_email,$courseid,
- $lonhost);
+ $lonhost,$statustocreate);
if ($result eq 'existingaccount') {
$r->print($output);
&print_footer($r);
return OK;
} else {
- $start_page =
- &Apache::loncommon::start_page($title,$js,
- {'no_inline_link' => 1,});
+ $start_page = &Apache::loncommon::start_page($title,$js);
&print_header($r,$start_page,$courseid);
$r->print($output);
&print_footer($r);
return OK;
}
}
- $start_page =
- &Apache::loncommon::start_page($title,$js,
- {'no_inline_link' => 1,});
- my @cancreate;
- my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
- if (ref($domconfig{'usercreation'}) eq 'HASH') {
- if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
- if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
- @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
- } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') &&
- ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
- @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
- }
- }
- }
+ $start_page = &Apache::loncommon::start_page($title,$js);
- if (@cancreate == 0) {
+ my %domconfig =
+ &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
+ my ($cancreate,$statustocreate) = &get_creation_controls($domain,$domconfig{'usercreation'});
+ if (@{$cancreate} == 0) {
&print_header($r,$start_page,$courseid);
my $output = '
'.&mt('Account creation unavailable').'
'.
''.
@@ -160,12 +146,13 @@ sub handler {
&print_header($r,$start_page,$courseid);
my ($msg,$sso_logout);
$sso_logout = &sso_logout_frag($r,$domain);
- if (grep(/^sso$/,@cancreate)) {
+ if (grep(/^sso$/,@{$cancreate})) {
$msg = '
'.&mt('Account creation').'
'.
&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.").' ';
$msg .= &username_check($sso_username,$domain,$domdesc,$courseid,
- $lonhost,$contact_email,$contact_name,$sso_logout);
+ $lonhost,$contact_email,$contact_name,
+ $sso_logout,$statustocreate);
} else {
$msg = '
'.&mt('Account creation unavailable').'
'.
''.&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.").'
'.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email).''.
@@ -201,7 +188,7 @@ sub handler {
if ($env{'form.phase'} eq 'username_activation') {
(my $result,$output,$nostart) =
&username_activation($r,$env{'form.uname'},$domain,$domdesc,
- $lonhost,$courseid);
+ $courseid);
if ($result eq 'ok') {
if ($nostart) {
return OK;
@@ -215,7 +202,7 @@ sub handler {
(my $result,$output) =
&username_validation($r,$env{'form.uname'},$domain,$domdesc,
$contact_name,$contact_email,$courseid,
- $lonhost);
+ $lonhost,$statustocreate);
if ($result eq 'existingaccount') {
$r->print($output);
&print_footer($r);
@@ -226,21 +213,21 @@ sub handler {
} elsif ($env{'form.create_with_email'}) {
&print_header($r,$start_page,$courseid);
$output = &process_email_request($env{'form.useremail'},$domain,$domdesc,
- $contact_name,$contact_email,\@cancreate,
+ $contact_name,$contact_email,$cancreate,
$lonhost,$domconfig{'usercreation'},
$courseid);
} elsif (!$token) {
&print_header($r,$start_page,$courseid);
my $now=time;
- if (grep(/^login$/,@cancreate)) {
+ if (grep(/^login$/,@{$cancreate})) {
my $jsh=Apache::File->new($include."/londes.js");
$r->print(<$jsh>);
$r->print(&javascript_setforms($now));
}
- if (grep(/^email$/,@cancreate)) {
+ if (grep(/^email$/,@{$cancreate})) {
$r->print(&javascript_validmail());
}
- $output = &print_username_form($domain,$domdesc,\@cancreate,$now,$lonhost,
+ $output = &print_username_form($domain,$domdesc,$cancreate,$now,$lonhost,
$courseid);
}
$r->print($output);
@@ -280,7 +267,7 @@ sub selfenroll_crumbs {
my ($r,$courseid,$desc) = @_;
&Apache::lonhtmlcommon::add_breadcrumb
({href=>"javascript:ToCatalog('backupcrumbs','')",
- text=>"Course Catalog"});
+ text=>"Course/Community Catalog"});
if ($env{'form.coursenum'} ne '') {
&Apache::lonhtmlcommon::add_breadcrumb
({href=>"javascript:ToCatalog('backupcrumbs','details')",
@@ -288,7 +275,7 @@ sub selfenroll_crumbs {
}
my $last_crumb;
if ($desc ne '') {
- $last_crumb = &mt('Self-enroll in [_1]',''.$desc.'');
+ $last_crumb = &mt('Self-enroll in [_1]',"$desc");
} else {
$last_crumb = &mt('Self-enroll');
}
@@ -299,23 +286,13 @@ sub selfenroll_crumbs {
return;
}
-sub validate_course {
- my ($courseid) = @_;
- my ($cdom,$cnum) = ($courseid =~ /^($match_domain)_($match_courseid)$/);
- if (($cdom ne '') && ($cnum ne '')) {
- if (&Apache::lonnet::is_course($cdom,$cnum)) {
- return ($courseid);
- }
- }
- return;
-}
-
sub javascript_setforms {
my ($now) = @_;
my $js = <
+
ENDSCRIPT
@@ -404,8 +381,14 @@ sub print_username_form {
}
if (grep(/^email$/,@{$cancreate})) {
$output .= '
'.&mt('Create account with an e-mail address as your username').'
';
- my $captchaform = &create_captcha();
- if ($captchaform) {
+ my ($captchaform,$error) = &Apache::loncommon::captcha_display('usercreation',$lonhost);
+ if ($error) {
+ my $helpdesk = '/adm/helpdesk?origurl=%2fadm%2fcreateaccount';
+ if ($courseid ne '') {
+ $helpdesk .= '&courseid='.$courseid;
+ }
+ $output .= ''.&mt('An error occurred generating the validation code[_1] required for an e-mail address to be used as username.',' ').'
'."\n".
+ ''."\n";
+ }
} else {
$output = &mt('Could not load javascript file [_1]','londes.js');
}
return $output;
}
+#
+# Retrieve rules for generating accounts from domain configuration
+# Can the user make a new account or just self-enroll?
+
+sub get_creation_controls {
+ my ($domain,$usercreation) = @_;
+ my (@cancreate,@statustocreate);
+ if (ref($usercreation) eq 'HASH') {
+ if (ref($usercreation->{'cancreate'}) eq 'HASH') {
+ if (ref($usercreation->{'cancreate'}{'statustocreate'}) eq 'ARRAY') {
+ @statustocreate = @{$usercreation->{'cancreate'}{'statustocreate'}};
+ if (@statustocreate == 0) {
+ my ($othertitle,$usertypes,$types) =
+ &Apache::loncommon::sorted_inst_types($domain);
+ if (ref($types) eq 'ARRAY') {
+ if (@{$types} == 0) {
+ @statustocreate = ('default');
+ }
+ } else {
+ @statustocreate = ('default');
+ }
+ }
+ } else {
+ @statustocreate = ('default');
+ my ($othertitle,$usertypes,$types) =
+ &Apache::loncommon::sorted_inst_types($domain);
+ if (ref($types) eq 'ARRAY') {
+ push(@statustocreate,@{$types});
+ }
+ }
+ if (ref($usercreation->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
+ @cancreate = @{$usercreation->{'cancreate'}{'selfcreate'}};
+ } elsif (($usercreation->{'cancreate'}{'selfcreate'} ne 'none') &&
+ ($usercreation->{'cancreate'}{'selfcreate'} ne '')) {
+ @cancreate = ($usercreation->{'cancreate'}{'selfcreate'});
+ }
+ }
+ }
+ return (\@cancreate,\@statustocreate);
+}
+
sub create_account {
- my ($r,$domain,$lonhost,$username,$domdesc) = @_;
+ my ($r,$domain,$username,$domdesc) = @_;
+# Get the token info
my ($retrieved,$output,$upass) = &process_credentials($env{'form.logtoken'},
$env{'form.serverid'});
+# $retrieved is 'ok' if things worked
+# $output is user error output
+# $upass is the decrypted password
# Error messages
my $error = ''.&mt('Error:').' ';
my $end = '
';
@@ -811,6 +810,7 @@ sub create_account {
&Apache::loncommon::end_page();
if ($retrieved eq 'ok') {
if ($env{'form.courseid'} ne '') {
+# See if we are allowed to use this username per domain rules (number of characters, etc)
my ($result,$userchkmsg) = &check_id($username,$domain,$domdesc);
if ($result eq 'fail') {
$output = $error.&mt('Invalid ID format').$end.
@@ -821,22 +821,32 @@ sub create_account {
} else {
return ('fail',$error.$output.$end.$rtnlink);
}
- # Call modifyuser
+ # Yes! We can do this. Valid token, valid username format
+ # Create an internally authenticated account with password $upass
+ # if the account does not exist yet
+ # Assign student/staff number $env{'form.cid'}, first name, last name, etc
my $result =
&Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
'internal',$upass,$env{'form.cfirstname'},
$env{'form.cmiddlename'},$env{'form.clastname'},
$env{'form.cgeneration'},undef,undef,$username);
$output = &mt('Generating user: [_1]',$result);
+ # Now that the user exists, we can have a homeserver
my $uhome = &Apache::lonnet::homeserver($username,$domain);
$output .= ' '.&mt('Home server: [_1]',$uhome).' '.
&Apache::lonnet::hostname($uhome).'
';
- return ('ok',$output);
+ return ('ok',$output,$uhome);
}
sub username_validation {
my ($r,$username,$domain,$domdesc,$contact_name,$contact_email,$courseid,
- $lonhost) = @_;
+ $lonhost,$statustocreate) = @_;
+# $username,$domain: for the user who needs to be validated
+# $domdesc: full name of the domain (for error messages)
+# $contact_name, $contact_email: name and email for user assistance (for error messages in &username_check
+# $courseid: ID of the course that the user should be validated for, goes into start_session
+# $statustocreate: -> inststatus in username_check ('faculty', 'staff', 'student', ...)
+
my ($retrieved,$output,$upass);
$username= &LONCAPA::clean_username($username);
@@ -851,7 +861,7 @@ sub username_validation {
if ($uhome ne 'no_host') {
my $result = &Apache::lonnet::authenticate($username,$upass,$domain);
if ($result ne 'no_host') {
- my %form = &start_session($r,$username,$domain,$lonhost,$courseid);
+ &start_session($r,$username,$domain,$uhome,$courseid);
$output = '
'.&mt('A LON-CAPA account already exists for username [_1] at this institution ([_2]).',''.$username.'',$domdesc).' '.&mt('The password entered was also correct so you have been logged in.');
return ('existingaccount',$output);
} else {
@@ -870,7 +880,8 @@ sub username_validation {
}
if ($authok eq 'authorized') {
$output = &username_check($username,$domain,$domdesc,$courseid,$lonhost,
- $contact_email,$contact_name);
+ $contact_email,$contact_name,undef,
+ $statustocreate);
} else {
$output = &login_failure_msg($courseid);
}
@@ -895,12 +906,12 @@ sub login_failure_msg {
}
sub username_check {
- my ($username,$domain,$domdesc,$courseid,$lonhost,$contact_email,$contact_name,
- $sso_logout) = @_;
+ my ($username,$domain,$domdesc,$courseid,$lonhost,$contact_email,
+ $contact_name,$sso_logout,$statustocreate) = @_;
my (%rulematch,%inst_results,$checkfail,$rowcount,$editable,$output,$msg,
%alerts,%curr_rules,%got_rules);
&call_rulecheck($username,$domain,\%alerts,\%rulematch,
- \%inst_results,\%curr_rules,%got_rules,'username');
+ \%inst_results,\%curr_rules,\%got_rules,'username');
if (ref($alerts{'username'}) eq 'HASH') {
if (ref($alerts{'username'}{$domain}) eq 'HASH') {
if ($alerts{'username'}{$domain}{$username}) {
@@ -916,6 +927,21 @@ sub username_check {
}
}
if (!$checkfail) {
+ if (ref($statustocreate) eq 'ARRAY') {
+ $checkfail = 'inststatus';
+ if (ref($inst_results{$username.':'.$domain}{inststatus}) eq 'ARRAY') {
+ foreach my $inststatus (@{$inst_results{$username.':'.$domain}{inststatus}}) {
+ if (grep(/^\Q$inststatus\E$/,@{$statustocreate})) {
+ undef($checkfail);
+ last;
+ }
+ }
+ } elsif (grep(/^default$/,@{$statustocreate})) {
+ undef($checkfail);
+ }
+ }
+ }
+ if (!$checkfail) {
$output = '