StaticPermissionManager.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  */
11 namespace wcmf\lib\security\impl;
12 
23 
24 /**
25  * StaticPermissionManager retrieves authorization rules from the
26  * application configuration.
27  *
28  * @author ingo herwig <ingo@wemove.com>
29  */
31 
32  const AUTHORIZATION_SECTION = 'authorization';
33 
34  private $configuration = null;
35  private $actionKeyProvider = null;
36 
37  private static $logger = null;
38 
39  /**
40  * Constructor
41  * @param $persistenceFacade
42  * @param $session
43  * @param $configuration
44  */
47  Configuration $configuration) {
48  parent::__construct($persistenceFacade, $session);
49  if (self::$logger == null) {
50  self::$logger = LogManager::getLogger(__CLASS__);
51  }
52  $this->configuration = $configuration;
53  $this->actionKeyProvider = new ConfigActionKeyProvider($this->configuration,
54  self::AUTHORIZATION_SECTION);
55  }
56 
57  /**
58  * @see PermissionManager::getPermissions()
59  */
60  public function getPermissions($resource, $context, $action) {
61  $result = null;
62  $actionKey = ActionKey::getBestMatch($this->actionKeyProvider, $resource, $context, $action);
63  if (strlen($actionKey) > 0) {
64  $result = $this->deserializePermissions($this->actionKeyProvider->getKeyValue($actionKey));
65  }
66  if (self::$logger->isDebugEnabled()) {
67  self::$logger->debug("Permissions for $resource?$context?$action (->$actionKey): ".trim(StringUtil::getDump($result)));
68  }
69  return $result;
70  }
71 
72  /**
73  * @see PermissionManager::setPermissions()
74  */
75  public function setPermissions($resource, $context, $action, $permissions) {
76  $permKey = ActionKey::createKey($resource, $context, $action);
77  $config = $this->getConfigurationInstance();
78  $configInstance = $config['instance'];
79  $isChanged = false;
80 
81  if ($permissions != null) {
82  // set permissions
83  $rolesStr = $this->serializePermissions($permissions);
84  if (strlen($rolesStr)) {
85  $configInstance->setValue($permKey, $rolesStr, self::AUTHORIZATION_SECTION, true);
86  $isChanged = true;
87  }
88  }
89  else {
90  // delete permissions
91  $configInstance->removeKey($permKey);
92  $isChanged = true;
93  }
94 
95  if ($isChanged) {
96  $configInstance->writeConfiguration(basename($config['file']));
97  }
98  }
99 
100  /**
101  * @see PermissionManager::createPermission()
102  */
103  public function createPermission($resource, $context, $action, $role, $modifier) {
104  return self::modifyPermission($resource, $context, $action, $role, $modifier);
105  }
106 
107  /**
108  * @see PermissionManager::removePermission()
109  */
110  public function removePermission($resource, $context, $action, $role) {
111  return self::modifyPermission($resource, $context, $action, $role, null);
112  }
113 
114  /**
115  * Modify a permission for the given role.
116  * @param $resource The resource (e.g. class name of the Controller or object id).
117  * @param $context The context in which the action takes place.
118  * @param $action The action to process.
119  * @param $role The role to authorize.
120  * @param $modifier One of the PERMISSION_MODIFIER_ constants, null, if the permission
121  * should be removed.
122  * @return boolean
123  */
124  protected function modifyPermission($resource, $context, $action, $role, $modifier) {
125 
126  $permKey = ActionKey::createKey($resource, $context, $action);
127  $permVal = '';
128  if ($modifier != null) {
129  $permVal = $modifier.$role;
130  }
131  $config = $this->getConfigurationInstance();
132  $configInstance = $config['instance'];
133  $value = $configInstance->getValue($permKey, self::AUTHORIZATION_SECTION);
134  if ($value === false && $modifier != null) {
135  $configInstance->setValue($permKey, $permVal, self::AUTHORIZATION_SECTION, true);
136  }
137  else {
138  // remove role from value
139  $newValue = preg_replace('/ +/', ' ', str_replace([PermissionManager::PERMISSION_MODIFIER_ALLOW.$role,
140  PermissionManager::PERMISSION_MODIFIER_DENY.$role], "", $value));
141  if (strlen($newValue) > 0) {
142  $configInstance->setValue($permKey, $newValue." ".$permVal, self::AUTHORIZATION_SECTION, false);
143  }
144  else {
145  $configInstance->removeKey($permKey, self::AUTHORIZATION_SECTION);
146  }
147  }
148 
149  $configInstance->writeConfiguration(basename($config['file']));
150  return true;
151  }
152 
153  /**
154  * Get the configuration instance and file that is used to store the permissions.
155  * @return Associative array with keys 'instance' and 'file'.
156  */
157  protected function getConfigurationInstance() {
158  // get config file to modify
159  $configFiles = $this->configuration->getConfigurations();
160  if (sizeof($configFiles) == 0) {
161  return false;
162  }
163 
164  // create a writable configuration and modify the permission
165  $mainConfig = $configFiles[0];
166  $config = new InifileConfiguration(dirname($mainConfig).'/');
167  $config->addConfiguration(basename($mainConfig));
168  return [
169  'instance' => $config,
170  'file' => $mainConfig
171  ];
172  }
173 }
174 ?>
Session is the interface for session implementations and defines access to session variables.
Definition: Session.php:19
static getDump($variable, $strlen=100, $width=25, $depth=10, $i=0, &$objects=[])
Get the dump of a variable as string.
Definition: StringUtil.php:29
StaticPermissionManager retrieves authorization rules from the application configuration.
static getBestMatch(ActionKeyProvider $actionKeyProvider, $resource, $context, $action)
Get an action key that matches a given combination of resource, context, action best.
Definition: ActionKey.php:55
InifileConfiguration reads the application configuration from ini files.
StringUtil provides support for string manipulation.
Definition: StringUtil.php:18
Implementations of Configuration give access to the application configuration.
createPermission($resource, $context, $action, $role, $modifier)
modifyPermission($resource, $context, $action, $role, $modifier)
Modify a permission for the given role.
static createKey($resource, $context, $action)
Create an action key from the given values.
Definition: ActionKey.php:33
PersistenceFacade defines the interface for PersistenceFacade implementations.
setPermissions($resource, $context, $action, $permissions)
deserializePermissions($value)
Parse a permissions string and return an associative array with the keys 'default',...
static getLogger($name)
Get the logger with the given name.
Definition: LogManager.php:37
ConfigActionKeyProvider searches for action keys in the application configuration.
An action key is a combination of a resource, context and action that is represented as a string.
Definition: ActionKey.php:22
removePermission($resource, $context, $action, $role)
getConfigurationInstance()
Get the configuration instance and file that is used to store the permissions.
AbstractPermissionManager is the base class for concrete PermissionManager implementations.
PermissionManager implementations are used to handle all authorization requests.
LogManager is used to retrieve Logger instances.
Definition: LogManager.php:20
serializePermissions($permissions)
Convert an associative permissions array with keys 'default', 'allow', 'deny' into a string.
__construct(PersistenceFacade $persistenceFacade, Session $session, Configuration $configuration)
Constructor.