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.');