• Main Page
  • Modules
  • Classes
  • Files
  • File List

classes/security/RoleDAO.inc.php

00001 <?php
00002 
00016 import('classes.security.Role');
00017 import('lib.pkp.classes.security.UserGroupAssignment');
00018 
00019 class RoleDAO extends DAO {
00021    var $userDao;
00022 
00026    function RoleDAO() {
00027       parent::DAO();
00028       $this->userDao =& DAORegistry::getDAO('UserDAO');
00029    }
00030 
00041    function &getUsersByRoleId($roleId = null, $pressId = null, $searchType = null, $search = null, $searchMatch = null, $dbResultRange = null) {
00042       $users = array();
00043 
00044       $paramArray = array(ASSOC_TYPE_USER, 'interest');
00045       if (isset($roleId)) $paramArray[] = (int) $roleId;
00046       if (isset($pressId)) $paramArray[] = (int) $pressId;
00047       // For security / resource usage reasons, a role or press ID
00048       // must be specified. Don't allow calls supplying neither.
00049       if ($pressId === null && $roleId === null) return null;
00050 
00051       $searchSql = '';
00052 
00053       $searchTypeMap = array(
00054          USER_FIELD_FIRSTNAME => 'u.first_name',
00055          USER_FIELD_LASTNAME => 'u.last_name',
00056          USER_FIELD_USERNAME => 'u.username',
00057          USER_FIELD_EMAIL => 'u.email',
00058          USER_FIELD_INTERESTS => 'cves.setting_value'
00059       );
00060 
00061       if (!empty($search) && isset($searchTypeMap[$searchType])) {
00062          $fieldName = $searchTypeMap[$searchType];
00063          switch ($searchMatch) {
00064             case 'is':
00065                $searchSql = "AND LOWER($fieldName) = LOWER(?)";
00066                $paramArray[] = $search;
00067                break;
00068             case 'contains':
00069                $searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
00070                $paramArray[] = '%' . $search . '%';
00071                break;
00072             case 'startsWith':
00073                $searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
00074                $paramArray[] = $search . '%';
00075                break;
00076          }
00077       } elseif (!empty($search)) switch ($searchType) {
00078          case USER_FIELD_USERID:
00079             $searchSql = 'AND u.user_id=?';
00080             $paramArray[] = $search;
00081             break;
00082          case USER_FIELD_INITIAL:
00083             $searchSql = 'AND LOWER(u.last_name) LIKE LOWER(?)';
00084             $paramArray[] = $search . '%';
00085             break;
00086       }
00087 
00088       $searchSql .= ' ORDER BY u.last_name, u.first_name'; // FIXME Add "sort field" parameter?
00089 
00090       $result =& $this->retrieveRange(
00091          'SELECT DISTINCT u.* FROM users AS u LEFT JOIN controlled_vocabs cv ON (cv.assoc_type = ? AND cv.assoc_id = u.user_id AND cv.symbolic = ?)
00092          LEFT JOIN controlled_vocab_entries cve ON (cve.controlled_vocab_id = cv.controlled_vocab_id)
00093          LEFT JOIN controlled_vocab_entry_settings cves ON (cves.controlled_vocab_entry_id = cve.controlled_vocab_entry_id),
00094          user_groups AS ug, user_user_groups AS uug
00095          WHERE ug.user_group_id = uug.user_group_id AND u.user_id = uug.user_id' . (isset($roleId) ? ' AND ug.role_id = ?' : '') . (isset($pressId) ? ' AND ug.context_id = ?' : '') . ' ' . $searchSql,
00096          $paramArray,
00097          $dbResultRange
00098       );
00099 
00100       $returner = new DAOResultFactory($result, $this->userDao, '_returnUserFromRowWithData');
00101       return $returner;
00102    }
00103 
00111    function userHasRole($pressId, $userId, $roleId) {
00112       $result =& $this->retrieve(
00113          'SELECT count(*) FROM user_groups ug JOIN user_user_groups uug ON ug.user_group_id = uug.user_group_id
00114          WHERE ug.context_id = ? AND uug.user_id = ? AND ug.role_id = ?',
00115          array((int) $pressId, (int) $userId, (int) $roleId)
00116       );
00117 
00118       // > 0 because user could belong to more than one user group with this role
00119       $returner = isset($result->fields[0]) && $result->fields[0] > 0 ? true : false;
00120 
00121       $result->Close();
00122       unset($result);
00123 
00124       return $returner;
00125    }
00126 
00133    function getByUserId($userId, $pressId = null) {
00134       $params = array((int) $userId);
00135       if ($pressId) $params[] = (int) $pressId;
00136       $result =& $this->retrieve(
00137          'SELECT  DISTINCT ug.role_id
00138          FROM  user_groups ug
00139             JOIN user_user_groups uug ON ug.user_group_id = uug.user_group_id
00140          WHERE uug.user_id = ?' . ($pressId?' AND ug.context_id = ?':''),
00141          $params
00142       );
00143 
00144       $roles = array();
00145       while ( !$result->EOF ) {
00146          $roles[] = new Role($result->fields[0]);
00147          $result->MoveNext();
00148       }
00149       $result->Close();
00150       unset($result);
00151 
00152       return $roles;
00153    }
00154 
00161    function getByUserIdGroupedByContext($userId) {
00162       $userGroupDao =& DAORegistry::getDAO('UserGroupDAO');
00163       $userGroupsFactory =& $userGroupDao->getByUserId($userId);
00164 
00165       $roles = array();
00166       while ($userGroup =& $userGroupsFactory->next()) {
00167          $roles[$userGroup->getContextId()][$userGroup->getRoleId()] = new Role($userGroup->getRoleId());
00168       }
00169 
00170       return $roles;
00171    }
00172 
00179    function getPressUsersRoleCount($pressId, $roleId) {
00180       $userGroupDao =& DAORegistry::getDAO('UserGroupDAO');
00181       return $userGroupDao->getContextUsersCount($pressId, null, $roleId);
00182    }
00183 
00189    function getRoleIdFromPath($rolePath) {
00190       switch ($rolePath) {
00191          case 'admin':
00192             return ROLE_ID_SITE_ADMIN;
00193          case 'manager':
00194             return ROLE_ID_PRESS_MANAGER;
00195          case 'author':
00196             return ROLE_ID_AUTHOR;
00197          case 'seriesEditor':
00198             return ROLE_ID_SERIES_EDITOR;
00199          case 'reviewer':
00200             return ROLE_ID_REVIEWER;
00201          case 'reader':
00202             return ROLE_ID_READER;
00203          default:
00204             return null;
00205       }
00206    }
00207 
00213    function getSortMapping($heading) {
00214       switch ($heading) {
00215          case 'username': return 'u.username';
00216          case 'name': return 'u.last_name';
00217          case 'email': return 'u.email';
00218          default: return null;
00219       }
00220    }
00221 
00228    function getRoleNames($pressOnly = false, $roleIds = null) {
00229       $siteRoleNames = array(ROLE_ID_SITE_ADMIN => 'user.role.siteAdmin');
00230       $pressRoleNames = array(
00231          ROLE_ID_PRESS_MANAGER => 'user.role.manager',
00232          ROLE_ID_SERIES_EDITOR => 'user.role.seriesEditor',
00233          ROLE_ID_PRESS_ASSISTANT => 'user.role.pressAssistant',
00234          ROLE_ID_AUTHOR => 'user.role.author',
00235          ROLE_ID_REVIEWER => 'user.role.reviewer'
00236       );
00237       $roleNames = $pressOnly ? $pressRoleNames : $siteRoleNames + $pressRoleNames;
00238 
00239       if(!empty($roleIds)) {
00240          $returner = array();
00241          foreach($roleIds as $roleId) {
00242             if(isset($roleNames[$roleId])) $returner[$roleId] = $roleNames[$roleId];
00243          }
00244          return $returner;
00245       } else {
00246          return $roleNames;
00247       }
00248    }
00249 }
00250 
00251 ?>

Generated on Mon Sep 17 2012 13:58:55 for Open Monograph Press by  doxygen 1.7.1