2019-06-21 04:52:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Controller;
|
2019-06-21 04:52:37 +08:00
|
|
|
use App\Http\Requests\CreateRoleRequest;
|
|
|
|
use App\Http\Requests\UpdateRoleRequest;
|
|
|
|
use App\Repositories\PermissionsRepository;
|
2019-06-21 11:19:35 +08:00
|
|
|
use App\Repositories\RoleRepository;
|
2020-02-09 02:29:34 +08:00
|
|
|
use App\Services\RoleService;
|
2019-06-21 04:52:37 +08:00
|
|
|
use Illuminate\Http\Request;
|
2020-02-09 02:29:34 +08:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Laracasts\Flash\Flash;
|
2019-06-21 04:52:37 +08:00
|
|
|
use Prettus\Repository\Criteria\RequestCriteria;
|
|
|
|
|
|
|
|
class RolesController extends Controller
|
|
|
|
{
|
|
|
|
private $permsRepo;
|
|
|
|
private $rolesRepo;
|
2020-02-09 02:29:34 +08:00
|
|
|
private $roleSvc;
|
2019-06-21 04:52:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AirlinesController constructor.
|
|
|
|
*
|
|
|
|
* @param PermissionsRepository $permsRepo
|
|
|
|
* @param RoleRepository $rolesRepo
|
2020-02-09 02:29:34 +08:00
|
|
|
* @param $roleSvc
|
2019-06-21 04:52:37 +08:00
|
|
|
*/
|
2020-02-09 02:29:34 +08:00
|
|
|
public function __construct(
|
|
|
|
PermissionsRepository $permsRepo,
|
|
|
|
RoleRepository $rolesRepo,
|
|
|
|
RoleService $roleSvc
|
|
|
|
) {
|
2019-06-21 04:52:37 +08:00
|
|
|
$this->permsRepo = $permsRepo;
|
|
|
|
$this->rolesRepo = $rolesRepo;
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->roleSvc = $roleSvc;
|
2019-06-21 04:52:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the Airlines.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
2019-06-21 11:19:35 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2019-06-21 04:52:37 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
|
|
|
$this->rolesRepo->pushCriteria(new RequestCriteria($request));
|
|
|
|
$roles = $this->rolesRepo->findWhere(['read_only' => false]);
|
|
|
|
|
|
|
|
return view('admin.roles.index', [
|
|
|
|
'roles' => $roles,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new Airlines.
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return view('admin.roles.create', [
|
|
|
|
'permissions' => $this->permsRepo->all(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created Airlines in storage.
|
|
|
|
*
|
|
|
|
* @param CreateRoleRequest $request
|
|
|
|
*
|
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2019-06-21 11:19:35 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2019-06-21 04:52:37 +08:00
|
|
|
*/
|
|
|
|
public function store(CreateRoleRequest $request)
|
|
|
|
{
|
|
|
|
$input = $request->all();
|
|
|
|
|
|
|
|
// Create a slug using the display name provided
|
|
|
|
$input['name'] = str_slug($input['display_name']);
|
|
|
|
|
|
|
|
$this->rolesRepo->create($input);
|
|
|
|
|
|
|
|
Flash::success('Role saved successfully.');
|
|
|
|
return redirect(route('admin.roles.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified role
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$roles = $this->rolesRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
if (empty($roles)) {
|
|
|
|
Flash::error('Role not found');
|
|
|
|
return redirect(route('admin.roles.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.roles.show', [
|
|
|
|
'roles' => $roles,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified roles
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
$role = $this->rolesRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
if (empty($role)) {
|
|
|
|
Flash::error('Role not found');
|
|
|
|
return redirect(route('admin.role.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.roles.edit', [
|
|
|
|
'role' => $role,
|
|
|
|
'permissions' => $this->permsRepo->all(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified Airlines in storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @param UpdateRoleRequest $request
|
|
|
|
*
|
2019-06-21 11:19:35 +08:00
|
|
|
* @return Response
|
2019-06-21 04:52:37 +08:00
|
|
|
*/
|
|
|
|
public function update($id, UpdateRoleRequest $request)
|
|
|
|
{
|
|
|
|
$role = $this->rolesRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
if (empty($role)) {
|
|
|
|
Flash::error('Role not found');
|
|
|
|
return redirect(route('admin.roles.index'));
|
|
|
|
}
|
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->roleSvc->updateRole($role, $request->all());
|
|
|
|
$this->roleSvc->setPermissionsForRole($role, $request->permissions);
|
2019-06-21 04:52:37 +08:00
|
|
|
|
|
|
|
Flash::success('Roles updated successfully.');
|
|
|
|
return redirect(route('admin.roles.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified Airlines from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
$roles = $this->rolesRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
if (empty($roles)) {
|
|
|
|
Flash::error('Role not found');
|
|
|
|
return redirect(route('admin.roles.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->rolesRepo->delete($id);
|
|
|
|
|
|
|
|
Flash::success('Role deleted successfully.');
|
|
|
|
return redirect(route('admin.roles.index'));
|
|
|
|
}
|
|
|
|
}
|