Browse Source

Merge branch 'staging' of https://git.oslog.id/ibnu/generic-ospro-backend into Dev-Farhan

pull/1/head
farhantock 9 months ago
parent
commit
d3adc9b22d
  1. 348
      app/Http/Controllers/AuthController.php
  2. 4
      app/Http/Controllers/DemoController.php
  3. 94
      app/Http/Controllers/HumanResourceController.php
  4. 2
      app/Http/Controllers/RoleController.php
  5. 1
      app/Models/Company.php
  6. 1
      app/Models/HumanResource.php
  7. 8
      routes/web.php

348
app/Http/Controllers/AuthController.php

@ -1,174 +1,174 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Models\Role;
use App\Models\Company;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Password;
use Illuminate\Validation\ValidationException;
const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php';
class AuthController extends Controller
{
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
public function login(Request $request)
{
$username = $request->username;
$password = $request->password;
$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);
$usernameCheck = false;
$passwordCheck = false;
if (User::where('username', $username)->exists())
$usernameCheck = true;
if (User::where('password', md5($password))->exists())
$passwordCheck = true;
if ($usernameCheck & $passwordCheck) {
$user = User::where('username', $username)->where('password', md5($password))->first();
if ($is_mobile) {
$fcm_token = $request->fcm_token;
if (!$fcm_token || $fcm_token == "")
return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400);
$dataUpdateFcm = array(
"fcm_token" => $fcm_token
);
$hr = User::find($user->id);
if ($hr)
$hr->update($dataUpdateFcm);
}
$dataRole = Role::find($user->role_id);
$dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id);
$configApp = Company::where('id', $user->company_id)->first();
if ($configApp) {
$logoLogin = json_decode($configApp->logo_login, true);
$favicon = json_decode($configApp->favicon_image, true);
$logoHeader = json_decode($configApp->logo_header, true);
$configApp->logo_login = $logoLogin;
$configApp->favicon_image = $favicon;
$configApp->logo_header = $logoHeader;
}
if ($configApp)
$user->configApp = $configApp;
if ($dataRole)
$user->role = $dataRole;
if ($dataHierarchy)
$user->hierarchy = $dataHierarchy;
if (!$token = Auth::login($user))
return response()->json(['error' => 'Unauthorized'], 401);
$ttl = 60;
if ($remember)
$ttl = 10080;
// todo : change existing md5 hashed function to laravel's originally bcrypt
/* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */
/* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */
return response()->json([
'code' => 200,
'data' => array(
'data_user' => $user,
'access_token' => $token,
'token_type' => 'bearer',
'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')->where('email', $email)->first();
if (!$user) {
return response()->json(['status' => 'error', 'message' => 'e-mail not found '], 400);
} else {
$this->reqHttpCurl($email, $hashed, $user->username, $user->name);
// $updateUser = User::where('email', $email)->update(['password'=> $hashed]);
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));
}
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Models\Role;
use App\Models\Company;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Password;
use Illuminate\Validation\ValidationException;
const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php';
class AuthController extends Controller
{
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
public function login(Request $request)
{
$username = $request->username;
$password = $request->password;
$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);
$usernameCheck = false;
$passwordCheck = false;
if (User::where('username', $username)->exists())
$usernameCheck = true;
if (User::where('password', md5($password))->exists())
$passwordCheck = true;
if ($usernameCheck & $passwordCheck) {
$user = User::where('username', $username)->where('password', md5($password))->first();
if ($is_mobile) {
$fcm_token = $request->fcm_token;
if (!$fcm_token || $fcm_token == "")
return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400);
$dataUpdateFcm = array(
"fcm_token" => $fcm_token
);
$hr = User::find($user->id);
if ($hr)
$hr->update($dataUpdateFcm);
}
$dataRole = Role::find($user->role_id);
$dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id);
$configApp = Company::where('id', $user->company_id)->first();
if ($configApp) {
$logoLogin = json_decode($configApp->logo_login, true);
$favicon = json_decode($configApp->favicon_image, true);
$logoHeader = json_decode($configApp->logo_header, true);
$configApp->logo_login = $logoLogin;
$configApp->favicon_image = $favicon;
$configApp->logo_header = $logoHeader;
}
if ($configApp)
$user->configApp = $configApp;
if ($dataRole)
$user->role = $dataRole;
if ($dataHierarchy)
$user->hierarchy = $dataHierarchy;
if (!$token = Auth::login($user))
return response()->json(['error' => 'Unauthorized'], 401);
$ttl = 60;
if ($remember)
$ttl = 10080;
// todo : change existing md5 hashed function to laravel's originally bcrypt
/* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */
/* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */
return response()->json([
'code' => 200,
'data' => array(
'data_user' => $user,
'access_token' => $token,
'token_type' => 'bearer',
'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')->where('email', $email)->first();
if (!$user) {
return response()->json(['status' => 'error', 'message' => 'e-mail not found '], 400);
} else {
$this->reqHttpCurl($email, $hashed, $user->username, $user->name);
// $updateUser = User::where('email', $email)->update(['password'=> $hashed]);
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));
}
}
}

4
app/Http/Controllers/DemoController.php

@ -12,8 +12,10 @@ class DemoController extends Controller
$this->validate($request, [
'name' => 'required',
'role' => 'required',
'email' => 'required',
'number_phone' => 'required',
'message' => 'required',
'status' => 'required',
'message' => 'required'
]);
$data = $request->all();

94
app/Http/Controllers/HumanResourceController.php

@ -2,12 +2,16 @@
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Support\Str;
use App\Models\UserToProyek;
use Illuminate\Http\Request;
use App\Models\HumanResource;
use App\Models\UserToActivity;
use App\Models\UserToProyek;
use Illuminate\Support\Facades\Artisan;
const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php';
class HumanResourceController extends Controller
{
public function add(Request $request)
@ -15,7 +19,7 @@ class HumanResourceController extends Controller
$this->validate($request, [
'role_id' => 'required',
'name' => 'required',
'ktp_number' => 'required|numeric|unique:m_users,ktp_number',
'ktp_number' => 'required|string|unique:m_users,ktp_number',
'employee_type' => 'required',
'company_id' => 'required'
]);
@ -35,6 +39,34 @@ class HumanResourceController extends Controller
}
}
public function add_user(Request $request)
{
$this->validate($request, [
'role_id' => 'required',
'name' => 'required',
'ktp_number' => 'required|string|unique:m_users,ktp_number',
'employee_type' => 'required',
'company_id' => 'required',
'username' => 'required',
'email' => 'required'
]);
$data = $request->all();
if (isset($request->password) && $request->password != "") {
$data['password'] = md5($request->password);
}
$result = HumanResource::create($data);
if ($result) {
$this->sendEmail($request->email, $request->password);
return response()->json(['status' => 'success', 'message' => 'Human Resource Pool successfull created', 'code' => 200]);
} else {
return response()->json(['status' => 'failed', 'message' => 'Human Resource Pool failed created', 'code' => 400]);
}
}
public function edit($id)
{
if (!$id || (int) $id < 0 || $id == "") {
@ -59,7 +91,7 @@ class HumanResourceController extends Controller
}
$humanresource = HumanResource::find($id);
$request->ktp_number !== $humanresource['ktp_number'] ? $this->validate($request,['ktp_number'=>'unique:m_users,ktp_number|numeric']) : '';
$request->ktp_number !== $humanresource['ktp_number'] ? $this->validate($request,['ktp_number'=>'unique:m_users,ktp_number|string']) : '';
$request->email !== $humanresource['email'] ? $this->validate($request,['email'=>'unique:m_users,email|email']) : '';
$request->password ? $this->validate($request,['password'=>'required|min:8']) : '';
@ -197,4 +229,60 @@ class HumanResourceController extends Controller
{
Artisan::call('sync:integration-human-resources');
}
public function sendEmail($email, $password)
{
$user = User::select('email', 'name', 'username')->where('email', $email)->first();
if (!$user) {
return response()->json(['status' => 'error', 'message' => 'e-mail not found '], 400);
} else {
$this->reqHttpCurl($email, $password, $user->username, $user->name);
return response()->json(['status' => 'error', 'code'=>400, 'message' => 'e-mail not found '], 400);
}
}
private function reqHttpCurl($email, $password, $username, $name)
{
$postData = [
"to" => $email,
"name" => $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,
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));
}
}
}

2
app/Http/Controllers/RoleController.php

@ -28,7 +28,7 @@ class RoleController extends Controller
if (!$result)
return response()->json(['status' => 'failed', 'message' => 'Failed to add data!', 'code' => 500], 500);
return response()->json(['status' => 'success', 'message' => 'Data added!', 'code' => 200], 200);
return response()->json(['status' => 'success', 'data' => $result, 'message' => 'Data added!', 'code' => 200], 200);
}
public function edit($id)

1
app/Models/Company.php

@ -32,6 +32,7 @@ class Company extends Model
'created_by',
'updated_at',
'updated_by',
'cluster',
'date_register'
];
}

1
app/Models/HumanResource.php

@ -26,6 +26,7 @@ class HumanResource extends Model
'birth_date',
'blood_type',
'ktp_number',
'company_id',
'employee_type',
'status_resource',
'created_at',

8
routes/web.php

@ -6,7 +6,15 @@ $router->get('/', function () use ($router) {
$router->group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($router) {
$router->post('/login', 'AuthController@login');
// Landing Route
$router->post('/send-email', 'AuthController@sendEmail');
$router->post('/company-management-user/add', 'CompanyController@add');
$router->post('/company-management-user/search', 'CompanyController@search');
$router->post('/human-resource-user/add', 'HumanResourceController@add_user');
$router->get('/company-management-user/edit/{id}', 'CompanyController@edit');
$router->post('/demo-management-user/add', 'DemoController@add');
$router->post('/role-user/add', 'RoleController@add');
$router->post('/menu-company-user/add-multiple', 'MenuCompanyController@addMultiple');
$router->post('/menu/add', 'MenuController@add');
$router->get('/menu/edit/{id}', 'MenuController@edit');

Loading…
Cancel
Save