Example #1
0
/**
 * Add new or change existing capabilities for a given role
 *
 * Usage: merge_role_caps( 'author', array( 'publish_posts' => false ) );
 *
 * @param string $role Role name
 * @param array $caps Key/value array of capabilities for this role
 */
function merge_role_caps($role, $caps)
{
    if (function_exists('wpcom_vip_merge_role_caps')) {
        wpcom_vip_merge_role_caps($role, $caps);
    } else {
        global $wp_user_roles;
        $role_obj = get_role($role);
        if (!$role_obj) {
            return;
        }
        $current_caps = (array) get_role_caps($role);
        $new_caps = array_merge($current_caps, (array) $caps);
        foreach ($new_caps as $cap => $role_can) {
            if ($role_can) {
                $role_obj->add_cap($cap);
            } else {
                $role_obj->remove_cap($cap);
            }
        }
        if (isset($wp_user_roles[$role])) {
            $wp_user_roles[$role]['capabilities'] = array_merge($current_caps, (array) $caps);
        }
    }
}
Example #2
0
/**
 * Load default logged in role capabilities for all logged in users
 * @return bool
 */
function load_defaultuser_role($return = false)
{
    global $CFG, $USER;
    if (!($sitecontext = get_context_instance(CONTEXT_SYSTEM))) {
        return false;
    }
    if (empty($CFG->defaultuserroleid)) {
        // Let's set the default to the guest role
        if (!($role = get_guest_role())) {
            $defaultuserroleid = $role->id;
        } else {
            return false;
        }
    } else {
        $defaultuserroleid = $CFG->defaultuserroleid;
    }
    $capabilities = get_role_caps($defaultuserroleid);
    // fix the guest user heritage:
    // If the default role is a guest role, then don't copy legacy:guest,
    // otherwise this user could get confused with a REAL guest. Also don't copy
    // course:view, which is a hack that's necessary because guest roles are
    // not really handled properly (see MDL-7513)
    if (!empty($capabilities[$sitecontext->id]['moodle/legacy:guest'])) {
        unset($capabilities[$sitecontext->id]['moodle/legacy:guest']);
        unset($capabilities[$sitecontext->id]['moodle/course:view']);
    }
    if ($return) {
        return $capabilities;
    } else {
        has_capability('clearcache');
        $USER->capabilities = $capabilities;
        return true;
    }
}