welcome back to dyb-tech
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace DMD\LaLigaApi\Service\Common;
|
||||
|
||||
use DMD\LaLigaApi\Entity\League;
|
||||
use DMD\LaLigaApi\Entity\User;
|
||||
use DMD\LaLigaApi\Enum\Role;
|
||||
use DMD\LaLigaApi\Repository\CustomRoleRepository;
|
||||
use DMD\LaLigaApi\Repository\LeagueRepository;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class AuthorizeRequest
|
||||
{
|
||||
public function __construct(
|
||||
public Security $security,
|
||||
public CustomRoleRepository $customRoleRepository,
|
||||
public LeagueRepository $leagueRepository
|
||||
)
|
||||
{}
|
||||
|
||||
public function authorizeLeaguePresident(int $leagueId): void
|
||||
{
|
||||
$userEntity = $this->security->getUser();
|
||||
if (is_null($userEntity))
|
||||
{
|
||||
throw new HttpException(Response::HTTP_FORBIDDEN, "Unauthorized.");
|
||||
}
|
||||
$customRole = $this->customRoleRepository->findBy([
|
||||
'name' => $leagueId . Role::LEAGUE_PRESIDENT->value,
|
||||
'userEntity' => $userEntity
|
||||
]);
|
||||
if (is_null($customRole))
|
||||
{
|
||||
throw new HttpException(Response::HTTP_FORBIDDEN, "Usuario no tiene permiso para editar la liga.");
|
||||
}
|
||||
}
|
||||
|
||||
public function teamCaptainRequest(int $leagueId, $teamId): User
|
||||
{
|
||||
$userEntity = $this->security->getUser();
|
||||
if (!$userEntity instanceof User)
|
||||
{
|
||||
throw new HttpException(Response::HTTP_FORBIDDEN, "Unauthorized");
|
||||
}
|
||||
$captainCustomRole = $this->customRoleRepository->findBy([
|
||||
'name' => $teamId . Role::TEAM_CAPTAIN->value,
|
||||
]);
|
||||
if (!is_null($captainCustomRole))
|
||||
{
|
||||
throw new HttpException(Response::HTTP_FORBIDDEN, "Equipo con id: $teamId ya tiene capitan");
|
||||
}
|
||||
$leagueMemberRole = $this->customRoleRepository->findBy([
|
||||
'name' => $leagueId . Role::LEAGUE_MEMBER->value,
|
||||
'user' => $userEntity
|
||||
]);
|
||||
if (is_null($leagueMemberRole))
|
||||
{
|
||||
throw new HttpException(Response::HTTP_FORBIDDEN, "Usuario no es miembro de la liga");
|
||||
}
|
||||
return $userEntity;
|
||||
}
|
||||
|
||||
public function isLeaguePresident(int $leagueId, User $leagueAdmin): bool
|
||||
{
|
||||
$adminRoles = $leagueAdmin->getCustomRoles();
|
||||
if (!$adminRoles->isEmpty())
|
||||
{
|
||||
foreach ($adminRoles as $adminRoleEntity)
|
||||
{
|
||||
$explodedRole = explode('_', $adminRoleEntity->getName());
|
||||
if (
|
||||
strtolower($explodedRole[1]) == 'league' &&
|
||||
strtolower($explodedRole[2]) == 'president' &&
|
||||
$explodedRole[0] == $leagueId
|
||||
)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new HttpException(Response::HTTP_NOT_FOUND,'Forbidden.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user