PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/manager/application/controllers/members.php

https://bitbucket.org/jerwinse/iagh-cms
PHP | 348 lines | 270 code | 51 blank | 27 comment | 45 complexity | c7cf70188caedf73a5abd56a8e3e9f92 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Members extends CI_Controller {
  3. private $permission = array();
  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. // debug($this->acl);
  19. if (!$this->acl->userRoles) {
  20. exit;
  21. }
  22. else{
  23. # get role permission
  24. $this->permission = $this->acl->getRolePerms($this->acl->userRoles[0]);
  25. $userPermission = $this->acl->getUserPerms($this->acl->userID);
  26. $this->userPermission = $userPermission[0]['id'];
  27. }
  28. }
  29. public function index(){
  30. $data = array();
  31. $resp = array();
  32. $rolePermission = "";
  33. $users = array();
  34. foreach($this->permission as $index => $val){
  35. $rolePermission = strtolower($val['name']);
  36. }
  37. if($this->userPermission <= 2){
  38. if($rolePermission!=""){
  39. switch($rolePermission){
  40. case "superuser":
  41. $allUsers = $this->acl->getAllUsers('full');
  42. for($i=0; $i<count($allUsers); $i++){
  43. if($allUsers[$i]['id']!=$this->acl->userID){
  44. $users[] = $allUsers[$i];
  45. }
  46. }
  47. break;
  48. case "administrator":
  49. $resp = $this->acl->getAllUsersByRole($this->acl->userRoles[0]);
  50. for($i=0; $i<count($resp); $i++){
  51. if($resp[$i]->id != $this->acl->userID){
  52. $users[] = get_object_vars($resp[$i]);
  53. }
  54. }
  55. break;
  56. }
  57. }
  58. }
  59. else {
  60. redirect('404');
  61. }
  62. // $data = $this->acl->getAllUsers('full');
  63. $data['isAllowed'] = $this->userPermission==1||$this->userPermission==2?true:false;
  64. $data['data'] = $users;
  65. $this->load->view('members/index.zpt', $data);
  66. }
  67. public function create(){
  68. if($this->userPermission <= 2){
  69. $data = array();
  70. $rolePermission = "";
  71. $permissions = array();
  72. $permissions = $this->acl->getAllPerms('full');
  73. foreach($this->permission as $index => $val){
  74. $rolePermission = strtolower($val['name']);
  75. }
  76. if($rolePermission == "superuser"){
  77. $roles = array();
  78. $roles = $this->acl->getAllRoles('full');
  79. }
  80. if($rolePermission == "administrator"){
  81. $roles = $this->acl->userRoles[0];
  82. }
  83. $data['isAddRequest'] = true;
  84. $data['roles'] = $roles;
  85. $data['permissions'] = $permissions;
  86. $this->load->view('members/memberForm.zpt', $data);
  87. }
  88. }
  89. public function edit(){
  90. if($this->userPermission <= 2){
  91. if(isset($_GET['id'])){
  92. $userId = $_GET['id'];
  93. $data = array();
  94. $rolePermission = "";
  95. $currentRole = 0;
  96. $currentPermission = 0;
  97. $permissions = array();
  98. $permissions = $this->acl->getAllPerms('full');
  99. foreach($this->permission as $index => $val){
  100. $rolePermission = strtolower($val['name']);
  101. }
  102. if($rolePermission == "superuser"){
  103. $roles = array();
  104. $roles = $this->acl->getAllRoles('full');
  105. }
  106. if($rolePermission == "administrator"){
  107. $roles = $this->acl->userRoles[0];
  108. }
  109. # get User Info
  110. $userInfo = $this->acl->getUser($userId);
  111. $userInfo = get_object_vars($userInfo[0]);
  112. $currentRole = $this->acl->getUserRoles($userId);
  113. $currentRole = isset($currentRole[0])?$currentRole[0]:0;
  114. $currentPermission = $this->acl->getUserPerms($userId);
  115. $currentPermission = isset($currentPermission[0]['id'])?$currentPermission[0]['id']:0;
  116. $data['user'] = $userInfo;
  117. $data['isAddRequest'] = false;
  118. $data['currentRole'] = $currentRole;
  119. $data['currentPermission'] = $currentPermission;
  120. $data['roles'] = $roles;
  121. $data['permissions'] = $permissions;
  122. $this->load->view('members/memberForm.zpt', $data);
  123. }
  124. else {
  125. exit;
  126. }
  127. }
  128. }
  129. public function settings(){
  130. $userId = $this->acl->userID;
  131. if($userId) {
  132. $data = array();
  133. $permissions = "";
  134. $roles = "";
  135. $userInfo = array();
  136. # get User Info
  137. $userInfo = $this->acl->getUser($userId);
  138. $userInfo = get_object_vars($userInfo[0]);
  139. $roles = $this->acl->userRoles[0];
  140. $currentPermission = $this->acl->getUserPerms($userId);
  141. $currentPermission = isset($currentPermission[0]['id'])?$currentPermission[0]['id']:0;
  142. $data['user'] = $userInfo;
  143. $data['roles'] = $roles;
  144. $data['permissions'] = $currentPermission;
  145. $this->load->view('members/myAccount.zpt', $data);
  146. }
  147. }
  148. public function docreate(){
  149. $res = array();
  150. $res['status'] = 0;
  151. $res['message'] = "";
  152. $data = array();
  153. $message = "";
  154. $post = $this->input->post('data');
  155. $param = json_decode($post);
  156. foreach ($param as $item){
  157. $data[$item->name] = $item->value;
  158. }
  159. /*
  160. * Parameter check
  161. * 1. Does username already exists?
  162. * 2. Does email already in use?
  163. * 3. Password matched?
  164. */
  165. $code = 0;
  166. $c_user = 1;
  167. $c_pass = 3;
  168. $c_email = 5;
  169. // Does username exists?
  170. if ($this->ion_auth->username_check($data['username']))
  171. $code = $c_user;
  172. // Password matched?
  173. if (strcmp($data['password'], $data['check']) != 0)
  174. $code = $code?$code:$c_pass;
  175. // Does email already in use?
  176. if ($this->ion_auth->email_check($data['email']))
  177. $code = $code?$code:$c_email;
  178. $code = trim($code, '|');
  179. $response = array();
  180. if ($code == 0){
  181. $response['status'] = 'ACK';
  182. // You've come this far. Congratulations! Create the account now.
  183. $username = $data['username'];
  184. $password = $data['password'];
  185. $email = $data['email'];
  186. $member_data = array(
  187. 'first_name' => $data['first_name'],
  188. 'last_name' => $data['last_name']
  189. );
  190. $id = $this->ion_auth->register($username, $password, $email, $member_data);
  191. $this->acl->assignUserRole($id, $data['role']);
  192. $this->acl->assignUserPermission($id, $data['permission']);
  193. $res['status'] = 1;
  194. $res['message'] = "You have successfully created a role.";
  195. }
  196. else {
  197. $response['status'] = 'NACK';
  198. $response['code'] = $code;
  199. $res['status'] = 0;
  200. switch ($code){
  201. case 1:
  202. $message = "Sorry, username already exists!";
  203. break;
  204. case 3:
  205. $message = "The password and confirm password do not match!";
  206. break;
  207. case 5:
  208. $message = "Email address already in use!";
  209. }
  210. $res['message'] = $message;
  211. }
  212. print_r(json_encode($res));
  213. }
  214. public function doedit(){
  215. $post = $this->input->post('data');
  216. $param = json_decode($post);
  217. $res = array();
  218. $res['status'] = 0;
  219. $data = array();
  220. foreach ($param as $item){
  221. $data[$item->name] = $item->value;
  222. }
  223. /*
  224. * Parameter check
  225. * 1. Does username already exists?
  226. * 2. Does email already in use?
  227. * 3. Password matched?
  228. */
  229. $code = 0;
  230. $c_user = 1;
  231. $c_pass = 3;
  232. $c_email = 5;
  233. # get User Info
  234. $userInfo = $this->acl->getUser($data['userid']);
  235. $userInfo = get_object_vars($userInfo[0]);
  236. if($userInfo['username']!=$data['username']){
  237. // Does username exists?
  238. if ($this->ion_auth->username_check($data['username']))
  239. $code = $c_user;
  240. }
  241. // Password matched?
  242. if (strcmp($data['password'], $data['check']) != 0)
  243. $code = $code?$code:$c_pass;
  244. if($userInfo['email']!=$data['email']){
  245. // Does email already in use?
  246. if ($this->ion_auth->email_check($data['email']))
  247. $code = $code?$code:$c_email;
  248. }
  249. if ($code == 0){
  250. // You've come this far. Congratulations! Create the account now.
  251. $username = $data['username'];
  252. $password = $data['password'];
  253. $email = $data['email'];
  254. $newData = array(
  255. 'first_name' => $data['first_name'],
  256. 'last_name' => $data['last_name'],
  257. 'username' => $data['username'],
  258. 'password' => $data['password'],
  259. 'email' => $data['email']
  260. );
  261. $this->ion_auth->updateUser($data['userid'], $newData);
  262. $this->acl->assignUserRole($data['userid'], $data['role']);
  263. $this->acl->assignUserPermission($data['userid'], $data['permission']);
  264. $res['status'] = 1;
  265. $res['message'] = "Changes have been successfully saved!.";
  266. }
  267. else {
  268. $res['status'] = 0;
  269. switch ($code){
  270. case 1:
  271. $message = "Sorry, username already exists! " . $data['userid'];
  272. break;
  273. case 3:
  274. $message = "The password and confirm password do not match!";
  275. break;
  276. case 5:
  277. $message = "Email address already in use!";
  278. }
  279. $res['message'] = $message;
  280. }
  281. print_r(json_encode($res));
  282. }
  283. public function deleteSelected(){
  284. $res = array();
  285. $res['status'] = 0;
  286. $res['message'] = "";
  287. if(isset($_POST['id']) && !empty($_POST['id'])){
  288. $id = explode(",", $_POST['id']);
  289. for($i=0; $i<count($id); $i++){
  290. $this->acl->removeUserPermission($id[$i]);
  291. $this->ion_auth->delete_user($id[$i]);
  292. }
  293. $res['status'] = 1;
  294. }
  295. print_r(json_encode($res));
  296. }
  297. }