1
0
forked from metin2/web

Added blocking, started e-mail validation, fixed locale strings

This commit is contained in:
2023-07-29 11:43:34 +03:00
parent c26d8e4642
commit 7de2a637c4
13 changed files with 237 additions and 139 deletions

View File

@ -16,8 +16,8 @@ class LoginController extends Controller
public function login(Request $request): RedirectResponse
{
$validator = Validator::make($request->all(), [
'login' => ['required'],
'password' => ['required'],
'login' => 'required|string',
'password' => 'required|string',
]);
if ($validator->fails())
@ -25,19 +25,36 @@ class LoginController extends Controller
$credentials = $validator->validated();
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
$user = Auth::user();
$user->ip = $request->ip();
$user->saveOrFail();
return redirect()->intended('user/administration');
// Validate the credentials
if (!Auth::once($credentials)) {
return redirect('user/login')->withErrors([
'login' => 'The provided credentials do not match our records.',
])->onlyInput('login');
}
return redirect('user/login')->withErrors([
'login' => 'The provided credentials do not match our records.',
])->onlyInput('login');
// The user is now available
$user = Auth::user();
// Check if the user is banned
if ($user->status->isBlocked()) {
Auth::logout();
$request->session()->invalidate();
return redirect('user/login')->withErrors([
'login' => 'Your account is blocked.',
])->onlyInput('login');
}
// Authenticate user
Auth::login($user);
$request->session()->regenerate();
// Save user's IP address
$user->ip = $request->ip();
$user->saveOrFail();
return redirect()->intended('user/administration');
}

View File

@ -21,7 +21,7 @@ class RedirectIfAuthenticated
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
return redirect('user/administration');
}
}

View File

@ -2,13 +2,15 @@
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\Enums\AccountStatusEnum;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class Account extends User
class Account extends User implements MustVerifyEmail
{
use HasApiTokens, HasFactory, Notifiable;
@ -26,9 +28,20 @@ class Account extends User
*/
protected $table = 'account';
const UPDATED_AT = null;
/**
* The name of the "created at" column.
*
* @var string|null
*/
const CREATED_AT = 'create_time';
/**
* The name of the "updated at" column.
*
* @var string|null
*/
const UPDATED_AT = null;
/**
* The attributes that are mass assignable.
*
@ -59,5 +72,48 @@ class Account extends User
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'status' => AccountStatusEnum::class
];
/**
* Determine if the user has verified their email address.
*
* @return bool
*/
public function hasVerifiedEmail(): bool
{
return $this->status != AccountStatusEnum::NOT_AVAILABLE;
}
/**
* Mark the given user's email as verified.
*
* @return bool
*/
public function markEmailAsVerified(): bool
{
return $this->forceFill([
'status' => AccountStatusEnum::OK,
])->save();
}
/**
* Send the email verification notification.
*
* @return void
*/
public function sendEmailVerificationNotification(): void
{
$this->notify(new VerifyEmail);
}
/**
* Get the email address that should be used for verification.
*
* @return string
*/
public function getEmailForVerification(): string
{
return $this->email;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Models\Enums;
enum AccountStatusEnum: string
{
case OK = 'OK';
case REPAIR = 'REPAIR';
case BLOCK = 'BLOCK';
case NOT_AVAILABLE = 'NOTAVAIL';
case BILLING_EXPIRED = 'NOBILL';
case BLOCK_LOGIN = 'BLKLOGIN';
case WEB_BLOCK = 'WEBBLK';
public function isBlocked(): bool
{
return match($this) {
self::BLOCK, self::BILLING_EXPIRED, self::BLOCK_LOGIN, self::WEB_BLOCK => true,
default => false,
};
}
}