Skip to content

Commit

Permalink
log testing added
Browse files Browse the repository at this point in the history
  • Loading branch information
hafijul233 committed May 28, 2024
1 parent 4be1093 commit ddc1aaa
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 49 deletions.
37 changes: 0 additions & 37 deletions src/Events/AccountFreezed.php

This file was deleted.

51 changes: 51 additions & 0 deletions src/Events/AccountFrozen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Fintech\Auth\Events;

use Fintech\Core\Attributes\ManagedByTrigger;
use Fintech\Core\Attributes\Variable;
use Fintech\Core\Interfaces\Bell\HasDynamicString;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

#[ManagedByTrigger(
name: 'User Account Frozen/Suspended',
description: 'When someone tries to enter into their account using the incorrect password and the number of incorrect passwords exceeds the threshold, this trigger is set off.',
enabled: true,
variables: [
new Variable(name: '__account_name__', description: 'Name of the user tried login.'),
new Variable(name: '__account_mobile__', description: 'Mobile number associate with requested user.'),
new Variable(name: '__account_email__', description: 'Email address associate with requested user.'),
new Variable(name: '__password_attempt_count__', description: 'Number of times wrong password attempted.'),
new Variable(name: '__account_status__', description: 'User account before frozen/suspended status.'),
new Variable(name: '__password_attempt_limit__', description: 'The maximum number of times a user may try to customize my system.'),
],
recipients: []
)]
class AccountFrozen implements HasDynamicString
{
use Dispatchable;
use SerializesModels;

public mixed $user;

public function aliases(): array
{
return [
'__account_name__' => $this->user->name ?? '',
'__account_mobile__' => $this->user->mobile ?? '',
'__account_email__' => $this->user->email ?? '',
'__password_attempt_count__' => $this->user->wrong_password ?? '',
'__account_status__' => $this->user->status ?? '',
'__password_attempt_limit__' => config('fintech.auth.password_threshold', 10),
];
}

/**
* Create a new event instance.
*/
public function __construct($user)
{
$this->user = $user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Exception;

class AccountFreezeException extends Exception
class AccountFrozenException extends Exception
{
//
}
3 changes: 3 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fintech\Auth\Traits\TransactionRelations;
use Fintech\Core\Traits\AuditableTrait;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -20,6 +21,8 @@
* Class User
* @package Fintech\Auth\Models
* @method getTeamIdFromToken()
* @property Collection $tokens
* @property int $wrong_password
*/
class User extends Authenticatable implements HasMedia
{
Expand Down
8 changes: 6 additions & 2 deletions src/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Fintech\Auth\Providers;

use Fintech\Auth\Events\AccountFreezed;
use Fintech\Auth\Events\AccountFrozen;
use Fintech\Auth\Events\AddToFavouriteAccepted;
use Fintech\Auth\Events\AddToFavouriteRejected;
use Fintech\Auth\Events\AddToFavouriteRequested;
Expand All @@ -12,6 +12,7 @@
use Fintech\Auth\Events\PasswordResetSuccessful;
use Fintech\Auth\Events\VerificationRequested;
use Fintech\Core\Listeners\TriggerListener;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Auth\Events\Lockout;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

Expand All @@ -23,6 +24,9 @@ class EventServiceProvider extends ServiceProvider
* @var array<class-string, array<int, class-string>>
*/
protected $listen = [
Attempting::class => [
TriggerListener::class
],
Lockout::class => [
TriggerListener::class
],
Expand All @@ -32,7 +36,7 @@ class EventServiceProvider extends ServiceProvider
PasswordResetSuccessful::class => [
TriggerListener::class
],
AccountFreezed::class => [
AccountFrozen::class => [
TriggerListener::class
],
LoggedIn::class => [
Expand Down
35 changes: 26 additions & 9 deletions src/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Fintech\Auth\Services;

use Exception;
use Fintech\Auth\Events\AccountFreezed;
use Fintech\Auth\Events\AccountFrozen;
use Fintech\Auth\Events\LoggedIn;
use Fintech\Auth\Exceptions\AccessForbiddenException;
use Fintech\Auth\Exceptions\AccountFreezeException;
use Fintech\Auth\Exceptions\AccountFrozenException;
use Fintech\Auth\Facades\Auth;
use Fintech\Auth\Interfaces\ProfileRepository;
use Fintech\Auth\Interfaces\UserRepository;
Expand All @@ -15,6 +15,10 @@
use Fintech\Core\Enums\Auth\PasswordResetOption;
use Fintech\Core\Enums\Auth\UserStatus;
use Fintech\MetaData\Facades\MetaData;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Auth\Events\Failed;
use Illuminate\Auth\Events\OtherDeviceLogout;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -215,7 +219,7 @@ public function reset($user, $field)
/**
* @param array $inputs
* @param string $guard
* @return User|BaseModel|null
* @return BaseModel|\Fintech\Auth\Models\User|User|null
* @throws Exception
*/
public function login(array $inputs, string $guard = 'web')
Expand All @@ -235,9 +239,14 @@ public function login(array $inputs, string $guard = 'web')

Auth::loginAttempt()->create($this->loginAttemptData(null, LoginStatus::Invalid, __('auth::messages.failed')));

event(new Attempting($guard, $inputs, false));

throw new Exception(__('auth::messages.failed'));
}

/**
* @var \Fintech\Auth\Models\User $attemptUser
*/
$attemptUser = $attemptUser->first();

if ($attemptUser->wrong_password > config('fintech.auth.password_threshold', 10)) {
Expand All @@ -246,14 +255,13 @@ public function login(array $inputs, string $guard = 'web')
'status' => UserStatus::Suspended->value,
]);

event(new AccountFreezed($attemptUser));
event(new AccountFrozen($attemptUser));

Auth::loginAttempt()->create($this->loginAttemptData($attemptUser->getKey(), LoginStatus::Banned, __('auth::messages.lockup')));

throw new AccountFreezeException(__('auth::messages.lockup'));
throw new AccountFrozenException(__('auth::messages.lockup'));
}


if (!Hash::check($password, $attemptUser->{$passwordField})) {

$wrongPasswordCount = $attemptUser->wrong_password + 1;
Expand All @@ -273,6 +281,8 @@ public function login(array $inputs, string $guard = 'web')
)
);

event(new Failed($guard, $attemptUser, $inputs));

throw new Exception(__('auth::messages.warning', [
'attempt' => $wrongPasswordCount,
'threshold' => config('fintech.auth.threshold.password', 10),
Expand All @@ -281,7 +291,12 @@ public function login(array $inputs, string $guard = 'web')

\Illuminate\Support\Facades\Auth::guard($guard)->login($attemptUser);

$attemptUser->tokens->each(fn ($token) => $token->delete());
if ($attemptUser->tokens->isNotEmpty()) {

$attemptUser->tokens->each(fn($token) => $token->delete());

event(new OtherDeviceLogout($guard, $attemptUser));
}

if (!$attemptUser->can('auth.login')) {

Expand All @@ -301,8 +316,6 @@ public function login(array $inputs, string $guard = 'web')
throw new AccessForbiddenException(__('auth::messages.forbidden', ['permission' => permission_format('auth.login', 'auth')]));
}

event(new LoggedIn($attemptUser));

Auth::loginAttempt()->create(
$this->loginAttemptData(
$attemptUser->getKey(),
Expand All @@ -311,6 +324,10 @@ public function login(array $inputs, string $guard = 'web')
)
);

event(new LoggedIn($attemptUser));

event(new Authenticated($guard, $attemptUser));

return $attemptUser;

}
Expand Down

0 comments on commit ddc1aaa

Please sign in to comment.