WordPress.org

Support

Support » Plugins and Hacks » User Access Manager » Check if user has rights to view category

Check if user has rights to view category

  • Hello,

    I have built my own custom category list function, which lists all my categories. What I need to do is check if the current user has rights to view those categories.

    I found this thread: http://wordpress.org/support/topic/plugin-user-access-manager-functions

    Sadly, the functions posted in this thread don’t work for me.

    What I need is a function like the one below, just with categories. I want to use a if function like “if (userhasrights) then show category, else don’t show category”

    global $userAccessManager;
    
    if (isset($userAccessManager)) {
        $userId = $user_ID;
        $uamAccessHandler = $userAccessManager->getAccessHandler();
        $userGroupsForUser = $uamAccessHandler->getUserGroupsForObject('user', $userId);
    
        print_r($userGroupsForUser);
    }

    http://wordpress.org/extend/plugins/user-access-manager/

Viewing 15 replies - 1 through 15 (of 17 total)
  • I just had this issue where I was using that snippet and the new version broke a client’s site.

    The plugin author changed the name of the variable to $oUserAccessManager, just replace $userAccessManager with $oUserAccessManager and you will be fine.

    I create some group with the plugin User Access Manager.
    the code liste below doesn’t returned any thing ! I tried with $oUserAccessManager… always empty array.

    Yes, I have the same problem @makssoud. No matter what I do I always get an empty array.

    Plugin Author GM_Alex

    @gm_alex

    Tested this

    <?php
    	global $oUserAccessManager;
    
    	if (isset($oUserAccessManager)) {
    		$iUserId = $user_ID;
    		$oUamAccessHandler = $oUserAccessManager->getAccessHandler();
    		$aUserGroupsForUser = $oUamAccessHandler->getUserGroupsForObject('user', $iUserId);
    
    		print_r($aUserGroupsForUser);
    	}
    ?>

    at the header.php of the twentytwelve template and it worked for me. Note: The admin user has no groups(!) because he is the admin and has access to all anyway. Test this code with a test user, which is assigned to some groups.

    Hi Alex, thanks for replying and the great plugin! Quick request, since some of us rely on this oUserManager Object, can you let us know in the changelog when it changes again? I had a mini-heart attack when I updated the plugin recently

    Plugin Author GM_Alex

    @gm_alex

    Yes I will make an clearer note for this in the future. The reason was that I changed my coding conventions, to make the code more readable.

    Plugin Author GM_Alex

    @gm_alex

    Can I mark the thread as resolved?

    I spend half a day, to get the groups of a post, until I realized that ALL TOPICS on Google search are rel on the old object name…

    Please create some new pages or How to, like the function above, on the FAQ or notes about plugin. Like this, a complete snippet for functions.php :

    function uamIsAccess(){
    	global $oUserAccessManager;
    	global $post;
    
    	if (isset($oUserAccessManager)) {
    		$iPostId = $post->ID;
    		$oUamAccessHandler = $oUserAccessManager->getAccessHandler();
    		$aGroups = $oUamAccessHandler->getUserGroupsForObject('post', $iPostId);
            if (count($aGroups) > 0) {
                $sLink = TXT_UAM_ASSIGNED_GROUPS.': ';
                foreach ($aGroups as $oGroup) {
                    $sLink .= $oGroup->getGroupName().', ';
                }
                $sLink = rtrim($sLink, ', ');
            }
    		return $sLink;
    	}
      }

    This return all the grouops assigned to current post.
    Use <?php echo uamIsAccess(); ?> in the whatever template file.

    And so on …

    Thanks Jonas.

    Your function above shows what group the Page/Post has access to. Do you happen to have a function which will check the currently logged in user’s access group?

    I am trying to show/hide template elements based on the user group(s), which I would know once they are logged in.

    Thanks!

    Hi,
    Im working on it myself and will start digg into it in a few days. But Here is a snippet that return true or false id the user has access to trhe current item in a loop or the global query

    I think it works .., You have to moderate the return for now, but I will post a complete solution later. You can see what is in the data with the dump, that you of course have to remove if using this.

    function uamHasAccess(){
    	global $oUserAccessManager;
    	if(!isset($oUserAccessManager)) return '';
    	$userID = get_current_user_id();
    	$uamAccessHandler = $oUserAccessManager->getAccessHandler();
    	//return $uamAccessHandler->checkUserAccess();
    
    	echo '<pre>';
    	var_dump($uamAccessHandler);
    	echo '</pre>';
      }

    call in a template with if(uamHasAccess()) { do stuff }

    The “Like this, a complete snippet for functions.php :” has a more complete version, and now works with categories or taxonomies.

    I also made a cache function so that a already known information loaded on that template page, doesnt have to query the stuff again.

    The plugin WILL slow down your site and grow negative, but this is common with many protectet systems. I try to keep this like s2member “quality”:

    function uamIsAccess($show_groups = 0){ 
    
    	global $oUserAccessManager;
    	if(!isset($oUserAccessManager)) return '';
    	if($show_groups === true) $show_groups = 1;
    	if($show_groups === false) $show_groups = 0;
    	if($show_groups == 0 || $show_groups == 1) {
    	global $post;
    	$sLink = '';
    	if (is_category()) {
    		$iPostId = get_query_var('cat');
    		$iPostType = 'category';
    	} else {
    		$iPostId = $post->ID;
    		$iPostType = get_post_type($iPostId);
    	}
    	//echo $iPostId;
    	if(isset($GLOBALS['UAM-CACHE'][$iPostId][$show_groups])) {
    		return $GLOBALS['UAM-CACHE'][$iPostId][$show_groups];
    	} else {
    		if (isset($oUserAccessManager)) {
    
    			//echo $iPostId. ' ' .$iPostType;
    			$oUamAccessHandler = $oUserAccessManager->getAccessHandler();
    			$aGroups = $oUamAccessHandler->getUserGroupsForObject($iPostType, $iPostId);
    			if (count($aGroups) > 0) {
    				if(!$show_groups) {
    					$sLink = __('This area is protected. Only users with the same capabilities can view this content', 'UA Network');
    				} else {
    					$sLink = TXT_UAM_ASSIGNED_GROUPS.' : ';
    					foreach ($aGroups as $oGroup) {
    						$sLink .= $oGroup->getGroupName().', ';
    					}
    					$sLink = rtrim($sLink, ', ');
    				}
    			}
    		}
    		$GLOBALS['UAM-CACHE'][$iPostId][$show_groups] = $sLink;
    		return $sLink;
    	}
    	} else return 'ERR: Invalid uamIsAccess() call. Arguments must be (bool) true or false';
    
    }

    This return 2 states, one the name of the assigned groups, like the after edit links for admins. And The false version, just telling the user that this is safe and protected.

    The latter, is to make the users feel good when commenting on protected stuff, or interacting whatever. My clients are happy anyway…

    echo uamIsAccess()
      echo uamIsAccess(true)
      if(uamIsAccess()) { this stuff is protected in any way ... }  else { This has no restrictions ... }

    I think it works to just paste in ya functions.php file

    Thanks Jonas. That is great. It’s nearly what I need, but not quite.

    What your function does works great for a particular page/template, but it changes from page to page with the context. In the instance where there are *no* special group permissions (content is open to all) there is no “group” value returned, so we can’t tie the meta content to a particular logged in user group.

    Is there a way to grab the UAM user group for the currently logged in user, so the variable will not change during the session?

    I need to co-brand a site for each user group and am hoping to handle that with this group variable for each logged-in user.

    Thanks for your great help.

    @efishinsea, so you need a function that return the current user permissions by UAM and echo it out constant wherever you need?

    More or less the reversed info by my snippets?
    Are you trying to build custom capabilities content parts in each template?

    I don understand your objectives

    “I need to co-brand a site for each user group and am hoping to handle that with this group variable for each logged-in user.”

    Im digging right now with UAM in a huge project so maybe I can help ya…

    For example, I want to be able to show a header to one particular UAM user group when someone belonging to that group is logged in. This is why I would need to know which group they belong to the entire time they are logged in, which should be available from the UAM object somewhere, right? I am just not sure how to access that information from the plugin.

    Ok, quick answer ,something like

    in single.php template
    and ya got saved header-family.php, header-clients.php

    group = uam_get_current_user_belongs_to();
    if(group == ‘clients’) get_header(‘clients’);
    else if(group == ‘family’) get_header(‘family’);
    else get_header();

    With above dummie php code (not ‘working’ code) there is a little bit more to it if a user belongs to more then one group

    The return value must be an array ( think ) and tested with if(in_array(‘family’, ‘groups’)) get_header(….

    Then a sub-function would be better in functions php

    function uamUserIs($request){
    $id = get_current_user_id();
    groups = the_function_to_retrive($id);
    if(in_array($request, ‘groups’)) return true;
    else return false;
    }

    then in a template

    if(uamUserIs(‘family’)) { get_header(‘family….
    else get_header();

    IS THIS what ya looking for?

    Then I can take a look at the_function_to_retrive($id);
    This should be in the plugin in API !

    Author?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Check if user has rights to view category’ is closed to new replies.
Skip to toolbar