Example #1
0
/**
 * Creates a new user from the "Users" form using $_POST information.
 *
 * It seems that the first half is for backwards compatibility, but only
 * has the ability to alter the user's role. NXTClass core seems to
 * use this function only in the second way, running edit_user() with
 * no id so as to create a new user.
 *
 * @since 2.0
 *
 * @param int $user_id Optional. User ID.
 * @return null|nxt_Error|int Null when adding user, nxt_Error or User ID integer when no parameters.
 */
function add_user()
{
    if (func_num_args()) {
        // The hackiest hack that ever did hack
        global $nxt_roles;
        $user_id = (int) func_get_arg(0);
        if (isset($_POST['role'])) {
            $new_role = sanitize_text_field($_POST['role']);
            // Don't let anyone with 'edit_users' (admins) edit their own role to something without it.
            if ($user_id != get_current_user_id() || $nxt_roles->role_objects[$new_role]->has_cap('edit_users')) {
                // If the new role isn't editable by the logged-in user die with error
                $editable_roles = get_editable_roles();
                if (empty($editable_roles[$new_role])) {
                    nxt_die(__('You can’t give users that role.'));
                }
                $user = new nxt_User($user_id);
                $user->set_role($new_role);
            }
        }
    } else {
        add_action('user_register', 'add_user');
        // See above
        return edit_user();
    }
}
Example #2
0
 /**
  * Installs the blog
  *
  * {@internal Missing Long Description}}
  *
  * @since 2.1.0
  *
  * @param string $blog_title Blog title.
  * @param string $user_name User's username.
  * @param string $user_email User's email.
  * @param bool $public Whether blog is public.
  * @param null $deprecated Optional. Not used.
  * @param string $user_password Optional. User's chosen password. Will default to a random password.
  * @return array Array keys 'url', 'user_id', 'password', 'password_message'.
  */
 function nxt_install($blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '')
 {
     global $nxt_rewrite;
     if (!empty($deprecated)) {
         _deprecated_argument(__FUNCTION__, '2.6');
     }
     nxt_check_mysql_version();
     nxt_cache_flush();
     make_db_current_silent();
     populate_options();
     populate_roles();
     update_option('blogname', $blog_title);
     update_option('admin_email', $user_email);
     update_option('blog_public', $public);
     $guessurl = nxt_guess_url();
     update_option('siteurl', $guessurl);
     // If not a public blog, don't ping.
     if (!$public) {
         update_option('default_pingback_flag', 0);
     }
     // Create default user.  If the user already exists, the user tables are
     // being shared among blogs.  Just set the role in that case.
     $user_id = username_exists($user_name);
     $user_password = trim($user_password);
     $email_password = false;
     if (!$user_id && empty($user_password)) {
         $user_password = nxt_generate_password(12, false);
         $message = __('<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.');
         $user_id = nxt_create_user($user_name, $user_password, $user_email);
         update_user_option($user_id, 'default_password_nag', true, true);
         $email_password = true;
     } else {
         if (!$user_id) {
             // Password has been provided
             $message = '<em>' . __('Your chosen password.') . '</em>';
             $user_id = nxt_create_user($user_name, $user_password, $user_email);
         } else {
             $message = __('User already exists. Password inherited.');
         }
     }
     $user = new nxt_User($user_id);
     $user->set_role('administrator');
     nxt_install_defaults($user_id);
     $nxt_rewrite->flush_rules();
     nxt_new_blog_notification($blog_title, $guessurl, $user_id, $email_password ? $user_password : __('The password you chose during the install.'));
     nxt_cache_flush();
     return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message);
 }
Example #3
0
            check_admin_referer('bulk-users');
            $editable_roles = get_editable_roles();
            if (empty($editable_roles[$_REQUEST['new_role']])) {
                nxt_die(__('You can&#8217;t give users that role.'));
            }
            if (isset($_REQUEST['users'])) {
                $userids = $_REQUEST['users'];
                $update = 'promote';
                foreach ($userids as $user_id) {
                    $user_id = (int) $user_id;
                    // If the user doesn't already belong to the blog, bail.
                    if (!is_user_member_of_blog($user_id)) {
                        nxt_die(__('Cheatin&#8217; uh?'));
                    }
                    $user = new nxt_User($user_id);
                    $user->set_role($_REQUEST['new_role']);
                }
            } else {
                $update = 'err_promote';
            }
            break;
    }
    restore_current_blog();
    nxt_safe_redirect(add_query_arg('update', $update, $referer));
    exit;
}
if (isset($_GET['action']) && 'update-site' == $_GET['action']) {
    nxt_safe_redirect($referer);
    exit;
}
add_screen_option('per_page', array('label' => _x('Users', 'users per page (screen options)')));
Example #4
0
/**
 * Function for safely deleting a role and transferring the deleted role's users to the default role.  Note that 
 * this function can be extremely intensive.  Whenever a role is deleted, it's best for the site admin to assign 
 * the user's of the role to a different role beforehand.
 *
 * @since 0.2.0
 * @param string $role The name of the role to delete.
 */
function members_delete_role($role)
{
    /* Get the default role. */
    $default_role = get_option('default_role');
    /* Don't delete the default role. Site admins should change the default before attempting to delete the role. */
    if ($role == $default_role) {
        return;
    }
    /* Get all users with the role to be deleted. */
    $users = get_users(array('role' => $role));
    /* Check if there are any users with the role we're deleting. */
    if (is_array($users)) {
        /* If users are found, loop through them. */
        foreach ($users as $user) {
            /* Create a new user object. */
            $new_user = new nxt_User($user->ID);
            /* If the user has the role, remove it and set the default. Do we need this check? */
            if ($new_user->has_cap($role)) {
                $new_user->remove_role($role);
                $new_user->set_role($default_role);
            }
        }
    }
    /* Remove the role. */
    remove_role($role);
}
Example #5
0
/**
 * Add a user to a blog.
 *
 * Use the 'add_user_to_blog' action to fire an event when
 * users are added to a blog.
 *
 * @since MU 1.0
 *
 * @param int $blog_id ID of the blog you're adding the user to.
 * @param int $user_id ID of the user you're adding.
 * @param string $role The role you want the user to have
 * @return bool
 */
function add_user_to_blog($blog_id, $user_id, $role)
{
    switch_to_blog($blog_id);
    $user = new nxt_User($user_id);
    if (empty($user->ID)) {
        restore_current_blog();
        return new nxt_Error('user_does_not_exist', __('That user does not exist.'));
    }
    if (!get_user_meta($user_id, 'primary_blog', true)) {
        update_user_meta($user_id, 'primary_blog', $blog_id);
        $details = get_blog_details($blog_id);
        update_user_meta($user_id, 'source_domain', $details->domain);
    }
    $user->set_role($role);
    do_action('add_user_to_blog', $user_id, $role, $blog_id);
    nxt_cache_delete($user_id, 'users');
    restore_current_blog();
    return true;
}