sarga/packages/Webkul/User/src/Http/Controllers/RoleController.php

186 lines
5.1 KiB
PHP
Raw Normal View History

2018-07-02 09:29:27 +00:00
<?php
namespace Webkul\User\Http\Controllers;
2018-12-21 12:48:34 +00:00
use Illuminate\Support\Facades\Event;
2021-10-26 11:08:49 +00:00
use Webkul\User\Repositories\AdminRepository;
2019-07-01 11:33:36 +00:00
use Webkul\User\Repositories\RoleRepository;
2018-07-02 09:29:27 +00:00
class RoleController extends Controller
{
/**
2021-10-26 11:08:49 +00:00
* Contains route related configuration.
*
* @var array
*/
2018-07-02 09:29:27 +00:00
protected $_config;
2018-07-31 07:50:54 +00:00
/**
2021-10-26 11:08:49 +00:00
* Role repository instance.
2018-07-31 07:50:54 +00:00
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\User\Repositories\RoleRepository
2018-07-31 07:50:54 +00:00
*/
2019-07-01 11:33:36 +00:00
protected $roleRepository;
2018-07-02 09:29:27 +00:00
2021-10-09 22:27:37 +00:00
/**
2021-10-26 11:08:49 +00:00
* Admin repository instance.
2021-10-09 22:27:37 +00:00
*
* @var \Webkul\User\Repositories\AdminRepository
*/
protected $adminRepository;
/**
* Create a new controller instance.
*
2021-10-09 22:27:37 +00:00
* @param \Webkul\User\Repositories\AdminRepository $adminRepository
2020-03-05 13:37:08 +00:00
* @param \Webkul\User\Repositories\RoleRepository $roleRepository
* @return void
*/
2021-10-26 11:08:49 +00:00
public function __construct(
RoleRepository $roleRepository,
AdminRepository $adminRepository
) {
$this->middleware('admin');
2019-07-01 11:33:36 +00:00
$this->roleRepository = $roleRepository;
2021-10-26 11:08:49 +00:00
2021-10-09 22:27:37 +00:00
$this->adminRepository = $adminRepository;
2018-07-31 07:50:54 +00:00
2018-07-02 09:29:27 +00:00
$this->_config = request('_config');
}
/**
* Display a listing of the resource.
*
2019-12-24 14:01:13 +00:00
* @return \Illuminate\View\View
2018-07-02 09:29:27 +00:00
*/
public function index()
{
return view($this->_config['view']);
}
/**
* Show the form for creating a new resource.
*
2019-12-24 14:01:13 +00:00
* @return \Illuminate\View\View
2018-07-02 09:29:27 +00:00
*/
public function create()
{
2019-03-13 13:42:00 +00:00
return view($this->_config['view']);
2018-07-02 09:29:27 +00:00
}
/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
2019-07-01 11:33:36 +00:00
public function store()
2018-07-02 09:29:27 +00:00
{
$this->validate(request(), [
2020-02-20 07:14:00 +00:00
'name' => 'required',
'permission_type' => 'required',
]);
2019-12-24 14:01:13 +00:00
Event::dispatch('user.role.create.before');
2018-12-21 12:48:34 +00:00
2019-07-01 11:33:36 +00:00
$role = $this->roleRepository->create(request()->all());
2018-12-21 12:48:34 +00:00
2019-12-24 14:01:13 +00:00
Event::dispatch('user.role.create.after', $role);
2018-07-02 09:29:27 +00:00
2019-01-16 08:38:39 +00:00
session()->flash('success', trans('admin::app.response.create-success', ['name' => 'Role']));
2018-07-02 09:29:27 +00:00
return redirect()->route($this->_config['redirect']);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
2019-12-24 14:01:13 +00:00
* @return \Illuminate\View\View
2018-07-02 09:29:27 +00:00
*/
public function edit($id)
{
2019-07-01 11:33:36 +00:00
$role = $this->roleRepository->findOrFail($id);
2018-07-02 09:29:27 +00:00
return view($this->_config['view'], compact('role'));
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
2019-07-01 11:33:36 +00:00
public function update($id)
2018-07-02 09:29:27 +00:00
{
2018-07-11 05:41:27 +00:00
$this->validate(request(), [
2020-02-20 07:14:00 +00:00
'name' => 'required',
2018-07-11 05:41:27 +00:00
'permission_type' => 'required',
]);
2021-10-09 22:27:37 +00:00
$params = request()->all();
2021-10-26 11:08:49 +00:00
/**
* Check for other admins if the role has been changed from all to custom.
*/
2021-10-09 22:27:37 +00:00
$isChangedFromAll = $params['permission_type'] == "custom" && $this->roleRepository->find($id)->permission_type == 'all';
if ($isChangedFromAll) {
$adminCountWithAllAccess = $this->adminRepository->getModel()
->leftJoin('roles', 'admins.role_id', '=', 'roles.id')
->where(["roles.permission_type" => "all"])
->get()
->count();
2021-10-26 11:08:49 +00:00
2021-10-09 22:27:37 +00:00
if ($adminCountWithAllAccess == 1) {
session()->flash('error', trans('admin::app.response.being-used', ['name' => 'Role', 'source' => 'Admin User']));
return redirect()->route($this->_config['redirect']);
}
}
2019-12-24 14:01:13 +00:00
Event::dispatch('user.role.update.before', $id);
2018-12-21 12:48:34 +00:00
2021-10-09 22:27:37 +00:00
$role = $this->roleRepository->update($params, $id);
2018-12-21 12:48:34 +00:00
2019-12-24 14:01:13 +00:00
Event::dispatch('user.role.update.after', $role);
2018-07-02 09:29:27 +00:00
2019-01-16 08:38:39 +00:00
session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Role']));
2018-07-02 09:29:27 +00:00
return redirect()->route($this->_config['redirect']);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
2019-07-01 11:33:36 +00:00
$role = $this->roleRepository->findOrFail($id);
2019-04-09 01:01:52 +00:00
if ($role->admins->count() >= 1) {
2019-04-09 07:59:31 +00:00
session()->flash('error', trans('admin::app.response.being-used', ['name' => 'Role', 'source' => 'Admin User']));
2021-10-26 11:08:49 +00:00
} elseif ($this->roleRepository->count() == 1) {
2019-04-09 01:01:52 +00:00
session()->flash('error', trans('admin::app.response.last-delete-error', ['name' => 'Role']));
2018-10-17 07:21:47 +00:00
} else {
2019-04-09 01:01:52 +00:00
try {
2019-12-24 14:01:13 +00:00
Event::dispatch('user.role.delete.before', $id);
2019-04-09 01:01:52 +00:00
2019-07-01 11:33:36 +00:00
$this->roleRepository->delete($id);
2018-12-21 12:48:34 +00:00
2019-12-24 14:01:13 +00:00
Event::dispatch('user.role.delete.after', $id);
2018-10-17 07:21:47 +00:00
2019-04-09 01:01:52 +00:00
session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Role']));
2018-12-21 12:48:34 +00:00
return response()->json(['message' => true], 200);
2021-10-26 11:08:49 +00:00
} catch (\Exception $e) {
2019-04-09 01:01:52 +00:00
session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Role']));
}
2018-10-17 07:21:47 +00:00
}
return response()->json(['message' => false], 400);
2018-07-02 09:29:27 +00:00
}
2021-10-26 11:08:49 +00:00
}