Exemple #1
0
 /**
  * Make the user an admin
  *
  * @return bool
  */
 public function makeAdmin()
 {
     // If already saved, use the standard function.
     if ($this->guid && !make_user_admin($this->guid)) {
         return false;
     }
     // need to manually set attributes since they've already been loaded.
     $this->attributes['admin'] = 'yes';
     return true;
 }
/**
 * Insert user into elgg user table using info from ldap
 * Tries to insert, otherwise returns error
 *
 * @return user or error (false?)
 */
function cas_insertUser($username, $ldap_attributes, $config)
{
    // name is 'cn' in ldap
    $name = $ldap_attributes['cn'];
    // remove periods from ldap username
    // ex. anthony.hopkins -> anthonyhopkins
    $uname = !empty($ldap_attributes['textUid']) ? $ldap_attributes['textUid'] : str_replace(".", "", $username);
    $email = $ldap_attributes['mail'];
    $user = new ElggUser();
    $user->username = $uname;
    $user->email = $email;
    $user->name = $name;
    $user->access_id = 2;
    $user->salt = generate_random_cleartext_password();
    // Note salt generated before password!
    // cas users don't need password stored locally
    // so create an invalid password
    // a real password can be saved at a later time if they become a local user
    $user->password = md5(time());
    //generate_user_password($user, $password);
    // returns guid or false
    $guid = $user->save();
    if (!$guid) {
        return false;
    }
    $obj = get_entity($guid);
    if (isset($config->casadminuser) && $config->casadminuser == $uname) {
        if ($obj instanceof \ElggUser) {
            //set context for permissions check
            elgg_push_context('au_cas_auth_make_admin');
            if (make_user_admin($guid)) {
                system_message(elgg_echo('admin:user:makeadmin:yes'));
            } else {
                register_error(elgg_echo('admin:user:makeadmin:no'));
            }
            // set context back
            elgg_pop_context();
        } else {
            register_error(elgg_echo('admin:user:makeadmin:no'));
        }
    }
    return $user;
}