2018-06-25 11:00:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\User\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\Response;
|
2018-12-21 12:48:34 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2018-07-31 07:50:54 +00:00
|
|
|
use Webkul\User\Repositories\AdminRepository as Admin;
|
|
|
|
|
use Webkul\User\Repositories\RoleRepository as Role;
|
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
|
|
|
|
|
{
|
2018-07-05 07:58:26 +00:00
|
|
|
/**
|
|
|
|
|
* Contains route related configuration
|
|
|
|
|
*
|
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
|
* AdminRepository object
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $admin;
|
2018-08-31 06:03:11 +00:00
|
|
|
|
2018-07-31 07:50:54 +00:00
|
|
|
/**
|
|
|
|
|
* RoleRepository object
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $role;
|
2018-06-25 11:00:42 +00:00
|
|
|
|
2018-07-05 07:58:26 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
2018-07-31 07:50:54 +00:00
|
|
|
* @param Webkul\User\Repositories\AdminRepository $admin
|
|
|
|
|
* @param Webkul\User\Repositories\RoleRepository $role
|
2018-07-05 07:58:26 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-07-31 07:50:54 +00:00
|
|
|
public function __construct(Admin $admin, Role $role)
|
2018-06-25 11:00:42 +00:00
|
|
|
{
|
2018-07-31 07:50:54 +00:00
|
|
|
$this->admin = $admin;
|
|
|
|
|
|
|
|
|
|
$this->role = $role;
|
|
|
|
|
|
2018-06-25 11:00:42 +00:00
|
|
|
$this->_config = request('_config');
|
|
|
|
|
|
|
|
|
|
$this->middleware('guest', ['except' => 'destroy']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
return view($this->_config['view']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
2018-07-31 07:50:54 +00:00
|
|
|
$roles = $this->role->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
|
2018-06-25 11:00:42 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2018-07-02 09:29:27 +00:00
|
|
|
public function store(UserForm $request)
|
2018-06-25 11:00:42 +00:00
|
|
|
{
|
2018-11-21 11:14:07 +00:00
|
|
|
$data = request()->all();
|
2018-12-19 06:50:43 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (isset($data['password']) && $data['password'])
|
2018-11-21 11:14:07 +00:00
|
|
|
$data['password'] = bcrypt($data['password']);
|
|
|
|
|
|
2018-12-21 12:48:34 +00:00
|
|
|
Event::fire('user.admin.create.before');
|
2018-12-22 10:42:42 +00:00
|
|
|
|
2018-12-21 12:48:34 +00:00
|
|
|
$admin = $this->admin->create($data);
|
|
|
|
|
|
|
|
|
|
Event::fire('user.admin.delete.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.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function edit($id)
|
|
|
|
|
{
|
2018-08-22 09:44:35 +00:00
|
|
|
$user = $this->admin->find($id);
|
2018-07-31 07:50:54 +00:00
|
|
|
|
|
|
|
|
$roles = $this->role->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
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2018-07-02 09:29:27 +00:00
|
|
|
public function update(UserForm $request, $id)
|
2018-06-25 11:00:42 +00:00
|
|
|
{
|
2018-08-30 13:22:15 +00:00
|
|
|
$data = request()->all();
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $data['password'])
|
2018-08-30 13:22:15 +00:00
|
|
|
unset($data['password']);
|
2018-11-21 11:14:07 +00:00
|
|
|
else
|
|
|
|
|
$data['password'] = bcrypt($data['password']);
|
2018-08-30 13:22:15 +00:00
|
|
|
|
2018-12-19 06:50:43 +00:00
|
|
|
if (isset($data['status'])) {
|
|
|
|
|
$data['status'] = 1;
|
|
|
|
|
} else {
|
|
|
|
|
$data['status'] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 12:48:34 +00:00
|
|
|
Event::fire('user.admin.update.before', $id);
|
|
|
|
|
|
|
|
|
|
$admin = $this->admin->update($data, $id);
|
|
|
|
|
|
|
|
|
|
Event::fire('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
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function destroy($id)
|
|
|
|
|
{
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($this->admin->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 {
|
2018-12-21 12:48:34 +00:00
|
|
|
Event::fire('user.admin.delete.before', $id);
|
|
|
|
|
|
2018-12-22 10:42:42 +00:00
|
|
|
if (auth()->guard('admin')->user()->id == $id) {
|
|
|
|
|
return view('admin::customers.confirm-password');
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-17 07:21:47 +00:00
|
|
|
$this->admin->delete($id);
|
|
|
|
|
|
2018-12-21 12:48:34 +00:00
|
|
|
Event::fire('user.admin.delete.after', $id);
|
|
|
|
|
|
2019-01-16 08:38:39 +00:00
|
|
|
session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Admin source']));
|
2018-10-17 07:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
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)) {
|
|
|
|
|
if ($this->admin->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;
|
|
|
|
|
|
|
|
|
|
Event::fire('user.admin.delete.before', $id);
|
|
|
|
|
|
|
|
|
|
$this->admin->delete($id);
|
|
|
|
|
|
|
|
|
|
Event::fire('user.admin.delete.after', $id);
|
|
|
|
|
|
|
|
|
|
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']);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-25 11:00:42 +00:00
|
|
|
}
|