tethys/app/Http/Controllers/Settings/Access/UserController.php

229 lines
7.2 KiB
PHP
Raw Normal View History

2018-08-06 12:30:51 +00:00
<?php
2018-09-04 14:51:04 +00:00
namespace App\Http\Controllers\Settings\Access;
2018-08-06 12:30:51 +00:00
use App\Http\Controllers\Controller;
use App\Mail\NewUser;
2018-08-29 15:18:15 +00:00
use App\Models\Role;
2018-09-10 13:09:10 +00:00
use App\Models\User;
2018-08-29 15:18:15 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
2018-08-06 12:30:51 +00:00
class UserController extends Controller
{
2018-08-29 15:18:15 +00:00
// public function __construct()
// {
// $this->middleware('permission:settings');
// }
2018-08-06 12:30:51 +00:00
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
//if (! Gate::allows('settings'))
//{
// return abort(401, 'Unauthorized action.');
//}
2018-08-29 15:18:15 +00:00
$users = User::with('roles')
->orderByDesc('id')
2018-10-18 14:51:46 +00:00
->paginate(5);
2018-09-04 14:51:04 +00:00
return view('settings.access.user.user', compact('users'))
2018-08-29 15:18:15 +00:00
->with('i', ($request->input('page', 1) - 1) * 5);
2018-08-06 12:30:51 +00:00
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//$roles = Role::pluck('name','name')->all();
2018-08-29 15:18:15 +00:00
$roles = Role::all('id', 'name');
2018-09-04 14:51:04 +00:00
return view('settings.access.user.create', compact('roles'));
2018-08-06 12:30:51 +00:00
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'login' => 'required',
'email' => 'required|email|unique:accounts',
2018-08-29 15:18:15 +00:00
'password' => 'required|min:6|confirmed',
2018-08-06 12:30:51 +00:00
//'roles' => 'required'
]);
//$input = $request->all();
$input = $request->only(['login', 'email', 'password']); //Retreive the name, email and password fields
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
2018-08-29 15:18:15 +00:00
$roles = $request['roles']; //Retrieving roles
2018-08-06 12:30:51 +00:00
//Checking if a role was selected
if (isset($roles)) {
foreach ($roles as $role) {
$role_r = Role::where('id', '=', $role)->firstOrFail();
2018-08-29 15:18:15 +00:00
$user->attachRole($role_r); //Assigning role to user
2018-08-06 12:30:51 +00:00
}
}
// inform main admin about new user
$adminUser = User::where('email', config('mail.mailadmin'))->first();
if ($adminUser) {
// Mail::to("receiver@example.com")->send(new DemoEmail($objDemo));
$details = [
'title' => 'New user ',
'admin_name' => $adminUser->login,
'email' => $user->email,
];
Mail::to($adminUser->email)->send(new NewUser($details));
}
2018-08-06 12:30:51 +00:00
return redirect()
2018-09-04 14:51:04 +00:00
->route('access.user.index')
2018-08-06 12:30:51 +00:00
->with('success', 'User has been created successfully');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$user = User::find($id);
2018-09-04 14:51:04 +00:00
return view('settings.access.user.show', compact('user'));
2018-08-06 12:30:51 +00:00
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$user = User::find($id);
if ($user == null) {
return abort(404, 'User not found.');
}
2018-08-06 12:30:51 +00:00
$roles = Role::all('id', 'name');
2018-08-06 12:30:51 +00:00
//$userRoles = $user->roles->pluck('name','name')->all();
$checkeds = $user->roles->pluck('id')->toArray();
2018-09-04 14:51:04 +00:00
return view('settings.access.user.edit', compact('user', 'roles', 'checkeds'));
2018-08-06 12:30:51 +00:00
}
private function validateUser($id, $current_password)
{
$user = User::findOrFail($id);
return Hash::check($current_password, $user->password);
}
2018-08-06 12:30:51 +00:00
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
// if model state is valid
2018-08-06 12:30:51 +00:00
$this->validate(request(), [
'login' => 'required',
'email' => 'required|email|unique:accounts,email,' . $id,
'password' => 'nullable|min:6|confirmed',
//'current_password' => 'required_with:password'
2018-08-06 12:30:51 +00:00
]);
2018-08-29 15:18:15 +00:00
$valid = true;
2018-08-29 15:18:15 +00:00
$user = User::findOrFail($id);
$roles = Role::all('id', 'name');
$input = $request->all();
$flash_message = '';
$errors = new \Illuminate\Support\MessageBag();
if (array_key_exists('current_password', $input)) {
// if user is not admin he must enter old_password if a new password is defined
if (!Auth::user()->hasRole('Administrator') && $input['current_password'] == null && $input['password'] != null) {
//ModelState.AddModelError("OldPassword", Resources.User_Edit_OldPasswordEmpty);
//$flash_message = 'Current password should not be empty.';
// add your error messages:
$errors->add('your_custom_error', 'Current password cannot not be empty, if you define a new password');
$valid = false;
}
2018-08-06 12:30:51 +00:00
if ($input['current_password'] != null && $this->validateUser($user->id, $input['current_password']) == false) {
//$flash_message = 'Password does not match the current password.';
$errors->add('your_custom_error', 'Password does not match the current password.');
$valid = false;
}
}
2018-08-06 12:30:51 +00:00
//$input = $request->only(['login', 'email', 'password']); //Retreive the name, email and password fields
if ($valid == true) {
$user->login = $input['login'];
$user->email = $input['email'];
if ($input['password']) {
$user->password = Hash::make($input['password']);
}
$user->save();
2018-08-06 12:30:51 +00:00
$roles = $request['roles']; //Retreive all roles
2018-08-29 15:18:15 +00:00
if (array_key_exists('roles', $input)) {
if (isset($roles)) {
$user->roles()->sync($roles); //If one or more role is selected associate user to roles
} else {
$user->roles()->detach(); //If no role is selected remove exisiting role associated to a user
}
}
return back()->with('flash_message', 'user successfully updated.');
// return redirect()
// ->route('access.user.index')
// ->with('flash_message', 'User successfully edited.');
}
return back()
->withInput($input)
->withErrors($errors);
2018-08-06 12:30:51 +00:00
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//Find a user with a given id and delete
$user = User::findOrFail($id);
$user->delete();
return redirect()
2018-09-04 14:51:04 +00:00
->route('access.user.index')
2018-08-06 12:30:51 +00:00
->with('flash_message', 'User successfully deleted.');
}
}