Dashboard sipadu mbip
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LoginController.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests\RegisterRequest;
  5. use App\Http\Controllers\Api\BaseController;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Str;
  8. use Hash;
  9. use Carbon\Carbon;
  10. use Crypt;
  11. use File;
  12. use Mail;
  13. use App\Model\Staff;
  14. use App\Model\StaffDetail;
  15. use App\Model\Module\Department;
  16. use App\Model\Module\DeedLaw;
  17. use App\Model\Module\Faulty;
  18. use App\Model\User;
  19. use App\Model\UserDetail;
  20. use App\Model\Module\Compound;
  21. use App\Model\Module\CodeMukim;
  22. use App\Jobs\StoreCompound;
  23. use App\Jobs\StoreNotice;
  24. class LoginController extends BaseController
  25. {
  26. /**
  27. * Create a login list controller.
  28. *
  29. * @return json
  30. */
  31. public function requestStaffLogin(Request $request) {
  32. $user = ''; $data = array();
  33. $rememberMe = false;
  34. $user = Staff::with(['StaffDetail' => function($q){
  35. $q->select('_id','full_name','identity','mobile','address','gred','no_badan','roles_access','profile_img');
  36. }])->where('no_badan',$request->no_badan)->where(function($q){
  37. $q->where('roles_access','PenguatKuasa')->orWhere('roles_access','PPenguatKuasa');
  38. })->first();
  39. if (!empty($user)) {
  40. if(Hash::check($request->password, $user->password)){
  41. // Check authorized
  42. do {
  43. $user->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
  44. $user->last_login_ip = $request->getClientIp();
  45. $user->api_token = '';
  46. $user->authorized = false;
  47. $user->save();
  48. }while($user->authorized);
  49. $token_id = '';
  50. do {
  51. $token_id = Str::random(32);
  52. } while (Staff::where("token_id", "=", $token_id)->first() instanceof Staff);
  53. $user->api_token = $token_id;
  54. $user->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
  55. $user->last_login_ip = $request->getClientIp();
  56. $user->authorized = true;
  57. $user->token_firebase = '';
  58. $user->save();
  59. //get all staff's related department names & ids
  60. $staff = StaffDetail::with('Department')->where('_id', $user->_id)->first();
  61. $jabatan = array();
  62. if(!empty($staff->Department))
  63. {
  64. foreach ($staff->Department as $key => $d)
  65. {
  66. array_push($jabatan , array(
  67. 'jbnama' => $d->jnama,
  68. 'jbkod' => $d->_id,
  69. ));
  70. }
  71. }
  72. //get all notice sections & deedlaws
  73. $notis = array();
  74. // $sec_faulty = Faulty::with('DeedLaw', 'Department')->whereIn('itkod', ['33', '9', '8', '10', '14', '36'])->get();
  75. // if(!empty($sec_faulty))
  76. // {
  77. // foreach ($sec_faulty as $sf)
  78. // {
  79. // array_push($notis, array(
  80. // 'k_id' => $sf->_id,
  81. // 'k_nama' => '['.$sf->sketr.'] '.$sf->nama,
  82. // 'a_id' => $sf->DeedLaw->_id,
  83. // 'a_nama' => $sf->DeedLaw->nama,
  84. // ));
  85. // }
  86. // }
  87. array_push($data, array(
  88. 'User' => $user->StaffDetail,
  89. 'token' => $user->api_token,
  90. 'jawatan' => $user->StaffDetail->gred,
  91. 'token_firebase' => $user->token_firebase,
  92. 'jabatan' => $jabatan,
  93. 'notis' => $notis,
  94. ));
  95. return $this->sendResponse($data, 'Berjaya log masuk');
  96. }else {
  97. return $this->sendError('Gagal', 'Emel atau kata-laluan tidak tepat');
  98. }
  99. }else {
  100. return $this->sendError('Gagal', 'Rekod tidak dijumpai');
  101. }
  102. }
  103. public function requestStaffLogout(Request $request){
  104. $loginUser = Staff::where('api_token', $request->api_token)->first();
  105. if(!empty($loginUser)){
  106. $loginUser->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
  107. $loginUser->last_login_ip = $request->getClientIp();
  108. $loginUser->api_token = '';
  109. $loginUser->authorized = false;
  110. $loginUser->save();
  111. return $this->sendResponse('Berjaya', 'Berjaya log keluar');
  112. }else {
  113. return $this->sendError('Gagal', 'Maaf, penguatkuasa tidak dapat log keluar');
  114. }
  115. }
  116. public function checkDeviceToken(Request $request){
  117. $loginUser = Staff::where('api_token', $request->api_token)->first();
  118. if(!empty($loginUser)){
  119. return $this->sendResponse($loginUser->api_token, 'Successfully get detail');
  120. }else {
  121. return $this->sendError('Failed', 'User Not Found');
  122. }
  123. }
  124. // public function resetPassword(Request $request)
  125. // {
  126. // $data = array();
  127. // $staff = Staff::where('api_token', $request->api_token)->first();
  128. // if(!empty($staff))
  129. // {
  130. // $ic_num = StaffDetail::with('Staff')->where('identity', $request->ic_number)->first();
  131. // $id = $ic_num->_id;
  132. // if(!empty($id))
  133. // {
  134. // $staffID = Staff::where('_id', $id)->first();
  135. // $staffID->password = $request->new_password;
  136. // }
  137. // else
  138. // {
  139. // return $this->sendError('Gagal', 'Identiti tidak dijumpai');
  140. // }
  141. // }
  142. // else
  143. // {
  144. // return $this->sendError('Gagal', 'penguatkuasa tidak wujud');
  145. // }
  146. // }
  147. public function testing(Request $request)
  148. {
  149. }
  150. }