CreatorRole.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 
18 
19 /**
20  * CreatorRole matches if the user created an entity.
21  *
22  * @author ingo herwig <ingo@wemove.com>
23  */
24 class CreatorRole {
25 
26  const CREATOR_ATTRIBUTE = 'creator';
27 
28  private $initialized = false;
29  private $permissionManager = null;
30  private $persistenceFacade = null;
31 
32  /**
33  * @see DynamicRole::match()
34  */
35  public function match(User $user, $resource) {
36  $result = null;
37  $extensionRemoved = preg_replace('/\.[^\.]*?$/', '', $resource);
38  if (($oidObj = ObjectId::parse($resource)) !== null || ($oidObj = ObjectId::parse($extensionRemoved)) !== null) {
39  $this->initialize();
40  $mapper = $this->persistenceFacade->getMapper($oidObj->getType());
41  if ($mapper->hasAttribute(self::CREATOR_ATTRIBUTE)) {
42  if ($oidObj->containsDummyIds()) {
43  // any user might be the creator of a new object
44  $result = true;
45  }
46  else {
47  $tmpPerm = $this->permissionManager->addTempPermission(
48  $oidObj->__toString(), '', PersistenceAction::READ);
49  if (($obj = $this->persistenceFacade->load($oidObj)) !== null) {
50  $creator = $obj->getValue(self::CREATOR_ATTRIBUTE);
51  $result = $creator === $user->getLogin();
52  }
53  $this->permissionManager->removeTempPermission($tmpPerm);
54  }
55  }
56  }
57  return $result;
58  }
59 
60  private function initialize() {
61  if (!$this->initialized) {
62  $this->permissionManager = ObjectFactory::getInstance('permissionManager');
63  $this->persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
64  $this->initialized = true;
65  }
66  }
67 }
68 ?>
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:28
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:135
DynamicRole is the interface for user roles based on attributes.
Definition: DynamicRole.php:20
CreatorRole matches if the user created an entity.
Definition: CreatorRole.php:24
static getInstance($name, $dynamicConfiguration=[])
getLogin()
Get the login of the user.
PersistenceAction values are used to define actions on PersistentObject instances.
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...
User is the interface for users.
Definition: User.php:18