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;
|
2019-07-01 11:33:36 +00:00
|
|
|
use Webkul\User\Repositories\RoleRepository;
|
2018-07-02 09:29:27 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Admin user role controller
|
|
|
|
|
*
|
|
|
|
|
* @author Jitendra Singh <jitendra@webkul.com>
|
|
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
|
|
|
|
class RoleController extends Controller
|
|
|
|
|
{
|
2018-07-05 07:58:26 +00:00
|
|
|
/**
|
|
|
|
|
* Contains route related configuration
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2018-07-02 09:29:27 +00:00
|
|
|
protected $_config;
|
2018-08-31 06:03:11 +00:00
|
|
|
|
2018-07-31 07:50:54 +00:00
|
|
|
/**
|
|
|
|
|
* RoleRepository object
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2019-07-01 11:33:36 +00:00
|
|
|
protected $roleRepository;
|
2018-07-02 09:29:27 +00:00
|
|
|
|
2018-07-05 07:58:26 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
2019-07-01 11:33:36 +00:00
|
|
|
* @param \Webkul\User\Repositories\RoleRepository $roleRepository
|
2018-07-05 07:58:26 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2019-07-01 11:33:36 +00:00
|
|
|
public function __construct(RoleRepository $roleRepository)
|
2018-07-02 09:29:27 +00:00
|
|
|
{
|
2018-08-31 06:03:11 +00:00
|
|
|
$this->middleware('admin');
|
|
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->roleRepository = $roleRepository;
|
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-08-19 09:30:24 +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-08-19 09:30:24 +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
|
|
|
{
|
2018-07-05 07:58:26 +00:00
|
|
|
$this->validate(request(), [
|
|
|
|
|
'name' => 'required',
|
|
|
|
|
'permission_type' => 'required',
|
|
|
|
|
]);
|
|
|
|
|
|
2018-12-21 12:48:34 +00:00
|
|
|
Event::fire('user.role.create.before');
|
|
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$role = $this->roleRepository->create(request()->all());
|
2018-12-21 12:48:34 +00:00
|
|
|
|
|
|
|
|
Event::fire('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-08-19 09:30:24 +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(), [
|
|
|
|
|
'name' => 'required',
|
|
|
|
|
'permission_type' => 'required',
|
|
|
|
|
]);
|
2018-08-31 06:03:11 +00:00
|
|
|
|
2018-12-21 12:48:34 +00:00
|
|
|
Event::fire('user.role.update.before', $id);
|
|
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$role = $this->roleRepository->update(request()->all(), $id);
|
2018-12-21 12:48:34 +00:00
|
|
|
|
|
|
|
|
Event::fire('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']));
|
2019-07-01 11:33:36 +00:00
|
|
|
} else if($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 {
|
|
|
|
|
Event::fire('user.role.delete.before', $id);
|
|
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->roleRepository->delete($id);
|
2018-12-21 12:48:34 +00:00
|
|
|
|
2019-04-09 01:01:52 +00:00
|
|
|
Event::fire('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
|
|
|
|
2019-04-09 07:47:19 +00:00
|
|
|
return response()->json(['message' => true], 200);
|
2019-04-09 01:01:52 +00:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
|
session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Role']));
|
|
|
|
|
}
|
2018-10-17 07:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-09 07:47:19 +00:00
|
|
|
return response()->json(['message' => false], 400);
|
2018-07-02 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
}
|