3
0
Fork 0
This repository has been archived on 2024-11-14. You can view files and clone it, but cannot push or open issues or pull requests.
ThemeParkPlus-Panel/app/Http/Controllers/Panel/UMSController.php

153 lines
4.4 KiB
PHP
Raw Normal View History

2020-02-26 13:59:58 +00:00
<?php
namespace App\Http\Controllers\Panel;
use App\ChangeEmail;
use App\Http\Controllers\Controller;
2021-06-27 19:01:43 +00:00
use App\Mail\EmailChange;
2020-02-26 13:59:58 +00:00
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();
2021-06-27 19:01:43 +00:00
$pages = (int) ceil($pages/10);
2020-02-26 13:59:58 +00:00
if($pages < 1 && $page == 1)
$page = 1;
if($page < 1 || ($pages > 0 && $page > $pages))
return redirect()->route('panel.ums', [
'page' => ($pages > 0 ? $pages : 1)
]);
2021-06-27 19:01:43 +00:00
$data = User::select('id', 'uuid', 'last_active', 'is_admin', 'is_root', 'email_verified_at')->get();
2020-02-26 13:59:58 +00:00
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');
2021-06-27 19:01:43 +00:00
$user = User::findOrFail($id);
2020-02-26 13:59:58 +00:00
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)
]);
2021-06-27 19:01:43 +00:00
Mail::to($request->get('email'))->send(new EmailChange($change));
die('Send! Email: '.$request->get('email'));
}
$validator = Validator::make($request->all(), [
'verified' => ['required', 'boolean']
]);
if(!$validator->passes())
return Redirect::back()->withErrors($validator);
if($request->get('verified')) {
$user->email_verified_at = empty($user->email_verified_at) ? date('Y-m-d H:i:s') : $user->email_verified_at;
} else {
$user->email_verified_at = null;
2020-02-26 13:59:58 +00:00
}
if(Auth::user()->is_root) {
$validator = Validator::make($request->all(), [
2021-06-27 19:01:43 +00:00
'admin' => ['required', 'numeric', 'min:0', 'max:2'],
2020-02-26 13:59:58 +00:00
]);
2021-06-27 19:01:43 +00:00
if (!$validator->passes())
2020-02-26 13:59:58 +00:00
return Redirect::back()->withErrors($validator);
2021-06-27 19:01:43 +00:00
$admin = $request->get('admin');
if ($admin == 1) {
$user->is_admin = 1;
$user->is_root = 0;
} else if($admin == 2) {
$user->is_admin = 0;
$user->is_root = 1;
2020-02-26 13:59:58 +00:00
} else {
2021-06-27 19:01:43 +00:00
$user->is_admin = 0;
$user->is_root = 0;
2020-02-26 13:59:58 +00:00
}
}
2021-06-27 19:01:43 +00:00
if($user->save()) {
session()->flash('success', 'Successfully edited user: '.$user->username());
} else {
session()->flash('error', 'Unable to edit user: '.$user->username());
}
2020-02-26 13:59:58 +00:00
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();
}
}