PageRenderTime 24ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/core/model/modx/processors/security/group/create.class.php

http://github.com/modxcms/revolution
PHP | 376 lines | 249 code | 39 blank | 88 comment | 31 complexity | 2447d648c6551c31d4b18b7aa042f99c MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Create a user group
  12. *
  13. * @param string $name (optional) The name of the new user group. Defaults to
  14. * Untitled User Group.
  15. * @param integer $parent (optional) The ID of the parent user group. Defaults
  16. * to 0.
  17. *
  18. * @package modx
  19. * @subpackage processors.security.group
  20. */
  21. class modUserGroupCreateProcessor extends modObjectCreateProcessor {
  22. public $classKey = 'modUserGroup';
  23. public $languageTopics = array('user');
  24. public $permission = 'usergroup_new';
  25. public $objectType = 'user_group';
  26. public $beforeSaveEvent = 'OnUserGroupBeforeFormSave';
  27. public $afterSaveEvent = 'OnUserGroupFormSave';
  28. public function initialize() {
  29. $this->setDefaultProperties(array(
  30. 'parent' => 0,
  31. ));
  32. return parent::initialize();
  33. }
  34. public function beforeSave() {
  35. $this->setUsersIn();
  36. $name = $this->getProperty('name');
  37. if (empty($name)) {
  38. $this->addFieldError('name',$this->modx->lexicon('user_group_err_ns_name'));
  39. }
  40. $parent = $this->getProperty('parent');
  41. if (empty($parent)) {
  42. $this->setProperty('parent',0);
  43. }
  44. if ($this->doesAlreadyExist(array('name' => $name))) {
  45. $this->addFieldError('name',$this->modx->lexicon('user_group_err_already_exists'));
  46. }
  47. return parent::beforeSave();
  48. }
  49. public function afterSave() {
  50. $this->setContexts();
  51. if ($this->modx->hasPermission('usergroup_user_edit')) {
  52. $this->setResourceGroups();
  53. }
  54. /* access wizard stuff */
  55. $flush = false;
  56. $users = $this->getProperty('aw_users','');
  57. if (!empty($users)) {
  58. $this->addUsersViaWizard($users);
  59. }
  60. $contexts = $this->getProperty('aw_contexts','');
  61. if (!empty($contexts)) {
  62. $contexts = is_array($contexts) ? $contexts : explode(',',$contexts);
  63. $contexts = array_unique($contexts);
  64. $adminPolicy = trim($this->getProperty('aw_manager_policy',0));
  65. if (!empty($adminPolicy)) {
  66. $this->addManagerContextAccessViaWizard($adminPolicy);
  67. }
  68. $policy = trim($this->getProperty('aw_contexts_policy',0));
  69. if ($this->addContextAccessViaWizard($contexts,$policy)) {
  70. $flush = true;
  71. }
  72. $resourceGroups = $this->getProperty('aw_resource_groups','');
  73. if (!empty($resourceGroups)) {
  74. $this->addResourceGroupsViaWizard($resourceGroups,$contexts);
  75. }
  76. $categories = $this->getProperty('aw_categories','');
  77. if (!empty($categories)) {
  78. $this->addElementCategoriesViaWizard($categories,$contexts);
  79. }
  80. $parallel = $this->getProperty('aw_parallel',false);
  81. if ($parallel) {
  82. $this->addParallelResourceGroup($contexts);
  83. }
  84. }
  85. if ($flush) {
  86. $this->modx->cacheManager->flushPermissions();
  87. }
  88. return parent::afterSave();
  89. }
  90. /**
  91. * Add user groups via a wizard property, which is a comma-separated list of username:role key pairs, ie:
  92. * jimbob:Member,johndoe:Administrator,marksmith
  93. *
  94. * If the Role is left off, it will default to the Member role.
  95. *
  96. * @param string|array $users
  97. * @return bool
  98. */
  99. public function addUsersViaWizard($users) {
  100. $users = is_array($users) ? $users : explode(',',$users);
  101. $users = array_unique($users);
  102. foreach ($users as $userKey) {
  103. $userKey = explode(':',$userKey);
  104. $c = intval($userKey[0]) > 0 ? trim($userKey[0]) : array('username' => trim($userKey[0]));
  105. /** @var modUser $user */
  106. $user = $this->modx->getObject('modUser',$c);
  107. if (empty($user)) continue;
  108. /** @var modUserGroupRole $role */
  109. if (empty($userKey[1])) $userKey[1] = 'Member';
  110. $c = intval($userKey[1]) > 0 ? trim($userKey[1]) : array('name' => trim($userKey[1]));
  111. $role = $this->modx->getObject('modUserGroupRole',$c);
  112. if (empty($role)) continue;
  113. /** @var modUserGroupMember $membership */
  114. $membership = $this->modx->newObject('modUserGroupMember');
  115. $membership->set('user_group',$this->object->get('id'));
  116. $membership->set('member',$user->get('id'));
  117. $membership->set('role',$role->get('id'));
  118. $membership->save();
  119. }
  120. return true;
  121. }
  122. /**
  123. * Add Manager Access via wizard property with a specified policy.
  124. *
  125. * @param int|string $adminPolicy
  126. * @return bool
  127. */
  128. public function addManagerContextAccessViaWizard($adminPolicy) {
  129. $c = intval($adminPolicy) > 0 ? $adminPolicy : array('name' => $adminPolicy);
  130. /** @var modAccessPolicy $policy */
  131. $policy = $this->modx->getObject('modAccessPolicy',$c);
  132. if (!$policy) return false;
  133. /** @var modAccessResourceGroup $acl */
  134. $acl = $this->modx->newObject('modAccessContext');
  135. $acl->fromArray(array(
  136. 'target' => 'mgr',
  137. 'principal_class' => 'modUserGroup',
  138. 'principal' => $this->object->get('id'),
  139. 'authority' => 9999,
  140. 'policy' => $policy->get('id'),
  141. ));
  142. $acl->save();
  143. return true;
  144. }
  145. /**
  146. * Add Context Access via wizard property.
  147. *
  148. * @param array $contexts
  149. * @return boolean
  150. */
  151. public function addContextAccessViaWizard(array $contexts) {
  152. /** @var modAccessPolicy $policy */
  153. $policy = $this->modx->getObject('modAccessPolicy',array(
  154. 'name' => 'Context',
  155. ));
  156. if (!$policy) return false;
  157. foreach ($contexts as $context) {
  158. /** @var modAccessResourceGroup $acl */
  159. $acl = $this->modx->newObject('modAccessContext');
  160. $acl->fromArray(array(
  161. 'target' => trim($context),
  162. 'principal_class' => 'modUserGroup',
  163. 'principal' => $this->object->get('id'),
  164. 'authority' => 9999,
  165. 'policy' => $policy->get('id'),
  166. ));
  167. $acl->save();
  168. }
  169. return true;
  170. }
  171. /**
  172. * @param string|array $resourceGroupNames
  173. * @param array $contexts
  174. * @return boolean
  175. */
  176. public function addResourceGroupsViaWizard($resourceGroupNames,array $contexts) {
  177. $resourceGroupNames = is_array($resourceGroupNames) ? $resourceGroupNames : explode(',',$resourceGroupNames);
  178. $resourceGroupNames = array_unique($resourceGroupNames);
  179. /** @var modAccessPolicy $policy */
  180. $policy = $this->modx->getObject('modAccessPolicy',array('name' => 'Resource'));
  181. if (!$policy) return false;
  182. foreach ($resourceGroupNames as $resourceGroupName) {
  183. /** @var modResourceGroup $resourceGroup */
  184. $resourceGroup = $this->modx->getObject('modResourceGroup',array('name' => trim($resourceGroupName)));
  185. if (!$resourceGroup) return false;
  186. foreach ($contexts as $context) {
  187. /** @var modAccessResourceGroup $acl */
  188. $acl = $this->modx->newObject('modAccessResourceGroup');
  189. $acl->fromArray(array(
  190. 'target' => $resourceGroup->get('id'),
  191. 'principal_class' => 'modUserGroup',
  192. 'principal' => $this->object->get('id'),
  193. 'authority' => 9999,
  194. 'policy' => $policy->get('id'),
  195. 'context_key' => trim($context),
  196. ));
  197. $acl->save();
  198. }
  199. }
  200. return true;
  201. }
  202. /**
  203. * Adds a Resource Group with the same name and grants access for the specified Contexts
  204. *
  205. * @param array $contexts
  206. * @return boolean
  207. */
  208. public function addParallelResourceGroup(array $contexts) {
  209. /** @var modResourceGroup $resourceGroup */
  210. $resourceGroup = $this->modx->getObject('modResourceGroup',array(
  211. 'name' => $this->object->get('name'),
  212. ));
  213. if (!$resourceGroup) {
  214. $resourceGroup = $this->modx->newObject('modResourceGroup');
  215. $resourceGroup->set('name',$this->object->get('name'));
  216. if (!$resourceGroup->save()) {
  217. return false;
  218. }
  219. }
  220. /** @var modAccessPolicy $policy */
  221. $policy = $this->modx->getObject('modAccessPolicy',array('name' => 'Resource'));
  222. if (!$policy) return false;
  223. foreach ($contexts as $context) {
  224. /** @var modAccessResourceGroup $acl */
  225. $acl = $this->modx->newObject('modAccessResourceGroup');
  226. $acl->fromArray(array(
  227. 'target' => $resourceGroup->get('id'),
  228. 'principal_class' => 'modUserGroup',
  229. 'principal' => $this->object->get('id'),
  230. 'authority' => 9999,
  231. 'policy' => $policy->get('id'),
  232. 'context_key' => trim($context),
  233. ));
  234. $acl->save();
  235. }
  236. return true;
  237. }
  238. /**
  239. * @param string|array $categoryNames
  240. * @param array $contexts
  241. * @return boolean
  242. */
  243. public function addElementCategoriesViaWizard($categoryNames,array $contexts) {
  244. $categoryNames = is_array($categoryNames) ? $categoryNames : explode(',',$categoryNames);
  245. $categoryNames = array_unique($categoryNames);
  246. /** @var modAccessPolicy $policy */
  247. $policy = $this->modx->getObject('modAccessPolicy',array('name' => 'Element'));
  248. if (!$policy) return false;
  249. foreach ($categoryNames as $categoryName) {
  250. /** @var modCategory $category */
  251. $category = $this->modx->getObject('modCategory',array('category' => trim($categoryName)));
  252. if (!$category) return false;
  253. foreach ($contexts as $context) {
  254. /** @var modAccessCategory $acl */
  255. $acl = $this->modx->newObject('modAccessCategory');
  256. $acl->fromArray(array(
  257. 'target' => $category->get('id'),
  258. 'principal_class' => 'modUserGroup',
  259. 'principal' => $this->object->get('id'),
  260. 'authority' => 9999,
  261. 'policy' => $policy->get('id'),
  262. 'context_key' => trim($context),
  263. ));
  264. $acl->save();
  265. }
  266. }
  267. return true;
  268. }
  269. /**
  270. * Set the users in the group
  271. * @return array
  272. */
  273. public function setUsersIn() {
  274. $users = $this->getProperty('users');
  275. $memberships = array();
  276. if (!empty($users)) {
  277. $users = is_array($users) ? $users : $this->modx->fromJSON($users);
  278. $memberships = array();
  279. foreach ($users as $userArray) {
  280. if (empty($userArray['id']) || empty($userArray['role'])) continue;
  281. /** @var modUserGroupMember $membership */
  282. $membership = $this->modx->newObject('modUserGroupMember');
  283. $membership->set('user_group',$this->object->get('id'));
  284. $membership->set('member',$userArray['id']);
  285. $membership->set('role',$userArray['role']);
  286. $memberships[] = $membership;
  287. }
  288. $this->object->addMany($memberships);
  289. }
  290. return $memberships;
  291. }
  292. /**
  293. * Set the Context ACLs for the Group
  294. * @return array
  295. */
  296. public function setContexts() {
  297. $contexts = $this->getProperty('contexts');
  298. $access = array();
  299. if (!empty($contexts)) {
  300. $contexts = is_array($contexts) ? $contexts : $this->modx->fromJSON($contexts);
  301. foreach ($contexts as $context) {
  302. /** @var modAccessContext $acl */
  303. $acl = $this->modx->newObject('modAccessContext');
  304. $acl->fromArray($context);
  305. $acl->set('principal',$this->object->get('id'));
  306. $acl->set('principal_class','modUserGroup');
  307. if ($acl->save()) {
  308. $access[] = $acl;
  309. }
  310. }
  311. }
  312. return $access;
  313. }
  314. /**
  315. * Set the Resource Group ACLs for the Group
  316. * @return array
  317. */
  318. public function setResourceGroups() {
  319. $resourceGroups = $this->getProperty('resource_groups');
  320. $access = array();
  321. if (!empty($resourceGroups)) {
  322. $resourceGroups = is_array($resourceGroups) ? $resourceGroups : $this->modx->fromJSON($resourceGroups);
  323. foreach ($resourceGroups as $resourceGroup) {
  324. /** @var modAccessResourceGroup $acl */
  325. $acl = $this->modx->newObject('modAccessResourceGroup');
  326. $acl->fromArray($resourceGroup);
  327. $acl->set('principal',$this->object->get('id'));
  328. $acl->set('principal_class','modUserGroup');
  329. if ($acl->save()) {
  330. $access[] = $acl;
  331. }
  332. }
  333. }
  334. return $access;
  335. }
  336. }
  337. return 'modUserGroupCreateProcessor';