2018-06-25 11:00:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\User\Http\Controllers;
|
|
|
|
|
|
2020-08-01 09:37:51 +00:00
|
|
|
use Hash;
|
2018-12-21 12:48:34 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2021-10-26 12:49:00 +00:00
|
|
|
use Illuminate\Support\Str;
|
2018-07-02 09:29:27 +00:00
|
|
|
use Webkul\User\Http\Requests\UserForm;
|
2020-08-01 09:37:51 +00:00
|
|
|
use Webkul\User\Repositories\AdminRepository;
|
2021-10-26 12:49:00 +00:00
|
|
|
use Webkul\User\Repositories\RoleRepository;
|
2018-06-25 11:00:42 +00:00
|
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
|
{
|
2018-07-05 07:58:26 +00:00
|
|
|
/**
|
2021-10-26 12:49:00 +00:00
|
|
|
* Contains route related configuration.
|
2018-07-05 07:58:26 +00:00
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2018-06-25 11:00:42 +00:00
|
|
|
protected $_config;
|
2018-08-31 06:03:11 +00:00
|
|
|
|
2018-07-31 07:50:54 +00:00
|
|
|
/**
|
2021-10-26 12:49:00 +00:00
|
|
|
* Admin repository instance.
|
2018-07-31 07:50:54 +00:00
|
|
|
*
|
2020-03-05 13:37:08 +00:00
|
|
|
* @var \Webkul\User\Repositories\AdminRepository
|
2018-07-31 07:50:54 +00:00
|
|
|
*/
|
2019-07-01 11:33:36 +00:00
|
|
|
protected $adminRepository;
|
2018-08-31 06:03:11 +00:00
|
|
|
|
2018-07-31 07:50:54 +00:00
|
|
|
/**
|
2021-10-26 12:49:00 +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-06-25 11:00:42 +00:00
|
|
|
|
2018-07-05 07:58:26 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
2020-03-05 13:37:08 +00:00
|
|
|
* @param \Webkul\User\Repositories\AdminRepository $adminRepository
|
|
|
|
|
* @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(
|
|
|
|
|
AdminRepository $adminRepository,
|
|
|
|
|
RoleRepository $roleRepository
|
2021-10-26 12:49:00 +00:00
|
|
|
) {
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->adminRepository = $adminRepository;
|
2018-07-31 07:50:54 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->roleRepository = $roleRepository;
|
2018-07-31 07:50:54 +00:00
|
|
|
|
2018-06-25 11:00:42 +00:00
|
|
|
$this->_config = request('_config');
|
|
|
|
|
|
|
|
|
|
$this->middleware('guest', ['except' => 'destroy']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
2019-12-24 14:01:13 +00:00
|
|
|
* @return \Illuminate\View\View
|
2018-06-25 11:00:42 +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-06-25 11:00:42 +00:00
|
|
|
*/
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
2019-07-01 11:33:36 +00:00
|
|
|
$roles = $this->roleRepository->all();
|
2018-07-02 09:29:27 +00:00
|
|
|
|
|
|
|
|
return view($this->_config['view'], compact('roles'));
|
2018-06-25 11:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
|
*
|
2018-07-02 09:29:27 +00:00
|
|
|
* @param \Webkul\User\Http\Requests\UserForm $request
|
2019-07-24 07:50:22 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2018-06-25 11:00:42 +00:00
|
|
|
*/
|
2018-07-02 09:29:27 +00:00
|
|
|
public function store(UserForm $request)
|
2018-06-25 11:00:42 +00:00
|
|
|
{
|
2019-07-24 07:50:22 +00:00
|
|
|
$data = $request->all();
|
2018-12-19 06:50:43 +00:00
|
|
|
|
2020-01-03 08:14:12 +00:00
|
|
|
if (isset($data['password']) && $data['password']) {
|
2018-11-21 11:14:07 +00:00
|
|
|
$data['password'] = bcrypt($data['password']);
|
2020-01-03 08:14:12 +00:00
|
|
|
$data['api_token'] = Str::random(80);
|
|
|
|
|
}
|
2018-11-21 11:14:07 +00:00
|
|
|
|
2019-12-24 14:01:13 +00:00
|
|
|
Event::dispatch('user.admin.create.before');
|
2018-12-22 10:42:42 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$admin = $this->adminRepository->create($data);
|
2018-12-21 12:48:34 +00:00
|
|
|
|
2019-12-24 14:01:13 +00:00
|
|
|
Event::dispatch('user.admin.create.after', $admin);
|
2018-06-25 11:00:42 +00:00
|
|
|
|
2019-01-16 08:38:39 +00:00
|
|
|
session()->flash('success', trans('admin::app.response.create-success', ['name' => 'User']));
|
2018-07-02 09:29:27 +00:00
|
|
|
|
|
|
|
|
return redirect()->route($this->_config['redirect']);
|
2018-06-25 11:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
|
*
|
2020-03-05 13:37:08 +00:00
|
|
|
* @param int $id
|
2019-12-24 14:01:13 +00:00
|
|
|
* @return \Illuminate\View\View
|
2018-06-25 11:00:42 +00:00
|
|
|
*/
|
|
|
|
|
public function edit($id)
|
|
|
|
|
{
|
2019-07-01 11:33:36 +00:00
|
|
|
$user = $this->adminRepository->findOrFail($id);
|
2018-07-31 07:50:54 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$roles = $this->roleRepository->all();
|
2018-07-02 09:29:27 +00:00
|
|
|
|
|
|
|
|
return view($this->_config['view'], compact('user', 'roles'));
|
2018-06-25 11:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
|
*
|
2018-07-02 09:29:27 +00:00
|
|
|
* @param \Webkul\User\Http\Requests\UserForm $request
|
2018-06-25 11:00:42 +00:00
|
|
|
* @param int $id
|
2020-03-05 13:37:08 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
2018-06-25 11:00:42 +00:00
|
|
|
*/
|
2018-07-02 09:29:27 +00:00
|
|
|
public function update(UserForm $request, $id)
|
2018-06-25 11:00:42 +00:00
|
|
|
{
|
2021-10-27 05:27:47 +00:00
|
|
|
$data = $this->prepareUserData($request, $id);
|
2021-10-26 12:49:00 +00:00
|
|
|
|
2021-10-27 05:27:47 +00:00
|
|
|
if ($data instanceof \Illuminate\Http\RedirectResponse) {
|
|
|
|
|
return $data;
|
2018-12-19 06:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
2019-12-24 14:01:13 +00:00
|
|
|
Event::dispatch('user.admin.update.before', $id);
|
2018-12-21 12:48:34 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$admin = $this->adminRepository->update($data, $id);
|
2018-12-21 12:48:34 +00:00
|
|
|
|
2021-10-27 05:27:47 +00:00
|
|
|
if (isset($data['password']) && $data['password']) {
|
2020-08-01 09:37:51 +00:00
|
|
|
Event::dispatch('user.admin.update-password', $admin);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 14:01:13 +00:00
|
|
|
Event::dispatch('user.admin.update.after', $admin);
|
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' => 'User']));
|
2018-07-02 09:29:27 +00:00
|
|
|
|
|
|
|
|
return redirect()->route($this->_config['redirect']);
|
2018-06-25 11:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
2020-03-05 13:37:08 +00:00
|
|
|
* @return \Illuminate\Http\Response|\Illuminate\View\View
|
2018-06-25 11:00:42 +00:00
|
|
|
*/
|
|
|
|
|
public function destroy($id)
|
|
|
|
|
{
|
2021-10-27 05:27:47 +00:00
|
|
|
$this->adminRepository->findOrFail($id);
|
2019-04-09 01:01:52 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
if ($this->adminRepository->count() == 1) {
|
2019-02-13 12:12:07 +00:00
|
|
|
session()->flash('error', trans('admin::app.response.last-delete-error', ['name' => 'Admin']));
|
2018-10-17 07:21:47 +00:00
|
|
|
} else {
|
2019-12-24 14:01:13 +00:00
|
|
|
Event::dispatch('user.admin.delete.before', $id);
|
2018-12-21 12:48:34 +00:00
|
|
|
|
2018-12-22 10:42:42 +00:00
|
|
|
if (auth()->guard('admin')->user()->id == $id) {
|
2020-02-19 10:52:19 +00:00
|
|
|
return response()->json([
|
|
|
|
|
'redirect' => route('super.users.confirm', ['id' => $id]),
|
|
|
|
|
]);
|
2018-12-22 10:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-09 01:01:52 +00:00
|
|
|
try {
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->adminRepository->delete($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' => 'Admin']));
|
2018-12-21 12:48:34 +00:00
|
|
|
|
2019-12-24 14:01:13 +00:00
|
|
|
Event::dispatch('user.admin.delete.after', $id);
|
2018-12-21 12:48:34 +00:00
|
|
|
|
2019-04-09 07:47:19 +00:00
|
|
|
return response()->json(['message' => true], 200);
|
2021-10-26 12:49:00 +00:00
|
|
|
} catch (\Exception $e) {
|
2019-04-09 01:01:52 +00:00
|
|
|
session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Admin']));
|
|
|
|
|
}
|
2018-10-17 07:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-09 07:47:19 +00:00
|
|
|
return response()->json(['message' => false], 400);
|
2018-06-25 11:00:42 +00:00
|
|
|
}
|
2018-12-22 10:42:42 +00:00
|
|
|
|
2020-02-19 10:52:19 +00:00
|
|
|
/**
|
|
|
|
|
* Show the form for confirming the user password.
|
|
|
|
|
*
|
2020-03-05 13:37:08 +00:00
|
|
|
* @param int $id
|
2020-02-19 10:52:19 +00:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
|
*/
|
|
|
|
|
public function confirm($id)
|
|
|
|
|
{
|
|
|
|
|
$user = $this->adminRepository->findOrFail($id);
|
|
|
|
|
|
|
|
|
|
return view($this->_config['view'], compact('user'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-22 10:42:42 +00:00
|
|
|
/**
|
2021-10-26 12:49:00 +00:00
|
|
|
* Destroy current after confirming.
|
2018-12-22 10:42:42 +00:00
|
|
|
*
|
2020-03-05 13:37:08 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
2018-12-22 10:42:42 +00:00
|
|
|
*/
|
|
|
|
|
public function destroySelf()
|
|
|
|
|
{
|
|
|
|
|
$password = request()->input('password');
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (Hash::check($password, auth()->guard('admin')->user()->password)) {
|
2019-07-01 11:33:36 +00:00
|
|
|
if ($this->adminRepository->count() == 1) {
|
2018-12-22 10:42:42 +00:00
|
|
|
session()->flash('error', trans('admin::app.users.users.delete-last'));
|
|
|
|
|
} else {
|
|
|
|
|
$id = auth()->guard('admin')->user()->id;
|
|
|
|
|
|
2019-12-24 14:01:13 +00:00
|
|
|
Event::dispatch('user.admin.delete.before', $id);
|
2018-12-22 10:42:42 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->adminRepository->delete($id);
|
2018-12-22 10:42:42 +00:00
|
|
|
|
2019-12-24 14:01:13 +00:00
|
|
|
Event::dispatch('user.admin.delete.after', $id);
|
2018-12-22 10:42:42 +00:00
|
|
|
|
|
|
|
|
session()->flash('success', trans('admin::app.users.users.delete-success'));
|
|
|
|
|
|
|
|
|
|
return redirect()->route('admin.session.create');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
session()->flash('warning', trans('admin::app.users.users.incorrect-password'));
|
|
|
|
|
|
|
|
|
|
return redirect()->route($this->_config['redirect']);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-27 05:27:47 +00:00
|
|
|
|
|
|
|
|
/**
|
2021-10-27 05:30:36 +00:00
|
|
|
* Prepare user data.
|
2021-10-27 05:27:47 +00:00
|
|
|
*
|
|
|
|
|
* @param \Webkul\User\Http\Requests\UserForm $request
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return array|\Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
private function prepareUserData(UserForm $request, $id)
|
|
|
|
|
{
|
|
|
|
|
$data = $request->validated();
|
|
|
|
|
|
|
|
|
|
$user = $this->adminRepository->find($id);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Password check.
|
|
|
|
|
*/
|
|
|
|
|
if (! $data['password']) {
|
|
|
|
|
unset($data['password']);
|
|
|
|
|
} else {
|
|
|
|
|
$data['password'] = bcrypt($data['password']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is user with `permission_type` all changed status.
|
|
|
|
|
*/
|
|
|
|
|
$data['status'] = isset($data['status']) ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
$isStatusChangedToInactive = (int) $data['status'] === 0 && (int) $user->status === 1;
|
|
|
|
|
|
|
|
|
|
if ($isStatusChangedToInactive && $this->adminRepository->countAdminsWithAllAccessAndActiveStatus() === 1) {
|
|
|
|
|
return $this->cannotChangeRedirectResponse('status');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is user with `permission_type` all role changed.
|
|
|
|
|
*/
|
|
|
|
|
$isRoleChanged = $user->role->permission_type === 'all'
|
|
|
|
|
&& isset($data['role_id'])
|
|
|
|
|
&& (int) $data['role_id'] !== $user->role_id;
|
|
|
|
|
|
|
|
|
|
if ($isRoleChanged && $this->adminRepository->countAdminsWithAllAccess() === 1) {
|
|
|
|
|
return $this->cannotChangeRedirectResponse('role');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cannot change redirect response.
|
|
|
|
|
*
|
|
|
|
|
* @param string $columnName
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
private function cannotChangeRedirectResponse(string $columnName): \Illuminate\Http\RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
session()->flash('error', trans('admin::app.response.cannot-change', [
|
|
|
|
|
'name' => $columnName
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
return redirect()->route($this->_config['redirect']);
|
|
|
|
|
}
|
2019-07-23 19:59:28 +00:00
|
|
|
}
|