[LON-CAPA-cvs] cvs: loncom /interface loncreateuser.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Tue, 11 Dec 2007 02:27:25 -0000


This is a MIME encoded message

--raeburn1197340045
Content-Type: text/plain

raeburn		Mon Dec 10 21:27:25 2007 EDT

  Modified files:              
    /loncom/interface	loncreateuser.pm 
  Log:
  - inclusion of user input fields in personal data table when modifying an existing user, determined by context and role-based settings stored in domain configuration
  - "force ID change" checkbox displayed if student/employeeID is potentially modifiable 
  - execution of proposed changes to user information checked against domain config (sensitive to context and active/future role(s) of target user in current context
  - results page displays appropriate messages to explain which proposed changes wener/were not executed
  - 'construction_space' context renamed 'author' for constency with domainprefs.pm- user information fields all editable if editor has mau priv in domain of target user (except for student/employeeID - which requires forceID checkbox checked). 
  
  
--raeburn1197340045
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20071210212725.txt"

Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.205 loncom/interface/loncreateuser.pm:1.206
--- loncom/interface/loncreateuser.pm:1.205	Sat Dec  8 14:19:03 2007
+++ loncom/interface/loncreateuser.pm	Mon Dec 10 21:27:24 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.205 2007/12/08 19:19:03 raeburn Exp $
+# $Id: loncreateuser.pm,v 1.206 2007/12/11 02:27:24 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -707,9 +707,11 @@
 <input type='hidden' name='makeuser' value='1' />
 <h2>$lt{'cnu'} "$ccuname" $lt{'ind'} $ccdomain</h2>
 ENDTITLE
-        $r->print('<div class="LC_left_float">'.
-                  &personal_data_display($ccuname,$ccdomain,$newuser,
-                                         %inst_results));
+        $r->print('<div class="LC_left_float">');
+        my $personal_table = 
+            &personal_data_display($ccuname,$ccdomain,$newuser,
+                                   $context,%inst_results);
+        $r->print($personal_table);
         my ($home_server_pick,$numlib) = 
             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                                                       'default','hide');
@@ -786,10 +788,12 @@
 $forminfo
 <h2>$lt{'cup'} "$ccuname" $lt{'id'} "$ccdomain"</h2>
 ENDCHANGEUSER
-        $r->print('<div class="LC_left_float">'.
-                  &personal_data_display($ccuname,$ccdomain,$newuser,
-                                         %inst_results));
-        if ($context eq 'domain') {
+        $r->print('<div class="LC_left_float">');
+        my ($personal_table,$showforceid) = 
+            &personal_data_display($ccuname,$ccdomain,$newuser,
+                                   $context,%inst_results);
+        $r->print($personal_table);
+        if ($showforceid) {
             $r->print(&Apache::lonuserutils::forceid_change($context));
         }
         $r->print('</div>');
@@ -1318,13 +1322,16 @@
 }
 
 sub personal_data_display {
-    my ($ccuname,$ccdomain,$newuser,%inst_results) = @_; 
-    my ($output,%userenv);
+    my ($ccuname,$ccdomain,$newuser,$context,%inst_results) = @_; 
+    my ($output,$showforceid,%userenv,%domconfig);
     if (!$newuser) {
         # Get the users information
         %userenv = &Apache::lonnet::get('environment',
                    ['firstname','middlename','lastname','generation',
                     'permanentemail','id'],$ccdomain,$ccuname);
+        %domconfig =
+            &Apache::lonnet::get_dom('configuration',['usermodification'],
+                                     $ccdomain);
     }
     my %lt=&Apache::lonlocal::texthash(
                 'pd'             => "Personal Data",
@@ -1362,21 +1369,47 @@
                 $output .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
             }
         } else {
+            my $canmodify = 0;
             if (&Apache::lonnet::allowed('mau',$ccdomain)) {
+                $canmodify = 1;
+            } else {
+                if (ref($domconfig{'usermodification'}) eq 'HASH') {
+                    if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
+                        foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
+                            if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
+                                if ($domconfig{'usermodification'}{$context}{$key}{$item}) { 
+                                    $canmodify = 1;
+                                    last;
+                                }
+                            }
+                        }
+                    }
+                } elsif ($context eq 'course') {
+                    $canmodify = 1;
+                }
+            }
+            if ($canmodify) {
                 $output .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
             } else {
                 $output .= $userenv{$item};
             }
+            if ($item eq 'id') {
+                $showforceid = $canmodify; 
+            } 
         }
         $output .= &Apache::lonhtmlcommon::row_closure(1);
     }
     $output .= &Apache::lonhtmlcommon::end_pick_box();
-    return $output;
+    if (wantarray) {
+        return ($output,$showforceid);
+    } else {
+        return $output;
+    }
 }
 
 # ================================================================= Phase Three
 sub update_user_data {
-    my ($r) = @_; 
+    my ($r,$context) = @_; 
     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                                           $env{'form.ccdomain'});
     # Error messages
@@ -1574,10 +1607,114 @@
         if ($tmp =~ /^(con_lost|error)/i) { 
             %userenv = ();
         }
-        # Check to see if we need to change user information
+        my $no_forceid_alert;
+        # Check to see if user information can be changed
+        my %domconfig =
+            &Apache::lonnet::get_dom('configuration',['usermodification'],
+                                     $env{'form.ccdomain'});
+        my @roletypes = ('active','future');
+        my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@roletypes,undef,$env{'request.role.domain'});
+        my @userroles;
+        my ($cnum,$cdom,$auname,$audom);
+        if ($context eq 'course') {
+            $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+            $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+            if ($cnum eq '' || $cdom eq '') {
+                my $cid = $env{'request.course.id'};
+                my %coursehash =
+                     &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
+                $cdom = $coursehash{'domain'};
+                $cnum = $coursehash{'num'};
+            } 
+        } elsif ($context eq 'author') {
+            $auname = $env{'user.name'};
+            $audom = $env{'user.domain'};     
+        }
+        foreach my $item (keys(%roles)) {
+            my ($rolenum,$roledom,$role) = split(/:/,$item);
+            if ($context eq 'course') {
+                if ($cnum ne '' && $cdom ne '') {
+                    if ($rolenum eq $cnum && $roledom eq $cdom) {
+                        if (!grep(/^\Q$role\E$/,@userroles)) {
+                            push(@userroles,$role);
+                        }
+                    }
+                }
+            } elsif ($context eq 'author') {
+                if ($rolenum eq $auname && $roledom eq $audom) {
+                    if (!grep(/^\Q$role\E$/,@userroles)) { 
+                        push(@userroles,$role);
+                    }
+                }
+            }
+        }
+        # Check for course or co-author roles being activated or re-enabled
+        if ($context eq 'author' || $context eq 'course') {
+            foreach my $key (keys(%env)) {
+                if ($context eq 'author') {
+                    if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
+                        if (!grep(/^\Q$1\E$/,@userroles)) {
+                            push(@userroles,$1);
+                        }
+                    } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
+                        if (!grep(/^\Q$1\E$/,@userroles)) {
+                            push(@userroles,$1);
+                        }
+                    }
+                } elsif ($context eq 'course') {
+                    if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
+                        if (!grep(/^\Q$1\E$/,@userroles)) {
+                            push(@userroles,$1);
+                        }
+                    } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
+                        if (!grep(/^\Q$1\E$/,@userroles)) {
+                            push(@userroles,$1);
+                        }
+                    }
+                }
+            }
+        }
+        #Check to see if we can change personal data for the user 
+        my (@mod_disallowed,@longroles);
+        foreach my $role (@userroles) {
+            if ($role eq 'cr') {
+                push(@longroles,'Custom');
+            } else {
+                push(@longroles,&Apache::lonnet::plaintext($role)); 
+            }
+        }
         foreach my $item ('firstname','middlename','lastname','generation','permanentemail','id') {
+            my $canmodify = 0;
+            if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
+                $canmodify = 1;
+            } else {
+                if ($context eq 'course' || $context eq 'author') {
+                    if (ref($domconfig{'usermodification'}) eq 'HASH') {
+                        if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
+                            foreach my $role (@userroles) {
+                                if (ref($domconfig{'usermodification'}{$context}{$role}) eq 'HASH') {
+                                    if ($domconfig{'usermodification'}{$context}{$role}{$item}) {
+                                        $canmodify = 1;
+                                        last;
+                                    }
+                                }
+                            }
+                        }
+                    }
+                } elsif ($context eq 'course') {
+                    if (grep(/^st$/,@userroles)) {
+                        $canmodify = 1;
+                    }
+                }
+            }
             # Strip leading and trailing whitespace
             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
+            if (!$canmodify) {
+                if ($env{'form.c'.$item} ne $userenv{$item}) {
+                    push(@mod_disallowed,$item);
+                }
+                $env{'form.c'.$item} = $userenv{$item};
+            }
         }
         # Check to see if we can change the ID/student number
         my $forceid = $env{'form.forceid'};
@@ -1591,6 +1728,7 @@
             (!$forceid)) {
             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                 $env{'form.cid'} = $userenv{'id'};
+                $no_forceid_alert = &mt('New student/employeeID does not match existing ID for this user.').'<br />'.&mt('Change is not permitted without checking the \'Force ID change\' checkbox on the previous page.').'<br />'."\n";        
             }
         }
         if ($env{'form.cid'} ne $userenv{'id'}) {
@@ -1654,13 +1792,12 @@
         if ($newisdefault) {
             $newdefquotatext = &get_defaultquota_text($settingstatus);
         }
-        if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}) && 
-            ($env{'form.cfirstname'}  ne $userenv{'firstname'}  ||
-             $env{'form.cmiddlename'} ne $userenv{'middlename'} ||
-             $env{'form.clastname'}   ne $userenv{'lastname'}   ||
-             $env{'form.cgeneration'} ne $userenv{'generation'} ||
-             $env{'form.cid'} ne $userenv{'id'}                 ||
-             $env{'form.cpermanentemail'} ne $userenv{'permanentemail'} )) {
+        if ($env{'form.cfirstname'}  ne $userenv{'firstname'}  ||
+            $env{'form.cmiddlename'} ne $userenv{'middlename'} ||
+            $env{'form.clastname'}   ne $userenv{'lastname'}   ||
+            $env{'form.cgeneration'} ne $userenv{'generation'} ||
+            $env{'form.cid'} ne $userenv{'id'}                 ||
+            $env{'form.cpermanentemail'} ne $userenv{'permanentemail'} ) {
             $namechanged = 1;
         }
         if ($namechanged || $quotachanged) {
@@ -1738,7 +1875,7 @@
     <td>$newportfolioquota Mb $newdefquotatext </td>
 END
                 $r->print(&Apache::loncommon::end_data_table_row().
-                          &Apache::loncommon::end_data_table());
+                          &Apache::loncommon::end_data_table().'<br />');
                 if ($env{'form.cid'} ne $userenv{'id'}) {
                     &Apache::lonnet::idput($env{'form.ccdomain'},
                          ($env{'form.ccuname'} => $env{'form.cid'}));
@@ -1769,7 +1906,7 @@
             } else { # error occurred
                 $r->print('<span class="LC_error">'.&mt('Unable to successfully change environment for').' '.
                       $env{'form.ccuname'}.' '.&mt('in domain').' '.
-                      $env{'form.ccdomain'}.'</span>');
+                      $env{'form.ccdomain'}.'</span><br />');
             }
         }  else { # End of if ($env ... ) logic
             # They did not want to change the users name or quota but we can
@@ -1789,6 +1926,27 @@
             $r->print('<br />['.$lt{'disk'}.': '.$oldportfolioquota.' Mb '. 
                  $olddefquotatext.']</h4>');
         }
+        if (@mod_disallowed) {
+            my ($rolestr,$contextname);
+            if (@longroles > 0) {
+                $rolestr = join(', ',@longroles);
+            } else {
+                $rolestr = &mt('No roles');
+            }
+            if ($context eq 'course') {
+                $contextname = &mt('course');
+            } elsif ($context eq 'author') {
+                $contextname = &mt('co-author');
+            }
+            $r->print(&mt('The following fields were not updated: ').'<ul>');
+            my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
+            foreach my $field (@mod_disallowed) {
+                $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
+            }
+            $r->print('</ul>'.&mt("You do not have the authority to change these fields given the user's current set of active/future [_1] roles: <span class=\"LC_cusr_emph\">[_2]</span>.",$contextname,$rolestr).'<br />'.&mt('Contact your <a href="[_1]">helpdesk</a> for more information.',"javascript:helpMenu('display')").'<br />');
+        }
+        $r->print($no_forceid_alert.
+                  &Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts, \%curr_rules));
     }
     ##
     my $now=time;
@@ -2398,7 +2556,7 @@
     if ($env{'request.course.id'}) {
         $context = 'course';
     } elsif ($env{'request.role'} =~ /^au\./) {
-        $context = 'construction_space';
+        $context = 'author';
     } else {
         $context = 'domain';
     }
@@ -2513,7 +2671,7 @@
                                               $context);
             }
         } elsif ($env{'form.phase'} eq 'update_user_data') {
-            &update_user_data($r);
+            &update_user_data($r,$context);
         } else {
             &print_username_entry_form($r,undef,$srch);
         }
@@ -2709,7 +2867,7 @@
                 }
             }
         }
-    } elsif ($context eq 'construction_space') {
+    } elsif ($context eq 'author') {
         $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
         $permission{'view'} = $permission{'cusr'};
     } else {

--raeburn1197340045--