PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/manager/application/controllers/roles.php

https://bitbucket.org/jerwinse/iagh-cms
PHP | 167 lines | 143 code | 20 blank | 4 comment | 18 complexity | 641908530205091a16b3faa218bcd772 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Roles extends CI_Controller {
  3. private $permission;
  4. private $userPermission;
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->tal->title = $this->config->item('title');
  9. $this->tal->base_url = substr(base_url(), 0, -1);
  10. # check if session expires
  11. if(!$this->ion_auth->user()->result()){
  12. exit;
  13. }
  14. $this->user = $this->ion_auth->user()->result();
  15. $this->user[0]->fullname = $this->user[0]->first_name . ' ' . $this->user[0]->last_name;
  16. $config = array('userID' => $this->user[0]->id);
  17. $this->load->library('acl', $config);
  18. if (!$this->acl->userRoles) {
  19. exit;
  20. }
  21. else{
  22. # get role permission
  23. $this->permission = $this->acl->getRolePerms($this->acl->userRoles[0]);
  24. $userPermission = $this->acl->getUserPerms($this->acl->userID);
  25. $this->userPermission = $userPermission[0]['id'];
  26. }
  27. }
  28. public function index()
  29. {
  30. $data = array();
  31. $roles = array();
  32. $roles = $this->acl->getAllRoles('full');
  33. $data['roles'] = $roles;
  34. $this->load->view('roles/index.zpt', $data);
  35. }
  36. public function create()
  37. {
  38. $data = array();
  39. $data['isAddRequest'] = true;
  40. $role[] = array('id'=>0);
  41. $data['role'] = $role;
  42. $permissions = $this->acl->getAllPerms('full');
  43. foreach($permissions as $index => $val){
  44. $rolePermission = strtolower($val['name']);
  45. }
  46. $data['permissions'] = $permissions;
  47. $data['currentPermission'] = 0;
  48. $this->load->view('roles/roleForm.zpt', $data);
  49. }
  50. public function docreate()
  51. {
  52. $post = $this->input->post('data');
  53. $param = json_decode($post);
  54. $data = array();
  55. foreach ($param as $item)
  56. {
  57. $data[$item->name] = $item->value;
  58. }
  59. $id = $this->acl->createRole($data);
  60. $response = array('status' => 'NACK', 'message' => 'Cannot save record.');
  61. if ($id) {
  62. $response = array('status' => 'ACK', 'message' => 'Record successfully created.');
  63. }
  64. $this->tal->data = json_encode($response);
  65. $this->tal->display('common/structure.zpt');
  66. }
  67. public function edit(){
  68. if(isset($_GET['roleId'])){
  69. $roleInfo = array();
  70. $roleId = $_GET['roleId'];
  71. $curPermId = $this->acl->getRolePerms($roleId);
  72. $roles = $this->acl->getAllRoles('full');
  73. if($roles){
  74. for($i=0; $i<count($roles); $i++){
  75. if($roles[$i]['id']==$roleId){
  76. $roleInfo[] = $roles[$i];
  77. }
  78. }
  79. }
  80. $permissions = $this->acl->getAllPerms('full');
  81. foreach($permissions as $index => $val){
  82. $rolePermission = strtolower($val['name']);
  83. }
  84. $data['currentPermission'] = $curPermId[0]['perm'];
  85. $data['permissions'] = $permissions;
  86. $data['isAddRequest'] = false;
  87. $data['role'] = $roleInfo;
  88. $this->load->view('roles/roleForm.zpt', $data);
  89. }
  90. }
  91. public function saveRole(){
  92. $res = array();
  93. $res['status'] = 0;
  94. $res['message'] = "";
  95. $post = $this->input->post('data');
  96. $params = json_decode($post);
  97. $dataParams = array();
  98. foreach($params as $item){
  99. $dataParams[$item->name] = $item->value;
  100. }
  101. $roleName = array("roleName"=>$dataParams['roleName']);
  102. # Edit Request
  103. if($dataParams['roleId']>0){
  104. $this->acl->updateRole($roleName, $dataParams['roleId']);
  105. $this->acl->updateRolePermission($dataParams['roleId'], $dataParams['permission']);
  106. $res = array("status"=> 1, "message"=>"Role has been successfully saved!");
  107. }
  108. # Add Request
  109. else {
  110. $id = $this->acl->createRole($roleName);
  111. if ($id) {
  112. $rolePermissionData = array("roleID"=>$id, "permID"=>$dataParams['permission']);
  113. $permID = $this->acl->createRolePermission($rolePermissionData);
  114. $res = array('status' => 1, 'message' => 'Record successfully created.');
  115. }
  116. else {
  117. $res = array('status' => 0, 'message' => 'Cannot save record.');
  118. }
  119. }
  120. print_r(json_encode($res));
  121. }
  122. public function deleteSelected(){
  123. $res = array();
  124. $res['status'] = 0;
  125. $res['message'] = "Failed to delete Role";
  126. if(isset($_POST['id']) && !empty($_POST['id'])){
  127. $id = explode(",", $_POST['id']);
  128. $counter = 0;
  129. for($i=0; $i<count($id); $i++){
  130. $roleMembers = $this->acl->getRoleMembers($id[$i]);
  131. if(!empty($roleMembers)){
  132. $counter++;
  133. }
  134. }
  135. if($counter>0){
  136. $res = array("status"=>0, "message"=>"Role is currently in use");
  137. }
  138. else{
  139. for($i=0; $i<count($id); $i++){
  140. $this->acl->removeRole($id[$i]);
  141. }
  142. $res = array("status"=>1, "message"=>"Role has been deleted");
  143. }
  144. }
  145. print_r(json_encode($res));
  146. }
  147. }