RoleModel::setUserRoles PHP Method

setUserRoles() public static method

public static setUserRoles ( &$Users, string $UserIDColumn = 'UserID', string $RolesColumn = 'Roles' )
$Users
$UserIDColumn string
$RolesColumn string
    public static function setUserRoles(&$Users, $UserIDColumn = 'UserID', $RolesColumn = 'Roles')
    {
        $UserIDs = array_unique(array_column($Users, $UserIDColumn));
        // Try and get all of the mappings from the cache.
        $Keys = array();
        foreach ($UserIDs as $UserID) {
            $Keys[$UserID] = formatString(UserModel::USERROLES_KEY, array('UserID' => $UserID));
        }
        $UserRoles = Gdn::cache()->get($Keys);
        if (!is_array($UserRoles)) {
            $UserRoles = array();
        }
        // Grab all of the data that doesn't exist from the DB.
        $MissingIDs = array();
        foreach ($Keys as $UserID => $Key) {
            if (!array_key_exists($Key, $UserRoles)) {
                $MissingIDs[$UserID] = $Key;
            }
        }
        if (count($MissingIDs) > 0) {
            $DbUserRoles = Gdn::sql()->select('ur.*')->from('UserRole ur')->whereIn('ur.UserID', array_keys($MissingIDs))->get()->resultArray();
            $DbUserRoles = Gdn_DataSet::Index($DbUserRoles, 'UserID', array('Unique' => false));
            // Store the user role mappings.
            foreach ($DbUserRoles as $UserID => $Rows) {
                $RoleIDs = array_column($Rows, 'RoleID');
                $Key = $Keys[$UserID];
                Gdn::cache()->store($Key, $RoleIDs);
                $UserRoles[$Key] = $RoleIDs;
            }
        }
        $AllRoles = self::roles();
        // roles indexed by role id.
        // Skip personal info roles
        if (!checkPermission('Garden.PersonalInfo.View')) {
            $AllRoles = array_filter($AllRoles, 'self::FilterPersonalInfo');
        }
        // Join the users.
        foreach ($Users as &$User) {
            $UserID = val($UserIDColumn, $User);
            $Key = $Keys[$UserID];
            $RoleIDs = val($Key, $UserRoles, array());
            $Roles = array();
            foreach ($RoleIDs as $RoleID) {
                if (!array_key_exists($RoleID, $AllRoles)) {
                    continue;
                }
                $Roles[$RoleID] = $AllRoles[$RoleID]['Name'];
            }
            setValue($RolesColumn, $User, $Roles);
        }
    }

Usage Example

コード例 #1
0
 /**
  * User management list.
  *
  * @since 2.0.0
  * @access public
  * @param mixed $Keywords Term or array of terms to filter list of users.
  * @param int $Page Page number.
  * @param string $Order Sort order for list.
  */
 public function index($Keywords = '', $Page = '', $Order = '')
 {
     $this->permission(array('Garden.Users.Add', 'Garden.Users.Edit', 'Garden.Users.Delete'), '', false);
     // Page setup
     $this->addJsFile('jquery.gardenmorepager.js');
     $this->addJsFile('user.js');
     $this->title(t('Users'));
     $this->setHighlightRoute('dashboard/user');
     Gdn_Theme::section('Moderation');
     // Form setup
     $this->Form->Method = 'get';
     // Input Validation.
     list($Offset, $Limit) = offsetLimit($Page, PagerModule::$DefaultPageSize);
     if (!$Keywords) {
         $Keywords = $this->Form->getFormValue('Keywords');
         if ($Keywords) {
             $Offset = 0;
         }
     }
     if (!is_string($Keywords)) {
         $Keywords = '';
     }
     // Put the Keyword back in the form
     if ($Keywords) {
         $this->Form->setFormValue('Keywords', $Keywords);
     }
     $UserModel = new UserModel();
     list($Offset, $Limit) = offsetLimit($Page, 30);
     // Determine our data filters.
     $Filter = $this->_getFilter();
     if ($Filter) {
         $Filter['Keywords'] = $Keywords;
     } else {
         $Filter = array('Keywords' => (string) $Keywords);
     }
     $Filter['Optimize'] = Gdn::userModel()->pastUserThreshold();
     // Sorting
     if (in_array($Order, array('DateInserted', 'DateFirstVisit', 'DateLastActive'))) {
         $Order = 'u.' . $Order;
         $OrderDir = 'desc';
     } else {
         $Order = 'u.Name';
         $OrderDir = 'asc';
     }
     // Get user list
     $this->UserData = $UserModel->search($Filter, $Order, $OrderDir, $Limit, $Offset);
     $this->setData('Users', $this->UserData);
     // Figure out our number of results and users.
     $showUserCount = $this->UserData->count();
     if (!Gdn::userModel()->pastUserThreshold()) {
         // Pfft, query that sucker however you want.
         $this->setData('RecordCount', $UserModel->searchCount($Filter));
     } else {
         // We have a user search, so at least set enough data for the Next pager.
         if ($showUserCount) {
             $this->setData('_CurrentRecords', $showUserCount);
         } else {
             // No search was done. Just give the total users overall. First, zero-out our pager.
             $this->setData('_CurrentRecords', 0);
             if (!Gdn::userModel()->pastUserMegaThreshold()) {
                 // Restoring this semi-optimized counter is our compromise to let non-mega sites know their exact total users.
                 $this->setData('UserCount', $UserModel->getCount());
             } else {
                 // Dang, yo. Get a table status guess instead of really counting.
                 $this->setData('UserEstimate', Gdn::userModel()->countEstimate());
             }
         }
     }
     // Add roles to the user data.
     RoleModel::setUserRoles($this->UserData->result());
     // Deliver json data if necessary
     if ($this->_DeliveryType != DELIVERY_TYPE_ALL && $this->_DeliveryMethod == DELIVERY_METHOD_XHTML) {
         $this->setJson('LessRow', $this->Pager->toString('less'));
         $this->setJson('MoreRow', $this->Pager->toString('more'));
         $this->View = 'users';
     }
     $this->render();
 }
All Usage Examples Of RoleModel::setUserRoles