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

158 lines
3.6 KiB
PHP
Raw Normal View History

2018-06-25 11:00:42 +00:00
<?php
namespace Webkul\User\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
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-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
*
* @var array
*/
protected $admin;
2018-07-31 07:50:54 +00:00
/**
* RoleRepository object
*
* @var array
*/
protected $role;
2018-06-25 11:00:42 +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
* @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
{
$this->middleware('admin');
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
{
$data = request()->all();
if(isset($data['password']) && $data['password'])
$data['password'] = bcrypt($data['password']);
$this->admin->create($data);
2018-06-25 11:00:42 +00:00
2018-07-02 09:29:27 +00:00
session()->flash('success', 'User created successfully.');
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();
if(!$data['password'])
unset($data['password']);
else
$data['password'] = bcrypt($data['password']);
2018-08-30 13:22:15 +00:00
$this->admin->update($data, $id);
2018-07-02 09:29:27 +00:00
session()->flash('success', 'User updated successfully.');
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)
{
2018-10-17 07:21:47 +00:00
if($this->admin->count() == 1) {
session()->flash('error', 'At least one admin is required.');
} else {
$this->admin->delete($id);
session()->flash('success', 'Admin source deleted successfully.');
}
return redirect()->back();
2018-06-25 11:00:42 +00:00
}
}