fix(auth): resolve login identities by active email and role
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Domains\Auth\Controllers;
|
|||||||
use App\Domains\Auth\Requests\ScannerLoginRequest;
|
use App\Domains\Auth\Requests\ScannerLoginRequest;
|
||||||
use App\Domains\Auth\Resources\UserResource;
|
use App\Domains\Auth\Resources\UserResource;
|
||||||
use App\Domains\Auth\Services\PasswordLoginService;
|
use App\Domains\Auth\Services\PasswordLoginService;
|
||||||
|
use App\Domains\Authorization\Enums\RoleCode;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
||||||
@@ -22,6 +23,7 @@ class ScannerLoginController extends Controller
|
|||||||
$credentials['password'],
|
$credentials['password'],
|
||||||
$request->ip(),
|
$request->ip(),
|
||||||
$request->userAgent(),
|
$request->userAgent(),
|
||||||
|
RoleCode::from($credentials['rol_codigo'] ?? RoleCode::Scanner->value),
|
||||||
);
|
);
|
||||||
|
|
||||||
$expirationMinutes = (int) config('sanctum.expiration');
|
$expirationMinutes = (int) config('sanctum.expiration');
|
||||||
|
|||||||
@@ -2,4 +2,16 @@
|
|||||||
|
|
||||||
namespace App\Domains\Auth\Requests;
|
namespace App\Domains\Auth\Requests;
|
||||||
|
|
||||||
class ScannerLoginRequest extends AdminAppLoginRequest {}
|
use App\Domains\Authorization\Enums\RoleCode;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class ScannerLoginRequest extends AdminAppLoginRequest
|
||||||
|
{
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
...parent::rules(),
|
||||||
|
'rol_codigo' => ['sometimes', Rule::in([RoleCode::Scanner->value, RoleCode::AdminApp->value])],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class AdminCredentialVerifier
|
|||||||
public function verify(string $email, string $password): bool
|
public function verify(string $email, string $password): bool
|
||||||
{
|
{
|
||||||
$admin = User::query()
|
$admin = User::query()
|
||||||
->where('email', mb_strtolower(trim($email)))
|
->where('active_email', mb_strtolower(trim($email)))
|
||||||
->where('rol_codigo', RoleCode::Admin->value)
|
->where('rol_codigo', RoleCode::Admin->value)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Domains\Auth\Services;
|
namespace App\Domains\Auth\Services;
|
||||||
|
|
||||||
use App\Domains\Auth\Models\User;
|
use App\Domains\Auth\Models\User;
|
||||||
|
use App\Domains\Authorization\Enums\RoleCode;
|
||||||
use App\Domains\Notification\Events\UserRegistered;
|
use App\Domains\Notification\Events\UserRegistered;
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use App\Domains\Tenant\Support\TenantDomainNormalizer;
|
use App\Domains\Tenant\Support\TenantDomainNormalizer;
|
||||||
@@ -124,12 +125,12 @@ class GoogleAuthService
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$user = User::query()->where('google_id', $googleId)->first();
|
$user = User::query()->where('rol_codigo', RoleCode::User->value)->where('google_id', $googleId)->first();
|
||||||
if ($user) {
|
if ($user) {
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
$user = User::query()->where('email', $email)->first();
|
$user = User::query()->where('rol_codigo', RoleCode::User->value)->where('active_email', mb_strtolower(trim($email)))->first();
|
||||||
if ($user) {
|
if ($user) {
|
||||||
$user->forceFill(['google_id' => $googleId])->save();
|
$user->forceFill(['google_id' => $googleId])->save();
|
||||||
|
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ class PasswordLoginService
|
|||||||
string $password,
|
string $password,
|
||||||
?string $ipAddress,
|
?string $ipAddress,
|
||||||
?string $userAgent,
|
?string $userAgent,
|
||||||
|
RoleCode $role = RoleCode::Scanner,
|
||||||
): User {
|
): User {
|
||||||
return $this->authenticateUser(
|
return $this->authenticateUser(
|
||||||
$email,
|
$email,
|
||||||
@@ -85,10 +86,12 @@ class PasswordLoginService
|
|||||||
null,
|
null,
|
||||||
$ipAddress,
|
$ipAddress,
|
||||||
$userAgent,
|
$userAgent,
|
||||||
null,
|
$role,
|
||||||
true,
|
true,
|
||||||
PermissionCode::ScanTickets->value,
|
PermissionCode::ScanTickets->value,
|
||||||
PasswordResetRequested::CHANNEL_SCANNER,
|
$role === RoleCode::AdminApp
|
||||||
|
? PasswordResetRequested::CHANNEL_ADMINAPP
|
||||||
|
: PasswordResetRequested::CHANNEL_SCANNER,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +123,7 @@ class PasswordLoginService
|
|||||||
$passwordResetChannel,
|
$passwordResetChannel,
|
||||||
): array {
|
): array {
|
||||||
$user = User::query()
|
$user = User::query()
|
||||||
->where('email', $normalizedEmail)
|
->where('active_email', $normalizedEmail)
|
||||||
->when(
|
->when(
|
||||||
$requiredRole !== null,
|
$requiredRole !== null,
|
||||||
fn ($query) => $query->where('rol_codigo', $requiredRole->value),
|
fn ($query) => $query->where('rol_codigo', $requiredRole->value),
|
||||||
|
|||||||
@@ -105,7 +105,8 @@ class InvitationPurchaseProvisioner
|
|||||||
private function userId(DateTimeInterface $now): int
|
private function userId(DateTimeInterface $now): int
|
||||||
{
|
{
|
||||||
$user = DB::table('users')
|
$user = DB::table('users')
|
||||||
->where('email', self::USER_EMAIL)
|
->where('active_email', self::USER_EMAIL)
|
||||||
|
->where('rol_codigo', 'user')
|
||||||
->whereNull('deleted_at')
|
->whereNull('deleted_at')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ class ScannerLoginControllerTest extends TestCase
|
|||||||
$response = $this->postJson('/api/v1/scanner/login', [
|
$response = $this->postJson('/api/v1/scanner/login', [
|
||||||
'email' => ' SCANNER@EXAMPLE.COM ',
|
'email' => ' SCANNER@EXAMPLE.COM ',
|
||||||
'password' => 'secret123',
|
'password' => 'secret123',
|
||||||
|
'rol_codigo' => RoleCode::AdminApp->value,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response
|
$response
|
||||||
|
|||||||
Reference in New Issue
Block a user