tethys/app/Http/Controllers/Settings/Access/RoleController.php

132 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2018-08-06 12:30:51 +00:00
<?php
2018-09-04 14:51:04 +00:00
namespace App\Http\Controllers\Settings\Access;
2018-08-06 12:30:51 +00:00
2018-08-29 15:18:15 +00:00
use App\Models\Role;
use App\Models\Permission;
2018-08-06 12:30:51 +00:00
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RoleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$roles = Role::all();
2018-09-04 14:51:04 +00:00
return view('settings.access.role.role', compact('roles'));
2018-08-06 12:30:51 +00:00
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$permissions = Permission::all('id', 'name');
2018-09-04 14:51:04 +00:00
return view('settings.access.role.create', compact('permissions'));
2018-08-06 12:30:51 +00:00
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
2018-08-29 15:18:15 +00:00
$this->validate($request, [
'name' => 'required',
'display_name' => 'max:255',
'description' => 'max:255'
]);
//$input = $request->all();
$input = $request->except('permissions');
//$input = $request->only(['name']); //Retreive the name field
$role = Role::create($input);
$permissions = $request['permissions']; //Retrieving permissions
//Checking if a role was selected
if (isset($permissions)) {
foreach ($permissions as $permission) {
$permission_r = Permission::where('id', '=', $permission)->firstOrFail();
$role->attachPermission($permission_r); //Assigning permission to role
}
}
return redirect()
2018-10-18 14:51:46 +00:00
->route('access.role.index')
->with('success', 'Role has been created successfully');
2018-08-06 12:30:51 +00:00
}
/**
* Display the specified resource.
*
* @param \App\Role $role
* @return \Illuminate\Http\Response
*/
public function show(Role $role)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Role $role
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$role = Role::find($id);
$permissions = Permission::all('id', 'name');
//$userRoles = $user->roles->pluck('name','name')->all();
2018-08-29 15:18:15 +00:00
$checkeds = $role->perms->pluck('id')->toArray();
2018-09-04 14:51:04 +00:00
return view('settings.access.role.edit', compact('role', 'permissions', 'checkeds'));
2018-08-06 12:30:51 +00:00
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Role $role
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate(request(), [
2018-08-29 15:18:15 +00:00
'name' => 'required',
'display_name' => 'max:255',
'description' => 'max:255'
2018-08-06 12:30:51 +00:00
]);
$role = Role::findOrFail($id);
$role->update($request->except('permissions'));
$permissions = $request->input('permissions') ? $request->input('permissions') : [];
//$role->syncPermissions($permissions);
if (isset($permissions)) {
2018-08-29 15:18:15 +00:00
$role->perms()->sync($permissions);//If one or more role is selected associate user to roles
2018-08-06 12:30:51 +00:00
} else {
2018-08-29 15:18:15 +00:00
$role->perms()->detach(); //If no role is selected remove exisiting role associated to a user
2018-08-06 12:30:51 +00:00
}
2018-09-04 14:51:04 +00:00
return redirect()->route('access.role.index')
2018-08-06 12:30:51 +00:00
->with('flash_message', 'Role successfully edited.');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Role $role
* @return \Illuminate\Http\Response
*/
public function destroy(Role $role)
{
//
}
}