PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/modules/rights/controllers/AuthItemController.php

https://gitlab.com/zenfork/vektor
PHP | 568 lines | 376 code | 70 blank | 122 comment | 30 complexity | 882f1678ab62a63801509405bdc28297 MD5 | raw file
  1. <?php
  2. /**
  3. * Rights authorization item controller class file.
  4. *
  5. * @author Christoffer Niska <cniska@live.com>
  6. * @copyright Copyright &copy; 2010 Christoffer Niska
  7. * @since 0.5
  8. */
  9. class AuthItemController extends RController
  10. {
  11. /**
  12. * @property RAuthorizer
  13. */
  14. private $_authorizer;
  15. /**
  16. * @property CAuthItem the currently loaded data model instance.
  17. */
  18. private $_model;
  19. /**
  20. * Initializes the controller.
  21. */
  22. public function init()
  23. {
  24. $this->_authorizer = $this->module->getAuthorizer();
  25. $this->layout = $this->module->layout;
  26. $this->defaultAction = 'permissions';
  27. // Register the scripts
  28. $this->module->registerScripts();
  29. }
  30. /**
  31. * @return array action filters
  32. */
  33. public function filters()
  34. {
  35. return array(
  36. 'accessControl'
  37. );
  38. }
  39. /**
  40. * Specifies the access control rules.
  41. * This method is used by the 'accessControl' filter.
  42. * @return array access control rules
  43. */
  44. public function accessRules()
  45. {
  46. return array(
  47. array('allow', // Allow superusers to access Rights
  48. 'actions'=>array(
  49. 'permissions',
  50. 'operations',
  51. 'tasks',
  52. 'roles',
  53. 'generate',
  54. 'create',
  55. 'update',
  56. 'delete',
  57. 'removeChild',
  58. 'assign',
  59. 'revoke',
  60. 'sortable',
  61. ),
  62. 'users'=>$this->_authorizer->getSuperusers(),
  63. ),
  64. array('deny', // Deny all users
  65. 'users'=>array('*'),
  66. ),
  67. );
  68. }
  69. /**
  70. * Displays the permission overview.
  71. */
  72. public function actionPermissions()
  73. {
  74. $dataProvider = new RPermissionDataProvider('permissions');
  75. // Get the roles from the data provider
  76. $roles = $dataProvider->getRoles();
  77. $roleColumnWidth = $roles!==array() ? 75/count($roles) : 0;
  78. // Initialize the columns
  79. $columns = array(
  80. array(
  81. 'name'=>'description',
  82. 'header'=>Rights::t('core', 'Item'),
  83. 'type'=>'raw',
  84. 'htmlOptions'=>array(
  85. 'class'=>'permission-column',
  86. 'style'=>'width:25%',
  87. ),
  88. ),
  89. );
  90. // Add a column for each role
  91. foreach( $roles as $roleName=>$role )
  92. {
  93. $columns[] = array(
  94. 'name'=>strtolower($roleName),
  95. 'header'=>$role->getNameText(),
  96. 'type'=>'raw',
  97. 'htmlOptions'=>array(
  98. 'class'=>'role-column',
  99. 'style'=>'width:'.$roleColumnWidth.'%',
  100. ),
  101. );
  102. }
  103. $view = 'permissions';
  104. $params = array(
  105. 'dataProvider'=>$dataProvider,
  106. 'columns'=>$columns,
  107. );
  108. // Render the view
  109. isset($_POST['ajax'])===true ? $this->renderPartial($view, $params) : $this->render($view, $params);
  110. }
  111. /**
  112. * Displays the operation management page.
  113. */
  114. public function actionOperations()
  115. {
  116. Yii::app()->user->rightsReturnUrl = array('authItem/operations');
  117. $dataProvider = new RAuthItemDataProvider('operations', array(
  118. 'type'=>CAuthItem::TYPE_OPERATION,
  119. 'sortable'=>array(
  120. 'id'=>'RightsOperationTableSort',
  121. 'element'=>'.operation-table',
  122. 'url'=>$this->createUrl('authItem/sortable'),
  123. ),
  124. ));
  125. // Render the view
  126. $this->render('operations', array(
  127. 'dataProvider'=>$dataProvider,
  128. 'isBizRuleEnabled'=>$this->module->enableBizRule,
  129. 'isBizRuleDataEnabled'=>$this->module->enableBizRuleData,
  130. ));
  131. }
  132. /**
  133. * Displays the operation management page.
  134. */
  135. public function actionTasks()
  136. {
  137. Yii::app()->user->rightsReturnUrl = array('authItem/tasks');
  138. $dataProvider = new RAuthItemDataProvider('tasks', array(
  139. 'type'=>CAuthItem::TYPE_TASK,
  140. 'sortable'=>array(
  141. 'id'=>'RightsTaskTableSort',
  142. 'element'=>'.task-table',
  143. 'url'=>$this->createUrl('authItem/sortable'),
  144. ),
  145. ));
  146. // Render the view
  147. $this->render('tasks', array(
  148. 'dataProvider'=>$dataProvider,
  149. 'isBizRuleEnabled'=>$this->module->enableBizRule,
  150. 'isBizRuleDataEnabled'=>$this->module->enableBizRuleData,
  151. ));
  152. }
  153. /**
  154. * Displays the role management page.
  155. */
  156. public function actionRoles()
  157. {
  158. Yii::app()->user->rightsReturnUrl = array('authItem/roles');
  159. $dataProvider = new RAuthItemDataProvider('roles', array(
  160. 'type'=>CAuthItem::TYPE_ROLE,
  161. 'sortable'=>array(
  162. 'id'=>'RightsRoleTableSort',
  163. 'element'=>'.role-table',
  164. 'url'=>$this->createUrl('authItem/sortable'),
  165. ),
  166. ));
  167. // Render the view
  168. $this->render('roles', array(
  169. 'dataProvider'=>$dataProvider,
  170. 'isBizRuleEnabled'=>$this->module->enableBizRule,
  171. 'isBizRuleDataEnabled'=>$this->module->enableBizRuleData,
  172. ));
  173. }
  174. /**
  175. * Displays the generator page.
  176. */
  177. public function actionGenerate()
  178. {
  179. // Get the generator and authorizer
  180. $generator = $this->module->getGenerator();
  181. // Createh the form model
  182. $model = new GenerateForm();
  183. // Form has been submitted
  184. if( isset($_POST['GenerateForm'])===true )
  185. {
  186. // Form is valid
  187. $model->attributes = $_POST['GenerateForm'];
  188. if( $model->validate()===true )
  189. {
  190. $items = array(
  191. 'tasks'=>array(),
  192. 'operations'=>array(),
  193. );
  194. // Get the chosen items
  195. foreach( $model->items as $itemname=>$value )
  196. {
  197. if( (bool)$value===true )
  198. {
  199. if( strpos($itemname, '*')!==false )
  200. $items['tasks'][] = $itemname;
  201. else
  202. $items['operations'][] = $itemname;
  203. }
  204. }
  205. // Add the items to the generator as tasks and operations and run the generator.
  206. $generator->addItems($items['tasks'], CAuthItem::TYPE_TASK);
  207. $generator->addItems($items['operations'], CAuthItem::TYPE_OPERATION);
  208. if( ($generatedItems = $generator->run())!==false && $generatedItems!==array() )
  209. {
  210. Yii::app()->getUser()->setFlash($this->module->flashSuccessKey,
  211. Rights::t('core', 'Authorization items created.')
  212. );
  213. $this->redirect(array('authItem/permissions'));
  214. }
  215. }
  216. }
  217. // Get all items that are available to be generated
  218. $items = $generator->getControllerActions();
  219. // We need the existing operations for comparason
  220. $authItems = $this->_authorizer->getAuthItems(array(
  221. CAuthItem::TYPE_TASK,
  222. CAuthItem::TYPE_OPERATION,
  223. ));
  224. $existingItems = array();
  225. foreach( $authItems as $itemName=>$item )
  226. $existingItems[ $itemName ] = $itemName;
  227. Yii::app()->clientScript->registerScript('rightsGenerateItemTableSelectRows',
  228. "jQuery('.generate-item-table').rightsSelectRows();"
  229. );
  230. // Render the view
  231. $this->render('generate', array(
  232. 'model'=>$model,
  233. 'items'=>$items,
  234. 'existingItems'=>$existingItems,
  235. ));
  236. }
  237. /**
  238. * Creates an authorization item.
  239. * @todo add type validation.
  240. */
  241. public function actionCreate()
  242. {
  243. $type = $this->getType();
  244. // Create the authorization item form
  245. $formModel = new AuthItemForm('create');
  246. if( isset($_POST['AuthItemForm'])===true )
  247. {
  248. $formModel->attributes = $_POST['AuthItemForm'];
  249. if( $formModel->validate()===true )
  250. {
  251. // Create the item
  252. $item = $this->_authorizer->createAuthItem($formModel->name, $type, $formModel->description, $formModel->bizRule, $formModel->data);
  253. $item = $this->_authorizer->attachAuthItemBehavior($item);
  254. // Set a flash message for creating the item
  255. Yii::app()->user->setFlash($this->module->flashSuccessKey,
  256. Rights::t('core', ':name created.', array(':name'=>$item->getNameText()))
  257. );
  258. // Redirect to the correct destination
  259. $this->redirect(Yii::app()->user->getRightsReturnUrl(array('authItem/permissions')));
  260. }
  261. }
  262. // Render the view
  263. $this->render('create', array(
  264. 'formModel'=>$formModel,
  265. ));
  266. }
  267. /**
  268. * Updates an authorization item.
  269. */
  270. public function actionUpdate()
  271. {
  272. // Get the authorization item
  273. $model = $this->loadModel();
  274. $itemName = $model->getName();
  275. // Create the authorization item form
  276. $formModel = new AuthItemForm('update');
  277. if( isset($_POST['AuthItemForm'])===true )
  278. {
  279. $formModel->attributes = $_POST['AuthItemForm'];
  280. if( $formModel->validate()===true )
  281. {
  282. // Update the item and load it
  283. $this->_authorizer->updateAuthItem($itemName, $formModel->name, $formModel->description, $formModel->bizRule, $formModel->data);
  284. $item = $this->_authorizer->authManager->getAuthItem($formModel->name);
  285. $item = $this->_authorizer->attachAuthItemBehavior($item);
  286. // Set a flash message for updating the item
  287. Yii::app()->user->setFlash($this->module->flashSuccessKey,
  288. Rights::t('core', ':name updated.', array(':name'=>$item->getNameText()))
  289. );
  290. // Redirect to the correct destination
  291. $this->redirect(Yii::app()->user->getRightsReturnUrl(array('authItem/permissions')));
  292. }
  293. }
  294. $type = Rights::getValidChildTypes($model->type);
  295. $exclude = array($this->module->superuserName);
  296. $childSelectOptions = Rights::getParentAuthItemSelectOptions($model, $type, $exclude);
  297. if( $childSelectOptions!==array() )
  298. {
  299. $childFormModel = new AuthChildForm();
  300. // Child form is submitted and data is valid
  301. if( isset($_POST['AuthChildForm'])===true )
  302. {
  303. $childFormModel->attributes = $_POST['AuthChildForm'];
  304. if( $childFormModel->validate()===true )
  305. {
  306. // Add the child and load it
  307. $this->_authorizer->authManager->addItemChild($itemName, $childFormModel->itemname);
  308. $child = $this->_authorizer->authManager->getAuthItem($childFormModel->itemname);
  309. $child = $this->_authorizer->attachAuthItemBehavior($child);
  310. // Set a flash message for adding the child
  311. Yii::app()->user->setFlash($this->module->flashSuccessKey,
  312. Rights::t('core', 'Child :name added.', array(':name'=>$child->getNameText()))
  313. );
  314. // Reidrect to the same page
  315. $this->redirect(array('authItem/update', 'name'=>urlencode($itemName)));
  316. }
  317. }
  318. }
  319. else
  320. {
  321. $childFormModel = null;
  322. }
  323. // Set the values for the form fields
  324. $formModel->name = $model->name;
  325. $formModel->description = $model->description;
  326. $formModel->type = $model->type;
  327. $formModel->bizRule = $model->bizRule!=='NULL' ? $model->bizRule : '';
  328. $formModel->data = $model->data!==null ? serialize($model->data) : '';
  329. $parentDataProvider = new RAuthItemParentDataProvider($model);
  330. $childDataProvider = new RAuthItemChildDataProvider($model);
  331. // Render the view
  332. $this->render('update', array(
  333. 'model'=>$model,
  334. 'formModel'=>$formModel,
  335. 'childFormModel'=>$childFormModel,
  336. 'childSelectOptions'=>$childSelectOptions,
  337. 'parentDataProvider'=>$parentDataProvider,
  338. 'childDataProvider'=>$childDataProvider,
  339. ));
  340. }
  341. /**
  342. * Deletes an operation.
  343. */
  344. public function actionDelete()
  345. {
  346. // We only allow deletion via POST request
  347. if( Yii::app()->request->isPostRequest===true )
  348. {
  349. $itemName = $this->getItemName();
  350. // Load the item and save the name for later use
  351. $item = $this->_authorizer->authManager->getAuthItem($itemName);
  352. $item = $this->_authorizer->attachAuthItemBehavior($item);
  353. // Delete the item
  354. $this->_authorizer->authManager->removeAuthItem($itemName);
  355. // Set a flash message for deleting the item
  356. Yii::app()->user->setFlash($this->module->flashSuccessKey,
  357. Rights::t('core', ':name deleted.', array(':name'=>$item->getNameText()))
  358. );
  359. // If AJAX request, we should not redirect the browser
  360. if( isset($_POST['ajax'])===false )
  361. $this->redirect(Yii::app()->user->getRightsReturnUrl(array('authItem/permissions')));
  362. }
  363. else
  364. {
  365. throw new CHttpException(400, Rights::t('core', 'Invalid request. Please do not repeat this request again.'));
  366. }
  367. }
  368. /**
  369. * Removes a child from an authorization item.
  370. */
  371. public function actionRemoveChild()
  372. {
  373. // We only allow deletion via POST request
  374. if( Yii::app()->request->isPostRequest===true )
  375. {
  376. $itemName = $this->getItemName();
  377. $childName = $this->getChildName();
  378. // Remove the child and load it
  379. $this->_authorizer->authManager->removeItemChild($itemName, $childName);
  380. $child = $this->_authorizer->authManager->getAuthItem($childName);
  381. $child = $this->_authorizer->attachAuthItemBehavior($child);
  382. // Set a flash message for removing the child
  383. Yii::app()->user->setFlash($this->module->flashSuccessKey,
  384. Rights::t('core', 'Child :name removed.', array(':name'=>$child->getNameText()))
  385. );
  386. // If AJAX request, we should not redirect the browser
  387. if( isset($_POST['ajax'])===false )
  388. $this->redirect(array('authItem/update', 'name'=>urlencode($itemName)));
  389. }
  390. else
  391. {
  392. throw new CHttpException(400, Rights::t('core', 'Invalid request. Please do not repeat this request again.'));
  393. }
  394. }
  395. /**
  396. * Adds a child to an authorization item.
  397. */
  398. public function actionAssign()
  399. {
  400. // We only allow deletion via POST request
  401. if( Yii::app()->request->isPostRequest===true )
  402. {
  403. $model = $this->loadModel();
  404. $childName = $this->getChildName();
  405. if( $childName!==null && $model->hasChild($childName)===false )
  406. $model->addChild($childName);
  407. // if AJAX request, we should not redirect the browser
  408. if( isset($_POST['ajax'])===false )
  409. $this->redirect(array('authItem/permissions'));
  410. }
  411. else
  412. {
  413. throw new CHttpException(400, Rights::t('core', 'Invalid request. Please do not repeat this request again.'));
  414. }
  415. }
  416. /**
  417. * Removes a child from an authorization item.
  418. */
  419. public function actionRevoke()
  420. {
  421. // We only allow deletion via POST request
  422. if( Yii::app()->request->isPostRequest===true )
  423. {
  424. $model = $this->loadModel();
  425. $childName = $this->getChildName();
  426. if( $childName!==null && $model->hasChild($childName)===true )
  427. $model->removeChild($childName);
  428. // if AJAX request, we should not redirect the browser
  429. if( isset($_POST['ajax'])===false )
  430. $this->redirect(array('authItem/permissions'));
  431. }
  432. else
  433. {
  434. throw new CHttpException(400, Rights::t('core', 'Invalid request. Please do not repeat this request again.'));
  435. }
  436. }
  437. /**
  438. * Processes the jui sortable.
  439. */
  440. public function actionSortable()
  441. {
  442. // We only allow sorting via POST request
  443. if( Yii::app()->request->isPostRequest===true )
  444. {
  445. $this->_authorizer->authManager->updateItemWeight($_POST['result']);
  446. }
  447. else
  448. {
  449. throw new CHttpException(400, Rights::t('core', 'Invalid request. Please do not repeat this request again.'));
  450. }
  451. }
  452. /**
  453. * @return string the item name or null if not set.
  454. */
  455. public function getItemName()
  456. {
  457. return isset($_GET['name'])===true ? urldecode($_GET['name']) : null;
  458. }
  459. /**
  460. * @return string the child name or null if not set.
  461. */
  462. public function getChildName()
  463. {
  464. return isset($_GET['child'])===true ? urldecode($_GET['child']) : null;
  465. }
  466. /**
  467. * Returns the authorization item type after validation.
  468. * @return int the type.
  469. */
  470. public function getType()
  471. {
  472. $type = $_GET['type'];
  473. $validTypes = array(CAuthItem::TYPE_OPERATION, CAuthItem::TYPE_TASK, CAuthItem::TYPE_ROLE);
  474. if( in_array($type, $validTypes)===true )
  475. return $type;
  476. else
  477. throw new CException(Rights::t('core', 'Invalid authorization item type.'));
  478. }
  479. /**
  480. * Returns the data model based on the primary key given in the GET variable.
  481. * If the data model is not found, an HTTP exception will be raised.
  482. */
  483. public function loadModel()
  484. {
  485. if( $this->_model===null )
  486. {
  487. $itemName = $this->getItemName();
  488. if( $itemName!==null )
  489. {
  490. $this->_model = $this->_authorizer->authManager->getAuthItem($itemName);
  491. $this->_model = $this->_authorizer->attachAuthItemBehavior($this->_model);
  492. }
  493. if( $this->_model===null )
  494. throw new CHttpException(404, Rights::t('core', 'The requested page does not exist.'));
  495. }
  496. return $this->_model;
  497. }
  498. }