user permissions

This commit is contained in:
2018-12-01 21:08:29 -06:00
parent 36fd0b0c22
commit a34d4c6d25
8 changed files with 187 additions and 18 deletions

View File

@@ -4,6 +4,9 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\UserRole;
use App\Models\UserPermission;
use DB;
class AdminController extends Controller
@@ -17,16 +20,55 @@ class AdminController extends Controller
return view('admin.dashboard');
}
public function addPermission(Request $request) {
//Get the user and permission from the form
$user = $request->user;
$permission = $request->permission;
//Get the character id from the username using the user table
$character = DB::table('users')->where('name', $user)->first();
//Check to see if the character already has the permission
$check = DB::table('user_permissions')->where(['character_id' => $character->character_id, 'permission' => $permission])->get();
//If the user doesn't have the permission then add it into the table
if($check === null) {
$perm = new UserPermission;
$perm->character_id = $character->character_id;
$perm->permission = $permission;
$perm->save();
}
}
public function removePermission(Request $request) {
//Get the user and permission to be removed from the form
$user = $request->user;
$permission = $request->permission;
//Get the character id from the username using the user table
$character = DB::table('users')->where('name', $user)->first();
//Check if the permission exists in the table
$check = DB::table('user_permissions')->where(['character_id' => $character->character_id, 'permission' => $permission])->get();
if($check !== null) {
DB::table('user_permissions')->where(['character_id' => $character->character_id,
'permission' => $permission])
->delete();
return view('admin.dashboard')->with('success', 'User Updated.');
} else {
return view('admin.dashboard')->with('error', 'User did not have the permission.');
}
}
public function addRole(Request $request) {
//Get the user and role from the form
$user = $request->user;
$role = $request->role;
//Get the character id from the username using the user table
$character = DB::table('users')->where('name', $user)->first();
//Delete the current roles from the database
DB::table('user_roles')->where(['character_id' => $character->character_id])->update([
'role' => $role,
]);
//Delete the current roles from the database to start with a clean state
DB::table('user_roles')->where(['character_id' => $character->character_id])->delete();
$userRoles = new UserRole;
$userRoles->character_id = $character->character_id;
$userRoles->role = $role;
$userRoles->save();
//Return the view and the message of user updated
return view('admin.dashboard')->with('success', 'User Updated.');
}
@@ -39,7 +81,7 @@ class AdminController extends Controller
$character = DB::table('users')->where('name', $user)->first();
$check = DB::table('user_roles')->where(['character_id' => $character->character_id, 'role' => $role])->get();
if($check !== null) {
DB::table('user_roles')->where(['character_id' => $character->id,
DB::table('user_roles')->where(['character_id' => $character->character_id,
'role' => $role])
->delete();
return view('admin.dashboard')->with('success', 'User Updated.');

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class UserPermission extends Model
{
/**
* The attributes that are mass assignable
*
* @var array
*/
protected $fillable = [
'character_id',
'permission',
];
protected $table = 'user_permissions';
/**
* The attributes that should be hidden for arrays
*
* @var array
*/
protected $hidden = [];
protected $guarded = [];
}

View File

@@ -23,6 +23,7 @@ class AuthServiceProvider extends ServiceProvider
/**
* Register any authentication / authorization services.
* These gates will always choose the highest roles
* We use gates in some of the graphics, but will work to utilize if statements instead shortly
*
* @return void
*/
@@ -31,8 +32,10 @@ class AuthServiceProvider extends ServiceProvider
$this->registerPolicies($gate);
$gate->define('isAdmin', function($user) {
//Get the roles the user has from the user_roles table and check against the gate we are creating
$check = DB::table('user_roles')->where('character_id', auth()->user()->character_id)->get(['role']);
if($check[0]->role === 'Admin') {
//User has the Admin role
return true;
} else {
return false;
@@ -40,8 +43,10 @@ class AuthServiceProvider extends ServiceProvider
});
$gate->define('isDirector', function($user) {
//Get the roles the user has from the user_roles table and check against the gate we are creating
$check = DB::table('user_roles')->where('character_id', auth()->user()->character_id)->get(['role']);
if($check[0]->role === 'Director') {
//User has the Director role
return true;
} else {
return false;
@@ -49,8 +54,10 @@ class AuthServiceProvider extends ServiceProvider
});
$gate->define('isUser', function($user) {
//Get the roles the user has from the user_roles table and check against the gate we are creating
$check = DB::table('user_roles')->where('character_id', auth()->user()->character_id)->get(['role']);
if($check[0]->role === 'User') {
//User has the User role
return true;
} else {
return false;
@@ -58,8 +65,10 @@ class AuthServiceProvider extends ServiceProvider
});
$gate->define('isGuest', function($user) {
//Get the roles the user has from the user_roles table and check against the gate we are creating
$check = DB::table('user_roles')->where('character_id', auth()->user()->character_id)->get(['role']);
if($check[0]->role === 'Guest') {
//User has the Guest role
return true;
} else {
return false;
@@ -67,8 +76,10 @@ class AuthServiceProvider extends ServiceProvider
});
$gate->define('isNone', function($user) {
//Get the roles the user has from the user_roles table and check against the gate we are creating
$check = DB::table('user_roles')->where('character_id', auth()->user()->character_id)->get(['role']);
if($check[0]->role === 'None') {
//User has no role
return true;
} else {
return false;

View File

@@ -35,7 +35,8 @@ class RouteServiceProvider extends ServiceProvider
*/
public function map()
{
$this->mapApiRoutes();
//Don't want to map Api Routes as we aren't going to be using an API
//$this->mapApiRoutes();
$this->mapWebRoutes();

View File

@@ -6,7 +6,8 @@ use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Models\UserRole;
use App\Models\UserPermission;
class User extends Authenticatable
{
@@ -45,17 +46,6 @@ class User extends Authenticatable
protected $guarded = [];
//Used in middleware to make sure a user is able to access many of the pages
//public function hasRole($role)
//{
// $check = User::role()->get(['role']);
// if($check == $role) {
// return true;
// } else {
// return false;
// }
//}
public function getUserType() {
return User::where('user_type')->get();
}
@@ -64,7 +54,62 @@ class User extends Authenticatable
return $this->hasOne('App\Models\UserRole', 'character_id');
}
public function permissions() {
return $this->hasMany('App\Models\UserPermission', 'character_id');
}
public function esitoken() {
return $this->hasOne('App\Models\EsiToken', 'character_id', 'character_id');
}
public function hasPermission($permission, $perm = true) {
//Check if the user has a specific permission
if(UserPermission::where(['character_id' => $this->character_id, 'permission' => $permission])->get()) {
if($perm === true) {
return true;
} else {
return false;
}
} else {
if($perm === true) {
return true;
} else {
return false;
}
}
}
public function hasRole($role, $permission = true) {
//If the user is a super user then he has all roles
if($this->hasSuperUser()) {
return true;
}
if(UserRole::where(['character_id' => $this->character_id, 'role' => $role])->get()) {
//Check for inverse permissions
if($permission === true) {
return true;
} else {
return false;
}
} else {
//Check for inverse permissions
if($permission === true) {
return true;
} else {
return false;
}
}
}
public function hasSuperUser() {
//Search for the super user role for the character from the database
$found = DB::table('user_roles')->where(['character_id' => $this->character_id, 'role' => 'SuperUser'])->get(['role']);
//If we find the SuperUser role, then the user has it, and returns true, else returns false
if($found == 'SuperUser') {
return true;
} else {
return false;
}
}
}

View File

@@ -0,0 +1,37 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserPermissionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if(!Schema::hasTable('user_permissions')) {
Schema::create('user_permissions', function (Blueprint $table) {
$table->increments('id');
$table->integer('character_id')->unsigned();
$table->foreign('character_id')->references('character_id')->on('users');
$table->string('permission');
$table->timestamps();
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_permissions');
}
}

View File

@@ -8,6 +8,7 @@ $baseDir = dirname($vendorDir);
return array(
'App\\Console\\Commands\\CorpJournal' => $baseDir . '/app/Console/Commands/corpJournal.php',
'App\\Console\\Commands\\GetCorps' => $baseDir . '/app/Console/Commands/getCorps.php',
'App\\Console\\Commands\\UpdateMoonPricing' => $baseDir . '/app/Console/Commands/UpdateMoonPricing.php',
'App\\Console\\Commands\\sendMail' => $baseDir . '/app/Console/Commands/sendmail.php',
'App\\Console\\Kernel' => $baseDir . '/app/Console/Kernel.php',
'App\\Exceptions\\Handler' => $baseDir . '/app/Exceptions/Handler.php',
@@ -58,6 +59,7 @@ return array(
'App\\Models\\Moon' => $baseDir . '/app/Models/Moon.php',
'App\\Models\\OrePrice' => $baseDir . '/app/Models/OrePrice.php',
'App\\Models\\Price' => $baseDir . '/app/Models/Price.php',
'App\\Models\\ScheduleJob' => $baseDir . '/app/Models/ScheduleJob.php',
'App\\Models\\Structure' => $baseDir . '/app/Models/Structure.php',
'App\\Models\\UserRole' => $baseDir . '/app/Models/UserRole.php',
'App\\Providers\\AppServiceProvider' => $baseDir . '/app/Providers/AppServiceProvider.php',

View File

@@ -403,6 +403,7 @@ class ComposerStaticInitc3f953f8a7291d41a76e1664339777c9
public static $classMap = array (
'App\\Console\\Commands\\CorpJournal' => __DIR__ . '/../..' . '/app/Console/Commands/corpJournal.php',
'App\\Console\\Commands\\GetCorps' => __DIR__ . '/../..' . '/app/Console/Commands/getCorps.php',
'App\\Console\\Commands\\UpdateMoonPricing' => __DIR__ . '/../..' . '/app/Console/Commands/UpdateMoonPricing.php',
'App\\Console\\Commands\\sendMail' => __DIR__ . '/../..' . '/app/Console/Commands/sendmail.php',
'App\\Console\\Kernel' => __DIR__ . '/../..' . '/app/Console/Kernel.php',
'App\\Exceptions\\Handler' => __DIR__ . '/../..' . '/app/Exceptions/Handler.php',
@@ -453,6 +454,7 @@ class ComposerStaticInitc3f953f8a7291d41a76e1664339777c9
'App\\Models\\Moon' => __DIR__ . '/../..' . '/app/Models/Moon.php',
'App\\Models\\OrePrice' => __DIR__ . '/../..' . '/app/Models/OrePrice.php',
'App\\Models\\Price' => __DIR__ . '/../..' . '/app/Models/Price.php',
'App\\Models\\ScheduleJob' => __DIR__ . '/../..' . '/app/Models/ScheduleJob.php',
'App\\Models\\Structure' => __DIR__ . '/../..' . '/app/Models/Structure.php',
'App\\Models\\UserRole' => __DIR__ . '/../..' . '/app/Models/UserRole.php',
'App\\Providers\\AppServiceProvider' => __DIR__ . '/../..' . '/app/Providers/AppServiceProvider.php',