WordPress.org

Ready to get started?Download WordPress

Forums

User Access Manager
Check if user has rights to view category (18 posts)

  1. dreiacht
    Member
    Posted 1 year ago #

    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/

  2. felixchi81
    Member
    Posted 1 year ago #

    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.

  3. makssoud
    Member
    Posted 1 year ago #

    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.

  4. dreiacht
    Member
    Posted 1 year ago #

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

  5. GM_Alex
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  6. felixchi81
    Member
    Posted 1 year ago #

    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

  7. GM_Alex
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  8. GM_Alex
    Member
    Plugin Author

    Posted 1 year ago #

    Can I mark the thread as resolved?

  9. Jonas Lundman
    Member
    Posted 1 year ago #

    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 ...

  10. efishinsea
    Member
    Posted 1 year ago #

    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!

  11. Jonas Lundman
    Member
    Posted 1 year ago #

    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 }

  12. Jonas Lundman
    Member
    Posted 1 year ago #

    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

  13. efishinsea
    Member
    Posted 1 year ago #

    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.

  14. Jonas Lundman
    Member
    Posted 1 year ago #

    @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...

  15. efishinsea
    Member
    Posted 1 year ago #

    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.

  16. Jonas Lundman
    Member
    Posted 1 year ago #

    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?

  17. efishinsea
    Member
    Posted 1 year ago #

    Yes, something like this. I think it could be simpler, but you have the right idea. I would probably want to use this in the main header.php include, but having it available globally for page templates too would be useful.

    In my case, users (other than admin) won't belong to multiple groups.

    There's an API? That's news to me...

  18. peterjonathan
    Member
    Posted 1 year ago #

    Firstly Alex thanks for a great plugin.

    Secondly Jonas thanks for the example code. The snippet below enabled me to do the user group validation required in my plugin.

    foreach ($aGroups as $oGroup) {
    $sLink .= $oGroup->getGroupName().', ';
    }

    This thread certainly resolved my requirment to check if a user was in a certain group.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic