Skip to content

Commit

Permalink
Minor auth refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
leepeuker committed Mar 2, 2024
1 parent c6eb4f9 commit a514ce2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
42 changes: 22 additions & 20 deletions src/Domain/User/Service/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,32 +140,19 @@ public function isUserAuthenticatedWithCookie() : bool

public function isUserPageVisibleForApiRequest(Request $request, UserEntity $targetUser) : bool
{
$userId = $this->getUserIdByApiToken($request);
$requestUserId = $this->getUserIdByApiToken($request);

$privacyLevel = $targetUser->getPrivacyLevel();

if ($privacyLevel === 2) {
return true;
}

if ($privacyLevel === 1 && $userId !== null) {
return true;
}

return $targetUser->getId() === $userId;
return $this->isUserPageVisibleForUser($targetUser, $requestUserId);
}

public function isUserPageVisibleForCurrentUser(int $privacyLevel, int $userId) : bool
public function isUserPageVisibleForWebRequest(UserEntity $targetUser) : bool
{
if ($privacyLevel === 2) {
return true;
}

if ($privacyLevel === 1 && $this->isUserAuthenticatedWithCookie() === true) {
return true;
$requestUserId = null;
if ($this->isUserAuthenticatedWithCookie() === true) {
$requestUserId = $this->getCurrentUserId();
}

return $this->isUserAuthenticatedWithCookie() === true && $this->getCurrentUserId() === $userId;
return $this->isUserPageVisibleForUser($targetUser, $requestUserId);
}

public function isValidAuthToken(string $token) : bool
Expand Down Expand Up @@ -244,6 +231,21 @@ public function setAuthenticationCookieAndNewSession(int $userId, string $token,
$this->sessionWrapper->set('userId', $userId);
}

private function isUserPageVisibleForUser(UserEntity $targetUser, ?int $requestUserId) : bool
{
$privacyLevel = $targetUser->getPrivacyLevel();

if ($privacyLevel === 2) {
return true;
}

if ($privacyLevel === 1 && $requestUserId !== null) {
return true;
}

return $targetUser->getId() === $requestUserId;
}

private function setAuthenticationToken(int $userId, string $deviceName, string $userAgent, DateTime $expirationDate) : string
{
$token = bin2hex(random_bytes(16));
Expand Down
6 changes: 2 additions & 4 deletions src/Domain/User/Service/UserPageAuthorizationChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ public function findUserIdIfCurrentVisitorIsAllowedToSeeUser(Request $request) :
return null;
}

$requestedUserId = $requestedUser->getId();

if ($this->authenticationService->isUserPageVisibleForCurrentUser($requestedUser->getPrivacyLevel(), $requestedUserId) === false) {
if ($this->authenticationService->isUserPageVisibleForWebRequest($requestedUser) === false) {
return null;
}

return $requestedUserId;
return $requestedUser->getId();
}
}

0 comments on commit a514ce2

Please sign in to comment.