middleware([ "auth" ]); $this->middleware([ "lang" ]); $this->middleware([ "check.auth:roles.show" ])->only("show", "index"); $this->middleware([ "check.auth:roles.create" ])->only("create", "store"); $this->middleware([ "check.auth:roles.edit" ])->only("edit", "update"); $this->middleware([ "check.auth:roles.delete" ])->only("delete"); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { $roles = Role::query()->where("id", "!=", 1)->where("id", "!=", 2)->paginate(20); return Response::detect("roles.index", [ "roles" => $roles]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return Response::detect("roles.create"); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $requestRole = $request->validate([ "name" => "required|max:255", "description" => "required|max:255" ]); $brugerPermissions = new Collection(); if ($request->value !== null) { foreach ($request->value as $valuee) { $brugerPermissions->add(\Spatie\Permission\Models\Permission::query()->where("name", "=", $valuee)->first()); } } $role = new Role($requestRole); $allRoles = Role::query()->where('name', '=', $request->name)->get(); // If there already is a role with that name, then don't create if(count($allRoles) > 0) return redirect()->route("roles.store"); else { // Else - Create it $role->save(); if (!$brugerPermissions->isEmpty()) { foreach ($brugerPermissions as $permission) { \Spatie\Permission\Models\Role::query()->find($role->id)->givePermissionTo($permission); } } $roles = Role::query()->paginate(20); return redirect()->route("roles.index", ['roles' => $roles]); } } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { return Response::detect("roles.show", [ "role" => $id]); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { if($id == 1 || $id == 2) return redirect()->route("roles.index"); $role = Role::find($id); return Response::detect("roles.edit", ["role" => $role]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { if($id == 1 || $id == 2) return redirect()->route("roles.index"); $data = $request->validate([ "name" => "required|max:255", "description" => "required|max:255" ]); $role = Role::find($id); $brugerPermissions = new Collection(); if ($request->value !== null) { foreach ($request->value as $valuee) { $brugerPermissions->add(Permission::query()->where("name", "=", $valuee)->first()); } } // Delete all permission to the role you're editing, so you can give new ones foreach (Permission::query()->get() as $permission) { Role::query()->find($id)->revokePermissionTo($permission); } // Give new permissions to the tole you're editing if (!$brugerPermissions->isEmpty()) { foreach ($brugerPermissions as $permission) { Role::query()->find($id)->givePermissionTo($permission); } } $allRoles = Role::query()->where('name', '=', $request->name)->where('id', '!=', $id)->get(); if(count($allRoles) > 0){ return redirect()->route("roles.index", [ "role" => $role ]); }else{ $role->update($data); $role->save(); $roles = Role::query()->paginate(20); return redirect()->route("roles.index", ['roles' => $roles]); } } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { if($id == 1 || $id == 2) return redirect()->route("roles.index"); $role = Role::find($id); $role->delete(); return redirect()->route("roles.index"); } public function search(Request $request){ if($request->ajax()){ $output = "