Skip to content

Commit

Permalink
Merge pull request #2 from andrenalin282/main
Browse files Browse the repository at this point in the history
Update Auth.php
  • Loading branch information
secure73 authored Oct 27, 2024
2 parents 0cec6e3 + 428ad84 commit 592494a
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/core/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Auth
public ?JWTToken $token;
private bool $isAuthenticated;
private ?int $user_id;
private ?array $user_roles;
public ?string $error;
/**
* Summary of __construct
Expand All @@ -27,6 +28,7 @@ public function __construct(Request $request, array $arrayRolesToAuthorize = nul
$this->request = $request;
$this->isAuthenticated = false;
$this->user_id = null;
$this->user_roles = null;
$this->request = $request;
$this->token = null;
$this->error = null;
Expand Down Expand Up @@ -58,21 +60,26 @@ public function getUserId(): int|null
return $this->user_id;
}

/**
* @param array<string> $allowedRoles
public function getUserRoles(): array|null
{
return $this->user_roles;
}

/**
* @param array<string> $roles
* @return bool
*/
private function authorize(array $allowedRoles): bool
private function authorize(array $roles): bool
{
$userRoles = array_map('trim', explode(',', $this->token->role));

foreach ($userRoles as $role) {
if (in_array($role, $allowedRoles, true)) {
// @phpstan-ignore-next-line
$user_roles = explode(',',$this->token->role);
foreach ($roles as $role) {
if (in_array($role, $user_roles)) {
return true;
}
}

return false;
return true;
}

private function checkExistedProcessedRequest(): bool
Expand All @@ -99,6 +106,7 @@ private function authenticate(): bool
$this->isAuthenticated = true;
$this->request->setJwtToken($jwt);
$this->user_id = $jwt->user_id;
$this->user_roles = explode(',', $jwt->role);
return true;
}
$existed_token = $this->request->getJwtToken();
Expand All @@ -109,6 +117,7 @@ private function authenticate(): bool
$this->token = $existed_token;
$this->isAuthenticated = true;
$this->user_id = $existed_token->user_id;
$this->user_roles = explode(',', $existed_token->role);
return true;
}
}

0 comments on commit 592494a

Please sign in to comment.