2015-08-08 15:05:30 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Oxbow\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Oxbow\Http\Requests;
|
2015-09-04 15:40:36 -04:00
|
|
|
use Oxbow\Services\SocialAuthService;
|
2015-08-08 15:05:30 -04:00
|
|
|
use Oxbow\User;
|
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UserController constructor.
|
|
|
|
* @param $user
|
|
|
|
*/
|
|
|
|
public function __construct(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
2015-08-29 10:03:42 -04:00
|
|
|
parent::__construct();
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the users.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$users = $this->user->all();
|
2015-08-29 10:03:42 -04:00
|
|
|
return view('users/index', ['users' => $users]);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new user.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermission('user-create');
|
2015-08-08 15:05:30 -04:00
|
|
|
return view('users/create');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created user in storage.
|
|
|
|
*
|
2015-08-29 10:03:42 -04:00
|
|
|
* @param Request $request
|
2015-08-08 15:05:30 -04:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermission('user-create');
|
2015-08-08 15:05:30 -04:00
|
|
|
$this->validate($request, [
|
2015-08-29 10:03:42 -04:00
|
|
|
'name' => 'required',
|
|
|
|
'email' => 'required|email',
|
|
|
|
'password' => 'required|min:5',
|
|
|
|
'password-confirm' => 'required|same:password',
|
|
|
|
'role' => 'required|exists:roles,id'
|
2015-08-08 15:05:30 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $this->user->fill($request->all());
|
|
|
|
$user->password = Hash::make($request->get('password'));
|
|
|
|
$user->save();
|
2015-08-29 10:03:42 -04:00
|
|
|
|
|
|
|
$user->attachRoleId($request->get('role'));
|
2015-08-08 15:05:30 -04:00
|
|
|
return redirect('/users');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified user.
|
|
|
|
*
|
2015-09-04 15:40:36 -04:00
|
|
|
* @param int $id
|
|
|
|
* @param SocialAuthService $socialAuthService
|
2015-08-08 15:05:30 -04:00
|
|
|
* @return Response
|
|
|
|
*/
|
2015-09-04 15:40:36 -04:00
|
|
|
public function edit($id, SocialAuthService $socialAuthService)
|
2015-08-08 15:05:30 -04:00
|
|
|
{
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermissionOr('user-update', function () use ($id) {
|
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-09-04 15:40:36 -04:00
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
$user = $this->user->findOrFail($id);
|
2015-09-04 15:40:36 -04:00
|
|
|
$activeSocialDrivers = $socialAuthService->getActiveDrivers();
|
|
|
|
return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers]);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified user in storage.
|
|
|
|
*
|
2015-08-29 10:03:42 -04:00
|
|
|
* @param Request $request
|
|
|
|
* @param int $id
|
2015-08-08 15:05:30 -04:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermissionOr('user-update', function () use ($id) {
|
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-08-08 15:05:30 -04:00
|
|
|
$this->validate($request, [
|
2015-08-29 10:03:42 -04:00
|
|
|
'name' => 'required',
|
2015-09-04 12:50:52 -04:00
|
|
|
'email' => 'required|email|unique:users,email,' . $id,
|
2015-08-29 10:03:42 -04:00
|
|
|
'password' => 'min:5',
|
|
|
|
'password-confirm' => 'same:password',
|
|
|
|
'role' => 'exists:roles,id'
|
2015-08-08 15:05:30 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $this->user->findOrFail($id);
|
2015-09-04 15:40:36 -04:00
|
|
|
$user->fill($request->except('password'));
|
2015-08-08 15:05:30 -04:00
|
|
|
|
2015-08-29 10:03:42 -04:00
|
|
|
if ($this->currentUser->can('user-update') && $request->has('role')) {
|
|
|
|
$user->attachRoleId($request->get('role'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->has('password') && $request->get('password') != '') {
|
2015-09-04 15:40:36 -04:00
|
|
|
//dd('cat');
|
2015-08-08 15:05:30 -04:00
|
|
|
$password = $request->get('password');
|
|
|
|
$user->password = Hash::make($password);
|
|
|
|
}
|
|
|
|
$user->save();
|
|
|
|
return redirect('/users');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the user delete page.
|
|
|
|
* @param $id
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function delete($id)
|
|
|
|
{
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermissionOr('user-delete', function () use ($id) {
|
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-08-08 15:05:30 -04:00
|
|
|
$user = $this->user->findOrFail($id);
|
|
|
|
return view('users/delete', ['user' => $user]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified user from storage.
|
|
|
|
*
|
2015-08-29 10:03:42 -04:00
|
|
|
* @param int $id
|
2015-08-08 15:05:30 -04:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermissionOr('user-delete', function () use ($id) {
|
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-08-08 15:05:30 -04:00
|
|
|
$user = $this->user->findOrFail($id);
|
|
|
|
$user->delete();
|
|
|
|
return redirect('/users');
|
|
|
|
}
|
|
|
|
}
|