|
|
|
@@ -1,160 +1,160 @@ |
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\api;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Requests\RegisterRequest;
|
|
|
|
use App\Http\Controllers\Api\BaseController;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
use Hash;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Crypt;
|
|
|
|
use File;
|
|
|
|
use Mail;
|
|
|
|
|
|
|
|
use App\Model\Staff;
|
|
|
|
use App\Model\StaffDetail;
|
|
|
|
use App\Model\Module\Department;
|
|
|
|
use App\Model\User;
|
|
|
|
use App\Model\UserDetail;
|
|
|
|
use App\Model\Module\Compound;
|
|
|
|
|
|
|
|
use App\Jobs\StoreCompound;
|
|
|
|
use App\Jobs\StoreNotice;
|
|
|
|
|
|
|
|
class LoginController extends BaseController
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a login list controller.
|
|
|
|
*
|
|
|
|
* @return json
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function requestStaffLogin(Request $request) {
|
|
|
|
|
|
|
|
$user = ''; $data = array();
|
|
|
|
|
|
|
|
$rememberMe = false;
|
|
|
|
$user = Staff::with(['StaffDetail' => function($q){
|
|
|
|
$q->select('_id','full_name','identity','mobile','address','gred','no_badan','roles_access','profile_img');
|
|
|
|
}])->where('no_badan',$request->no_badan)->where(function($q){
|
|
|
|
$q->where('roles_access','PenguatKuasa')->orWhere('roles_access','PPenguatKuasa');
|
|
|
|
})->first();
|
|
|
|
if (!empty($user)) {
|
|
|
|
if(Hash::check($request->password, $user->password)){
|
|
|
|
|
|
|
|
// Check authorized
|
|
|
|
do {
|
|
|
|
$user->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
|
|
|
|
$user->last_login_ip = $request->getClientIp();
|
|
|
|
$user->api_token = '';
|
|
|
|
$user->authorized = false;
|
|
|
|
$user->save();
|
|
|
|
}while($user->authorized);
|
|
|
|
|
|
|
|
$token_id = '';
|
|
|
|
do {
|
|
|
|
$token_id = str_random(32);
|
|
|
|
} while (Staff::where("token_id", "=", $token_id)->first() instanceof Staff);
|
|
|
|
|
|
|
|
$user->api_token = $token_id;
|
|
|
|
$user->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
|
|
|
|
$user->last_login_ip = $request->getClientIp();
|
|
|
|
$user->authorized = true;
|
|
|
|
$user->token_firebase = '';
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$staff = StaffDetail::with('Department')->where('_id', $user->_id)->first();
|
|
|
|
if(!empty($staff->Department)){
|
|
|
|
foreach ($staff->Department as $key => $d) {
|
|
|
|
$tempJ[] = $d->_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$jabatan = implode( ", ", $tempJ );
|
|
|
|
}
|
|
|
|
|
|
|
|
array_push($data, array(
|
|
|
|
'User' => $user->StaffDetail,
|
|
|
|
'token' => $user->api_token,
|
|
|
|
'jawatan' => $user->StaffDetail->gred,
|
|
|
|
'token_firebase' => $user->token_firebase,
|
|
|
|
'jbkod' => $jabatan
|
|
|
|
));
|
|
|
|
|
|
|
|
return $this->sendResponse($data, 'Berjaya log masuk');
|
|
|
|
|
|
|
|
}else {
|
|
|
|
return $this->sendError('Gagal', 'Emel atau kata-laluan tidak tepat');
|
|
|
|
}
|
|
|
|
|
|
|
|
}else {
|
|
|
|
return $this->sendError('Gagal', 'Rekod tidak dijumpai');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requestStaffLogout(Request $request){
|
|
|
|
|
|
|
|
$loginUser = Staff::where('api_token', $request->api_token)->first();
|
|
|
|
|
|
|
|
if(!empty($loginUser)){
|
|
|
|
|
|
|
|
$loginUser->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
|
|
|
|
$loginUser->last_login_ip = $request->getClientIp();
|
|
|
|
$loginUser->api_token = '';
|
|
|
|
$loginUser->authorized = false;
|
|
|
|
$loginUser->save();
|
|
|
|
|
|
|
|
return $this->sendResponse('Berjaya', 'Berjaya log keluar');
|
|
|
|
}else {
|
|
|
|
return $this->sendError('Gagal', 'Maaf, penguatkuasa tidak dapat log keluar');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkDeviceToken(Request $request){
|
|
|
|
|
|
|
|
$loginUser = Staff::where('api_token', $request->api_token)->first();
|
|
|
|
|
|
|
|
if(!empty($loginUser)){
|
|
|
|
return $this->sendResponse($loginUser->api_token, 'Successfully get detail');
|
|
|
|
}else {
|
|
|
|
return $this->sendError('Failed', 'User Not Found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// public function resetPassword(Request $request)
|
|
|
|
// {
|
|
|
|
// $data = array();
|
|
|
|
// $staff = Staff::where('api_token', $request->api_token)->first();
|
|
|
|
// if(!empty($staff))
|
|
|
|
// {
|
|
|
|
// $ic_num = StaffDetail::with('Staff')->where('identity', $request->ic_number)->first();
|
|
|
|
// $id = $ic_num->_id;
|
|
|
|
// if(!empty($id))
|
|
|
|
// {
|
|
|
|
// $staffID = Staff::where('_id', $id)->first();
|
|
|
|
// $staffID->password = $request->new_password;
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// return $this->sendError('Gagal', 'Identiti tidak dijumpai');
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// return $this->sendError('Gagal', 'penguatkuasa tidak wujud');
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
public function testing(Request $request)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
$test = '02399'; //filter_var('000099', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
(int)$test += 1;
|
|
|
|
dd($test);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
|
|
|
<?php |
|
|
|
|
|
|
|
namespace App\Http\Controllers\api; |
|
|
|
|
|
|
|
use Illuminate\Http\Request; |
|
|
|
use App\Http\Requests\RegisterRequest; |
|
|
|
use App\Http\Controllers\Api\BaseController; |
|
|
|
use Illuminate\Support\Facades\Auth; |
|
|
|
|
|
|
|
use Hash; |
|
|
|
use Carbon\Carbon; |
|
|
|
use Crypt; |
|
|
|
use File; |
|
|
|
use Mail; |
|
|
|
|
|
|
|
use App\Model\Staff; |
|
|
|
use App\Model\StaffDetail; |
|
|
|
use App\Model\Module\Department; |
|
|
|
use App\Model\User; |
|
|
|
use App\Model\UserDetail; |
|
|
|
use App\Model\Module\Compound; |
|
|
|
|
|
|
|
use App\Jobs\StoreCompound; |
|
|
|
use App\Jobs\StoreNotice; |
|
|
|
|
|
|
|
class LoginController extends BaseController |
|
|
|
{ |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a login list controller. |
|
|
|
* |
|
|
|
* @return json |
|
|
|
*/ |
|
|
|
|
|
|
|
public function requestStaffLogin(Request $request) { |
|
|
|
|
|
|
|
$user = ''; $data = array(); |
|
|
|
|
|
|
|
$rememberMe = false; |
|
|
|
$user = Staff::with(['StaffDetail' => function($q){ |
|
|
|
$q->select('_id','full_name','identity','mobile','address','gred','no_badan','roles_access','profile_img'); |
|
|
|
}])->where('no_badan',$request->no_badan)->where(function($q){ |
|
|
|
$q->where('roles_access','PenguatKuasa')->orWhere('roles_access','PPenguatKuasa'); |
|
|
|
})->first(); |
|
|
|
if (!empty($user)) { |
|
|
|
if(Hash::check($request->password, $user->password)){ |
|
|
|
|
|
|
|
// Check authorized |
|
|
|
do { |
|
|
|
$user->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString(); |
|
|
|
$user->last_login_ip = $request->getClientIp(); |
|
|
|
$user->api_token = ''; |
|
|
|
$user->authorized = false; |
|
|
|
$user->save(); |
|
|
|
}while($user->authorized); |
|
|
|
|
|
|
|
$token_id = ''; |
|
|
|
do { |
|
|
|
$token_id = str_random(32); |
|
|
|
} while (Staff::where("token_id", "=", $token_id)->first() instanceof Staff); |
|
|
|
|
|
|
|
$user->api_token = $token_id; |
|
|
|
$user->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString(); |
|
|
|
$user->last_login_ip = $request->getClientIp(); |
|
|
|
$user->authorized = true; |
|
|
|
$user->token_firebase = ''; |
|
|
|
$user->save(); |
|
|
|
|
|
|
|
$staff = StaffDetail::with('Department')->where('_id', $user->_id)->first(); |
|
|
|
if(!empty($staff->Department)){ |
|
|
|
foreach ($staff->Department as $key => $d) { |
|
|
|
$tempJ[] = $d->_id; |
|
|
|
} |
|
|
|
|
|
|
|
$jabatan = implode( ", ", $tempJ ); |
|
|
|
} |
|
|
|
|
|
|
|
array_push($data, array( |
|
|
|
'User' => $user->StaffDetail, |
|
|
|
'token' => $user->api_token, |
|
|
|
'jawatan' => $user->StaffDetail->gred, |
|
|
|
'token_firebase' => $user->token_firebase, |
|
|
|
'jbkod' => $jabatan |
|
|
|
)); |
|
|
|
|
|
|
|
return $this->sendResponse($data, 'Berjaya log masuk'); |
|
|
|
|
|
|
|
}else { |
|
|
|
return $this->sendError('Gagal', 'Emel atau kata-laluan tidak tepat'); |
|
|
|
} |
|
|
|
|
|
|
|
}else { |
|
|
|
return $this->sendError('Gagal', 'Rekod tidak dijumpai'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function requestStaffLogout(Request $request){ |
|
|
|
|
|
|
|
$loginUser = Staff::where('api_token', $request->api_token)->first(); |
|
|
|
|
|
|
|
if(!empty($loginUser)){ |
|
|
|
|
|
|
|
$loginUser->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString(); |
|
|
|
$loginUser->last_login_ip = $request->getClientIp(); |
|
|
|
$loginUser->api_token = ''; |
|
|
|
$loginUser->authorized = false; |
|
|
|
$loginUser->save(); |
|
|
|
|
|
|
|
return $this->sendResponse('Berjaya', 'Berjaya log keluar'); |
|
|
|
}else { |
|
|
|
return $this->sendError('Gagal', 'Maaf, penguatkuasa tidak dapat log keluar'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function checkDeviceToken(Request $request){ |
|
|
|
|
|
|
|
$loginUser = Staff::where('api_token', $request->api_token)->first(); |
|
|
|
|
|
|
|
if(!empty($loginUser)){ |
|
|
|
return $this->sendResponse($loginUser->api_token, 'Successfully get detail'); |
|
|
|
}else { |
|
|
|
return $this->sendError('Failed', 'User Not Found'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// public function resetPassword(Request $request) |
|
|
|
// { |
|
|
|
// $data = array(); |
|
|
|
// $staff = Staff::where('api_token', $request->api_token)->first(); |
|
|
|
// if(!empty($staff)) |
|
|
|
// { |
|
|
|
// $ic_num = StaffDetail::with('Staff')->where('identity', $request->ic_number)->first(); |
|
|
|
// $id = $ic_num->_id; |
|
|
|
// if(!empty($id)) |
|
|
|
// { |
|
|
|
// $staffID = Staff::where('_id', $id)->first(); |
|
|
|
// $staffID->password = $request->new_password; |
|
|
|
// } |
|
|
|
// else |
|
|
|
// { |
|
|
|
// return $this->sendError('Gagal', 'Identiti tidak dijumpai'); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// else |
|
|
|
// { |
|
|
|
// return $this->sendError('Gagal', 'penguatkuasa tidak wujud'); |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
public function testing(Request $request) |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
$test = '02399'; //filter_var('000099', FILTER_SANITIZE_NUMBER_INT); |
|
|
|
(int)$test += 1; |
|
|
|
dd($test); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |