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

/applications/garden/controllers/role.php

https://github.com/mischka/Garden
PHP | 135 lines | 78 code | 25 blank | 32 comment | 20 complexity | 21987d512a9d3c750c06fb8bf2b5e011 MD5 | raw file
  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Mark O'Sullivan
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
  8. Contact Mark O'Sullivan at mark [at] lussumo [dot] com
  9. */
  10. /**
  11. * RBAC (Role Based Access Control)
  12. */
  13. class RoleController extends GardenController {
  14. public $Uses = array('Database', 'Form', 'Gdn_RoleModel');
  15. public function Add() {
  16. if ($this->Head)
  17. $this->Head->Title(Translate('Add Role'));
  18. $this->Permission('Garden.Roles.Manage');
  19. // Load default permissions.
  20. //$PermissionModel = Gdn::PermissionModel();
  21. //$this->SetData('PermissionData', $PermissionModel->GetPermissionsEdit(0, FALSE), TRUE);
  22. // Use the edit form with no roleid specified.
  23. $this->View = 'Edit';
  24. $this->Edit();
  25. }
  26. public function Delete($RoleID = FALSE) {
  27. if ($this->Head)
  28. $this->Head->Title(Translate('Delete Role'));
  29. $this->Permission('Garden.Roles.Manage');
  30. $this->AddSideMenu('garden/role');
  31. $Role = $this->RoleModel->GetByRoleID($RoleID);
  32. if ($Role->Deletable == '0')
  33. $this->Form->AddError('You cannot delete this role.');
  34. // Make sure the form knows which item we are deleting.
  35. $this->Form->AddHidden('RoleID', $RoleID);
  36. // Figure out how many users will be affected by this deletion
  37. $this->AffectedUsers = $this->RoleModel->GetUserCount($RoleID);
  38. // Figure out how many users will be orphaned by this deletion
  39. $this->OrphanedUsers = $this->RoleModel->GetUserCount($RoleID, TRUE);
  40. // Get a list of roles other than this one that can act as a replacement
  41. $this->ReplacementRoles = $this->RoleModel->GetByNotRoleID($RoleID);
  42. if ($this->Form->AuthenticatedPostBack()) {
  43. // Make sure that a replacement role has been selected if there were going to be orphaned users
  44. if ($this->OrphanedUsers > 0) {
  45. $Validation = new Gdn_Validation();
  46. $Validation->ApplyRule('ReplacementRoleID', 'Required', 'You must choose a replacement role for orphaned users.');
  47. $Validation->Validate($this->Form->FormValues());
  48. $this->Form->SetValidationResults($Validation->Results());
  49. }
  50. if ($this->Form->ErrorCount() == 0) {
  51. // Go ahead and delete the Role
  52. $this->RoleModel->Delete($RoleID, $this->Form->GetValue('ReplacementRoleID'));
  53. $this->RedirectUrl = Url('garden/role');
  54. $this->StatusMessage = Gdn::Translate('Deleting role...');
  55. }
  56. }
  57. $this->Render();
  58. }
  59. //public $HasJunctionPermissionData;
  60. public function Edit($RoleID = FALSE) {
  61. if ($this->Head && $this->Head->Title() == '')
  62. $this->Head->Title(Translate('Edit Role'));
  63. $this->Permission('Garden.Roles.Manage');
  64. $this->AddSideMenu('garden/role');
  65. $PermissionModel = Gdn::PermissionModel();
  66. $this->Role = $this->RoleModel->GetByRoleID($RoleID);
  67. // $this->EditablePermissions = is_object($this->Role) ? $this->Role->EditablePermissions : '1';
  68. if ($this->Head)
  69. $this->Head->AddScript('/js/library/jquery.gardencheckboxgrid.js');
  70. // Set the model on the form.
  71. $this->Form->SetModel($this->RoleModel);
  72. // Make sure the form knows which item we are editing.
  73. $this->Form->AddHidden('RoleID', $RoleID);
  74. $LimitToSuffix = !$this->Role || $this->Role->CanSession == '1' ? '' : 'View';
  75. // Load all permissions based on enabled applications and plugins
  76. //$this->SetData('PermissionData', $PermissionModel->GetPermissions($RoleID, $LimitToSuffix), TRUE);
  77. // If seeing the form for the first time...
  78. if ($this->Form->AuthenticatedPostBack() === FALSE) {
  79. // Get the role data for the requested $RoleID and put it into the form.
  80. $this->SetData('PermissionData', $PermissionModel->GetPermissionsEdit($RoleID ? $RoleID : 0, $LimitToSuffix), true);
  81. $this->Form->SetData($this->Role);
  82. } else {
  83. // If the form has been posted back...
  84. // 2. Save the data (validation occurs within):
  85. if ($RoleID = $this->Form->Save()) {
  86. $this->StatusMessage = Gdn::Translate('Your changes have been saved.');
  87. $this->RedirectUrl = Url('garden/role');
  88. // Reload the permission data.
  89. $this->SetData('PermissionData', $PermissionModel->GetPermissionsEdit($RoleID, $LimitToSuffix), true);
  90. }
  91. }
  92. $this->Render();
  93. }
  94. public function Index() {
  95. $this->Permission('Garden.Roles.Manage');
  96. $this->AddSideMenu('garden/role');
  97. if ($this->Head) {
  98. $this->Head->AddScript('/js/library/jquery.tablednd.js');
  99. $this->Head->AddScript('/js/library/jquery.ui.packed.js');
  100. $this->Head->Title(Translate('Roles & Permissions'));
  101. }
  102. $this->RoleData = $this->RoleModel->Get();
  103. $this->Render();
  104. }
  105. public function Initialize() {
  106. parent::Initialize();
  107. if ($this->Menu)
  108. $this->Menu->HighlightRoute('/garden/settings');
  109. }
  110. }