513 lines
21 KiB
PHP
513 lines
21 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Helpers\Detector;
|
|
use App\ResourceCategory;
|
|
use App\ResourceExtension;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use App\User;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Log;
|
|
use mysql_xdevapi\Table;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
|
|
class UserController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware([ "auth" ])->only(["accountupdate", "accountedit", "accounteditpass", "account", "logout"]);
|
|
|
|
$this->middleware([ "guest" ])->only(["login", "signup", "signupStore"]);
|
|
$this->middleware([ "lang" ])->except(["login", "showLogin", "showForgot", "forgot", "signup", "nameCheck", "signupStore"]);
|
|
|
|
$this->middleware([ "check.auth:user.show" ])->only("show", "index");
|
|
$this->middleware([ "check.auth:user.create" ])->only("create");
|
|
$this->middleware([ "check.auth:user.edit" ])->only(["edit", "update"]);
|
|
$this->middleware([ "check.auth:user.delete" ])->only("delete");
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param Request $request
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$users = User::query()->where("id", "!=", 1)->orderBy('id', 'asc')->get();
|
|
|
|
return Response::detect("users.index", [ "users" => $users ]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function create()
|
|
{
|
|
$roles = Role::all();
|
|
return Response::detect("users.create", ['roles' => $roles]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
"name_first" => "required|max:255",
|
|
"name_last" => "required|max:255",
|
|
"email" => "required",
|
|
"password" => "required|max:60",
|
|
"phone" => "required|max:8|min:8",
|
|
//"eduenddate" => "",
|
|
//"education" => "required"
|
|
]);
|
|
|
|
|
|
$allUsersEmail = User::query()->where('email', '=', $request->email)->get();
|
|
$allUsersPhone = User::query()->where('phone', '=', $request->phone)->get();
|
|
|
|
if ($request->password != $request->password2)
|
|
return redirect()->route('users.create')->with('error#notsamepass', '<p><b>Der stod ikke det samme i `Password` & `Confirm Password`!</b></p>');
|
|
elseif (count($allUsersEmail) > 0)
|
|
return redirect()->route('users.create')->with('EmailExists', '<p><b>Der findes allerede en bruger med den Email!</b></p>');
|
|
elseif (count($allUsersPhone) > 0)
|
|
return redirect()->route('users.create')->with('PhoneExists', '<p><b>Der findes allerede en bruger med det Telefon Nr.!</b></p>');
|
|
|
|
$roles = $request->input("roles", [ "Bruger" ]);
|
|
|
|
$user = new User($data);
|
|
|
|
if($request->file("resource")) {
|
|
$user->resource_id = ResourceController::store($request)->id;
|
|
}
|
|
|
|
$user->assignRole($roles);
|
|
$user->save();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('users.index');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$user = User::find($id);
|
|
|
|
return Response::detect("users.show", [
|
|
"user" => $user
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$roles = Role::all();
|
|
$user = User::find($id);
|
|
|
|
if($id == 1)
|
|
return redirect()->route("users.index");
|
|
|
|
|
|
return Response::detect("users.edit", [
|
|
"user" => $user,
|
|
"roles" => $roles,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
if($id == 1)
|
|
return redirect()->route("users.index");
|
|
|
|
$user = User::find($id);
|
|
|
|
if($request->file("resource")) {
|
|
$user->resource_id = ResourceController::store($request)->id;
|
|
}
|
|
|
|
$user->save();
|
|
|
|
$allUsersEmail = User::query()->where('email', '=', $request->email)->where('id', '!=', $id)->get();
|
|
$allUsersPhone = User::query()->where('phone', '=', $request->phone)->where('id', '!=', $id)->get();
|
|
|
|
if($id === Auth::id() || auth()->user()->hasPermissionTo("user.edit")) {
|
|
if ($request->roles != null && auth()->user()->hasPermissionTo("user.edit")) { //You can only edit roles on the admin site, so if there is an input roles, then update user info and edit roles
|
|
if(count($allUsersEmail) > 0)
|
|
return redirect()->route('users.edit', ['user' => $id])->with('EmailExists', '<p><b>Der findes allerede en bruger med den Email!</b></p>');
|
|
elseif(count($allUsersPhone) > 0)
|
|
return redirect()->route('users.edit', ['user' => $id])->with('PhoneExists', '<p><b>Der findes allerede en bruger med det Telefon Nr.!</b></p>');
|
|
elseif ($request->password != "") { // If you have edited the password, then run this
|
|
if ($request->password == $request->password2) { // If the password is the same as confirm password, then update everything from user
|
|
$data = $request->all();
|
|
/** @var User $user */
|
|
$user->update($data);
|
|
} else // Else go back with an error message
|
|
return redirect()->route("users.edit", ['user' => $id])->with('error#notsamepass', '<p><b>Der stod ikke det samme i `Password` & `Bekræft Adgangskode`!</b></p>');
|
|
}
|
|
else { // If you haven't touched the password, then update everything else but password
|
|
$data = $request->only(['name_first', 'name_last', 'email', 'phone', 'roles']);
|
|
/** @var User $user */
|
|
$user->update($data);
|
|
}
|
|
|
|
$user->roles()->detach();
|
|
$user->forgetCachedPermissions();
|
|
|
|
foreach ($request->roles as $role) {
|
|
$user->assignRole($role);
|
|
}
|
|
}
|
|
}
|
|
$users = User::query()->paginate(20);
|
|
|
|
return redirect()->route("users.index");
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
if($id == 1)
|
|
return redirect()->route("users.index");
|
|
|
|
if(Auth::user()->hasPermissionTo("user.delete")) {
|
|
$user = User::find($id);
|
|
|
|
$user->delete();
|
|
}
|
|
|
|
return redirect()->route("users.index");
|
|
}
|
|
|
|
/*******************************************/
|
|
/* Authentication */
|
|
/*******************************************/
|
|
|
|
public function showLogin() {
|
|
if(Auth::user() == null)
|
|
return Response::detect("users.login");
|
|
else
|
|
return redirect()->back();
|
|
}
|
|
|
|
public function login(Request $request) {
|
|
$data = $request->only("email", "password");
|
|
$remember = $request->rememberpassword;
|
|
|
|
if(Auth::attempt($data, $remember)) {
|
|
return redirect()->route("root.index");
|
|
}
|
|
|
|
|
|
|
|
if (request()->cookie('languagesSetting') == "dk")
|
|
return redirect()->back(303)->with('error#wrongcredentials', '<p class="text-center text-white"><b>Email eller Password matchede ikke!</b></p>');
|
|
if (request()->cookie('languagesSetting') == "en")
|
|
return redirect()->back(303)->with('error#wrongcredentials', '<p class="text-center text-white"><b>Wrong Email or Password!</b></p>');
|
|
|
|
return redirect()->back(303)->with('error#wrongcredentials', '<p class="text-center text-white"><b>Email eller Password matchede ikke!</b></p>');
|
|
}
|
|
|
|
public function logout(Request $request) {
|
|
Auth::logout();
|
|
|
|
if (request()->cookie('languagesSetting') == "dk")
|
|
return redirect()->route("users.login")->with('success#loggedout', '<p class="text-center text-white"><b>Du er hermed logget ud!</b></p>');
|
|
if (request()->cookie('languagesSetting') == "en")
|
|
return redirect()->route("users.login")->with('success#loggedout', '<p class="text-center text-white"><b>You are logged out!</b></p>');
|
|
|
|
return redirect()->route("users.login")->with('success#loggedout', '<p class="text-center text-white"><b>Du er hermed logget ud!</b></p>');
|
|
}
|
|
|
|
/*******************************************/
|
|
/* Forgot password */
|
|
/*******************************************/
|
|
|
|
public function showForgot(){
|
|
return Response::detect('users.forgot');
|
|
}
|
|
|
|
public function forgot(Request $request){
|
|
$user = User::query()->where('email', '=', $request->email)->first();
|
|
|
|
if($user == null){
|
|
return redirect()->back()->with('errornosuchuser', '<p class="text-center text-white"><b>Denne email findes ikke i systemet!</b></p>');
|
|
}
|
|
|
|
//Send email
|
|
$email = $user->email;
|
|
$pswd = "";
|
|
|
|
//Generate password
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
$n = 6;
|
|
$randomString = '';
|
|
|
|
for ($i = 0; $i < $n; $i++) {
|
|
$index = rand(0, strlen($characters) - 1);
|
|
$randomString .= $characters[$index];
|
|
}
|
|
|
|
$pswd = $randomString;
|
|
|
|
$user->setPasswordAttribute($pswd);
|
|
$user->update();
|
|
|
|
$subject = "SDE Skolehjem reset password";
|
|
$msg = "Hej " . $user->name_first . " ". $user->name_last . ".\n\nDin adgangskode er nu: " . $pswd;
|
|
|
|
mail($email, $subject, $msg);
|
|
|
|
return redirect()->route('users.login');
|
|
}
|
|
|
|
/*******************************************/
|
|
/* Account */
|
|
/*******************************************/
|
|
|
|
public function account()
|
|
{
|
|
return Response::detect("users.account");
|
|
}
|
|
|
|
public function accountedit()
|
|
{
|
|
return Response::detect("users.edit");
|
|
}
|
|
|
|
public function accounteditpic()
|
|
{
|
|
return Response::detect("users.editpic");
|
|
}
|
|
|
|
public function accounteditpass()
|
|
{
|
|
return Response::detect("users.editpass");
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function accountupdate(Request $request)
|
|
{
|
|
/** @var User $user */
|
|
$user = User::find(Auth::id());
|
|
|
|
if ($request->input('password') != null) { // If you're editing the password
|
|
$data = $request->only(['name_first', 'name_last', 'email', 'phone', 'password']);
|
|
if ($request->input('password') != $request->input('confirmpassword')) { // If new password and new password confirm is not the same, go back with fail message.
|
|
if (request()->cookie('languagesSetting') == "dk")
|
|
return redirect()->route("users.accounteditpass")->with('error#notsamepass', '<p class="text-center"><b>Der stod ikke det samme i Nyt Password & Confirm Password!</b></p>');
|
|
elseif (request()->cookie('languagesSetting') == "en")
|
|
return redirect()->route("users.accounteditpass")->with('error#notsamepass', '<p class="text-center"><b>New Password & Confirm Password was not the same!</b></p>');
|
|
else
|
|
return redirect()->route("users.accounteditpass")->with('error#notsamepass', '<p class="text-center"><b>Der stod ikke det samme i Nyt Password & Confirm Password!</b></p>');
|
|
} elseif (!Hash::check($request->input('oldpassword'), $user->password)) { // If the written current password and current password in DB is not the same, go back with fail message.
|
|
if (request()->cookie('languagesSetting') == "dk")
|
|
return redirect()->route("users.accounteditpass")->with('error#oldpass', '<p class="text-center"><b>Det indtastede password i Nuværende Password er ikke dit nuværende password!</b></p>');
|
|
elseif (request()->cookie('languagesSetting') == "en")
|
|
return redirect()->route("users.accounteditpass")->with('error#oldpass', '<p class="text-center"><b>The entered Current Password was not the same as your current password!</b></p>');
|
|
else
|
|
return redirect()->route("users.accounteditpass")->with('error#oldpass', '<p class="text-center"><b>Det indtastede password i Nuværende Password er ikke dit nuværende password!</b></p>');
|
|
} else { // If new password and current password is the same AND current written and current DB password is the same. Then update and logout.
|
|
/** @var User $user */
|
|
$user->update($data);
|
|
Auth::logout();
|
|
|
|
if (request()->cookie('languagesSetting') == "dk")
|
|
return redirect()->route("users.login")->with('success#passwordchange', '<p class="text-center text-white"><b>Dit password er hermed ændret!</b></p>');
|
|
elseif (request()->cookie('languagesSetting') == "en")
|
|
return redirect()->route("users.login")->with('success#passwordchange', '<p class="text-center text-white"><b>Your password was changed successfully!</b></p>');
|
|
else
|
|
return redirect()->route("users.login")->with('success#passwordchange', '<p class="text-center text-white"><b>Dit password er hermed ændret!</b></p>');
|
|
}
|
|
} else if($request->file("resource")) { // Else if you're editing the profile pic
|
|
$resource = ResourceController::storeMime($request, ResourceCategory::query()->where("name", "=", "image")->first());
|
|
|
|
if($resource !== null) {
|
|
$user->update(["resource_id" => $resource->id]);
|
|
}
|
|
else {
|
|
return redirect()->route("users.accounteditpic");
|
|
}
|
|
|
|
return redirect()->route("users.account");
|
|
} else { // Else if you're not editing the password but anything else (Email, Phone Number). Then update user.
|
|
$data = $request->only(['email', 'phone']);
|
|
$user->update($data);
|
|
|
|
if (request()->cookie('languagesSetting') == "dk")
|
|
return redirect()->route("users.account")->with('success#credentialschanged', '<p class="text-center"><b>Dine oplysninger er hermed ændret!</b></p>');
|
|
elseif (request()->cookie('languagesSetting') == "en")
|
|
return redirect()->route("users.account")->with('success#credentialschanged', '<p class="text-center"><b>Your information has changed!</b></p>');
|
|
else
|
|
return redirect()->route("users.account")->with('success#credentialschanged', '<p class="text-center"><b>Dine oplysninger er hermed ændret!</b></p>');
|
|
}
|
|
$users = User::query()->paginate(20);
|
|
|
|
return redirect()->route("users.index");
|
|
}
|
|
|
|
public function createajax() {
|
|
// Check the UserEvent table if there is a row that has the user_id AND the event_id
|
|
$User = User::query()->where('id', '=', auth()->user()->id);
|
|
|
|
// If you are in the Event, then remove yourself.
|
|
if (count($User->get()) > 0) {
|
|
// If not, then it keeps going and saves and shows a success message
|
|
$User->update([ "wants_emails" => true ]);
|
|
|
|
if (request()->cookie('languagesSetting') == "dk")
|
|
return '<p class="text-center"><b>Du får nu mails, når der kommer nyheder!</b></p>';
|
|
if (request()->cookie('languagesSetting') == "en")
|
|
return "<p class='text-center'><b>You'll now receive a mail when there's news!</b></p>";
|
|
|
|
return '<p class="text-center"><b>Du får nu mails, når der kommer nyheder!</b></p>';
|
|
}
|
|
}
|
|
|
|
public function createajaxcancel() {
|
|
// Check the UserEvent table if there is a row that has the user_id AND the event_id
|
|
$User = User::query()->where('id', '=', auth()->user()->id);
|
|
|
|
// If you are in the Event, then remove yourself.
|
|
if (count($User->get()) > 0) {
|
|
// If not, then it keeps going and saves and shows a success message
|
|
$User->update([ "wants_emails" => false ]);
|
|
|
|
if (request()->cookie('languagesSetting') == "dk")
|
|
return '<p class="text-center"><b>Du får ikke mails mere, når kommer nyheder!</b></p>';
|
|
if (request()->cookie('languagesSetting') == "en")
|
|
return "<p class='text-center'><b>You'll no longer receive a mail when there's news!</b></p>";
|
|
|
|
return '<p class="text-center"><b>Du får ikke mails mere, når kommer nyheder!</b></p>';
|
|
}
|
|
}
|
|
|
|
/*******************************************/
|
|
/* Settings */
|
|
/*******************************************/
|
|
public function setLanguages(Request $request){
|
|
if($request->ajax()){
|
|
if($request->lang === "en"){
|
|
$response = new Response('Set Cookie');
|
|
$response->withCookie(cookie('languagesSetting', $request->lang, 5259488));
|
|
|
|
return $response;
|
|
}elseif($request->lang === "dk"){
|
|
$response = new Response('Set Cookie');
|
|
$response->withCookie(cookie('languagesSetting', $request->lang, 5259488));
|
|
|
|
return $response;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function setDarkMode(Request $request){
|
|
if($request->ajax()){
|
|
if($request->darkmode === "dark"){
|
|
$response = new Response('dark');
|
|
$response->withCookie(cookie('mode', $request->darkmode, 5259488));
|
|
|
|
return $response;
|
|
}elseif($request->darkmode === "light"){
|
|
$response = new Response('light');
|
|
$response->withCookie(cookie('mode', $request->darkmode, 5259488));
|
|
|
|
return $response;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function nameCheck(Request $request){
|
|
$usersemail = User::query()->where('email', 'LIKE', $request->mailCheck)->get();
|
|
$usersphone = User::query()->where('phone', '=', $request->phoneCheck)->get();
|
|
if (count($usersemail) > 0 && count($usersphone) > 0) {
|
|
return 'both';
|
|
}
|
|
elseif(count($usersemail) > 0 && $request->mailCheck !== NULL){
|
|
return 'email';
|
|
}
|
|
elseif(count($usersphone) > 0 && $request->phoneCheck !== NULL){
|
|
return 'phone';
|
|
}
|
|
}
|
|
|
|
public function nameCheckUpdate(Request $request){
|
|
$usersemail = User::query()->where('email', 'LIKE', $request->mailCheck)->where('id', '!=', $request->id)->get();
|
|
$usersphone = User::query()->where('phone', '=', $request->phoneCheck)->where('id', '!=', $request->id)->get();
|
|
if (count($usersemail) > 0 && count($usersphone) > 0) {
|
|
return 'both';
|
|
}
|
|
elseif(count($usersemail) > 0 && $request->mailCheck !== NULL){
|
|
return 'email';
|
|
}
|
|
elseif(count($usersphone) > 0 && $request->phoneCheck !== NULL){
|
|
return 'phone';
|
|
}
|
|
}
|
|
|
|
public function signup(){
|
|
//return redirect()->route("users.signup");
|
|
return Response::detect("users.signup");
|
|
}
|
|
|
|
public function signupStore(Request $request){
|
|
$data = $request->validate([
|
|
"name_first" => "required|max:255",
|
|
"name_last" => "required|max:255",
|
|
"email" => "required",
|
|
"password" => "required|max:60",
|
|
"phone" => "required|max:8|min:8",
|
|
]);
|
|
|
|
|
|
$user = new User($data);
|
|
|
|
$user->assignRole("Bruger");
|
|
$user->save();
|
|
|
|
return redirect()->route('users.login');
|
|
}
|
|
|
|
}
|