Open Journal Systems  2.4.3
 All Classes Namespaces Functions Variables Groups Pages
RoleDAO.inc.php
1 <?php
2 
18 import('classes.security.Role');
19 
20 class RoleDAO extends DAO {
24  function RoleDAO() {
25  parent::DAO();
26  $this->userDao =& DAORegistry::getDAO('UserDAO');
27  }
28 
36  function &getRole($journalId, $userId, $roleId) {
37  $result =& $this->retrieve(
38  'SELECT * FROM roles WHERE journal_id = ? AND user_id = ? AND role_id = ?',
39  array(
40  (int) $journalId,
41  (int) $userId,
42  (int) $roleId
43  )
44  );
45 
46  $returner = null;
47  if ($result->RecordCount() != 0) {
48  $returner =& $this->_returnRoleFromRow($result->GetRowAssoc(false));
49  }
50 
51  $result->Close();
52  unset($result);
53 
54  return $returner;
55  }
56 
62  function &_returnRoleFromRow(&$row) {
63  $role = new Role();
64  $role->setJournalId($row['journal_id']);
65  $role->setUserId($row['user_id']);
66  $role->setRoleId($row['role_id']);
67 
68  HookRegistry::call('RoleDAO::_returnRoleFromRow', array(&$role, &$row));
69 
70  return $role;
71  }
72 
77  function insertRole(&$role) {
78  return $this->update(
79  'INSERT INTO roles
80  (journal_id, user_id, role_id)
81  VALUES
82  (?, ?, ?)',
83  array(
84  (int) $role->getJournalId(),
85  (int) $role->getUserId(),
86  (int) $role->getRoleId()
87  )
88  );
89  }
90 
95  function deleteRole(&$role) {
96  return $this->update(
97  'DELETE FROM roles WHERE journal_id = ? AND user_id = ? AND role_id = ?',
98  array(
99  (int) $role->getJournalId(),
100  (int) $role->getUserId(),
101  (int) $role->getRoleId()
102  )
103  );
104  }
105 
112  function &getRolesByUserId($userId, $journalId = null) {
113  $roles = array();
114  $params = array((int) $userId);
115  if ($journalId !== null) $params[] = (int) $journalId;
116 
117  $result =& $this->retrieve(
118  'SELECT * FROM roles WHERE user_id = ?
119  ' . (isset($journalId) ? ' AND journal_id = ?' : '') . '
120  ORDER BY journal_id',
121  $params
122  );
123 
124  while (!$result->EOF) {
125  $roles[] =& $this->_returnRoleFromRow($result->GetRowAssoc(false));
126  $result->moveNext();
127  }
128 
129  $result->Close();
130  unset($result);
131 
132  return $roles;
133  }
134 
141  function &getByUserIdGroupedByContext($userId) {
142  $roles = $this->getRolesByUserId($userId);
143 
144  $groupedRoles = array();
145  foreach ($roles as $role) {
146  $groupedRoles[$role->getJournalId()][$role->getRoleId()] =& $role;
147  unset($role);
148  }
149 
150  return $groupedRoles;
151  }
152 
163  function &getUsersByRoleId($roleId = null, $journalId = null, $searchType = null, $search = null, $searchMatch = null, $dbResultRange = null, $sortBy = null, $sortDirection = SORT_DIRECTION_ASC) {
164  $users = array();
165 
166  $paramArray = array('interest');
167  if (isset($roleId)) $paramArray[] = (int) $roleId;
168  if (isset($journalId)) $paramArray[] = (int) $journalId;
169 
170  // For security / resource usage reasons, a role or journal ID
171  // must be specified. Don't allow calls supplying neither.
172  if ($journalId === null && $roleId === null) return null;
173 
174  $searchSql = '';
175 
176  $searchTypeMap = array(
177  USER_FIELD_FIRSTNAME => 'u.first_name',
178  USER_FIELD_LASTNAME => 'u.last_name',
179  USER_FIELD_USERNAME => 'u.username',
180  USER_FIELD_EMAIL => 'u.email',
181  USER_FIELD_INTERESTS => 'cves.setting_value'
182  );
183 
184  if (!empty($search) && isset($searchTypeMap[$searchType])) {
185  $fieldName = $searchTypeMap[$searchType];
186  switch ($searchMatch) {
187  case 'is':
188  $searchSql = "AND LOWER($fieldName) = LOWER(?)";
189  $paramArray[] = $search;
190  break;
191  case 'contains':
192  $searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
193  $paramArray[] = '%' . $search . '%';
194  break;
195  case 'startsWith':
196  $searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
197  $paramArray[] = $search . '%';
198  break;
199  }
200  } elseif (!empty($search)) switch ($searchType) {
201  case USER_FIELD_USERID:
202  $searchSql = 'AND u.user_id=?';
203  $paramArray[] = $search;
204  break;
205  case USER_FIELD_INITIAL:
206  $searchSql = 'AND LOWER(u.last_name) LIKE LOWER(?)';
207  $paramArray[] = $search . '%';
208  break;
209  }
210 
211  $searchSql .= ($sortBy?(' ORDER BY ' . $this->getSortMapping($sortBy) . ' ' . $this->getDirectionMapping($sortDirection)) : '');
212 
213  $result =& $this->retrieveRange(
214  'SELECT DISTINCT u.*
215  FROM users u
216  LEFT JOIN controlled_vocabs cv ON (cv.symbolic = ?)
217  LEFT JOIN user_interests ui ON (ui.user_id = u.user_id)
218  LEFT JOIN controlled_vocab_entries cve ON (cve.controlled_vocab_id = cv.controlled_vocab_id AND cve.controlled_vocab_entry_id = ui.controlled_vocab_entry_id)
219  LEFT JOIN controlled_vocab_entry_settings cves ON (cves.controlled_vocab_entry_id = cve.controlled_vocab_entry_id),
220  roles AS r WHERE u.user_id = r.user_id ' . (isset($roleId)?'AND r.role_id = ?':'') . (isset($journalId) ? ' AND r.journal_id = ?' : '') . ' ' . $searchSql,
221  $paramArray,
222  $dbResultRange
223  );
224 
225  $returner = new DAOResultFactory($result, $this->userDao, '_returnUserFromRowWithData');
226  return $returner;
227  }
228 
238  function &getUsersByJournalId($journalId, $searchType = null, $search = null, $searchMatch = null, $dbResultRange = null, $sortBy = null, $sortDirection = SORT_DIRECTION_ASC) {
239  $users = array();
240 
241  $paramArray = array('interest', (int) $journalId);
242  $searchSql = '';
243 
244  $searchTypeMap = array(
245  USER_FIELD_FIRSTNAME => 'u.first_name',
246  USER_FIELD_LASTNAME => 'u.last_name',
247  USER_FIELD_USERNAME => 'u.username',
248  USER_FIELD_EMAIL => 'u.email',
249  USER_FIELD_INTERESTS => 'cves.setting_value'
250  );
251 
252  if (!empty($search) && isset($searchTypeMap[$searchType])) {
253  $fieldName = $searchTypeMap[$searchType];
254  switch ($searchMatch) {
255  case 'is':
256  $searchSql = "AND LOWER($fieldName) = LOWER(?)";
257  $paramArray[] = $search;
258  break;
259  case 'contains':
260  $searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
261  $paramArray[] = '%' . $search . '%';
262  break;
263  case 'startsWith':
264  $searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
265  $paramArray[] = $search . '%';
266  break;
267  }
268  } elseif (!empty($search)) switch ($searchType) {
269  case USER_FIELD_USERID:
270  $searchSql = 'AND u.user_id=?';
271  $paramArray[] = $search;
272  break;
273  case USER_FIELD_INITIAL:
274  $searchSql = 'AND LOWER(u.last_name) LIKE LOWER(?)';
275  $paramArray[] = $search . '%';
276  break;
277  }
278 
279  $searchSql .= ($sortBy?(' ORDER BY ' . $this->getSortMapping($sortBy) . ' ' . $this->getDirectionMapping($sortDirection)) : '');
280 
281  $result =& $this->retrieveRange(
282  'SELECT DISTINCT u.*
283  FROM users u
284  LEFT JOIN controlled_vocabs cv ON (cv.symbolic = ?)
285  LEFT JOIN user_interests ui ON (ui.user_id = u.user_id)
286  LEFT JOIN controlled_vocab_entries cve ON (cve.controlled_vocab_id = cv.controlled_vocab_id AND ui.controlled_vocab_entry_id = cve.controlled_vocab_entry_id)
287  LEFT JOIN controlled_vocab_entry_settings cves ON (cves.controlled_vocab_entry_id = cve.controlled_vocab_entry_id),
288  roles AS r WHERE u.user_id = r.user_id AND r.journal_id = ? ' . $searchSql,
289  $paramArray,
290  $dbResultRange
291  );
292 
293  $returner = new DAOResultFactory($result, $this->userDao, '_returnUserFromRowWithData');
294  return $returner;
295  }
296 
303  function getJournalUsersCount($journalId, $roleId = null) {
304  $userDao =& DAORegistry::getDAO('UserDAO');
305 
306  $params = array((int) $journalId);
307  if ($roleId !== null) $params[] = (int) $roleId;
308 
309  $result =& $this->retrieve(
310  'SELECT COUNT(DISTINCT(user_id)) FROM roles WHERE journal_id = ?' . ($roleId === null?'':' AND role_id = ?'),
311  $params
312  );
313 
314  $returner = $result->fields[0];
315 
316  $result->Close();
317  unset($result);
318 
319  return $returner;
320  }
321 
328  function getJournalUsersRoleCount($journalId, $roleId) {
329  $result =& $this->retrieve(
330  'SELECT COUNT(DISTINCT(user_id)) FROM roles WHERE journal_id = ? AND role_id = ?',
331  array (
332  (int) $journalId,
333  (int) $roleId
334  )
335  );
336 
337  $returner = $result->fields[0];
338 
339  $result->Close();
340  unset($result);
341 
342  return $returner;
343  }
344 
350  function &getRolesByJournalId($journalId = null, $roleId = null) {
351  $params = array();
352  $conditions = array();
353  if (isset($journalId)) {
354  $params[] = (int) $journalId;
355  $conditions[] = 'journal_id = ?';
356  }
357  if (isset($roleId)) {
358  $params[] = (int) $roleId;
359  $conditions[] = 'role_id = ?';
360  }
361 
362  $result =& $this->retrieve(
363  'SELECT * FROM roles' . (empty($conditions) ? '' : ' WHERE ' . join(' AND ', $conditions)),
364  $params
365  );
366 
367  $returner = new DAOResultFactory($result, $this, '_returnRoleFromRow');
368  return $returner;
369  }
370 
375  function deleteRoleByJournalId($journalId) {
376  return $this->update(
377  'DELETE FROM roles WHERE journal_id = ?', (int) $journalId
378  );
379  }
380 
387  function deleteRoleByUserId($userId, $journalId = null, $roleId = null) {
388  return $this->update(
389  'DELETE FROM roles WHERE user_id = ?' . (isset($journalId) ? ' AND journal_id = ?' : '') . (isset($roleId) ? ' AND role_id = ?' : ''),
390  isset($journalId) && isset($roleId) ? array((int) $userId, (int) $journalId, (int) $roleId)
391  : (isset($journalId) ? array((int) $userId, (int) $journalId)
392  : (isset($roleId) ? array((int) $userId, (int) $roleId) : (int) $userId))
393  );
394  }
395 
406  function roleExists($journalId, $userId, $roleId) {
407  if (Config::getVar('debug', 'deprecation_warnings')) trigger_error('Deprecated function.');
408  return $this->userHasRole($journalId, $userId, $roleId);
409  }
410 
418  function userHasRole($journalId, $userId, $roleId) {
419  $result =& $this->retrieve(
420  'SELECT COUNT(*) FROM roles WHERE journal_id = ? AND user_id = ? AND role_id = ?', array((int) $journalId, (int) $userId, (int) $roleId)
421  );
422  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
423 
424  $result->Close();
425  unset($result);
426 
427  return $returner;
428  }
429 
436  function getRoleName($roleId, $plural = false) {
437  switch ($roleId) {
438  case ROLE_ID_SITE_ADMIN:
439  return 'user.role.siteAdmin' . ($plural ? 's' : '');
440  case ROLE_ID_JOURNAL_MANAGER:
441  return 'user.role.manager' . ($plural ? 's' : '');
442  case ROLE_ID_EDITOR:
443  return 'user.role.editor' . ($plural ? 's' : '');
444  case ROLE_ID_SECTION_EDITOR:
445  return 'user.role.sectionEditor' . ($plural ? 's' : '');
446  case ROLE_ID_LAYOUT_EDITOR:
447  return 'user.role.layoutEditor' . ($plural ? 's' : '');
448  case ROLE_ID_REVIEWER:
449  return 'user.role.reviewer' . ($plural ? 's' : '');
450  case ROLE_ID_COPYEDITOR:
451  return 'user.role.copyeditor' . ($plural ? 's' : '');
452  case ROLE_ID_PROOFREADER:
453  return 'user.role.proofreader' . ($plural ? 's' : '');
454  case ROLE_ID_AUTHOR:
455  return 'user.role.author' . ($plural ? 's' : '');
456  case ROLE_ID_READER:
457  return 'user.role.reader' . ($plural ? 's' : '');
458  case ROLE_ID_SUBSCRIPTION_MANAGER:
459  return 'user.role.subscriptionManager' . ($plural ? 's' : '');
460  default:
461  return '';
462  }
463  }
464 
470  function getRolePath($roleId) {
471  switch ($roleId) {
472  case ROLE_ID_SITE_ADMIN:
473  return 'admin';
474  case ROLE_ID_JOURNAL_MANAGER:
475  return 'manager';
476  case ROLE_ID_EDITOR:
477  return 'editor';
478  case ROLE_ID_SECTION_EDITOR:
479  return 'sectionEditor';
480  case ROLE_ID_LAYOUT_EDITOR:
481  return 'layoutEditor';
482  case ROLE_ID_REVIEWER:
483  return 'reviewer';
484  case ROLE_ID_COPYEDITOR:
485  return 'copyeditor';
486  case ROLE_ID_PROOFREADER:
487  return 'proofreader';
488  case ROLE_ID_AUTHOR:
489  return 'author';
490  case ROLE_ID_READER:
491  return 'reader';
492  case ROLE_ID_SUBSCRIPTION_MANAGER:
493  return 'subscriptionManager';
494  default:
495  return '';
496  }
497  }
498 
504  function getRoleIdFromPath($rolePath) {
505  switch ($rolePath) {
506  case 'admin':
507  return ROLE_ID_SITE_ADMIN;
508  case 'manager':
509  return ROLE_ID_JOURNAL_MANAGER;
510  case 'editor':
511  return ROLE_ID_EDITOR;
512  case 'sectionEditor':
513  return ROLE_ID_SECTION_EDITOR;
514  case 'layoutEditor':
515  return ROLE_ID_LAYOUT_EDITOR;
516  case 'reviewer':
517  return ROLE_ID_REVIEWER;
518  case 'copyeditor':
519  return ROLE_ID_COPYEDITOR;
520  case 'proofreader':
521  return ROLE_ID_PROOFREADER;
522  case 'author':
523  return ROLE_ID_AUTHOR;
524  case 'reader':
525  return ROLE_ID_READER;
526  case 'subscriptionManager':
527  return ROLE_ID_SUBSCRIPTION_MANAGER;
528  default:
529  return null;
530  }
531  }
532 
538  function getSortMapping($heading) {
539  switch ($heading) {
540  case 'username': return 'u.username';
541  case 'name': return 'u.last_name';
542  case 'email': return 'u.email';
543  case 'id': return 'u.user_id';
544  default: return null;
545  }
546  }
547 }
548 
549 ?>
& getRole($journalId, $userId, $roleId)
Definition: RoleDAO.inc.php:36
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& getUsersByJournalId($journalId, $searchType=null, $search=null, $searchMatch=null, $dbResultRange=null, $sortBy=null, $sortDirection=SORT_DIRECTION_ASC)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
deleteRoleByUserId($userId, $journalId=null, $roleId=null)
deleteRole(&$role)
Definition: RoleDAO.inc.php:95
getRoleIdFromPath($rolePath)
Operations for retrieving and modifying Role objects.
Definition: RoleDAO.inc.php:20
& getRolesByUserId($userId, $journalId=null)
Describes user roles within the system and the associated permissions.
Definition: Role.inc.php:30
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
& getRolesByJournalId($journalId=null, $roleId=null)
& getUsersByRoleId($roleId=null, $journalId=null, $searchType=null, $search=null, $searchMatch=null, $dbResultRange=null, $sortBy=null, $sortDirection=SORT_DIRECTION_ASC)
getJournalUsersCount($journalId, $roleId=null)
getJournalUsersRoleCount($journalId, $roleId)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
getVar($section, $key, $default=null)
Definition: Config.inc.php:34
& getByUserIdGroupedByContext($userId)
& getDAO($name, $dbconn=null)
deleteRoleByJournalId($journalId)
roleExists($journalId, $userId, $roleId)
& _returnRoleFromRow(&$row)
Definition: RoleDAO.inc.php:62
insertRole(&$role)
Definition: RoleDAO.inc.php:77
call($hookName, $args=null)
userHasRole($journalId, $userId, $roleId)
getSortMapping($heading)
getRolePath($roleId)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
getDirectionMapping($direction)
Definition: DAO.inc.php:588
getRoleName($roleId, $plural=false)