]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-9574 + MDL-9607 Add Change-own-password and Edit-own-profile capabilities
authorskodak <skodak>
Thu, 26 Apr 2007 21:41:08 +0000 (21:41 +0000)
committerskodak <skodak>
Thu, 26 Apr 2007 21:41:08 +0000 (21:41 +0000)
lang/en_utf8/moodle.php
lang/en_utf8/role.php
lib/db/access.php
lib/moodlelib.php
login/change_password.php
login/forgot_password.php
user/edit.php
user/tabs.php
user/view.php
version.php

index 34db40ad16028d1611184fa58c4b089d453fbcf0..e2d2b0ace4220628906d73c3b9262fb2c740c082 100644 (file)
@@ -500,6 +500,14 @@ line at the top of your web browser window.
 
 If you need help, please contact the site administrator,
 $a->admin';
+$string['emailpasswordchangeinfodisabled'] = 'Hi $a->firstname,
+
+Someone (probably you) has requested a new password for your
+account on \'$a->sitename\'.
+
+Unfortunately your account on this site is disabled and can not be reset, 
+please contact the site administrator,
+$a->admin';
 $string['emailpasswordchangeinfofail'] = 'Hi $a->firstname,
 
 Someone (probably you) has requested a new password for your
index 851f1210c5f8003c026c38b70a90a4ef752d344e..813464c3257e166ec76e02e22ddeb77393c6feb0 100644 (file)
@@ -116,8 +116,10 @@ $string['site:uploadusers'] = 'Upload new users from file';
 $string['site:viewfullnames'] = 'Always see full names of users';
 $string['site:viewparticipants'] = 'View participants';
 $string['site:viewreports'] = 'View reports';
+$string['user:changeownpassword'] = 'Change own password';
 $string['user:create'] = 'Create users';
 $string['user:delete'] = 'Delete users';
+$string['user:editownprofile'] = 'Edit own user profile';
 $string['user:editprofile'] = 'Edit user profile';
 $string['user:loginas'] = 'Login as other users';
 $string['user:readuserblogs'] = 'See all user blogs';
index fb2284bae9c8730965af42dfb1ab4dce60741075..185cf34cf2fb8feeb2ee9a7b3b4ef86247c31661 100644 (file)
@@ -719,13 +719,35 @@ $moodle_capabilities = array(
                 
            'riskbitmask' => RISK_SPAM,          
                 
-           'captype' => 'read',         
+           'captype' => 'write',        
            'contextlevel' => CONTEXT_USER,      
            'legacy' => array(   
               'admin' => CAP_ALLOW      
            )    
        ),
 
+    'moodle/user:editownprofile' => array(      
+
+        'captype' => 'write',     
+        'contextlevel' => CONTEXT_SYSTEM,      
+        'legacy' => array(   
+            'guest' => CAP_PROHIBIT,
+            'user' => CAP_ALLOW,
+            'admin' => CAP_ALLOW
+        )    
+    ),
+
+    'moodle/user:changeownpassword' => array(      
+
+        'captype' => 'write',     
+        'contextlevel' => CONTEXT_SYSTEM,      
+        'legacy' => array(   
+            'guest' => CAP_PROHIBIT,
+            'user' => CAP_ALLOW,
+            'admin' => CAP_ALLOW
+        )    
+    ),
+
     // The next 3 might make no sense for some roles, e.g teacher, etc.
     // since the next level up is site. These are more for the parent role
     'moodle/user:readuserposts' => array(
index 3df079b095d11ceab05c0247b89bfb7c6a3c1a37..0a98799af593f10996985dd682647f38ee2e3851 100644 (file)
@@ -3575,7 +3575,7 @@ function reset_password_and_mail($user) {
     $from = get_admin();
 
     $userauth = get_auth_plugin($user->auth);
-    if (!$userauth->can_reset_password()) {
+    if (!$userauth->can_reset_password() or !is_enabled_auth($user->auth)) {
         trigger_error("Attempt to reset user password for user $user->username with Auth $user->auth.");
         return false;
     }
@@ -3676,15 +3676,23 @@ function send_password_change_info($user) {
 
     $site = get_site();
     $from = get_admin();
+    $systemcontext = get_context_instance(CONTEXT_SYSTEM);
 
     $data = new object();
     $data->firstname = $user->firstname;
     $data->sitename = format_string($site->fullname);
     $data->admin = fullname($from).' ('. $from->email .')';
 
-     $userauth = get_auth_plugin($user->auth);
+    $userauth = get_auth_plugin($user->auth);
+
+    if (!is_enabled_auth($user->auth) or $user->auth == 'nologin') {
+        $message = get_string('emailpasswordchangeinfodisabled', '', $data);
+        $subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
+        return email_to_user($user, $from, $subject, $message);
+    }
+
     if ($userauth->can_change_password() and $userauth->change_password_url()) {
-        // we have some external url for password cahnging
+        // we have some external url for password changing
         $data->link .= $userauth->change_password_url();
 
     } else {
@@ -3692,7 +3700,7 @@ function send_password_change_info($user) {
         $data->link = '';
     }
 
-    if (!empty($data->link)) {
+    if (!empty($data->link) and has_capability('moodle/user:changeownpassword', $systemcontext, $user->id)) {
         $message = get_string('emailpasswordchangeinfo', '', $data);
         $subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
     } else {
index 6a6b938b720b068965e54c0d20342d3b48ef5411..525f02db8cc75f082ee20d7c6b4318a9bb47c34e 100644 (file)
         error('No such course!');
     }
 
-    // require proper login; guest can not change password
-    // TODO: add change password capability so that we can prevent participants from changing password
-    if (empty($USER->id) or isguestuser() or has_capability('moodle/legacy:guest', $systemcontext, $USER->id, false)) {
+    // require proper login; guest user can not change password
+    if (empty($USER->id) or isguestuser()) {
         if (empty($SESSION->wantsurl)) {
             $SESSION->wantsurl = $CFG->httpswwwroot.'/login/change_password.php';
         }
         redirect($CFG->httpswwwroot.'/login/index.php');
     }
 
+    // do not require change own password cap if change forced
+    if (!get_user_preferences('auth_forcepasswordchange', false)) {
+        require_capability('moodle/user:changeownpassword', $systemcontext);
+    }
+
     // do not allow "Logged in as" users to change any passwords
     if (!empty($USER->realuser)) {
         error('Can not use this script when "Logged in as"!');
index 4de719922459da33ebb31ba9ad481bd1e1538780..6244ace64eb2b18dc73395cdf9d0c25ff453b689 100644 (file)
@@ -12,7 +12,7 @@ $p_username = optional_param('s', false, PARAM_RAW);
 
 httpsrequired();
 
-$sitecontext = get_context_instance(CONTEXT_SYSTEM);
+$systemcontext = get_context_instance(CONTEXT_SYSTEM);
 
 // setup text strings
 $strforgotten = get_string('passwordforgotten');
@@ -41,11 +41,13 @@ if ($p_secret !== false) {
         // make sure that url relates to a valid user
 
         // check this isn't guest user
-        // TODO: add change password capability so that we can prevent participants to change password
-        if (has_capability('moodle/legacy:guest', $sitecontext, $user->id, false)) {
+        if (isguestuser($user)) {
             error('You cannot reset the guest password');
         }
 
+        // make sure user is allowed to change password
+        require_capability('moodle/user:changeownpassword', $systemcontext, $user->id);
+
         // override email stop and mail new password
         $user->emailstop = 0;
         if (!reset_password_and_mail($user)) {
@@ -97,9 +99,14 @@ if ($mform->is_cancelled()) {
     if ($user and !empty($user->confirmed)) {
 
         $userauth = get_auth_plugin($user->auth);
+        if (has_capability('moodle/user:changeownpassword', $systemcontext, $user->id)) {
+            // send email (make sure mail block is off)
+            $user->mailstop = 0;
+        }
 
-        if ($userauth->can_reset_password()) {
-            // reset internal password and notify user
+        if ($userauth->can_reset_password() and is_enabled_auth($user->auth)
+          and has_capability('moodle/user:changeownpassword', $systemcontext, $user->id)) {
+            // send reset password confirmation
 
             // set 'secret' string
             $user->secret = random_string(15);
@@ -107,15 +114,11 @@ if ($mform->is_cancelled()) {
                 error('error setting user secret string');
             }
 
-            // send email (make sure mail block is off)
-            $user->mailstop = 0;
             if (!send_password_change_confirmation_email($user)) {
                 error('error sending password change confirmation email');
             }
 
         } else {
-            // send email (make sure mail block is off)
-            $user->mailstop = 0;
             if (!send_password_change_info($user)) {
                 error('error sending password change confirmation email');
             }
index a34776e2c9e74eace12556e50a4d7891cbd9dae4..c72612728517869e526ecc4fcedb4d0ae93f1d46 100644 (file)
         redirect($CFG->httpswwwroot.'/login/index.php');
     }
 
-    if (isguest()) { //TODO: add proper capability to edit own profile
+    $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
+    $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
+
+    if (isguestuser()) {
         print_error('guestnoeditprofile');
     }
 
     }
 
     // check access control
-    if ($user->id != $USER->id) {
+    if ($user->id == $USER->id) {
+        //editing own profile
+        require_capability('moodle/user:editownprofile', $systemcontext);
+
+    } else {
         // teachers, parents, etc.
-        $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
         require_capability('moodle/user:editprofile', $personalcontext);
         // no editing of guest user account
         if (isguestuser($user->id)) {
index 5d71c422c01c0644a0fc2aff672d886b7f96a460..f2c0be822090e166cf2c44f0e27a0f90998d5d04 100644 (file)
         }
 
         $edittype = 'none';
-        if (is_mnet_remote_user($user)) {
+        if (isguestuser($user)) {
+            // guest account can not be edited
+
+        } else if (is_mnet_remote_user($user)) {
             // cannot edit remote users
 
-        } else if (isguest() or !isloggedin()) {
-            // can not edit guest like accounts - TODO: add capability to edit own profile
-            
+        } else if (isguestuser() or !isloggedin()) {
+            // guests and not logged in can not edit own profile
+
         } else if ($USER->id == $user->id) {
             if (has_capability('moodle/user:update', $systemcontext)) {
                 $edittype = 'advanced';
-            } else {
+            } else if (has_capability('moodle/user:editownprofile', $systemcontext)) {
                 $edittype = 'normal';
             }
 
index 03cc1b38aad82da2ce37c22c319f69ed5d3feacd..6f59476217b497dfb5a2614d7b1e3a7e4c540bc4 100644 (file)
     }
 
     if ($course->id == SITEID) {
-        $coursecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);   // SYSTEM context
+        $coursecontext = get_context_instance(CONTEXT_SYSTEM);   // SYSTEM context
     } else {
         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);   // Course context
     }
     $usercontext   = get_context_instance(CONTEXT_USER, $user->id);       // User context
+    $systemcontext = get_context_instance(CONTEXT_SYSTEM);   // SYSTEM context
     
     if (!empty($CFG->forcelogin) || $course->id != SITEID) {
         // do not force parents to enrol
     $userauth = get_auth_plugin($user->auth);
 
     $passwordchangeurl = false;
-    if (/*$currentuser and */$userauth->can_change_password() and !isguest()) { //TODO: add proper capability for password changing
-        if ($userauth->change_password_url()) {
-            $passwordchangeurl = $userauth->change_password_url();
-        } else {
+    if ($currentuser and $userauth->can_change_password() and !isguestuser() and has_capability('moodle/user:changeownpassword', $systemcontext)) {
+        if (!$passwordchangeurl = $userauth->change_password_url()) {
             if (empty($CFG->loginhttps)) {
                 $passwordchangeurl = "$CFG->wwwroot/login/change_password.php";
             } else {
index aa3f3f92e06a108cd8bd9df1495f019d16e3a6fa..1f934a65f294544e89daf8d6f3c979ecec7232e3 100644 (file)
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-   $version = 2007042601;  // YYYYMMDD = date
+   $version = 2007042700;  // YYYYMMDD = date
                            //       XY = increments within a single day
 
    $release = '1.9 dev';    // Human-friendly version name