AbstractUser.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 a user that is persistent.
23  *
24  * @author ingo herwig <ingo@wemove.com>
25  */
26 abstract class AbstractUser extends Node implements User {
27 
28  private $_roles = null;
29 
30  private static $_roleConfig = null;
31 
32  /**
33  * @see User::setLogin()
34  */
35  public function setLogin($login) {
36  $this->setValue('login', $login);
37  }
38 
39  /**
40  * @see User::getLogin()
41  */
42  public function getLogin() {
43  return $this->getValue('login');
44  }
45 
46  /**
47  * @see User::setPassword()
48  */
49  public function setPassword($password) {
50  $this->setValue('password', $password);
51  }
52 
53  /**
54  * @see User::getPassword()
55  */
56  public function getPassword() {
57  return $this->getValue('password');
58  }
59 
60  /**
61  * @see User::verifyPassword()
62  */
63  public function verifyPassword($password, $passwordHash) {
64  return PasswordService::verify($password, $passwordHash);
65  }
66 
67  /**
68  * @see User::setConfig()
69  */
70  public function setConfig($config) {
71  $this->setValue('config', $config);
72  }
73 
74  /**
75  * @see User::getConfig()
76  */
77  public function getConfig() {
78  return $this->getValue('config');
79  }
80 
81  /**
82  * @see User::hasRole()
83  */
84  public function hasRole($roleName) {
85  $roles = $this->getRoles();
86  for ($i=0, $count=sizeof($roles); $i<$count; $i++) {
87  if ($roles[$i]->getName() == $roleName) {
88  return true;
89  }
90  }
91  return false;
92  }
93 
94  /**
95  * @see User::getRoles()
96  */
97  public function getRoles() {
98  if (!$this->_roles) {
99  $principalFactory = ObjectFactory::getInstance('principalFactory');
100  $this->_roles = $principalFactory->getUserRoles($this, true);
101  }
102  return $this->_roles;
103  }
104 
105  /**
106  * @see PersistentObject::beforeInsert()
107  */
108  public function beforeInsert() {
109  $this->ensureHashedPassword();
110  }
111 
112  /**
113  * @see PersistentObject::beforeUpdate()
114  */
115  public function beforeUpdate() {
116  $this->ensureHashedPassword();
117  $this->setRoleConfig();
118  }
119 
120  /**
121  * Hash password property if not done already.
122  */
123  protected function ensureHashedPassword() {
124  // the password is expected to be stored in the 'password' value
125  $password = $this->getValue('password');
126  if (strlen($password) > 0) {
127  $info = password_get_info($password);
128  if ($info['algo'] != PASSWORD_BCRYPT) {
129  $this->setValue('password', PasswordService::hash($password));
130  }
131  }
132  }
133 
134  /**
135  * Set the configuration of the currently associated role, if no
136  * configuration is set already.
137  */
138  protected function setRoleConfig() {
139  if (strlen($this->getConfig()) == 0) {
140  // check added nodes for Role instances
141  foreach ($this->getAddedNodes() as $relationName => $nodes) {
142  foreach ($nodes as $node) {
143  if ($node instanceof Role) {
144  $roleName = $node->getName();
145  $roleConfigs = self::getRoleConfigs();
146  if (isset($roleConfigs[$roleName])) {
147  $this->setConfig($roleConfigs[$roleName]);
148  break;
149  }
150  }
151  }
152  }
153  }
154  }
155 
156  /**
157  * @see PersistentObject::setValue()
158  */
159  public function setValue($name, $value, $forceSet=false, $trackChange=true) {
160  // prevent overwriting the password with an empty value
161  // the password is expected to be stored in the 'password' value
162  if (!($name == 'password' && strlen(trim($value)) == 0)) {
163  parent::setValue($name, $value, $forceSet, $trackChange);
164  }
165  }
166 
167  /**
168  * @see PersistentObject::validateValue()
169  */
170  public function validateValue($name, $value, Message $message) {
171  parent::validateValue($name, $value, $message);
172 
173  // validate the login property
174  // the login is expected to be stored in the 'login' value
175  if ($name == 'login') {
176  if (strlen(trim($value)) == 0) {
177  throw new ValidationException($message->getText("The user requires a login name"));
178  }
179  $principalFactory = ObjectFactory::getInstance('principalFactory');
180  $user = $principalFactory->getUser($value);
181  if ($user != null && $user->getOID() != $this->getOID()) {
182  throw new ValidationException($message->getText("The login '%0%' already exists", array($value)));
183  }
184  }
185 
186  // validate the password property if the user is newly created
187  // the password is expected to be stored in the 'password' value
188  if ($name == 'password') {
189  if ($this->getState() == self::STATE_NEW && strlen(trim($value)) == 0) {
190  throw new ValidationException($message->getText("The password can't be empty"));
191  }
192  }
193  }
194 
195  /**
196  * Get the role configurations from the application configuration
197  * @return Array with role names as keys and config file names as values
198  */
199  protected static function getRoleConfigs() {
200  if (self::$_roleConfig == null) {
201  // load role config if existing
202  $config = ObjectFactory::getInstance('configuration');
203  if (($roleConfig = $config->getSection('roleconfig')) !== false) {
204  self::$_roleConfig = $roleConfig;
205  }
206  }
207  return self::$_roleConfig;
208  }
209 }
210 ?>
ensureHashedPassword()
Hash password property if not done already.
getValue($name)
Definition: Node.php:91
Default implementation of a user that is persistent.
static hash($password)
Hash the given cleartext password.
getAddedNodes()
Get the object ids of the nodes that were added since the node was loaded.
Definition: Node.php:351
User is the interface for users.
Definition: User.php:18
static verify($password, $passwordHash)
Check if the given hash represents the given password.
Role is the interface for user roles.
Definition: Role.php:18
validateValue($name, $value, Message $message)
static getInstance($name, $dynamicConfiguration=array())
setValue($name, $value, $forceSet=false, $trackChange=true)
Message is used to get localized messages to be used in the user interface.
Definition: Message.php:23
ValidationException signals an exception in validation.
setRoleConfig()
Set the configuration of the currently associated role, if no configuration is set already...
static getRoleConfigs()
Get the role configurations from the application configuration.
getText($message, $parameters=null, $lang='')
Get a localized string.
getOID()
Get the object id of the user.
Node adds the concept of relations to PersistentObject.
Definition: Node.php:34