PageRenderTime 30ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/role/policyedit.php

https://github.com/Yannix/ezpublish
PHP | 382 lines | 310 code | 47 blank | 25 comment | 53 complexity | 073447b58d5d9fa739f513b434616fba MD5 | raw file
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  4. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  5. * @version //autogentag//
  6. * @package kernel
  7. */
  8. $Module = $Params['Module'];
  9. $policyID = $Params['PolicyID'];
  10. $policy = eZPolicy::fetchTemporaryCopy( $policyID );
  11. $policyID = $policy->attribute( 'id' );
  12. $originalPolicyID = $policy->attribute( 'original_id' );
  13. if( $policy === null )
  14. {
  15. return $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' );
  16. }
  17. $currentModule = $policy->attribute( 'module_name' );
  18. $currentFunction = $policy->attribute( 'function_name' );
  19. $roleID = $policy->attribute( 'role_id' );
  20. $role = eZRole::fetch( $roleID );
  21. $roleName = $role->attribute( 'name' );
  22. $limitationValueList = $policy->limitationList();
  23. $nodeList = array();
  24. $subtreeList = array();
  25. if ( $currentModule == '*' )
  26. {
  27. $functions = array();
  28. }
  29. else
  30. {
  31. $mod = eZModule::exists( $currentModule );
  32. $functions = $mod->attribute( 'available_functions' );
  33. }
  34. $currentFunctionLimitations = array();
  35. if ( isset( $functions[$currentFunction] ) && $functions[$currentFunction] )
  36. {
  37. foreach ( $functions[$currentFunction] as $key => $limitation )
  38. {
  39. if ( ( count( $limitation['values'] ) == 0 ) && array_key_exists( 'class', $limitation ) )
  40. {
  41. $obj = new $limitation['class']( array() );
  42. $limitationValueList = call_user_func_array( array( $obj, $limitation['function'] ), $limitation['parameter'] );
  43. $limitationValueArray = array();
  44. foreach ( $limitationValueList as $limitationValue )
  45. {
  46. $limitationValuePair = array();
  47. $limitationValuePair['Name'] = $limitationValue['name'];
  48. $limitationValuePair['value'] = $limitationValue['id'];
  49. $limitationValueArray[] = $limitationValuePair;
  50. }
  51. $limitation['values'] = $limitationValueArray;
  52. }
  53. $currentFunctionLimitations[ $key ] = $limitation;
  54. }
  55. }
  56. $http = eZHTTPTool::instance();
  57. if ( $http->hasPostVariable( 'DeleteNodeButton' ) )
  58. {
  59. processDropdownLimitations( $policy, $currentModule, $currentFunction, $currentFunctionLimitations );
  60. if ( $http->hasPostVariable( 'DeleteNodeIDArray' ) )
  61. {
  62. $deletedIDList = $http->postVariable( 'DeleteNodeIDArray' );
  63. $db = eZDB::instance();
  64. $db->begin();
  65. foreach ( $deletedIDList as $deletedID )
  66. {
  67. eZPolicyLimitationValue::removeByValue( $deletedID, $policyID );
  68. }
  69. $db->commit();
  70. }
  71. /* Clean up policy cache */
  72. eZUser::cleanupCache();
  73. }
  74. if ( $http->hasPostVariable( 'DeleteSubtreeButton' ) )
  75. {
  76. processDropdownLimitations( $policy, $currentModule, $currentFunction, $currentFunctionLimitations );
  77. // store the temporary policy
  78. if ( $http->hasPostVariable( 'DeleteSubtreeIDArray' ) )
  79. {
  80. $deletedIDList = $http->postVariable( 'DeleteSubtreeIDArray' );
  81. $db = eZDB::instance();
  82. $db->begin();
  83. foreach ( $deletedIDList as $deletedID )
  84. {
  85. $subtree = eZContentObjectTreeNode::fetch( $deletedID, false, false );
  86. $path = $subtree['path_string'];
  87. eZPolicyLimitationValue::removeByValue( $path, $policyID );
  88. }
  89. $db->commit();
  90. }
  91. /* Clean up policy cache */
  92. eZUser::cleanupCache();
  93. }
  94. // Fetch node limitations
  95. $nodeIDList = array();
  96. $nodeLimitation = eZPolicyLimitation::fetchByIdentifier( $policyID, 'Node' );
  97. if ( $nodeLimitation != null )
  98. {
  99. $nodeLimitationID = $nodeLimitation->attribute( 'id' );
  100. $nodeLimitationValues = eZPolicyLimitationValue::fetchList( $nodeLimitationID );
  101. foreach ( $nodeLimitationValues as $nodeLimitationValue )
  102. {
  103. $nodeID = $nodeLimitationValue->attribute( 'value' );
  104. $nodeIDList[] = $nodeID;
  105. $node = eZContentObjectTreeNode::fetch( $nodeID );
  106. $nodeList[] = $node;
  107. }
  108. }
  109. // Fetch subtree limitations
  110. $subtreeLimitation = eZPolicyLimitation::fetchByIdentifier( $policyID, 'Subtree' );
  111. if ( $subtreeLimitation != null )
  112. {
  113. $subtreeLimitationID = $subtreeLimitation->attribute('id');
  114. $subtreeLimitationValues = eZPolicyLimitationValue::fetchList( $subtreeLimitationID );
  115. foreach ( $subtreeLimitationValues as $subtreeLimitationValue )
  116. {
  117. $subtreePath = $subtreeLimitationValue->attribute( 'value' );
  118. $subtreeObject = eZContentObjectTreeNode::fetchByPath( $subtreePath );
  119. if ( $subtreeObject )
  120. {
  121. $subtreeID = $subtreeObject->attribute( 'node_id' );
  122. if ( !isset( $subtreeIDList ) )
  123. $subtreeIDList = array();
  124. $subtreeIDList[] = $subtreeID;
  125. $subtree = eZContentObjectTreeNode::fetch( $subtreeID );
  126. $subtreeList[] = $subtree;
  127. }
  128. }
  129. }
  130. $http->setSessionVariable( 'DisableRoleCache', 1 );
  131. if ( $http->hasPostVariable( 'DiscardChange' ) )
  132. {
  133. $policy->removeThis();
  134. $Module->redirectTo( $Module->functionURI( 'edit' ) . '/' . $roleID . '/' );
  135. }
  136. if ( $http->hasPostVariable( 'UpdatePolicy' ) )
  137. {
  138. // Set flag for audit role-change. If true audit will be processed after applying
  139. $http->setSessionVariable( 'RoleWasChanged', true );
  140. $hasNodeLimitation = false;
  141. $hasLimitation = false;
  142. $db = eZDB::instance();
  143. $db->begin();
  144. $limitationList = eZPolicyLimitation::fetchByPolicyID( $policyID );
  145. foreach ( $limitationList as $limitation )
  146. {
  147. $limitationID = $limitation->attribute( 'id' );
  148. $limitationIdentifier = $limitation->attribute( 'identifier' );
  149. if ( $limitationIdentifier != 'Node' and $limitationIdentifier != 'Subtree' )
  150. eZPolicyLimitation::removeByID( $limitationID );
  151. if ( $limitationIdentifier == 'Node' )
  152. {
  153. $nodeLimitationValues = eZPolicyLimitationValue::fetchList( $limitationID );
  154. if ( $nodeLimitationValues != null )
  155. $hasNodeLimitation = true;
  156. else
  157. eZPolicyLimitation::removeByID( $limitationID );
  158. }
  159. if ( $limitationIdentifier == 'Subtree' )
  160. {
  161. $nodeLimitationValues = eZPolicyLimitationValue::fetchList( $limitationID );
  162. if ( $nodeLimitationValues != null )
  163. $hasLimitation = true;
  164. else
  165. eZPolicyLimitation::removeByID( $limitationID );
  166. }
  167. }
  168. if ( processDropdownLimitations( $policy, $currentModule, $currentFunction, $currentFunctionLimitations ) === true )
  169. $hasLimitation = true;
  170. $policy->store();
  171. // Replace the real edited policy with the temporary one
  172. $policy->saveTemporary();
  173. $db->commit();
  174. /* Clean up policy cache */
  175. eZUser::cleanupCache();
  176. $Module->redirectTo( $Module->functionURI( 'edit' ) . '/' . $roleID . '/');
  177. }
  178. if ( $http->hasPostVariable( 'BrowseLimitationNodeButton' ) )
  179. {
  180. processDropdownLimitations( $policy, $currentModule, $currentFunction, $currentFunctionLimitations );
  181. eZContentBrowse::browse( array( 'action_name' => 'FindLimitationNode',
  182. 'content' => array( 'policy_id' => $originalPolicyID ),
  183. 'from_page' => '/role/policyedit/' . $originalPolicyID ),
  184. $Module );
  185. return;
  186. }
  187. if ( $http->hasPostVariable( 'BrowseLimitationSubtreeButton' ) )
  188. {
  189. processDropdownLimitations( $policy, $currentModule, $currentFunction, $currentFunctionLimitations );
  190. eZContentBrowse::browse( array( 'action_name' => 'FindLimitationSubtree',
  191. 'content' => array( 'policy_id' => $originalPolicyID ),
  192. 'from_page' => '/role/policyedit/' . $originalPolicyID ),
  193. $Module );
  194. return;
  195. }
  196. if ( $http->hasPostVariable( 'SelectedNodeIDArray' ) and
  197. $http->postVariable( 'BrowseActionName' ) == 'FindLimitationNode' and
  198. !$http->hasPostVariable( 'BrowseCancelButton' ) )
  199. {
  200. $db = eZDB::instance();
  201. $db->begin();
  202. $limitationList = eZPolicyLimitation::fetchByPolicyID( $policyID );
  203. // Remove other limitations. When the policy is applied to node, no other constraints needed.
  204. // Removes limitations only from a DropList if it is specified in the module.
  205. if ( isset( $currentFunctionLimitations['Node']['DropList'] ) )
  206. {
  207. $dropList = $currentFunctionLimitations['Node']['DropList'];
  208. foreach ( $limitationList as $limitation )
  209. {
  210. $limitationID = $limitation->attribute( 'id' );
  211. $limitationIdentifier = $limitation->attribute( 'identifier' );
  212. if ( in_array( $limitationIdentifier, $dropList ) )
  213. {
  214. eZPolicyLimitation::removeByID( $limitationID );
  215. }
  216. }
  217. }
  218. else
  219. {
  220. foreach ( $limitationList as $limitation )
  221. {
  222. $limitationID = $limitation->attribute( 'id' );
  223. $limitationIdentifier = $limitation->attribute( 'identifier' );
  224. if ( $limitationIdentifier != 'Node' and $limitationIdentifier != 'Subtree' )
  225. eZPolicyLimitation::removeByID( $limitationID );
  226. }
  227. }
  228. $db->commit();
  229. $selectedNodeIDList = $http->postVariable( 'SelectedNodeIDArray' );
  230. if ( $nodeLimitation == null )
  231. $nodeLimitation = eZPolicyLimitation::createNew( $policyID, 'Node' );
  232. foreach ( $selectedNodeIDList as $nodeID )
  233. {
  234. if ( !in_array( $nodeID, $nodeIDList ) )
  235. {
  236. $nodeLimitationValue = eZPolicyLimitationValue::createNew( $nodeLimitation->attribute( 'id' ), $nodeID );
  237. $node = eZContentObjectTreeNode::fetch( $nodeID );
  238. $nodeList[] = $node;
  239. }
  240. }
  241. }
  242. if ( $http->hasPostVariable( 'SelectedNodeIDArray' ) and
  243. $http->postVariable( 'BrowseActionName' ) == 'FindLimitationSubtree' and
  244. !$http->hasPostVariable( 'BrowseCancelButton' ) )
  245. {
  246. $selectedSubtreeIDList = $http->postVariable( 'SelectedNodeIDArray' );
  247. $db = eZDB::instance();
  248. $db->begin();
  249. if ( $subtreeLimitation == null )
  250. $subtreeLimitation = eZPolicyLimitation::createNew( $policyID, 'Subtree' );
  251. foreach ( $selectedSubtreeIDList as $nodeID )
  252. {
  253. if ( !isset( $subtreeIDList ) or !in_array( $nodeID, $subtreeIDList ) )
  254. {
  255. $subtree = eZContentObjectTreeNode::fetch( $nodeID );
  256. $pathString = $subtree->attribute( 'path_string' );
  257. $policyLimitationValue = eZPolicyLimitationValue::createNew( $subtreeLimitation->attribute( 'id' ), $pathString );
  258. $subtreeList[] = $subtree;
  259. }
  260. }
  261. $db->commit();
  262. }
  263. $currentLimitationList = array();
  264. foreach ( $currentFunctionLimitations as $currentFunctionLimitation )
  265. {
  266. $currentLimitationList[$currentFunctionLimitation['name']] = '-1';
  267. }
  268. $limitationList = eZPolicyLimitation::fetchByPolicyID( $policyID );
  269. foreach ( $limitationList as $limitation )
  270. {
  271. $limitationID = $limitation->attribute( 'id' );
  272. $limitationIdentifier = $limitation->attribute( 'identifier' );
  273. $limitationValueList = eZPolicyLimitationValue::fetchList( $limitationID );
  274. $valueList = array();
  275. foreach ( $limitationValueList as $limitationValue )
  276. {
  277. $valueList[] = $limitationValue->attribute( 'value' );
  278. }
  279. $currentLimitationList[$limitationIdentifier] = $valueList;
  280. }
  281. $Module->setTitle( 'Edit policy' );
  282. $tpl = eZTemplate::factory();
  283. $tpl->setVariable( 'Module', $Module );
  284. $tpl->setVariable( 'current_function', $currentFunction );
  285. $tpl->setVariable( 'role_id', $roleID );
  286. $tpl->setVariable( 'role_name', $roleName );
  287. $tpl->setVariable( 'current_module', $currentModule );
  288. $tpl->setVariable( 'function_limitations', $currentFunctionLimitations );
  289. $tpl->setVariable( 'policy_id', $originalPolicyID );
  290. $tpl->setVariable( 'policy_limitation_list', $limitationValueList );
  291. $tpl->setVariable( 'node_list', $nodeList );
  292. $tpl->setVariable( 'subtree_list', $subtreeList );
  293. $tpl->setVariable( 'current_limitation_list', $currentLimitationList );
  294. $Result = array();
  295. $Result['path'] = array( array( 'url' => false,
  296. 'text' => ezpI18n::tr( 'kernel/role', 'Editing policy' ) ) );
  297. $Result['content'] = $tpl->fetch( 'design:role/policyedit.tpl' );
  298. /**
  299. * Applies the POST submitted limitations as found in the dropdowns
  300. * @param eZPolicy $policy
  301. * @param string $currentModule
  302. * @param string $currentFunction
  303. * @param array $currentFunctionLimitations
  304. *
  305. * @return bool True if limitations were found, false otherwise
  306. */
  307. function processDropdownLimitations( &$policy, $currentModule, $currentFunction, $currentFunctionLimitations )
  308. {
  309. $hasLimitation = false;
  310. $http = eZHTTPTool::instance();
  311. $db = eZDB::instance();
  312. $db->begin();
  313. foreach ( $currentFunctionLimitations as $functionLimitation )
  314. {
  315. if ( $http->hasPostVariable( $functionLimitation['name'] ) and
  316. $functionLimitation['name'] != 'Node' and
  317. $functionLimitation['name'] != 'Subtree' )
  318. {
  319. $limitationValueList = $http->postVariable( $functionLimitation['name'] );
  320. if ( !in_array('-1', $limitationValueList ) )
  321. {
  322. $hasLimitation = true;
  323. $policyLimitation = eZPolicyLimitation::createNew( $policy->attribute( 'id' ),
  324. $functionLimitation['name'] );
  325. foreach ( $limitationValueList as $limitationValue )
  326. {
  327. eZPolicyLimitationValue::createNew( $policyLimitation->attribute( 'id' ), $limitationValue );
  328. }
  329. }
  330. }
  331. }
  332. $db->commit();
  333. return $hasLimitation;
  334. }
  335. ?>