Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
KatjaGlassConsulting committed Mar 24, 2023
2 parents 99afedf + bc8b95e commit 455b1b2
Show file tree
Hide file tree
Showing 45 changed files with 3,293 additions and 1,261 deletions.
15 changes: 15 additions & 0 deletions API/ApprovalBundleApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ public function __construct(
* response=200,
* description="URL to submitted week"
* )
*
* @SWG\Parameter(
* name="user",
* in="query",
* type="integer",
* description="User ID to get information for",
* required=false,
* ),
* @SWG\Parameter(
* name="date",
* in="query",
* type="string",
* description="Date as monday of selected week: Y-m-d",
* required=true,
* )
*
* @Rest\Post(path="/add_to_approve")
* @ApiSecurity(name="apiUser")
Expand Down
8 changes: 8 additions & 0 deletions API/ApprovalNextWeekApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public function __construct(
* response=200,
* description="Status of selected week"
* )
*
* @SWG\Parameter(
* name="user",
* in="query",
* type="integer",
* description="User ID to get information for",
* required=false,
* )
*
* @Rest\Get(path="/next-week")
* @ApiSecurity(name="apiUser")
Expand Down
187 changes: 187 additions & 0 deletions API/ApprovalOvertimeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KimaiPlugin\ApprovalBundle\API;

use App\Repository\UserRepository;
use Exception;
use DateTime;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use KimaiPlugin\ApprovalBundle\Repository\ApprovalRepository;
use Nelmio\ApiDocBundle\Annotation\Security as ApiSecurity;
use Swagger\Annotations as SWG;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use KimaiPlugin\ApprovalBundle\Enumeration\ConfigEnum;
use KimaiPlugin\ApprovalBundle\Toolbox\SettingsTool;

/**
* @SWG\Tag(name="ApprovalBundleApi")
*/
final class ApprovalOvertimeController extends AbstractController
{
/**
* @var UserRepository
*/
private $userRepository;
/**
* @var ViewHandlerInterface
*/
private $viewHandler;
/**
* @var ApprovalRepository
*/
private $approvalRepository;
/**
* @var AuthorizationCheckerInterface
*/
private $security;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var SettingsTool
*/
private $settingsTool;

public function __construct(
ViewHandlerInterface $viewHandler,
UserRepository $userRepository,
ApprovalRepository $approvalRepository,
AuthorizationCheckerInterface $security,
TranslatorInterface $translator,
SettingsTool $settingsTool
) {
$this->viewHandler = $viewHandler;
$this->userRepository = $userRepository;
$this->approvalRepository = $approvalRepository;
$this->security = $security;
$this->translator = $translator;
$this->settingsTool = $settingsTool;
}

/**
* @SWG\Response(
* response=200,
* description="Get overtime for that year"
* )
*
* @SWG\Parameter(
* name="user",
* in="query",
* type="integer",
* description="User ID to get information for",
* required=false,
* ),
* @SWG\Parameter(
* name="date",
* in="query",
* type="string",
* description="Date to get overtime until/including this date: Y-m-d",
* required=true,
* )
*
* @Rest\Get(path="/overtime_year")
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
* @throws Exception
*/
public function overtimeForYearUntil(Request $request): Response
{
$selectedUserId = $request->query->get('user', -1);
$seletedDate = new DateTime($request->query->get('date'));

if (!$this->settingsTool->getConfiguration(ConfigEnum::APPROVAL_OVERTIME_NY)) {
return $this->viewHandler->handle(
new View(
$this->translator->trans('api.noOvertimeSetting'),
200
)
);
}

$currentUser = $this->userRepository->find($this->getUser()->getId());

if ($selectedUserId !== -1) {
if (!$this->isGrantedViewAllApproval() && !$this->isGrantedViewTeamApproval()) {
return $this->error400($this->translator->trans('api.accessDenied'));
}
if (
!$this->isGrantedViewAllApproval() &&
$this->isGrantedViewTeamApproval() &&
empty($this->checkIfUserInTeam($currentUser, $selectedUserId))
) {
return $this->error400($this->translator->trans('api.wrongTeam'));
}
$selectedUser = $this->userRepository->find($selectedUserId);
if (!$selectedUser || !$selectedUser->isEnabled()) {
return $this->error404($this->translator->trans('api.wrongUser'));
}
$currentUser = $selectedUser;
}

$overtime = $this->approvalRepository->getExpectedActualDurationsForYear($currentUser, $seletedDate);

if ($overtime) {
return $this->viewHandler->handle(
new View(
$overtime,
200
)
);
}
return $this->error404($this->translator->trans('api.noData'));
}

private function isGrantedViewAllApproval(): bool
{
return $this->security->isGranted('view_all_approval');
}

private function isGrantedViewTeamApproval(): bool
{
return $this->security->isGranted('view_team_approval');
}

protected function error404(string $message): Response
{
return $this->viewHandler->handle(
new View($message, 404)
);
}

protected function error400(string $message): Response
{
return $this->viewHandler->handle(
new View($message, 400)
);
}

protected function checkIfUserInTeam($user, $selectedUserId): array
{
return array_filter(
$user->getTeams(),
function ($team) use ($selectedUserId) {
foreach ($team->getUsers() as $user) {
if ($user->getId() == $selectedUserId) {
return true;
}
}

return false;
}
);
}
}
Loading

0 comments on commit 455b1b2

Please sign in to comment.