XF 2.0 Using finder to get users with permission

Rayman

Member
Hi,

I'm looking to use Finder to return all users that have a specific permission (the permission is set for some usergroups only).

What would be the best way to achieve this?
 
Last edited:
im thinking of... first get the groups that have permission and then fi\nd all users that are in those groups. All via a query.

would this be the best approach? or is there an existing function I can use?
 
These are general examples which might point you in the right direction.

This would be for a specific secondary_group_ids:

PHP:
$finder = \XF::finder('XF:User')->where('secondary_group_ids', 3)->fetch();

For selecting users with permission combinations.

PHP:
$finder1 = \XF::finder('XF:User')->where('permission_combination_id', 14)->fetch();

For selecting users matching is_admin. The is_staff is also available. In fact, do a dump after these and you'll see the ArrayCollection.

PHP:
$finder1 = \XF::finder('XF:User')->where('is_admin', 1)->fetch();
 
Thanks LPH, but unfortunately that doesn't really fit what I'm trying to accomplish. I was really hoping for there to be something like "hasPermission", but instead of it being for a single user, it would return all users that have the permission.

I ended up with the following to return a list of users that have a specific permission set. I'm not really sure if this is the best approach but I've pasted it below incase anyone is looking to achieve the same thing.

Permission
tjMGYCISRlalwuHpAZa1oA.png


Code
Code:
public function getUsersWithPermission($permission_group_id, $permission_id, $permission_value)
  {
      $users = $this->db()->fetchAll("
   SELECT
     xf_user.user_id, xf_user.username, xf_permission_combination.cache_value
   FROM
     xf_user
   INNER JOIN
     xf_permission_combination ON xf_user.permission_combination_id = xf_permission_combination.permission_combination_id;
");

      foreach ($users AS $id => $user)
      {
          $cache_value = unserialize($user['cache_value']);
          if (isset($cache_value[$permission_group_id][$permission_id]))
          {
              if ($cache_value[$permission_group_id][$permission_id] != $permission_value)
              {
                  unset($users[$id]);
              }
          }
      }

      return $users;
  }
 
Thanks LPH, but unfortunately that doesn't really fit what I'm trying to accomplish. I was really hoping for there to be something like "hasPermission", but instead of it being for a single user, it would return all users that have the permission.

I ended up with the following to return a list of users that have a specific permission set. I'm not really sure if this is the best approach but I've pasted it below incase anyone is looking to achieve the same thing.

Permission
tjMGYCISRlalwuHpAZa1oA.png


Code
Code:
public function getUsersWithPermission($permission_group_id, $permission_id, $permission_value)
  {
      $users = $this->db()->fetchAll("
   SELECT
     xf_user.user_id, xf_user.username, xf_permission_combination.cache_value
   FROM
     xf_user
   INNER JOIN
     xf_permission_combination ON xf_user.permission_combination_id = xf_permission_combination.permission_combination_id;
");

      foreach ($users AS $id => $user)
      {
          $cache_value = unserialize($user['cache_value']);
          if (isset($cache_value[$permission_group_id][$permission_id]))
          {
              if ($cache_value[$permission_group_id][$permission_id] != $permission_value)
              {
                  unset($users[$id]);
              }
          }
      }

      return $users;
  }
Where would I paste and run this code for it to return the list of users?
 
Top Bottom