DefaultPrincipalFactory.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
20 
21 /**
22  * Default implementation of PrincipalFactory.
23  * Retrieves users and roles from the storage.
24  *
25  * @author ingo herwig <ingo@wemove.com>
26  */
28 
29  private $_persistenceFacade = null;
30  private $_permissionManager = null;
31  private $_userType = null;
32  private $_roleType = null;
33 
34  private $_roleRelationNames = null;
35 
36  /**
37  * Constructor
38  * @param $persistenceFacade
39  * @param $permissionManager
40  * @param $userType Entity type name of User instances
41  * @param $roleType Entity type name of Role instances
42  */
43  public function __construct(PersistenceFacade $persistenceFacade,
44  PermissionManager $permissionManager, $userType, $roleType) {
45  $this->_persistenceFacade = $persistenceFacade;
46  $this->_permissionManager = $permissionManager;
47  $this->_userType = $userType;
48  $this->_roleType = $roleType;
49  }
50 
51  /**
52  * @see PrincipalFactory::getUser()
53  */
54  public function getUser($login, $useTempPermission=false) {
55  if ($useTempPermission) {
56  $this->_permissionManager->addTempPermission($this->_userType, '', PersistenceAction::READ);
57  }
58 
59  $user = $this->_persistenceFacade->loadFirstObject($this->_userType, BuildDepth::SINGLE,
60  array(
61  new Criteria($this->_userType, 'login', '=', $login)
62  ), null);
63 
64  if ($useTempPermission) {
65  $this->_permissionManager->removeTempPermission($this->_userType, '', PersistenceAction::READ);
66  }
67  return $user;
68  }
69 
70  /**
71  * @see PrincipalFactory::getUserRoles()
72  */
73  public function getUserRoles(User $user, $useTempPermission=false) {
74  if ($useTempPermission) {
75  $this->_permissionManager->addTempPermission($this->_roleType, '', PersistenceAction::READ);
76  }
77 
78  // initialize role relation definition
79  if ($this->_roleRelationNames == null) {
80  $this->_roleRelationNames = array();
81  $mapper = $user->getMapper();
82  foreach ($mapper->getRelationsByType($this->_roleType) as $relation) {
83  $this->_roleRelationNames[] = $relation->getOtherRole();
84  }
85  }
86 
87  foreach ($this->_roleRelationNames as $roleName) {
88  $user->loadChildren($roleName);
89  }
90 
91  if ($useTempPermission) {
92  $this->_permissionManager->removeTempPermission($this->_roleType, '', PersistenceAction::READ);
93  }
94 
95  // TODO add role nodes from addedNodes array
96  // use getChildrenEx, because we are interessted in the type
97  return $user->getChildrenEx(null, null, $this->_roleType, null);
98  }
99 
100  /**
101  * @see PrincipalFactory::getRole()
102  */
103  public function getRole($name, $useTempPermission=false) {
104  if ($useTempPermission) {
105  $this->_permissionManager->addTempPermission($this->_roleType, '', PersistenceAction::READ);
106  }
107 
108  $role = $this->_persistenceFacade->loadFirstObject($this->_roleType, BuildDepth::SINGLE,
109  array(
110  new Criteria($this->_roleType, 'name', '=', $name)
111  ), null);
112 
113  if ($useTempPermission) {
114  $this->_permissionManager->removeTempPermission($this->_roleType, '', PersistenceAction::READ);
115  }
116  return $role;
117  }
118 }
119 ?>
User is the interface for users.
Definition: User.php:18
__construct(PersistenceFacade $persistenceFacade, PermissionManager $permissionManager, $userType, $roleType)
Constructor.
Criteria defines a condition on a PersistentObject's attribute used to select specific instances...
Definition: Criteria.php:21
PrincipalFactory implementations are used to retrieve User and Role instances.
PermissionManager implementations are used to handle all authorization requests.
PersistenceFacade defines the interface for PersistenceFacade implementations.