permissions systems 2.0
This commit is contained in:
@@ -188,9 +188,9 @@ class LoginController extends Controller
|
||||
* @param charId
|
||||
*/
|
||||
private function SetRole($role, $charId) {
|
||||
$permission = new UserPermission;
|
||||
$permission = new UserRole;
|
||||
$permission->character_id = $charId;
|
||||
$permission->permission = $role;
|
||||
$permission->role = $role;
|
||||
$permission->save();
|
||||
}
|
||||
|
||||
@@ -235,13 +235,13 @@ class LoginController extends Controller
|
||||
private function GetRole($refreshToken, $charId) {
|
||||
$accountType = $this->GetAccountType($refreshToken, $charId);
|
||||
if($accountType == 'Guest') {
|
||||
$role = 'role.guest';
|
||||
$role = 'Guest';
|
||||
} else if($accountType == 'Legacy'){
|
||||
$role = 'role.user';
|
||||
$role = 'User';
|
||||
} else if($accountType == 'W4RP') {
|
||||
$role = 'role.user';
|
||||
$role = 'User';
|
||||
} else {
|
||||
$role = 'role.none';
|
||||
$role = 'None';
|
||||
}
|
||||
|
||||
return $role;
|
||||
|
||||
@@ -61,6 +61,7 @@ class Kernel extends HttpKernel
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
||||
'callback' => \App\Http\Middleware\Callback::class,
|
||||
'role' => \App\Http\Middleware\RequireRole::class,
|
||||
'permission' => \App\Http\Middleware\RequirePermission::class,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -18,58 +18,11 @@ class RequirePermission
|
||||
*/
|
||||
public function handle($request, Closure $next, $permission)
|
||||
{
|
||||
$confirmed = false;
|
||||
if(strpos($permission, 'role.') !== false) {
|
||||
$confirmed = $this->CheckRole($permission);
|
||||
} else {
|
||||
$confirmed = $this->CheckPermission($permission);
|
||||
}
|
||||
|
||||
if($confirmed === false) {
|
||||
abort(403, "You don't have permission to access this area.");
|
||||
}
|
||||
$perms = UserPermission::where(['character_id' => auth()->user()->character_id, 'permission'=> $permission])->get(['permission']);
|
||||
|
||||
abort_unless(auth()->check() && isset($perms[0]->permission), 403, "You don't have the correct permission to be in this area.");
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
private function CheckPermission($permission) {
|
||||
$confirmed = false;
|
||||
|
||||
$checks = UserPermission::where(['character_id' => auth()->user()->character_id, 'permission' => $permission])->get(['permission']);
|
||||
foreach($checks as $check) {
|
||||
if($check === $permission) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function CheckRole($role) {
|
||||
$confirmed = false;
|
||||
|
||||
$ranking = [
|
||||
'role.none' => 0,
|
||||
'role.guest' => 1,
|
||||
'role.user' => 2,
|
||||
'role.director' => 3,
|
||||
'role.admin' => 4,
|
||||
];
|
||||
//Using eloquent let's get the roles for the character
|
||||
$checks = UserPermission::where('character_id', auth()->user()->character_id)->get(['permission']);
|
||||
|
||||
foreach($checks as $check) {
|
||||
if(!isset($check->permission)) {
|
||||
abort(403, "You don't have the correct role to view this area.");
|
||||
}
|
||||
|
||||
if(strpos($role, 'role.') !== false) {
|
||||
if($ranking[$check->permission] >= $ranking[$role]) {
|
||||
$confirmed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $confirmed;
|
||||
}
|
||||
}
|
||||
|
||||
42
app/Http/Middleware/RequireRole.php
Normal file
42
app/Http/Middleware/RequireRole.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
|
||||
class RequireRole
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next, $role)
|
||||
{
|
||||
$confirmed = false;
|
||||
|
||||
$ranking = [
|
||||
'None' => 0,
|
||||
'Guest' => 1,
|
||||
'User' => 2,
|
||||
'Admin' => 3,
|
||||
'SuperUser' => 4,
|
||||
];
|
||||
|
||||
$check = UserPermission::where('character_id', auth()->user()->character_id)->get(['role']);
|
||||
|
||||
if(!isset($check[0]->role)) {
|
||||
abort(403, "You don't any roles. You don't belong here.");
|
||||
}
|
||||
|
||||
if($ranking[$check->permission] >= $ranking[$role]) {
|
||||
$confirmed = true;
|
||||
}
|
||||
|
||||
abort_unless(auth()->check() && $confirmed, 403, "You don't have the correct role to be in this area.");
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
19
app/Models/User/UserRole.php
Normal file
19
app/Models/User/UserRole.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class UserRole extends Model
|
||||
{
|
||||
protected $table = 'users_role';
|
||||
|
||||
protected $fillable = [
|
||||
'character_id',
|
||||
'role',
|
||||
];
|
||||
|
||||
public function user() {
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,10 @@ class User extends Authenticatable
|
||||
return User::where('user_type')->get();
|
||||
}
|
||||
|
||||
public function role() {
|
||||
return $this->hasOne('\App\Models\User\UserRole', 'character_id', 'character_id');
|
||||
}
|
||||
|
||||
public function permissions() {
|
||||
return $this->hasMany('App\Models\User\UserPermission', 'character_id');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateUserRolesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
if(!Schema::hasTable('user_roles')) {
|
||||
Schema::create('user_roles', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->integer('character_id')->unsigned();
|
||||
$table->foreign('character_id')->references('character_id')->on('users');
|
||||
$table->string('role')->default('None');
|
||||
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('user_roles');
|
||||
}
|
||||
}
|
||||
@@ -8,14 +8,14 @@
|
||||
<li class="nav-item dropdown">
|
||||
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdoownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Moons</a>
|
||||
<div class="dropdown-menu" aria-labelledby="navbarDropDownMenuLink">
|
||||
@if(auth()->user()->hasPermission('role.guest'))
|
||||
@if(auth()->user()->hasRole('Guest'))
|
||||
<a class="dropdown-item" href="/moons/display">Display Moons</a>
|
||||
@endif
|
||||
@if(auth()->user()->hasPermission('role.user'))
|
||||
@if(auth()->user()->hasRole('User'))
|
||||
<a class="dropdown-item" href="/moons/display">Display Moons</a>
|
||||
<a class="dropdown-item" href="/moons/display/worth">Moon Worth</a>
|
||||
@endif
|
||||
@if(auth()->user()->hasPermission('role.admin'))
|
||||
@if(auth()->user()->hasRole('Admin'))
|
||||
<a class="dropdown-item" href="/moons/admin/display">Display Moons</a>
|
||||
<a class="dropdown-item" href="/moons/display/worth">Moon Worth</a>
|
||||
<a class="dropdown-item" href="/moons/admin/addmoon">Add Moon</a>
|
||||
@@ -61,7 +61,7 @@
|
||||
<div class="dropdown-menu" aria-labelledby="navbarDropDownMenuLink">
|
||||
<a class="dropdown-item" href="/structures/taxes/display">Current Taxes</a>
|
||||
<a class="dropdown-item" href="/structures/register">Register Structure</a>
|
||||
@if(auth()->user()->hasPermission('role.admin'))
|
||||
@if(auth()->user()->hasRole('Admin'))
|
||||
<a class="dropdown-item" href="/structures/admin/taxes/display">Corp Taxes</a>
|
||||
<a class="dropdown-item" href="/structures/admin/taxes/industry">Industry Taxes</a>
|
||||
<a class="dropdown-item" href="/structures/admin/taxes/reprocessing">Reprocessing Taxes</a>
|
||||
@@ -75,7 +75,7 @@
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/scopes/select">Add Esi Scopes</a>
|
||||
</li>
|
||||
@if(auth()->user()->hasPermission('role.admin'))
|
||||
@if(auth()->user()->hasRole('Admin'))
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/admin/dashboard">Admin</a>
|
||||
</li>
|
||||
|
||||
4
vendor/composer/autoload_classmap.php
vendored
4
vendor/composer/autoload_classmap.php
vendored
@@ -6,6 +6,7 @@ $vendorDir = dirname(dirname(__FILE__));
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'App\\AllowedLogin' => $baseDir . '/app/Models/Admin/AllowedLogin.php',
|
||||
'App\\Console\\Commands\\CalculateMarketTax' => $baseDir . '/app/Console/Commands/calculatemarkettax.php',
|
||||
'App\\Console\\Commands\\CorpJournal' => $baseDir . '/app/Console/Commands/corpJournal.php',
|
||||
'App\\Console\\Commands\\DumpFleets' => $baseDir . '/app/Console/Commands/dumpFleets.php',
|
||||
@@ -67,7 +68,6 @@ return array(
|
||||
'App\\Library\\SeatHelper' => $baseDir . '/app/Library/SeatHelper.php',
|
||||
'App\\Library\\Structures\\JumpBridgeFuel' => $baseDir . '/app/Library/Structures/JumpBridgeFuel.php',
|
||||
'App\\Library\\Structures\\StructureTaxHelper' => $baseDir . '/app/Library/Structures/StructureTaxHelper.php',
|
||||
'App\\Models\\Admin\\AllowedLogin' => $baseDir . '/app/Models/Admin/AllowedLogin.php',
|
||||
'App\\Models\\Character\\CharacterToCorporation' => $baseDir . '/app/Models/Charcter/CharacterToCorporation.php',
|
||||
'App\\Models\\Config' => $baseDir . '/app/Models/Config.php',
|
||||
'App\\Models\\Corporation\\AllianceCorp' => $baseDir . '/app/Models/Corporation/AllianceCorp.php',
|
||||
@@ -100,7 +100,6 @@ return array(
|
||||
'App\\Models\\ScheduledTask\\ScheduleJob' => $baseDir . '/app/Models/ScheduledTask/ScheduleJob.php',
|
||||
'App\\Models\\User\\AvailableUserPermission' => $baseDir . '/app/Models/User/AvailableUserPermission.php',
|
||||
'App\\Models\\User\\UserPermission' => $baseDir . '/app/Models/User/UserPermission.php',
|
||||
'App\\Models\\User\\UserRole' => $baseDir . '/app/Models/User/UserRole.php',
|
||||
'App\\Models\\User\\UserToCorporation' => $baseDir . '/app/Models/User/UserToCorporation.php',
|
||||
'App\\Providers\\AppServiceProvider' => $baseDir . '/app/Providers/AppServiceProvider.php',
|
||||
'App\\Providers\\AuthServiceProvider' => $baseDir . '/app/Providers/AuthServiceProvider.php',
|
||||
@@ -108,6 +107,7 @@ return array(
|
||||
'App\\Providers\\EventServiceProvider' => $baseDir . '/app/Providers/EventServiceProvider.php',
|
||||
'App\\Providers\\RouteServiceProvider' => $baseDir . '/app/Providers/RouteServiceProvider.php',
|
||||
'App\\User' => $baseDir . '/app/User.php',
|
||||
'App\\UserRole' => $baseDir . '/app/UserRole.php',
|
||||
'AvailableUserPermissions' => $baseDir . '/database/seeds/AvailableUserPermissions.php',
|
||||
'BeyondCode\\DumpServer\\DumpServerCommand' => $vendorDir . '/beyondcode/laravel-dump-server/src/DumpServerCommand.php',
|
||||
'BeyondCode\\DumpServer\\DumpServerServiceProvider' => $vendorDir . '/beyondcode/laravel-dump-server/src/DumpServerServiceProvider.php',
|
||||
|
||||
4
vendor/composer/autoload_static.php
vendored
4
vendor/composer/autoload_static.php
vendored
@@ -460,6 +460,7 @@ class ComposerStaticInitc3f953f8a7291d41a76e1664339777c9
|
||||
);
|
||||
|
||||
public static $classMap = array (
|
||||
'App\\AllowedLogin' => __DIR__ . '/../..' . '/app/Models/Admin/AllowedLogin.php',
|
||||
'App\\Console\\Commands\\CalculateMarketTax' => __DIR__ . '/../..' . '/app/Console/Commands/calculatemarkettax.php',
|
||||
'App\\Console\\Commands\\CorpJournal' => __DIR__ . '/../..' . '/app/Console/Commands/corpJournal.php',
|
||||
'App\\Console\\Commands\\DumpFleets' => __DIR__ . '/../..' . '/app/Console/Commands/dumpFleets.php',
|
||||
@@ -521,7 +522,6 @@ class ComposerStaticInitc3f953f8a7291d41a76e1664339777c9
|
||||
'App\\Library\\SeatHelper' => __DIR__ . '/../..' . '/app/Library/SeatHelper.php',
|
||||
'App\\Library\\Structures\\JumpBridgeFuel' => __DIR__ . '/../..' . '/app/Library/Structures/JumpBridgeFuel.php',
|
||||
'App\\Library\\Structures\\StructureTaxHelper' => __DIR__ . '/../..' . '/app/Library/Structures/StructureTaxHelper.php',
|
||||
'App\\Models\\Admin\\AllowedLogin' => __DIR__ . '/../..' . '/app/Models/Admin/AllowedLogin.php',
|
||||
'App\\Models\\Character\\CharacterToCorporation' => __DIR__ . '/../..' . '/app/Models/Charcter/CharacterToCorporation.php',
|
||||
'App\\Models\\Config' => __DIR__ . '/../..' . '/app/Models/Config.php',
|
||||
'App\\Models\\Corporation\\AllianceCorp' => __DIR__ . '/../..' . '/app/Models/Corporation/AllianceCorp.php',
|
||||
@@ -554,7 +554,6 @@ class ComposerStaticInitc3f953f8a7291d41a76e1664339777c9
|
||||
'App\\Models\\ScheduledTask\\ScheduleJob' => __DIR__ . '/../..' . '/app/Models/ScheduledTask/ScheduleJob.php',
|
||||
'App\\Models\\User\\AvailableUserPermission' => __DIR__ . '/../..' . '/app/Models/User/AvailableUserPermission.php',
|
||||
'App\\Models\\User\\UserPermission' => __DIR__ . '/../..' . '/app/Models/User/UserPermission.php',
|
||||
'App\\Models\\User\\UserRole' => __DIR__ . '/../..' . '/app/Models/User/UserRole.php',
|
||||
'App\\Models\\User\\UserToCorporation' => __DIR__ . '/../..' . '/app/Models/User/UserToCorporation.php',
|
||||
'App\\Providers\\AppServiceProvider' => __DIR__ . '/../..' . '/app/Providers/AppServiceProvider.php',
|
||||
'App\\Providers\\AuthServiceProvider' => __DIR__ . '/../..' . '/app/Providers/AuthServiceProvider.php',
|
||||
@@ -562,6 +561,7 @@ class ComposerStaticInitc3f953f8a7291d41a76e1664339777c9
|
||||
'App\\Providers\\EventServiceProvider' => __DIR__ . '/../..' . '/app/Providers/EventServiceProvider.php',
|
||||
'App\\Providers\\RouteServiceProvider' => __DIR__ . '/../..' . '/app/Providers/RouteServiceProvider.php',
|
||||
'App\\User' => __DIR__ . '/../..' . '/app/User.php',
|
||||
'App\\UserRole' => __DIR__ . '/../..' . '/app/UserRole.php',
|
||||
'AvailableUserPermissions' => __DIR__ . '/../..' . '/database/seeds/AvailableUserPermissions.php',
|
||||
'BeyondCode\\DumpServer\\DumpServerCommand' => __DIR__ . '/..' . '/beyondcode/laravel-dump-server/src/DumpServerCommand.php',
|
||||
'BeyondCode\\DumpServer\\DumpServerServiceProvider' => __DIR__ . '/..' . '/beyondcode/laravel-dump-server/src/DumpServerServiceProvider.php',
|
||||
|
||||
Reference in New Issue
Block a user