create mapping entity

main
n0emis 4 years ago
parent 3d06a539c8
commit b9f786059e
Signed by: n0emis
GPG Key ID: 00FAF748B777CF10

@ -0,0 +1,35 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateHttpUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('http_users', function (Blueprint $table) {
$table->increments('id');
$table->string('remote_id');
$table->string('user_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('http_users');
}
}

@ -0,0 +1,15 @@
<?php
namespace Modules\HttpHeaderAuth\Entities;
use Illuminate\Database\Eloquent\Model;
class HttpUser extends Model
{
protected $fillable = ['remote_id', 'user_id'];
public function user()
{
return $this->belongsTo('App\User');
}
}

@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Password;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Factory; use Illuminate\Database\Eloquent\Factory;
use App\User; use App\User;
use Modules\HttpHeaderAuth\Entities;
class HttpHeaderAuthServiceProvider extends ServiceProvider class HttpHeaderAuthServiceProvider extends ServiceProvider
{ {
@ -36,7 +37,9 @@ class HttpHeaderAuthServiceProvider extends ServiceProvider
public function hooks() public function hooks()
{ {
\Eventy::addAction('middleware.web.custom_handle', function ($request) { \Eventy::addAction('middleware.web.custom_handle', function ($request) {
if (!$request->user() && isset($_SERVER['HTTP_X_AUTH_EMAIL'])) { if (!$request->user() && isset($_SERVER['HTTP_X_AUTH_SUBJECT'])) {
$httpuser = Entities\HttpUser::where('remote_id', $_SERVER['HTTP_X_AUTH_SUBJECT'])->first();
if (!isset($httpuser)) {
$user = User::where('email', $_SERVER['HTTP_X_AUTH_EMAIL'])->first(); $user = User::where('email', $_SERVER['HTTP_X_AUTH_EMAIL'])->first();
if (!isset($user)) { if (!isset($user)) {
$user = User::create([ $user = User::create([
@ -45,8 +48,22 @@ class HttpHeaderAuthServiceProvider extends ServiceProvider
"last_name" => ".", "last_name" => ".",
"password" => str_random(64) "password" => str_random(64)
]); ]);
$user->save();
} }
if (isset($user->email)) {
$httpuser = Entities\HttpUser::create([
"remote_id" => $_SERVER['HTTP_X_AUTH_SUBJECT'],
"user_id" => $user,
]);
} else {
$user = $httpuser->user;
$user->email = $_SERVER['HTTP_X_AUTH_EMAIL'];
$user->first_name = $_SERVER['HTTP_X_AUTH_USERNAME'];
$user->save();
}
if (isset($user)) {
\Auth::login($user); \Auth::login($user);
} }
} }

Loading…
Cancel
Save