admin = $admin; $this->user = $user; $this->format = $httpStatusCode; $this->module = "admin"; $this->model = "admin"; $this->password_logs = $password_logs; $this->admin_logs = $admin_logs; $this->systemPreference = $systemPreference; } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { $params = [ 'search' => ($request->has('_search') ? $request->get('_search') : NULL), 'page_size' => ($request->has('page_size') ? $request->get('page_size') : 10), 'page' => ($request->has('page') ? $request->get('page') : 10), 'sorting' => ParameterHelper::prepareSortingParameter($request), 'filter' => [ 'role' => ($request->has('role') ? $request->get('role') : null), 'status' => ($request->has('status') ? $request->get('status') : null) ] ]; $list = $this->admin->listing($params); if(count($list)) { $additionals = $this->format->success("Success",[],false); $data = AdminResource::collection($list)->additional($additionals); return $data->response()->setStatusCode(200); } else { return $this->format->success("No records found",[]); } } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(AdminFormValidation $request) { $id = $this->admin->store($request); if($id) { $this->user->store($request, 1); $this->admin_logs->log($id,self::MODULE,'STORE'); return $this->format->created(); } else { return $this->format->notFound(); } } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($uuid) { $details = $this->admin->getByField(['admin_uuid' => $uuid]); if($details->count()) { $details = $details[0]; // check if there's existing generated password $last_password_log = $this->password_logs->getLastLog($details['admin_id']); if($last_password_log) { $hasExceeded = StringHelper::check_time_exceeded($last_password_log['created_dt'], '+15 minutes'); if($last_password_log['is_generated'] == 1 && $hasExceeded == true) { $details['generated_password'] = $last_password_log['generated_password']; } } $additionals = $this->format->success("Success",[],false); $data = (new AdminResource($details))->additional($additionals); return $data->response()->setStatusCode(200); } else return $this->format->notFound(); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(AdminFormValidation $request, $uuid) { // get old data $old_data = $this->admin->getByField(['admin_uuid' => $uuid]); if($old_data->count()) { if($this->admin->update($request,$uuid)) { $this->user->updateAdmin($request, $old_data[0]['username']); if($request->has('password')) { $currentUser = CurrentUserHelper::get_currentAdmin(); $this->password_logs->store($old_data[0]['admin_id'],$request->get('password'),$currentUser->admin_id, true); } $this->admin_logs->log($old_data[0]['admin_id'],self::MODULE,'UPDATE'); return $this->format->success("ADMIN UPDATED"); } else { return $this->format->badRequest('Something went wrong'); } } return $this->format->notFound(); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($uuid) { $admin_details = $this->admin->getByField(['admin_uuid' => $uuid]); if($admin_details->count()) { $currentUser = CurrentUserHelper::get_currentAdmin(); if($admin_details[0]['admin_id'] != $currentUser->admin_id) { $user_details = $this->user->getUsername($admin_details[0]['username']); $this->admin->delete($uuid); if($this->user->delete($user_details['id']) ){ $this->admin_logs->log($admin_details[0]['admin_id'],self::MODULE,'DELETE'); return $this->format->success("Admin Successfully Deleted"); } } else return $this->format->badRequest('Cannot delete own account'); } else return $this->format->notFound(); } public function batch_delete(Request $request) { $admin_uuid = $request->has('admin_uuid') ? $request->get('admin_uuid') : null; if($admin_uuid) { $admin_details = $this->admin->getDetailsWhereIn('admin_uuid',$admin_uuid,'user'); $currentUser = CurrentUserHelper::get_currentAdmin(); $id = array(); foreach ($admin_details as $key => $value) { if($value['admin_id'] != $currentUser->admin_id) { $id[] = $value['user']['id']; $this->admin_logs->log($value['admin_id'],self::MODULE,'DELETE'); } else return $this->format->badRequest('Cannot delete own acount'); } if($this->admin->delete($admin_uuid) && $this->user->delete($id)) { return $this->format->success("Admin Successfully Deleted"); } else { return $this->format->badRequest('Something went wrong'); } } else { $data['admin_uuid'] = 'admin_uuid is required'; return $this->format->unprocessableEntity("Submit at least one admin",$data); } } public function generate_password(Request $request) { $admin_uuid = ($request->has('admin_uuid') ? $request->get('admin_uuid') : NULL); if($admin_uuid == NULL) { $data['password'] = StringHelper::random_string(8,1); } else { $data['password'] = StringHelper::random_string(8,1); $admin = $this->admin->getByField(['admin_uuid' => $admin_uuid]); $logs = $this->password_logs->getByField(['admin_id' => $admin[0]['admin_id']]); if(count($logs) > 0) { $ctr = 1; foreach ($logs as $key => $value) { if($ctr <= 2) { if($value['password'] == md5($data['password'])) $data['password'] = StringHelper::random_string(8,1); } } } } return $this->format->success("Password Generated",$data); } public function myProfile() { $currentUser = CurrentUserHelper::get_currentAdmin(); $admin = $this->admin->show($currentUser->admin_uuid); $rpreference = $this->systemPreference->getByField('name', 'logo'); $admin->logo = $rpreference[0]->value; if ($admin){ return $this->format->success("SUCCESS",$admin); }else{ return $this->format->notFound(); } } public function changeStatus(Request $request) { $admin_uuid = ($request->has('admin_uuid') ? $request->get('admin_uuid') : NULL); $old_data = $this->admin->getByField(['admin_uuid' => $admin_uuid]); if($old_data->count()) { $currentUser = CurrentUserHelper::get_currentAdmin(); if($old_data[0]['admin_id'] != $currentUser->admin_id) { // $newStatus = $old_data[0]['status'] == 0 ? 1 : 0; $newStatus = StaticContents::admin_status($request->get('status'), true); if($this->admin->changeStatus($newStatus,$admin_uuid)) { if($newStatus == 1) { $this->user->force_logout($old_data[0]['username']); } $this->admin_logs->log($old_data[0]['admin_id'],self::MODULE,'UPDATE','Changed Status to '.($newStatus == 1 ? "inactive" : "active")); return $this->format->success("User Account is ".($newStatus == 1 ? "deactivated" : "activated"),["status" => ($newStatus == 1 ? "inactive" : "active")]); } } else return $this->format->badRequest('Cannot update own acount'); } return $this->format->notFound(); } }