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