getUserCount() public method

Returns the number of users assigned to the provided RoleID. If $UsersOnlyWithThisRole is TRUE, it will return the number of users who are assigned to this RoleID and NO OTHER.
public getUserCount ( $RoleID, $UsersOnlyWithThisRole = false )
Ejemplo n.º 1
0
 /**
  * Show list of roles.
  *
  * @since 2.0.0
  * @access public
  */
 public function index($roleID = null)
 {
     $this->_permission();
     $this->setHighlightRoute('dashboard/role');
     $this->addJsFile('jquery.tablednd.js');
     $this->title(t('Roles & Permissions'));
     if (!$roleID) {
         $roles = $this->RoleModel->getWithRankPermissions()->resultArray();
         // Check to see which roles can be modified.
         foreach ($roles as &$row) {
             $canModify = true;
             if (!Gdn::session()->checkPermission('Garden.Settings.Manage')) {
                 foreach ($this->RoleModel->RankPermissions as $permission) {
                     if ($row[$permission] && !Gdn::session()->checkPermission($permission)) {
                         $canModify = false;
                         break;
                     }
                 }
             }
             $row['CanModify'] = $canModify;
         }
         $this->setData('Roles', $roles);
     } elseif ($this->deliveryType() === DELIVERY_TYPE_DATA) {
         // This is an API request. Get the role in a nicer format.
         $role = $this->RoleModel->getID($roleID, DATASET_TYPE_ARRAY);
         // Get the global permissions.
         $permissions = Gdn::permissionModel()->getGlobalPermissions($roleID);
         unset($permissions['PermissionID']);
         // Get the per-category permissions.
         $permissions['Category'] = $this->RoleModel->getCategoryPermissions($roleID);
         $role['Permissions'] = $permissions;
         $this->setData('Role', $role);
         saveToConfig('Api.Clean', false, false);
     } else {
         $Role = $this->RoleModel->getID($roleID);
         $this->setData('Roles', [$Role]);
     }
     // Grab the total users for each role.
     if (is_array($this->data('Roles'))) {
         $pastThreshold = Gdn::userModel()->pastUserMegaThreshold();
         $thresholdTypeExceptions = RoleModel::getDefaultTypes();
         unset($thresholdTypeExceptions[RoleModel::TYPE_MEMBER]);
         $roles = $this->data('Roles');
         foreach ($roles as &$role) {
             if ($pastThreshold && !in_array($role['Type'], $thresholdTypeExceptions)) {
                 $countUsers = t('View');
             } else {
                 $countUsers = $this->RoleModel->getUserCount($role['RoleID']);
             }
             $role['CountUsers'] = $countUsers;
         }
         $this->setData('Roles', $roles);
     }
     $this->render();
 }
Ejemplo n.º 2
0
 /**
  * Remove a role.
  *
  * @since 2.0.0
  * @access public
  */
 public function delete($RoleID = false)
 {
     if (!$this->_permission($RoleID)) {
         return;
     }
     $this->title(t('Delete Role'));
     $this->addSideMenu('dashboard/role');
     $Role = $this->RoleModel->getByRoleID($RoleID);
     if ($Role->Deletable == '0') {
         $this->Form->addError('You cannot delete this role.');
     }
     // Make sure the form knows which item we are deleting.
     $this->Form->addHidden('RoleID', $RoleID);
     // Figure out how many users will be affected by this deletion
     $this->AffectedUsers = $this->RoleModel->getUserCount($RoleID);
     // Figure out how many users will be orphaned by this deletion
     $this->OrphanedUsers = $this->RoleModel->getUserCount($RoleID, true);
     // Get a list of roles other than this one that can act as a replacement
     $this->ReplacementRoles = $this->RoleModel->getByNotRoleID($RoleID);
     if ($this->Form->authenticatedPostBack()) {
         // Make sure that a replacement role has been selected if there were going to be orphaned users
         if ($this->OrphanedUsers > 0) {
             $Validation = new Gdn_Validation();
             $Validation->applyRule('ReplacementRoleID', 'Required', 'You must choose a replacement role for orphaned users.');
             $Validation->validate($this->Form->formValues());
             $this->Form->setValidationResults($Validation->results());
         }
         if ($this->Form->errorCount() == 0) {
             // Go ahead and delete the Role
             $this->RoleModel->deleteAndReplace($RoleID, $this->Form->getValue('ReplacementRoleID'));
             $this->RedirectUrl = url('dashboard/role');
             $this->informMessage(t('Deleting role...'));
         }
     }
     $this->render();
 }