wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.PrincipalController.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.PrincipalController.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/presentation/class.Controller.php");
20 require_once(BASE."wcmf/lib/presentation/class.WCMFInifileParser.php");
21 require_once(BASE."wcmf/lib/persistence/class.PersistenceFacade.php");
22 require_once(BASE."wcmf/lib/model/class.Node.php");
23 require_once(BASE."wcmf/lib/util/class.ObjectFactory.php");
24 require_once(BASE."wcmf/lib/presentation/ListboxFunctions.php");
25 
26 /**
27  * @class PrincipalController
28  * @ingroup Controller
29  * @brief PrincipalController is used to edit users and roles.
30  *
31  * <b>Input actions:</b>
32  * - @em newprincipal Create a new principal of the given type
33  * - @em editprincipal Edit a given principal
34  * - @em save Save changes to the given principal
35  * - @em delprincipal Delete the given principal
36  *
37  * <b>Output actions:</b>
38  * - @em overview If the action was delprincipal
39  * - @em ok In any other case
40  *
41  * @param[in,out] newtype The principal type to create (user or role)
42  * @param[in] deleteoids A comma separated list of principal object ids to delete
43  * @param[in,out] oid The object id of the principal to edit
44  * @param[in] <oid> A node defining what to save. The node should only contain those values, that should be changed
45  * This may be achived by creating the node using the node constructor (instead of using PersistenceFacade::create)
46  * and setting the values on it.
47  * @param[in] changepassword If given, the password will tried to be changed
48  * @param[in] newpassword1 The new password of the current user
49  * @param[in] newpassword2 The new password of the current user repeated
50  * @param[in] principals The list of all users that should belong to the displayed role or
51  * the list of all roles that the displayed user should have
52  * @param[out] principal The principal to display
53  * @param[out] principalBaseList The list of all users, if a role is displayed or
54  * the list of all roles if a user is displayed
55  * @param[out] principalList The list of all users that belong to the displayed role or
56  * the list of all roles that the displayed user has
57  *
58  * @author ingo herwig <ingo@wemove.com>
59  */
61 {
62  var $_userManager = null;
63 
64  /**
65  * @see Controller::initialize()
66  */
67  function initialize(&$request, &$response)
68  {
69  if (strlen($request->getContext()) == 0)
70  {
71  $request->setContext('user');
72  $response->setContext('user');
73  }
74 
75  parent::initialize($request, $response);
76 
77  // create UserManager instance
78  $objectFactory = &ObjectFactory::getInstance();
79  $this->_userManager = &$objectFactory->createInstanceFromConfig('implementation', 'UserManager');
80  }
81  /**
82  * @see Controller::validate()
83  */
84  function validate()
85  {
86  if($this->_request->getAction() == 'newprincipal')
87  {
88  if(strlen($this->_request->getValue('newtype')) == 0)
89  {
90  $this->setErrorMsg("No 'newtype' given in data.");
91  return false;
92  }
93  }
94  if(in_array($this->_request->getAction(), array('editprincipal', 'save')))
95  {
96  if(strlen($this->_request->getValue('oid')) == 0)
97  {
98  $this->setErrorMsg("No 'oid' given in data.");
99  return false;
100  }
101  }
102  if($this->_request->getAction() == 'delprincipal')
103  {
104  if(strlen($this->_request->getValue('deleteoids')) == 0)
105  {
106  $this->setErrorMsg("No 'deleteoids' given in data.");
107  return false;
108  }
109  }
110  return true;
111  }
112  /**
113  * @see Controller::hasView()
114  */
115  function hasView()
116  {
117  if ($this->_request->getAction() == 'delprincipal')
118  return false;
119  else
120  return true;
121  }
122  /**
123  * Process action and assign data to View.
124  * @return Array of given context and action 'overview' on delete.
125  * False else (Stop action processing chain).
126  * @see Controller::executeKernel()
127  */
128  function executeKernel()
129  {
130  $persistenceFacade = &PersistenceFacade::getInstance();
131  $this->_userManager->startTransaction();
132 
133  // process actions
134 
135  // DELETE
136  if($this->_request->getAction() == 'delprincipal')
137  {
138  $deleteOIDs = split(',', $this->_request->getValue('deleteoids'));
139  foreach($deleteOIDs as $oid)
140  {
141  $this->beforeDelete($this->_userManager->getPrincipal($oid));
142  $this->_userManager->removePrincipal($oid);
143  }
144  // return
145  $this->_userManager->commitTransaction();
146  $this->_response->setAction('overview');
147  return true;
148  }
149 
150  // NEW
151  if($this->_request->getAction() == 'newprincipal')
152  {
153  $newType = $this->_request->getValue('newtype');
154  $newNode = new Node($newType);
155 
156  if($newType == 'user')
157  $newPrincipal = &$this->_userManager->createUser('', '', $newNode->getOID(), '', '');
158  else
159  $newPrincipal = &$this->_userManager->createRole($newNode->getOID());
160 
161  // set the login/name to the oid
162  if($newType == 'user')
163  $this->_userManager->setUserProperty($newNode->getOID(), USER_PROPERTY_LOGIN, 'user'.$newPrincipal->getDBID());
164  else
165  $this->_userManager->setRoleProperty($newNode->getOID(), ROLE_PROPERTY_NAME, 'role'.$newPrincipal->getDBID());
166  $newPrincipal->save();
167 
168  $this->afterInsert($newPrincipal);
169 
170  // redirect to edit view by changing the request parameters for the following code
171  $this->_request->setAction('editprincipal');
172  $this->_request->setValue('oid', $newPrincipal->getOID());
173  }
174 
175  // EDIT, SAVE
176  if (in_array($this->_request->getAction(), array('editprincipal', 'save')) || in_array($this->_request->getContext(), array('user', 'role')))
177  {
178  // load model
179  $principal = &$this->_userManager->getPrincipal($this->_request->getValue('oid'));
180 
181  // save changes
182  if ($this->_request->getAction() == 'save')
183  {
184  $saveNode = &$this->_request->getValue($this->_request->getValue('oid'));
185 
186  if (strtolower(get_class($principal)) == strtolower(UserManager::getUserClassName()))
187  {
188  // properties
190  {
191  $value = $saveNode->getValue($property, DATATYPE_ATTRIBUTE);
192  if ($value != $principal->getValue($property, DATATYPE_ATTRIBUTE))
193  {
194  $this->_userManager->setUserProperty($principal->getLogin(), $property, $value);
195  $principal->setValue($property, $value, DATATYPE_ATTRIBUTE);
196  }
197  }
198  // password
199  if ($this->_request->hasValue('changepassword'))
200  {
201  $this->_userManager->resetPassword($principal->getLogin(), $this->_request->getValue('newpassword1'),
202  $this->_request->getValue('newpassword2'));
203  }
204  // roles
205  $roles = $this->_userManager->listRoles();
206  $userRoles = $this->_userManager->listUserRoles($principal->getLogin());
207  $principals = $this->_request->getValue('principals');
208  foreach($roles as $curRole)
209  {
210  if ((is_array($principals) && in_array($curRole, $principals)) && (!is_array($userRoles) || !in_array($curRole, $userRoles)))
211  $this->_userManager->addUserToRole($curRole, $principal->getLogin());
212  if ((!is_array($principals) || !in_array($curRole, $principals)) && (is_array($userRoles) && in_array($curRole, $userRoles)))
213  $this->_userManager->removeUserFromRole($curRole, $principal->getLogin());
214  }
215  }
216  if (strtolower(get_class($principal)) == strtolower(UserManager::getRoleClassName()))
217  {
218  // properties
219  foreach(array(ROLE_PROPERTY_NAME) as $property)
220  {
221  $value = $saveNode->getValue($property, DATATYPE_ATTRIBUTE);
222  if ($value != $principal->getValue($property, DATATYPE_ATTRIBUTE))
223  {
224  $this->_userManager->setRoleProperty($principal->getName(), $property, $value);
225  $principal->setValue($property, $value, DATATYPE_ATTRIBUTE);
226  }
227  }
228  // members
229  $users = $this->_userManager->listUsers();
230  $roleMembers = $this->_userManager->listRoleMembers($principal->getName());
231  $principals = $this->_request->getValue('principals');
232  foreach($users as $curUser)
233  {
234  if (in_array($curUser, $principals) && !in_array($curUser, $roleMembers))
235  $this->_userManager->addUserToRole($principal->getName(), $curUser);
236  if (!in_array($curUser, $principals) && in_array($curUser, $roleMembers))
237  $this->_userManager->removeUserFromRole($principal->getName(), $curUser);
238  }
239  }
240  $this->afterUpdate($this->_userManager->getPrincipal($this->_request->getValue('oid')));
241  }
242 
243  // reload model
244  $principal = &$this->_userManager->getPrincipal($this->_request->getValue('oid'));
245  if (strtolower(get_class($principal)) == strtolower(UserManager::getUserClassName()))
246  {
247  $principalBaseList = $this->_userManager->listRoles();
248  $principalList = $this->_userManager->listUserRoles($principal->getLogin());
249  }
250  elseif (strtolower(get_class($principal)) == strtolower(UserManager::getRoleClassName()))
251  {
252  $principalBaseList = $this->_userManager->listUsers();
253  $principalList = $this->_userManager->listRoleMembers($principal->getName());
254  }
255 
256  // assign model to view
257  $this->_response->setValue('oid', $this->_request->getValue('oid'));
258  $this->_response->setValue('newtype', $this->_request->getValue('newtype'));
259  $this->_response->setValue('principal', $principal);
260  $this->_response->setValue('principalBaseList', join("|", $principalBaseList));
261  $this->_response->setValue('principalList', join(",", $principalList));
262 
263  $configFiles = WCMFInifileParser::getIniFiles();
264  array_push($configFiles, '');
265  $this->_response->setValue('configFiles', join("|", $configFiles));
266  }
267 
268  $this->_userManager->commitTransaction();
269 
270  // success
271  $this->_response->setAction('ok');
272  return false;
273  }
274  /**
275  * Called before deleting an exisiting principal.
276  * @note subclasses will override this to implement special application requirements.
277  * @param principal A reference to the principal to delete (@see UserManager::getPrincipal).
278  */
279  function beforeDelete(&$principal) {}
280  /**
281  * Called after inserting a new principal.
282  * @note subclasses will override this to implement special application requirements.
283  * @param principal A reference to the principal inserted (@see UserManager::getPrincipal).
284  */
285  function afterInsert(&$principal) {}
286  /**
287  * Called after updating an existing principal.
288  * @note subclasses will override this to implement special application requirements.
289  * @param principal A reference to the principal updated (@see UserManager::getPrincipal).
290  */
291  function afterUpdate(&$principal) {}
292 }
293 ?>
const USER_PROPERTY_LOGIN
Node is the basic component for building trees (although a Node can have one than more parents)...
Definition: class.Node.php:118
const ROLE_PROPERTY_NAME
const DATATYPE_ATTRIBUTE
initialize(&$request, &$response)
const USER_PROPERTY_NAME
Controller is the base class of all controllers. If a Controller has a view it is expected to reside ...
const USER_PROPERTY_FIRSTNAME
PrincipalController is used to edit users and roles.
const USER_PROPERTY_CONFIG