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

226 lines
6.0 KiB
PHP
Raw Normal View History

2018-06-25 11:00:42 +00:00
<?php
namespace Webkul\User\Http\Controllers;
use Illuminate\Support\Str;
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\AdminRepository;
use Webkul\User\Repositories\RoleRepository;
2018-07-02 09:29:27 +00:00
use Webkul\User\Http\Requests\UserForm;
2018-12-22 10:42:42 +00:00
use Hash;
2018-06-25 11:00:42 +00:00
/**
* Admin user controller
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class UserController extends Controller
{
/**
* Contains route related configuration
*
* @var array
*/
2018-06-25 11:00:42 +00:00
protected $_config;
2018-07-31 07:50:54 +00:00
/**
* AdminRepository object
*
2019-07-01 11:33:36 +00:00
* @var Object
2018-07-31 07:50:54 +00:00
*/
2019-07-01 11:33:36 +00:00
protected $adminRepository;
2018-07-31 07:50:54 +00:00
/**
* RoleRepository object
*
2019-07-01 11:33:36 +00:00
* @var Object
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
/**
* Create a new controller instance.
*
2019-07-01 11:33:36 +00:00
* @param \Webkul\User\Repositories\AdminRepository $adminRepository
* @param \Webkul\User\Repositories\RoleRepository $roleRepository
* @return void
*/
2019-07-01 11:33:36 +00:00
public function __construct(
AdminRepository $adminRepository,
RoleRepository $roleRepository
)
2018-06-25 11:00:42 +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();
if (isset($data['password']) && $data['password']) {
$data['password'] = bcrypt($data['password']);
$data['api_token'] = Str::random(80);
}
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.
*
2019-08-19 10:31:38 +00:00
* @param integer $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
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 update(UserForm $request, $id)
2018-06-25 11:00:42 +00:00
{
2019-07-24 07:50:22 +00:00
$data = $request->all();
2018-08-30 13:22:15 +00:00
if (! $data['password']) {
2018-08-30 13:22:15 +00:00
unset($data['password']);
} else {
$data['password'] = bcrypt($data['password']);
}
2018-08-30 13:22:15 +00:00
if (isset($data['status'])) {
$data['status'] = 1;
} else {
$data['status'] = 0;
}
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
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
2019-12-24 14:01:13 +00:00
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
2018-06-25 11:00:42 +00:00
*/
public function destroy($id)
{
2019-07-01 11:33:36 +00:00
$user = $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) {
return view('admin::customers.confirm-password');
}
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
return response()->json(['message' => true], 200);
2019-07-24 07:50:22 +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
}
return response()->json(['message' => false], 400);
2018-06-25 11:00:42 +00:00
}
2018-12-22 10:42:42 +00:00
/**
* destroy current after confirming
*
* @return mixed
*/
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']);
}
}
}