2019-03-18 08:53:00 -03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-09-08 19:45:49 -03:00
|
|
|
use App\Models\User;
|
2019-12-18 18:20:14 -03:00
|
|
|
use Illuminate\Support\Facades\App;
|
2019-03-18 08:53:00 -03:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2019-04-15 22:18:29 -03:00
|
|
|
use Illuminate\Support\Facades\Redirect;
|
2020-07-29 11:58:25 -03:00
|
|
|
use Illuminate\Support\Facades\Request;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use Inertia\Inertia;
|
2019-03-18 08:53:00 -03:00
|
|
|
|
|
|
|
class UsersController extends Controller
|
|
|
|
{
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return Inertia::render('Users/Index', [
|
|
|
|
'filters' => Request::all('search', 'role', 'trashed'),
|
|
|
|
'users' => Auth::user()->account->users()
|
|
|
|
->orderByName()
|
|
|
|
->filter(Request::only('search', 'role', 'trashed'))
|
|
|
|
->get()
|
|
|
|
->transform(function ($user) {
|
|
|
|
return [
|
|
|
|
'id' => $user->id,
|
|
|
|
'name' => $user->name,
|
|
|
|
'email' => $user->email,
|
|
|
|
'owner' => $user->owner,
|
2019-08-09 12:33:47 -03:00
|
|
|
'photo' => $user->photoUrl(['w' => 40, 'h' => 40, 'fit' => 'crop']),
|
2019-03-18 08:53:00 -03:00
|
|
|
'deleted_at' => $user->deleted_at,
|
|
|
|
];
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return Inertia::render('Users/Create');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store()
|
|
|
|
{
|
2019-08-09 12:33:47 -03:00
|
|
|
Request::validate([
|
|
|
|
'first_name' => ['required', 'max:50'],
|
|
|
|
'last_name' => ['required', 'max:50'],
|
|
|
|
'email' => ['required', 'max:50', 'email', Rule::unique('users')],
|
|
|
|
'password' => ['nullable'],
|
|
|
|
'owner' => ['required', 'boolean'],
|
|
|
|
'photo' => ['nullable', 'image'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
Auth::user()->account->users()->create([
|
|
|
|
'first_name' => Request::get('first_name'),
|
|
|
|
'last_name' => Request::get('last_name'),
|
|
|
|
'email' => Request::get('email'),
|
|
|
|
'password' => Request::get('password'),
|
|
|
|
'owner' => Request::get('owner'),
|
|
|
|
'photo_path' => Request::file('photo') ? Request::file('photo')->store('users') : null,
|
|
|
|
]);
|
2019-04-15 22:18:29 -03:00
|
|
|
|
2019-05-21 20:12:49 -03:00
|
|
|
return Redirect::route('users')->with('success', 'User created.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function edit(User $user)
|
|
|
|
{
|
|
|
|
return Inertia::render('Users/Edit', [
|
|
|
|
'user' => [
|
|
|
|
'id' => $user->id,
|
|
|
|
'first_name' => $user->first_name,
|
|
|
|
'last_name' => $user->last_name,
|
|
|
|
'email' => $user->email,
|
|
|
|
'owner' => $user->owner,
|
2019-08-09 12:33:47 -03:00
|
|
|
'photo' => $user->photoUrl(['w' => 60, 'h' => 60, 'fit' => 'crop']),
|
2019-03-18 08:53:00 -03:00
|
|
|
'deleted_at' => $user->deleted_at,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update(User $user)
|
|
|
|
{
|
2019-12-18 19:33:46 -03:00
|
|
|
if (App::environment('demo') && $user->isDemoUser()) {
|
2019-12-18 19:34:14 -03:00
|
|
|
return Redirect::back()->with('error', 'Updating the demo user is not allowed.');
|
2019-12-18 18:20:14 -03:00
|
|
|
}
|
|
|
|
|
2019-03-18 08:53:00 -03:00
|
|
|
Request::validate([
|
|
|
|
'first_name' => ['required', 'max:50'],
|
|
|
|
'last_name' => ['required', 'max:50'],
|
|
|
|
'email' => ['required', 'max:50', 'email', Rule::unique('users')->ignore($user->id)],
|
|
|
|
'password' => ['nullable'],
|
|
|
|
'owner' => ['required', 'boolean'],
|
2019-08-09 12:33:47 -03:00
|
|
|
'photo' => ['nullable', 'image'],
|
2019-03-18 08:53:00 -03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user->update(Request::only('first_name', 'last_name', 'email', 'owner'));
|
|
|
|
|
2019-08-09 12:33:47 -03:00
|
|
|
if (Request::file('photo')) {
|
|
|
|
$user->update(['photo_path' => Request::file('photo')->store('users')]);
|
|
|
|
}
|
|
|
|
|
2019-03-18 08:53:00 -03:00
|
|
|
if (Request::get('password')) {
|
|
|
|
$user->update(['password' => Request::get('password')]);
|
|
|
|
}
|
2019-04-15 22:18:29 -03:00
|
|
|
|
2019-12-18 19:34:14 -03:00
|
|
|
return Redirect::back()->with('success', 'User updated.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy(User $user)
|
|
|
|
{
|
2019-12-18 19:33:46 -03:00
|
|
|
if (App::environment('demo') && $user->isDemoUser()) {
|
2019-12-18 19:34:14 -03:00
|
|
|
return Redirect::back()->with('error', 'Deleting the demo user is not allowed.');
|
2019-12-18 18:20:14 -03:00
|
|
|
}
|
|
|
|
|
2019-03-18 08:53:00 -03:00
|
|
|
$user->delete();
|
2019-04-15 22:18:29 -03:00
|
|
|
|
2019-12-18 19:34:14 -03:00
|
|
|
return Redirect::back()->with('success', 'User deleted.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function restore(User $user)
|
|
|
|
{
|
|
|
|
$user->restore();
|
2019-04-15 22:18:29 -03:00
|
|
|
|
2019-12-18 19:34:14 -03:00
|
|
|
return Redirect::back()->with('success', 'User restored.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
}
|