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

/manager/application/controllers/usermanagement.php

https://bitbucket.org/jerwinse/iagh-cms
PHP | 311 lines | 284 code | 24 blank | 3 comment | 43 complexity | 2e23c176fff2614b011492b614da80c1 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Usermanagement extends CI_Controller {
  3. private $workspaceId;
  4. private $userPermission;
  5. private $permission;
  6. public function __construct(){
  7. parent::__construct();
  8. $this->tal->title = $this->config->item('title');
  9. $this->tal->base_url = substr(base_url(), 0, -1);
  10. $this->user = $this->ion_auth->user()->result();
  11. if(!$this->ion_auth->user()->result()){
  12. exit;
  13. }
  14. $this->user[0]->fullname = $this->user[0]->first_name . ' ' . $this->user[0]->last_name;
  15. $config = array('userID' => $this->user[0]->id);
  16. $this->load->library('acl', $config);
  17. $this->load->library('curl');
  18. $this->load->library('upload');
  19. $this->load->helper(array('form', 'url', 'file'));
  20. if (!$this->acl->userRoles) {
  21. exit;
  22. }
  23. else{
  24. $this->workspaceId = $this->acl->userRoles[0];
  25. # get role permission
  26. $this->permission = $this->acl->getRolePerms($this->acl->userRoles[0]);
  27. $userPermission = $this->acl->getUserPerms($this->acl->userID);
  28. $this->userPermission = $userPermission[0]['id'];
  29. }
  30. }
  31. public function papiCall($method="GET", $function="", $options=array()){
  32. $papi = array();
  33. $papi['method'] = $method;
  34. $papi['papiCall'] = $function;
  35. $papi['data'] = $options;
  36. $this->curl->PAPIConfig($papi);
  37. $res = $this->curl->execute();
  38. return json_decode($res['message']);
  39. }
  40. public function listUsers(){
  41. $data['users'] = array();
  42. $options = array("ID"=>$this->workspaceId);
  43. $apiRes = $this->papiCall("GET", "Workspace.Users.GetAll", $options);
  44. if($apiRes->Status){
  45. $data['users'] = $apiRes->Users;
  46. }
  47. $this->load->view('usermanagement/users.zpt', $data);
  48. }
  49. public function addUser(){
  50. $data['isAddRequest'] = true;
  51. $this->load->view('usermanagement/userForm.zpt', $data);
  52. }
  53. public function editUser(){
  54. $data['isAddRequest'] = false;
  55. if(isset($_GET['id'])){
  56. $id = $_GET['id'];
  57. $options = array("UserID"=>$id, "WorkspaceID"=>$this->workspaceId);
  58. $apiRes = $this->papiCall("GET", "User.Workspace.Access.Check", $options);
  59. if($apiRes->Status){
  60. $data['user'] = $apiRes->User;
  61. $data['userWorkspace'] = $apiRes->UserWorkspace;
  62. }
  63. $this->load->view('usermanagement/userForm.zpt', $data);
  64. }
  65. }
  66. public function saveUser(){
  67. $res['status'] = 0;
  68. $validDomain = false;
  69. if(isset($_POST['data'])){
  70. $params = json_decode($_POST['data']);
  71. $dataParam = array();
  72. foreach($params as $item){
  73. $dataParam[$item->name] = $item->value;
  74. }
  75. $input_ = explode("@", $dataParam['email']);
  76. if(isset($input_[1])){
  77. $domainName = $input_[1];
  78. $apiRes = $this->papiCall("GET", "Workspace.Get", array("ID"=>$this->workspaceId));
  79. if($apiRes->Status){
  80. $domains = (!empty($apiRes->Workspace->Domains)?explode(",", $apiRes->Workspace->Domains):array());
  81. if(in_array($domainName, $domains)){
  82. $validDomain = true;
  83. }
  84. else{
  85. $res['message'] = "Domain is not allowed";
  86. }
  87. }
  88. }
  89. else{
  90. $res['message'] = "Enter valid email address";
  91. }
  92. if($validDomain){
  93. $options = array(
  94. "Email" => $dataParam['email'],
  95. "Timezone" => $dataParam['timezone'],
  96. "TimeMorning" => $dataParam['timemorning'],
  97. "TimeNoon" => $dataParam['timenoon'],
  98. "TimeNight" => $dataParam['timenight'],
  99. "FirstName" => $dataParam['firstname'],
  100. "LastName" => $dataParam['lastname'],
  101. "AboutMe" => $dataParam['aboutme'],
  102. "ImageURL" => $dataParam['imageurl'],
  103. "Birthday" => $dataParam['birthday'],
  104. "Country" => $dataParam['country'],
  105. "Gender" => $dataParam['gender'],
  106. "MiddleName" => $dataParam['middlename']
  107. );
  108. $userWorkspaceOptions = array(
  109. "Title" => $dataParam['title'],
  110. "Position" => $dataParam['position'],
  111. "EmployeeID" => $dataParam['employeeid'],
  112. "DepartmentID" => $dataParam['departmentid'],
  113. "SupervisorID" => $dataParam['supervisorid']
  114. );
  115. # new user
  116. if($dataParam['userId'] == "0"){
  117. $options['Password'] = $dataParam['password'];
  118. $apiRes = $this->papiCall("POST", "User.Create", $options);
  119. if($apiRes->Status){
  120. if($apiRes->User->ID){
  121. $userID = $apiRes->User->ID;
  122. $grantOptions = array("UserID"=>$userID, "WorkspaceID"=>$this->workspaceId);
  123. $apiRes = $this->papiCall("POST", "User.Workspace.Access.Grant", $grantOptions);
  124. if($apiRes->Status){
  125. $userWorkspaceOptions['UserID'] = $userID;
  126. $userWorkspaceOptions['WorkspaceID'] = $this->workspaceId;
  127. $apiRes = $this->papiCall("POST", "User.Workspace.Access.Edit", $userWorkspaceOptions);
  128. if($apiRes->Status){
  129. $res['status'] = 1;
  130. $res['message'] = "User successfully added!";
  131. }
  132. else{
  133. $res['message'] = $apiRes->StatusMessage;
  134. }
  135. }
  136. else{
  137. $res['message'] = $apiRes->StatusMessage;
  138. }
  139. }
  140. }
  141. else{
  142. $res['message'] = $apiRes->StatusMessage;
  143. $res['status'] = 0;
  144. }
  145. }
  146. # edit user
  147. else{
  148. $options['ID'] = $dataParam['userId'];
  149. $apiRes = $this->papiCall("POST", "User.Update", $options);
  150. if($apiRes->Status){
  151. $userWorkspaceOptions['UserID'] = $dataParam['userId'];
  152. $userWorkspaceOptions['WorkspaceID'] = $this->workspaceId;
  153. $apiRes = $this->papiCall("POST", "User.Workspace.Access.Edit", $userWorkspaceOptions);
  154. if($apiRes->Status){
  155. $res['status'] = 1;
  156. $res['message'] = "Details have been successfully saved!";
  157. }
  158. else{
  159. $res['message'] = $apiRes->StatusMessage;
  160. }
  161. }
  162. else{
  163. $res['message'] = $apiRes->StatusMessage;
  164. }
  165. }
  166. }
  167. }
  168. print_r(json_encode($res));
  169. }
  170. public function deleteSelected(){
  171. $res = array();
  172. $res['status'] = 0;
  173. $res['message'] = "";
  174. if(isset($_POST['id']) && !empty($_POST['id'])){
  175. $papiCall = $_POST['papiCall'];
  176. $id =explode(",", $_POST['id']);
  177. for($i=0; $i<count($id); $i++){
  178. $apiRes = $this->papiCall("POST", $papiCall, array("UserID"=>$id[$i], "WorkspaceID"=>$this->workspaceId));
  179. }
  180. $res['status'] = 1;
  181. }
  182. print_r(json_encode($res));
  183. }
  184. public function checkAvailability(){
  185. $email = $_POST['email'];
  186. $res['status'] = 0;
  187. $res['message'] = "";
  188. if(empty($email) || $email == ""){
  189. $res['message'] = "Enter valid email address";
  190. }
  191. else{
  192. $userId = $_POST['userid'];
  193. $input_ = explode("@", $email);
  194. if(isset($input_[1])){
  195. $domainName = $input_[1];
  196. $apiRes = $this->papiCall("GET", "Workspace.Users.GetAll", array("ID"=>$this->workspaceId));
  197. if($apiRes->Status){
  198. $domains = (!empty($apiRes->Workspace->Domains)?explode(",", $apiRes->Workspace->Domains):array());
  199. if(in_array($domainName, $domains)){
  200. $apiRes = $this->papiCall("GET", "User.GetAll", array());
  201. if($apiRes->Status){
  202. $valid = true;
  203. for($i=0; $i<count($apiRes->Users); $i++){
  204. if($email == $apiRes->Users[$i]->Email){
  205. if($userId != $apiRes->Users[$i]->ID){
  206. $valid = false;
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. if($valid){
  213. $res['message'] = "{$email} is available!";
  214. }
  215. else{
  216. $res['message'] = "{$email} is not available!";
  217. }
  218. }
  219. else{
  220. $res['message'] = "Domain is not allowed";
  221. }
  222. }
  223. }
  224. else{
  225. $res['message'] = "Enter valid email address";
  226. }
  227. }
  228. print_r(json_encode($res));
  229. }
  230. public function emailNotification(){
  231. $this->load->view('usermanagement/emailNotification.zpt');
  232. }
  233. public function sendNotification(){
  234. $res['status'] = 0;
  235. $res['message'] = "Failed to send verification code";
  236. $input = json_decode($_POST['data']);
  237. $param = array();
  238. foreach ($input as $item){
  239. $param[$item->name] = $item->value;
  240. }
  241. $email_ = explode("@", $param['email']);
  242. if(isset($email_[1])){
  243. $option = array("ID"=>$this->workspaceId);
  244. $apiRes = $this->papiCall("GET", "Workspace.Users.GetAll", $option);
  245. if($apiRes->Status){
  246. $users = $apiRes->Users;
  247. $emailExists = false;
  248. for($i=0; $i<count($users); $i++){
  249. if($param['email'] == $users[$i]->UserID->Email){
  250. $emailExists = true;
  251. }
  252. }
  253. if($emailExists){
  254. $subject = "Verification Code";
  255. $message = "This is a test message";
  256. $from = "admin@iagh.com";
  257. $to = $param['email'];
  258. $config = array(
  259. 'protocol' => 'mail'
  260. );
  261. $this->load->library('email', $config);
  262. $this->email->from($from);
  263. $this->email->to($to);
  264. $this->email->subject($subject);
  265. $this->email->message($message);
  266. if($this->email->send()){
  267. $res['status'] = 1;
  268. $res['message'] = "Email notification sent";
  269. }
  270. else{
  271. $res['message'] = "Unable to send message";
  272. }
  273. }
  274. else{
  275. $res['message'] = "Email does not exist";
  276. }
  277. }
  278. else{
  279. $res['message'] = $apiRes->StatusMessage;
  280. }
  281. }
  282. else {
  283. $res['message'] = "Enter valid email address";
  284. }
  285. print_r(json_encode($res));
  286. }
  287. }