farhantock
9 months ago
8 changed files with 429 additions and 109 deletions
@ -1,103 +1,174 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
namespace App\Http\Controllers; |
namespace App\Http\Controllers; |
||||||
|
|
||||||
use Illuminate\Support\Facades\Auth; |
use App\Models\Role; |
||||||
use Illuminate\Http\Request; |
use App\Models\User; |
||||||
use Illuminate\Support\Facades\Hash; |
use App\Models\Company; |
||||||
|
|
||||||
use App\Models\User; |
use Illuminate\Support\Str; |
||||||
use App\Models\Role; |
use Illuminate\Http\Request; |
||||||
use App\Models\Company; |
use Illuminate\Http\JsonResponse; |
||||||
use Illuminate\Http\JsonResponse; |
use Illuminate\Support\Facades\Auth; |
||||||
use Illuminate\Support\Facades\Password; |
use Illuminate\Support\Facades\Hash; |
||||||
use Illuminate\Validation\ValidationException; |
use Illuminate\Support\Facades\Password; |
||||||
|
use Illuminate\Validation\ValidationException; |
||||||
|
|
||||||
class AuthController extends Controller |
const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php'; |
||||||
{ |
class AuthController extends Controller |
||||||
public function __construct() |
{ |
||||||
{ |
public function __construct() |
||||||
$this->middleware('auth:api', ['except' => ['login']]); |
{ |
||||||
} |
$this->middleware('auth:api', ['except' => ['login','sendEmail']]); |
||||||
|
} |
||||||
public function login(Request $request) |
|
||||||
{ |
public function login(Request $request) |
||||||
$username = $request->username; |
{ |
||||||
$password = $request->password; |
$username = $request->username; |
||||||
$remember = $request->remember; |
$password = $request->password; |
||||||
$is_mobile = $request->is_mobile; |
$remember = $request->remember; |
||||||
|
$is_mobile = $request->is_mobile; |
||||||
if (empty($username) || empty($password)) |
|
||||||
return response()->json(['status' => 'error', 'message' => 'You must fill all the fields'], 400); |
if (empty($username) || empty($password)) |
||||||
|
return response()->json(['status' => 'error', 'message' => 'You must fill all the fields'], 400); |
||||||
$usernameCheck = false; |
|
||||||
$passwordCheck = false; |
$usernameCheck = false; |
||||||
|
$passwordCheck = false; |
||||||
if (User::where('username', $username)->exists()) |
|
||||||
$usernameCheck = true; |
if (User::where('username', $username)->exists()) |
||||||
|
$usernameCheck = true; |
||||||
if (User::where('password', md5($password))->exists()) |
|
||||||
$passwordCheck = true; |
if (User::where('password', md5($password))->exists()) |
||||||
|
$passwordCheck = true; |
||||||
if ($usernameCheck & $passwordCheck) { |
|
||||||
$user = User::where('username', $username)->where('password', md5($password))->first(); |
if ($usernameCheck & $passwordCheck) { |
||||||
if ($is_mobile) { |
$user = User::where('username', $username)->where('password', md5($password))->first(); |
||||||
$fcm_token = $request->fcm_token; |
if ($is_mobile) { |
||||||
|
$fcm_token = $request->fcm_token; |
||||||
if (!$fcm_token || $fcm_token == "") |
|
||||||
return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400); |
if (!$fcm_token || $fcm_token == "") |
||||||
|
return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400); |
||||||
$dataUpdateFcm = array( |
|
||||||
"fcm_token" => $fcm_token |
$dataUpdateFcm = array( |
||||||
); |
"fcm_token" => $fcm_token |
||||||
|
); |
||||||
$hr = User::find($user->id); |
|
||||||
|
$hr = User::find($user->id); |
||||||
if ($hr) |
|
||||||
$hr->update($dataUpdateFcm); |
if ($hr) |
||||||
} |
$hr->update($dataUpdateFcm); |
||||||
|
} |
||||||
$dataRole = Role::find($user->role_id); |
|
||||||
$dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id); |
$dataRole = Role::find($user->role_id); |
||||||
$configApp = Company::where('id', $user->company_id)->first(); |
$dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id); |
||||||
|
$configApp = Company::where('id', $user->company_id)->first(); |
||||||
if ($configApp) |
if ($configApp) { |
||||||
$user->configApp = $configApp; |
$logoLogin = json_decode($configApp->logo_login, true); |
||||||
|
$favicon = json_decode($configApp->favicon_image, true); |
||||||
if ($dataRole) |
$logoHeader = json_decode($configApp->logo_header, true); |
||||||
$user->role = $dataRole; |
$configApp->logo_login = $logoLogin; |
||||||
|
$configApp->favicon_image = $favicon; |
||||||
if ($dataHierarchy) |
$configApp->logo_header = $logoHeader; |
||||||
$user->hierarchy = $dataHierarchy; |
} |
||||||
|
|
||||||
if (!$token = Auth::login($user)) |
if ($configApp) |
||||||
return response()->json(['error' => 'Unauthorized'], 401); |
$user->configApp = $configApp; |
||||||
|
|
||||||
$ttl = 60; |
if ($dataRole) |
||||||
if ($remember) |
$user->role = $dataRole; |
||||||
$ttl = 10080; |
|
||||||
|
if ($dataHierarchy) |
||||||
// todo : change existing md5 hashed function to laravel's originally bcrypt |
$user->hierarchy = $dataHierarchy; |
||||||
/* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */ |
|
||||||
/* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */ |
if (!$token = Auth::login($user)) |
||||||
|
return response()->json(['error' => 'Unauthorized'], 401); |
||||||
return response()->json([ |
|
||||||
'code' => 200, |
$ttl = 60; |
||||||
'data' => array( |
if ($remember) |
||||||
'data_user' => $user, |
$ttl = 10080; |
||||||
'access_token' => $token, |
|
||||||
'token_type' => 'bearer', |
// todo : change existing md5 hashed function to laravel's originally bcrypt |
||||||
'expires_in' => auth()->factory()->getTTL() * $ttl, |
/* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */ |
||||||
), |
/* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */ |
||||||
]); |
|
||||||
} else { |
return response()->json([ |
||||||
if (!$usernameCheck && !$passwordCheck) |
'code' => 200, |
||||||
return response()->json(['code' => 201, 'message' => "username and password doesn't match"], 201); |
'data' => array( |
||||||
if (!$passwordCheck) |
'data_user' => $user, |
||||||
return response()->json(['code' => 201, 'message' => "password doesn't match"], 201); |
'access_token' => $token, |
||||||
if (!$usernameCheck) |
'token_type' => 'bearer', |
||||||
return response()->json(['code' => 201, 'message' => "username doesn't match"], 201); |
'expires_in' => auth()->factory()->getTTL() * $ttl, |
||||||
} |
), |
||||||
} |
]); |
||||||
} |
} else { |
||||||
|
if (!$usernameCheck && !$passwordCheck) |
||||||
|
return response()->json(['code' => 201, 'message' => "username and password doesn't match"], 201); |
||||||
|
if (!$passwordCheck) |
||||||
|
return response()->json(['code' => 201, 'message' => "password doesn't match"], 201); |
||||||
|
if (!$usernameCheck) |
||||||
|
return response()->json(['code' => 201, 'message' => "username doesn't match"], 201); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public function sendEmail(Request $request) |
||||||
|
{ |
||||||
|
$hashed = Str::random(15); |
||||||
|
$email = $request->email; |
||||||
|
$user = User::select('email', 'name', 'username')->where('email', $email)->first(); |
||||||
|
|
||||||
|
if (!$user || empty($email)) { |
||||||
|
return response()->json(['status' => 'error', 'message' => 'e-mail not found '], 400); |
||||||
|
} else { |
||||||
|
$this->reqHttpCurl($email, $hashed, $user->username, $user->name); |
||||||
|
if (User::where('email', $email)->update(['password' => md5($hashed)])) |
||||||
|
return response()->json(['status' => 'success', 'code' => 200, 'message' => 'Password already sent to mail'], 200); |
||||||
|
|
||||||
|
return response()->json(['status' => 'error', 'code' => 400, 'message' => 'e-mail not found '], 400); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private function reqHttpCurl($email, $password, $username, $name) |
||||||
|
{ |
||||||
|
$postData = [ |
||||||
|
"to" => $email, |
||||||
|
"username" => $name, |
||||||
|
"username" => $username, |
||||||
|
"password" => $password, |
||||||
|
"from" => "app.integrasia@integrasiautama.com", |
||||||
|
"alias_from" => "OSPRO", |
||||||
|
"subject" => "Registration OSPRO", |
||||||
|
"body" => "registration-ospro" |
||||||
|
]; |
||||||
|
|
||||||
|
$curl = curl_init(); |
||||||
|
|
||||||
|
curl_setopt_array($curl, array( |
||||||
|
CURLOPT_URL => URL_EMAIL, // your preferred url |
||||||
|
CURLOPT_RETURNTRANSFER => true, |
||||||
|
CURLOPT_ENCODING => "", |
||||||
|
CURLOPT_MAXREDIRS => 10, |
||||||
|
CURLOPT_TIMEOUT => 30000, |
||||||
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||||||
|
CURLOPT_CUSTOMREQUEST => "POST", |
||||||
|
CURLOPT_POSTFIELDS => json_encode($postData), |
||||||
|
CURLOPT_HTTPHEADER => array( |
||||||
|
// Set here requred headers |
||||||
|
"accept: */*", |
||||||
|
"accept-language: en-US,en;q=0.8", |
||||||
|
"content-type: application/json", |
||||||
|
), |
||||||
|
)); |
||||||
|
|
||||||
|
$response = curl_exec($curl); |
||||||
|
$err = curl_error($curl); |
||||||
|
|
||||||
|
curl_close($curl); |
||||||
|
|
||||||
|
if ($err) { |
||||||
|
echo "cURL Error #:" . $err; |
||||||
|
} else { |
||||||
|
print_r(json_decode($response)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
@ -0,0 +1,116 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace App\Http\Controllers; |
||||||
|
|
||||||
|
use App\Models\Demo; |
||||||
|
use Illuminate\Http\Request; |
||||||
|
|
||||||
|
class DemoController extends Controller |
||||||
|
{ |
||||||
|
public function add(Request $request) |
||||||
|
{ |
||||||
|
$this->validate($request, [ |
||||||
|
'name' => 'required', |
||||||
|
'role' => 'required', |
||||||
|
'email' => 'required', |
||||||
|
'number_phone' => 'required', |
||||||
|
'status' => 'required', |
||||||
|
'message' => 'required' |
||||||
|
]); |
||||||
|
|
||||||
|
$data = $request->all(); |
||||||
|
|
||||||
|
$data['created_by'] = '@System'; |
||||||
|
$data['status'] = 'New Request'; |
||||||
|
|
||||||
|
$result = Demo::create($data); |
||||||
|
|
||||||
|
if ($result) { |
||||||
|
return response()->json(['status' => 'success', 'data' => $result, 'message' => 'Request Demo successfully!', 'code' => 200], 200); |
||||||
|
} else { |
||||||
|
return response()->json(['status' => 'failed', 'message' => 'Request Demo failed!', 'code' => 400], 400); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function edit($id) |
||||||
|
{ |
||||||
|
if (!$id || (int) $id < 0 || $id == "") { |
||||||
|
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); |
||||||
|
die(); |
||||||
|
} |
||||||
|
|
||||||
|
$result = Demo::find($id); |
||||||
|
|
||||||
|
if ($result) { |
||||||
|
return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200); |
||||||
|
} else { |
||||||
|
return response()->json(['status' => 'failed', 'message' => 'failed get data Demo, please try again later!', 'code' => 400], 400); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function update(Request $request, $id) |
||||||
|
{ |
||||||
|
if (!$id || (int) $id < 0 || $id == "") { |
||||||
|
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); |
||||||
|
} |
||||||
|
|
||||||
|
$data = Demo::find($id); |
||||||
|
|
||||||
|
|
||||||
|
if ($data) { |
||||||
|
$result = $data->update($request->all()); |
||||||
|
} else { |
||||||
|
return response()->json(['status' => 'failed', 'message' => 'data Demo not found!', 'code' => 400], 400); |
||||||
|
die(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
if ($result) { |
||||||
|
return response()->json(['status' => 'success', 'message' => 'data Demo successfully updated!', 'code' => 200], 200); |
||||||
|
} else { |
||||||
|
return response()->json(['status' => 'failed', 'message' => 'data Demo failed updated!', 'code' => 400], 400); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function delete($id) |
||||||
|
{ |
||||||
|
$data = Demo::find($id); |
||||||
|
|
||||||
|
if ($data) { |
||||||
|
$delete = $data->delete(); |
||||||
|
} else { |
||||||
|
return response()->json(['status' => 'failed', 'message' => 'data Demo not found!', 'code' => 400], 400); |
||||||
|
die(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
if ($delete) { |
||||||
|
return response()->json(['status' => 'success', 'message' => 'data Demo successfully deleted!', 'code' => 200], 200); |
||||||
|
} else { |
||||||
|
return response()->json(['status' => 'failed', 'message' => 'data Demo failed deleted!', 'code' => 400], 400); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function search(Request $request) |
||||||
|
{ |
||||||
|
$payload = $request->all(); |
||||||
|
$dataBuilder = $this->setUpPayload($payload, 'm_demo'); |
||||||
|
$builder = $dataBuilder['builder']; |
||||||
|
$countBuilder = $dataBuilder['count']; |
||||||
|
$dataGet = $builder->get(); |
||||||
|
$totalRecord = $countBuilder->count(); |
||||||
|
return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200); |
||||||
|
} |
||||||
|
|
||||||
|
public function list() |
||||||
|
{ |
||||||
|
$data = Demo::all(); |
||||||
|
$countData = $data->count(); |
||||||
|
|
||||||
|
if ($data) { |
||||||
|
return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200); |
||||||
|
} else { |
||||||
|
return response()->json(['status' => 'failed', 'message' => 'failed get list Demo, please try again later!', 'code' => 400], 400); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,27 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace App\Models; |
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model; |
||||||
|
|
||||||
|
class Demo extends Model |
||||||
|
{ |
||||||
|
protected $table = 'm_demo'; |
||||||
|
|
||||||
|
const CREATED_AT = 'created_at'; |
||||||
|
const UPDATED_AT = 'updated_at'; |
||||||
|
|
||||||
|
protected $fillable = [ |
||||||
|
'name', |
||||||
|
'email', |
||||||
|
'role', |
||||||
|
'number_phone', |
||||||
|
'status', |
||||||
|
'message', |
||||||
|
'created_at', |
||||||
|
'created_by', |
||||||
|
'updated_at', |
||||||
|
'updated_by', |
||||||
|
'company_id' |
||||||
|
]; |
||||||
|
} |
Loading…
Reference in new issue