Merge branch 'MDL-51415-master' of git://github.com/jleyva/moodle
authorDavid Monllao <davidm@moodle.com>
Wed, 30 Sep 2015 06:15:29 +0000 (14:15 +0800)
committerDavid Monllao <davidm@moodle.com>
Wed, 30 Sep 2015 06:15:29 +0000 (14:15 +0800)
1  2 
user/externallib.php

diff --combined user/externallib.php
@@@ -53,10 -53,7 +53,10 @@@ class core_user_external extends extern
                              'username' =>
                                  new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config.'),
                              'password' =>
 -                                new external_value(PARAM_RAW, 'Plain text password consisting of any characters'),
 +                                new external_value(PARAM_RAW, 'Plain text password consisting of any characters', VALUE_OPTIONAL),
 +                            'createpassword' =>
 +                                new external_value(PARAM_BOOL, 'True if password should be created and mailed to user.',
 +                                    VALUE_OPTIONAL),
                              'firstname' =>
                                  new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
                              'lastname' =>
          $transaction = $DB->start_delegated_transaction();
  
          $userids = array();
 +        $createpassword = false;
          foreach ($params['users'] as $user) {
              // Make sure that the username doesn't already exist.
              if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id))) {
                  throw new invalid_parameter_exception('Invalid theme: '.$user['theme']);
              }
  
 +            // Make sure we have a password or have to create one.
 +            if (empty($user['password']) && empty($user['createpassword'])) {
 +                throw new invalid_parameter_exception('Invalid password: you must provide a password, or set createpassword.');
 +            }
 +
              $user['confirmed'] = true;
              $user['mnethostid'] = $CFG->mnet_localhost_id;
  
              }
              // End of user info validation.
  
 +            $createpassword = !empty($user['createpassword']);
 +            unset($user['createpassword']);
 +            if ($createpassword) {
 +                $user['password'] = '';
 +                $updatepassword = false;
 +            } else {
 +                $updatepassword = true;
 +            }
 +
              // Create the user data now!
 -            $user['id'] = user_create_user($user, true, false);
 +            $user['id'] = user_create_user($user, $updatepassword, false);
  
              // Custom fields.
              if (!empty($user['customfields'])) {
                  profile_save_data((object) $user);
              }
  
 +            if ($createpassword) {
 +                $userobject = (object)$user;
 +                setnew_password_and_mail($userobject);
 +                unset_user_preference('create_password', $userobject);
 +                set_user_preference('auth_forcepasswordchange', 1, $userobject);
 +            }
 +
              // Trigger event.
              \core\event\user_created::create_from_userid($user['id'])->trigger();
  
              profile_view($user, $usercontext);
          } else {
              // Case like user/view.php.
-             if (!$currentuser and !is_enrolled($coursecontext, $user->id)) {
+             if (!$currentuser and !can_access_course($course, $user, '', true)) {
                  throw new moodle_exception('notenrolledprofile');
              }