login sso

parent 8b6ee874
<?php
namespace App\Http\Controllers\Authentication;
use App\Http\Controllers\Controller;
use App\Repositories\AuthRepository;
use App\Repositories\BiodataRepository;
use App\Repositories\UsersRepository;
use App\User;
use Auth;
use Exception;
use GuzzleHttp\Client as GuzzleHttpClient;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Str;
class LoginController extends Controller
{
private $authRepo;
private $userRepo;
private $biodataRepo;
private $userDetailRepo;
public function __construct(
AuthRepository $authRepo,
UsersRepository $userRepo,
BiodataRepository $biodataRepo
) {
$this->authRepo = $authRepo;
$this->userRepo = $userRepo;
$this->biodataRepo = $biodataRepo;
}
public function index()
{
try {
$sessso = $_COOKIE['email_sso'];
if (isset($sessso)) {
$urlsso = 'https://sso.unesa.ac.id/api/user-cookie';
$cek_sesisso = file_get_contents($urlsso."/$sessso");
$rr = (json_decode($cek_sesisso));
$emailsso = $rr->email;
$useridsso = $rr->userid;
}
$user = $this->userRepo->find(null, null, $emailsso);
if ($user) {
// Login
return $this->getlogin($user->id);
} else {
// Add User
if (isset($sessso)) {
$auth = $this->authRepo->getEmailAuth($emailsso);
$this->getadduser($auth);
return redirect()->route('home');
}
}
return Redirect::to('https://sso.unesa.ac.id/dashboard');
} catch (\Exception $sessso) {
return View('auth.login');
}
}
public function sso(Request $request, $email, $session_id)
{
$auth = $this->authRepo->sso($request, $email, $session_id);
if (!is_array($auth)) {
return redirect('https://sso.unesa.ac.id/user');
}
$user = $this->userRepo->find(null, null, $auth[0]->email);
if ($user) {
// Login
return $this->getlogin($user->id);
} else {
// Add User
return $this->getadduser($auth);
}
return redirect()->route('/');
}
private function getlogin($userid)
{
Auth::loginUsingId($userid);
return redirect()->to('/');
}
private function getAdduser($auth)
{
$biodata = $this->biodataRepo->biodata($auth);
if ($biodata) {
DB::beginTransaction();
try {
$id = (string) Str::uuid();
$this->userRepo->storeSso($id, $biodata);
$this->biodataRepo->storeSso($id, $biodata);
Auth::loginUsingId($id);
DB::commit();
return redirect()->intended('/');
} catch (Exception $ex) {
DB::rollBack();
}
}
return redirect()->route('login');
}
// public function check($email)
// {
// $clientbiodata = new GuzzleHttpClient();
// $apiRequestbiodata = $clientbiodata->request('GET', 'https://sso.unesa.ac.id/userid/'. $email);
// $auth = json_decode($apiRequestbiodata->getBody()->getContents());
// // $auth[0] = (object)['userid' => '18020074065'];
// $check = $this->biodataRepo->siakadu($auth);
// dd($check);
// }
}
<?php
namespace App\Http\Controllers\Authentication;
use Auth;
use Alert;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Http\Controllers\Controller;
use App\Repositories\RoleRepository;
use App\Repositories\BiodataRepository;
use App\Repositories\EmailRepository;
use App\Repositories\UserRepository;
use App\Repositories\IsdmRepository;
class RegisterController extends Controller
{
public function __construct(
RoleRepository $roleRepo,
BiodataRepository $biodataRepo,
EmailRepository $emailRepo,
UserRepository $userRepo,
IsdmRepository $isdmRepo
) {
$this->middleware('guest');
$this->roleRepo = $roleRepo;
$this->biodataRepo = $biodataRepo;
$this->emailRepo = $emailRepo;
$this->userRepo = $userRepo;
$this->isdmRepo = $isdmRepo;
}
public function index()
{
return view('auth.register');
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
public function create(Request $request)
{
$checkEmail = $this->emailRepo->checkEmail($request->input('email'));
if (!is_array($checkEmail)) {
Alert::error('Email tidak ditemukan');
return redirect()->back();
}
$user = $this->userRepo->find(null, $request->input('email'));
if ($user) {
Alert::warning('Email sudah terdaftar');
return redirect()->back();
}
$id = (string)Str::uuid();
$isdm = $this->isdmRepo->isdm($checkEmail[0]->userid);
if (!$isdm) {
Alert::error('Data kepegawaian tidak ditemukan');
return redirect()->back();
}
$this->userRepo->create($id, $isdm, $checkEmail[0]->email);
$this->roleRepo->create($id, $isdm, 'admin');
$this->biodataRepo->create($id, $isdm, $checkEmail[0]->email);
Auth::loginUsingId($id);
return redirect()->route('home');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Biodata extends Model
{
use HasFactory;
protected $table = 'biodata';
protected $fillable = ['id', 'user_id', 'nip', 'nidn', 'name', 'fakultas', 'prodi', 'telephone', 'phone', 'email', 'web', 'userid_created', 'userid_updated'];
}
......@@ -2,7 +2,6 @@
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
......@@ -24,6 +23,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'id',
'name',
'email',
'password',
......
<?php
namespace App\Repositories;
use GuzzleHttp\Client as GuzzleHttpClient;
class AuthRepository
{
public function sso($request, $email, $session_id)
{
// Get Token
try {
$clientauthscsso = new GuzzleHttpClient();
$apiRequestauthscsso = $clientauthscsso->request('GET', 'https://sso.unesa.ac.id/check-secret-token/'.$session_id);
$cektoken = json_decode($apiRequestauthscsso->getBody()->getContents());
} catch (\Exception $apiRequestauthscsso) {
// dd('Gagal Masuk Tahap 1');
$error = 'Token Tidak Ditemukan';
return $error;
}
// Check Validation Token
try {
$clientauthtknsso = new GuzzleHttpClient();
$apiRequestauthtknsso = $clientauthtknsso->request('GET', 'https://sso.unesa.ac.id/check-token/'.$cektoken);
$checkakses = json_decode($apiRequestauthtknsso->getBody()->getContents());
} catch (\Exception $apiRequestauthtknsso) {
// dd('Gagal Masuk Tahap 2');
$error = 'Token Tidak Valid';
return $error;
}
// Get Account
try {
$clientbiodata = new GuzzleHttpClient();
$apiRequestbiodata = $clientbiodata->request('GET', 'https://sso.unesa.ac.id/userid/'.$checkakses->email);
$aksessso = json_decode($apiRequestbiodata->getBody()->getContents());
} catch (\Exception $apiRequestbiodata) {
$gagal_login = 'Data Tidak Ditemukan';
return $error;
}
$success = $aksessso;
return $success;
}
public function getEmailAuth($email)
{
try {
$clientbiodata = new GuzzleHttpClient();
$apiRequestbiodata = $clientbiodata->request('GET', 'https://sso.unesa.ac.id/userid/'.$email);
$aksessso = json_decode($apiRequestbiodata->getBody()->getContents());
} catch (\Exception $apiRequestbiodata) {
$gagal_login = 'Data Tidak Ditemukan';
return $error;
}
$success = $aksessso;
return $success;
}
public function getAccount($nim)
{
$url = 'https://siakadu.unesa.ac.id/api/apiunggun';
$data = ['username' => $nim, 'kondisi' => 'cekhakakses'];
$x = kirim_data($url, 'post', $data);
$user = unserialize($x['isi']);
return $user;
}
}
<?php
namespace App\Repositories;
use App\Models\Biodata;
use GuzzleHttp\Client as GuzzleHttpClient;
use Illuminate\Support\Str;
class BiodataRepository
{
private $model;
public function __construct(Biodata $model)
{
$this->model = $model;
}
public function biodata($auth)
{
if ($auth[0]->jenis == 'P') {
return $this->isdm($auth);
}
if ($auth[0]->jenis == 'M') {
return null;
}
}
public function isdm($auth)
{
$client = new GuzzleHttpClient();
$apiRequest = $client->request('GET', 'https://i-sdm.unesa.ac.id/biodataumum/'.$auth[0]->userid);
$isdm = json_decode($apiRequest->getBody()->getContents());
$data['email'] = $auth[0]->email;
$data['name'] = $isdm[0]->nama;
$data['nip'] = $isdm[0]->nip;
$data['nidn'] = $isdm[0]->nidn;
$data['fakultas'] = $isdm[0]->namahomebase;
$data['prodi'] = $isdm[0]->namasatker;
return $data;
}
public function siakadu($auth)
{
$userid = $auth[0]->userid;
$url = 'https://siakadu.unesa.ac.id/api/apiunggun';
$data = ['username' => $userid, 'kondisi' => 'cekhakakses'];
$x = kirim_data($url, 'post', $data);
$user = unserialize($x['isi']);
$data['email'] = $auth[0]->email;
$data['name'] = $user['data_mahasiswa']['nm_pd'];
$data['noid'] = $user['username'];
$data['role'] = 'mahasiswa';
$data['fakultas'] = $user['nama_fakultas'];
$data['prodi'] = $user['nama_prodi'];
$data['login_type'] = 'sso';
return $data;
}
public function storeSso($id, $data)
{
$data['user_id'] = $id;
$data['id'] = $id = (string) Str::uuid();
return $this->model->create($data);
}
}
<?php
namespace App\Repositories;
abstract class Repository
{
protected $model;
public function findId($with = null, $id = null)
{
return $this->model
->when($with, function ($query) use ($with) {
return $query->with($with);
})
->when($id, function ($query) use ($id) {
return $query->where('id', $id);
})
->first();
}
public function store($request)
{
$request['userid_created'] = auth()->user()->id;
return $this->model->create($request);
}
public function update($request, $model)
{
$request['userid_updated'] = auth()->user()->id;
return $model->update($request);
}
public function destroy($model)
{
return $model->delete();
}
}
<?php
namespace App\Repositories;
use App\Models\User;
use Auth;
use Illuminate\Support\Str;
class UsersRepository
{
private $model;
public function __construct(User $model)
{
$this->model = $model;
}
public function getSelect()
{
return $this->model
->orderBy('name', 'ASC')
->pluck('name', 'id');
}
public function find($id = null, $with = null, $email = null)
{
return $this->model
->when($with, function ($query) use ($with) {
return $query->with($with);
})
->when($id, function ($query) use ($id) {
return $query->where('id', $id);
})
->when($email, function ($query) use ($email) {
return $query->where('email', $email);
})
->first();
}
public function get($with = null, $name = null)
{
return $this->model
->when($with, function ($query) use ($with) {
return $query->with($with);
})
->when($name, function ($query) use ($name) {
return $query->where('name', 'LIKE', '%'.$name.'%');
})
->orderBy('name', 'ASC')
->get();
}
public function paginate($with = null, $name = null, $limit = 10)
{
return $this->model
->when($with, function ($query) use ($with) {
return $query->with($with);
})
->when($name, function ($query) use ($name) {
return $query->where('name', 'LIKE', '%'.$name.'%');
})
->orderBy('name', 'ASC')
->paginate($limit);
}
public function store($request)
{
$data = $request->except('_token');
$data['id'] = (string) Str::uuid();
$data['password'] = bcrypt($request->input('password'));
$data['login_type'] = 'app';
$data['userid_created'] = Auth::user()->id;
$data['userid_updated'] = Auth::user()->id;
return $this->model->create($data);
}
public function storeSso($id, $data)
{
$data['id'] = $id;
$data['password'] = bcrypt($data['nip'].'s3cr3t5');
return $this->model->create($data);
}
public function update($request, User $user)
{
$data = $request->except('_token');
if ($request->input('password')) {
$data['password'] = bcrypt($request->input('password'));
} else {
unset($data['password']);
}
$data['userid_updated'] = Auth::user()->id;
return $user->update($data);
}
public function destroy(User $user)
{
$user->delete();
return $user;
}
}
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Authentication\LoginController;
use App\Http\Controllers\User\BukuAjarController;
use App\Http\Controllers\User\FasilitasController;
use App\Http\Controllers\User\KekayaanIntelController;
......@@ -12,7 +12,7 @@ use App\Http\Controllers\User\PublikasiArtikelController;
use App\Http\Controllers\User\PublikasiJurnalController;
use App\Http\Controllers\User\StafPendukungController;
use App\Http\Controllers\User\UnitBisnisController;
use App\Http\Controllers\Authentication\LoginController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
......@@ -29,11 +29,13 @@ Route::get('/', function () {
return view('dashboard');
});
Route::get('login', [LoginController::class, 'index'])->name('login');
Route::get('sso/{email}/{sessionid}', [LoginController::class, 'sso']);
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::resource('/bukuajar', BukuAjarController::class);
Route::resource('/fasilitas', FasilitasController::class);
Route::resource('/kekayaanintelek', KekayaanIntelController::class);
......@@ -45,5 +47,3 @@ Route::resource('/publikasijurnal', PublikasiJurnalController::class);
Route::resource('/penyelengaraseminar', PenyelenggaraanSeminarController::class);
Route::resource('/stafpendukung', StafPendukungController::class);
Route::resource('/unitbisnis', UnitBisnisController::class);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment