• I’m sure I’m missing something simple here, but I’m having some issues with user caps.

    In our multisite installation (~250 sites), we used some custom code to remove the default “Administrator” role and replace it with a custom role that has many of the same caps, but a few that are missing (yes, I realize we could have just removed caps from the Administrator role, but it seemed cleaner this way, in case we needed to add full admins back at some point).

    In the past, we’d kept our admins from being able to activate plugins by not including the activate_plugins cap in the custom admin role. This week, we decided to give them that capability, but I can’t get that working properly.

    What I’ve done:
    I have some code set up to see if the current user is a member of the custom role (umw_site_manager) and whether or not the user is able to activate_plugins. If they are a member of that role, but they do not have the cap, I have the code set up to modify the umw_site_manager role to add that capability to it.

    What I expected:
    After activating that code and loading the dashboard as a umw_site_manager user, I expected the user to see the “Plugins” menu in the admin menu. I refreshed the dashboard a few times to make sure that things got updated in the database and the autoload cache had been updated appropriately.

    What I got:
    The user still does not have the “Plugins” menu in the admin menu. If I try to go directly to the plugins page by using the URL, I get the “You dont’ have sufficient permissions…” error.

    I’m not sure why it’s not working the way I’d expect. As I said, I’m sure I missed something simple, but I’m just not seeing it. I suspect it’s one of two things, but I could be wrong:

    1. The activate_plugins cap might be insufficient to allow a user to see the plugins menu and activate plugins (though, I’ve looked through the WordPress source, and it seems that that cap should be sufficient)
    2. There is some function I need to use to reconcile the user caps with the role caps, but I’m missing it.

    I’ve checked the database, and the activate_plugins cap has definitely been added to the appropriate role. The capabilities entry in the usermeta table shows that the test user has two caps (gform_full_access – a capability used with Gravity Forms, and umw_site_manager), so it doesn’t look like the individual capabilities for the user are overriding the role capabilities.

    What am I missing? Any help would be greatly appreciated. Following is the related bit of code I’m using for this purpose:

    if ( current_user_can( 'umw_site_manager' ) && ! current_user_can( 'activate_plugins' ) ) {
    			$role = get_role( 'umw_site_manager' );
    			$role->add_cap( 'activate_plugins' );
    			/* I put this next line in, just in case */
    			wp_update_user( array( 'ID' => get_current_user_id(), 'role' => 'umw_site_manager' ) );
    		}

    I’ve also written some code to output the current user object in an HTML comment within my admin area while I’m testing, along with the results of current_user_can( 'activate_plugins' ) and the results of $current_user->has_cap( 'activate_plugins' ).

    The dumped data from the user clearly shows activate_plugins within the allcaps property, but both of the tests return boolean false.

    Any ideas what might be stopping this capability from being applied the way I’d like it to be to my users? Thanks.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    No idea really, everything appears to be in order AFAIK.

    I have an idea for what to investigate though. You may have done this, either way the explicit results could be informative. Hook the filter ‘user_has_cap’ and dump the passed array, which is the allcaps user property. As a umw_site_manager user, attempt to load the plugins page and examine the dump.

    In order for $current_user->has_cap( 'activate_plugins' ) to return false as you are seeing, something in this array must be empty (according to the empty() function), even if the ‘activate_plugins’ key has a true value. What that is could be a good clue to where the problem lies.

Viewing 1 replies (of 1 total)
  • The topic ‘Updated Role Caps, But User Still Can't Use New Cap’ is closed to new replies.