129 lines
3.6 KiB
PHP
129 lines
3.6 KiB
PHP
|
<?php
|
||
|
namespace App\Http\Controllers\Panel;
|
||
|
|
||
|
use App\ChangeEmail;
|
||
|
use App\Http\Controllers\Controller;
|
||
|
use App\Notifications\SendMailChange;
|
||
|
use App\User;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Illuminate\Support\Facades\Auth;
|
||
|
use Illuminate\Support\Facades\Mail;
|
||
|
use Illuminate\Support\Facades\Redirect;
|
||
|
use Illuminate\Support\Facades\Validator;
|
||
|
use Illuminate\Support\Str;
|
||
|
|
||
|
class UMSController extends Controller
|
||
|
{
|
||
|
/**
|
||
|
* Create a new controller instance.
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function __construct()
|
||
|
{
|
||
|
$this->middleware(['auth', 'verified', '2fa']);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Show the application dashboard.
|
||
|
*
|
||
|
* @param int $page
|
||
|
* @return \Illuminate\Contracts\Support\Renderable
|
||
|
*/
|
||
|
public function index($page = 1)
|
||
|
{
|
||
|
$pages = User::count();
|
||
|
$pages = (int) ceil($pages/25);
|
||
|
if($pages < 1 && $page == 1)
|
||
|
$page = 1;
|
||
|
|
||
|
if($page < 1 || ($pages > 0 && $page > $pages))
|
||
|
return redirect()->route('panel.ums', [
|
||
|
'page' => ($pages > 0 ? $pages : 1)
|
||
|
]);
|
||
|
|
||
|
$data = User::select('id', 'uuid', 'last_active', 'is_admin', 'is_root')->get();
|
||
|
return view('panel.ums.index')->with([
|
||
|
'users' => $data,
|
||
|
'page' => $page,
|
||
|
'pages' => $pages
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
public function info($id) {
|
||
|
$user = User::findOrFail($id);
|
||
|
return view('panel.ums.info')->with([
|
||
|
'user' => $user
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
public function edit($id) {
|
||
|
$user = Auth::user();
|
||
|
if(!$user->is_root && !$user->is_admin)
|
||
|
return Redirect::route('panel.ums');
|
||
|
|
||
|
return view('panel.ums.edit')->with([
|
||
|
'user' => $user
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
public function update(Request $request) {
|
||
|
if(!$request->has('id'))
|
||
|
return Redirect::back();
|
||
|
|
||
|
$user = User::findOrFail($request->get('id'));
|
||
|
if($request->get('email') !== $user->email) {
|
||
|
$validator = Validator::make($request->all(), [
|
||
|
'email' => ['required', 'email', 'unique:users,email', 'max:255']
|
||
|
]);
|
||
|
|
||
|
if(!$validator->passes())
|
||
|
return Redirect::back()->withErrors($validator);
|
||
|
|
||
|
$change = ChangeEmail::create([
|
||
|
'user_id' => $user->id,
|
||
|
'email' => $request->get('email'),
|
||
|
'token' => Str::random(12)
|
||
|
]);
|
||
|
|
||
|
Mail::to($user)->send(new SendMailChange($change));
|
||
|
}
|
||
|
|
||
|
if(Auth::user()->is_root) {
|
||
|
$validator = Validator::make($request->all(), [
|
||
|
'is_admin' => ['required', 'boolean'],
|
||
|
'is_root' => ['required', 'boolean']
|
||
|
]);
|
||
|
|
||
|
if(!$validator->passes())
|
||
|
return Redirect::back()->withErrors($validator);
|
||
|
|
||
|
$user->is_admin = $request->get('is_admin');
|
||
|
$user->is_root = $request->get('is_root');
|
||
|
if($user->save()) {
|
||
|
session()->flash('success', 'Successfully edited user: '.$user->uuid);
|
||
|
} else {
|
||
|
session()->flash('error', 'Unable to edit user: '.$user->uuid);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return Redirect::route('panel.ums');
|
||
|
}
|
||
|
|
||
|
public function delete($id) {
|
||
|
$auth = Auth::user();
|
||
|
if(!$auth->is_admin && !$auth->is_root)
|
||
|
return Redirect::route('panel.home');
|
||
|
|
||
|
$user = User::findOrFail($id);
|
||
|
if($user->delete()) {
|
||
|
session()->flash('success', 'Successfully deleted user: '.$user->firstname);
|
||
|
} else {
|
||
|
session()->flash('error', 'Unable to delete user: '.$user->firstname);
|
||
|
}
|
||
|
|
||
|
return Redirect::back();
|
||
|
}
|
||
|
|
||
|
}
|