Skip to content

Commit

Permalink
Add helper function to calculate and return assigned user privilege s…
Browse files Browse the repository at this point in the history
…copes
  • Loading branch information
Akshay Kumar committed May 31, 2023
1 parent 51e0a03 commit bd8d28c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
39 changes: 38 additions & 1 deletion Services/UVDeskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Webkul\UVDesk\SupportCenterBundle\Entity\KnowledgebaseWebsite;
use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;

class UVDeskService
{
Expand All @@ -35,11 +36,12 @@ class UVDeskService
' ',
];

public function __construct(ContainerInterface $container, RequestStack $requestStack, EntityManagerInterface $entityManager)
public function __construct(ContainerInterface $container, RequestStack $requestStack, EntityManagerInterface $entityManager, UserService $userService)
{
$this->container = $container;
$this->requestStack = $requestStack;
$this->entityManager = $entityManager;
$this->userService = $userService;
}

public function updatesLocales($locales)
Expand Down Expand Up @@ -260,6 +262,7 @@ public function getActiveConfiguration($websiteId)
public function getSupportPrivelegesResources()
{
$translator = $this->container->get('translator');

return [
'ticket' => [
'ROLE_AGENT_CREATE_TICKET' => $translator->trans('Can create ticket'),
Expand Down Expand Up @@ -459,4 +462,38 @@ public function generateCompleteLocalResourcePathUri($resource)

return $this->completeLocalResourcePathUri . $resource;
}

public function getAvailableUserAccessScopes($user, $userInstance)
{
$supportRole = $userInstance->getSupportRole();
$isAdminAccessGranted = in_array($supportRole->getId(), [1, 2]) ? true : false;

$availableSupportPrivileges = $this->getSupportPrivelegesResources();
$resolvedAvailableSupportPrivileges = [];

foreach ($availableSupportPrivileges as $index => $collection) {
foreach ($collection as $privilegeId => $privilegeDescription) {
$resolvedAvailableSupportPrivileges[] = $privilegeId;
}
}

if (false == $isAdminAccessGranted) {
$assignedUserSupportPrivileges = $this->userService->getAssignedUserSupportPrivilegeDetails($user, $userInstance);
$resolvedAssignedUserSupportPrivileges = [];

foreach ($assignedUserSupportPrivileges as $assignedSupportPrivilege) {
foreach ($assignedSupportPrivilege['privileges'] as $privilegeId) {
$resolvedAssignedUserSupportPrivileges[] = $privilegeId;
}
}

return array_map(function ($supportPrivilege) {
return strtolower(str_replace('ROLE_AGENT_', '', $supportPrivilege));
}, $resolvedAssignedUserSupportPrivileges);
}

return array_map(function ($supportPrivilege) {
return strtolower(str_replace('ROLE_AGENT_', '', $supportPrivilege));
}, $resolvedAvailableSupportPrivileges);
}
}
26 changes: 21 additions & 5 deletions Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,11 +724,14 @@ public function getUserDetailById($userId) {
public function getUserPrivilegeIds($userId)
{
$qb = $this->entityManager->createQueryBuilder();
$qb->select('supportPrivileges.id')->from(User::class, 'user')
->leftJoin('user.userInstance','userInstance')
->leftJoin('userInstance.supportPrivileges','supportPrivileges')
->andwhere('user.id = :userId')
->setParameter('userId', $userId);
$qb
->select('supportPrivileges.id')
->from(User::class, 'user')
->leftJoin('user.userInstance','userInstance')
->leftJoin('userInstance.supportPrivileges','supportPrivileges')
->andwhere('user.id = :userId')
->setParameter('userId', $userId)
;

return array_column($qb->getQuery()->getArrayResult(), 'id');
}
Expand Down Expand Up @@ -888,4 +891,17 @@ public function getCustomersCountForKudos($container)

return $qb->getQuery()->getSingleScalarResult();
}

public function getAssignedUserSupportPrivilegeDetails($user, $userInstance)
{
$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder
->select('DISTINCT privilege.id, privilege.name, privilege.privileges')
->from(SupportPrivilege::class, 'privilege')
->leftJoin('privilege.users','userInstance')
->where('userInstance.id = :userInstanceId')->setParameter('userInstanceId', $userInstance->getId())
;

return $queryBuilder->getQuery()->getResult();
}
}

0 comments on commit bd8d28c

Please sign in to comment.