Skip to content

Commit

Permalink
Merge pull request #81 from VSVverkeerskunde/feature/VSVGVQ-130
Browse files Browse the repository at this point in the history
VSVGVQ-130 Add a simple dashboard view with route and firewall setting.
  • Loading branch information
stejes committed Aug 16, 2018
2 parents 46c0ea9 + f77c081 commit 1d05ce1
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ security:
- { path: '^/(nl|fr)/view/questions', roles: [ROLE_VSV, ROLE_ADMIN] }
- { path: '^/(nl|fr)/view/users', roles: [ROLE_VSV, ROLE_ADMIN] }
- { path: '^/(nl|fr)/view/companies', roles: [ROLE_CONTACT, ROLE_VSV, ROLE_ADMIN] }
- { path: '^/(nl|fr)/view/dashboard', roles: [ROLE_CONTACT, ROLE_VSV, ROLE_ADMIN] }
8 changes: 8 additions & 0 deletions config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ users_export:
requirements:
_locale: nl|fr

dashboard:
path: /{_locale}/view/dashboard/{companyId}
controller: VSV\GVQ_API\Dashboard\Controllers\DashboardViewController::dashboard
methods: [GET]
defaults: { companyId: null }
requirements:
_locale: nl|fr

documents_kickoff:
path: /documents/{document}
methods: [GET]
Expand Down
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,6 @@ services:
$teamRepository: '@team_repository'
$questionResultRepository: '@question_result_redis_repository'
tags: ['controller.service_arguments']

VSV\GVQ_API\Dashboard\Controllers\DashboardViewController:
tags: ['controller.service_arguments']
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<file>./src/Command/RedisCommand.php</file>
<file>./src/Company/Controllers/CompanyViewController.php</file>
<file>./src/Company/Forms/CompanyFormType.php</file>
<file>./src/Dashboard/Controllers/DashboardViewController.php</file>
<file>./src/Question/Controllers/QuestionViewController.php</file>
<file>./src/Question/Forms/QuestionFormType.php</file>
<file>./src/Quiz/Controllers/QuizController.php</file>
Expand Down
4 changes: 2 additions & 2 deletions src/Account/Controllers/AccountViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ public function login(Request $request): Response
$this->get('security.token_storage')->setToken($token);
$this->get('session')->set('_security_main', serialize($token));

if ($this->get('security.authorization_checker')->isGranted('ROLE_VSV')) {
if ($this->get('security.authorization_checker')->isGranted(['ROLE_VSV', 'ROLE_ADMIN'])) {
return $this->redirectToRoute('questions_view_index');
} else {
return $this->redirectToRoute('companies_view_index');
return $this->redirectToRoute('dashboard');
}
}
$this->addFlash('warning', $this->translator->trans('Account.inactive'));
Expand Down
26 changes: 26 additions & 0 deletions src/Company/Repositories/CompanyDoctrineRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use VSV\GVQ_API\Company\Models\Company;
use VSV\GVQ_API\Company\Repositories\Entities\CompanyEntity;
use VSV\GVQ_API\Company\ValueObjects\Alias;
use VSV\GVQ_API\User\Models\User;
use VSV\GVQ_API\User\Repositories\Entities\UserEntity;

class CompanyDoctrineRepository extends AbstractDoctrineRepository implements CompanyRepository
{
Expand Down Expand Up @@ -114,6 +116,30 @@ public function getAll(): ?Companies
/** @var CompanyEntity[] $companyEntities */
$companyEntities = $this->objectRepository->findAll();

return $this->toCompanies($companyEntities);
}

/**
* @inheritdoc
*/
public function getAllByUser(User $user): ?Companies
{
/** @var CompanyEntity[] $companyEntities */
$companyEntities = $this->objectRepository->findBy(
[
'userEntity' => UserEntity::fromUser($user),
]
);

return $this->toCompanies($companyEntities);
}

/**
* @param CompanyEntity[] $companyEntities
* @return null|Companies
*/
private function toCompanies(array $companyEntities): ?Companies
{
if (empty($companyEntities)) {
return null;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Company/Repositories/CompanyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use VSV\GVQ_API\Common\ValueObjects\NotEmptyString;
use VSV\GVQ_API\Company\Models\Company;
use VSV\GVQ_API\Company\ValueObjects\Alias;
use VSV\GVQ_API\User\Models\User;

interface CompanyRepository
{
Expand Down Expand Up @@ -42,4 +43,10 @@ public function getByAlias(Alias $alias): ?Company;
* @return null|Companies
*/
public function getAll(): ?Companies;

/**
* @param User $user
* @return null|Companies
*/
public function getAllByUser(User $user): ?Companies;
}
124 changes: 124 additions & 0 deletions src/Dashboard/Controllers/DashboardViewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php declare(strict_types=1);

namespace VSV\GVQ_API\Dashboard\Controllers;

use Ramsey\Uuid\Uuid;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use VSV\GVQ_API\Company\Models\Companies;
use VSV\GVQ_API\Company\Models\Company;
use VSV\GVQ_API\Company\Repositories\CompanyRepository;
use VSV\GVQ_API\User\Repositories\UserRepository;
use VSV\GVQ_API\User\ValueObjects\Email;

class DashboardViewController extends AbstractController
{
/**
* @var UserRepository
*/
private $userRepository;

/**
* @var CompanyRepository
*/
private $companyRepository;

/**
* @param UserRepository $userRepository
* @param CompanyRepository $companyRepository
*/
public function __construct(
UserRepository $userRepository,
CompanyRepository $companyRepository
) {
$this->userRepository = $userRepository;
$this->companyRepository = $companyRepository;
}

/**
* @param string $companyId
* @return Response
*/
public function dashboard(?string $companyId): Response
{
$companies = $this->getCompaniesForUser();

$company = $this->getActiveCompany($companies, $companyId);

return $this->render(
'dashboard/dashboard.html.twig',
[
'companies' => $companies? $companies->toArray() : [],
'company' => $company,
]
);
}

/**
* @return Companies|null
*/
private function getCompaniesForUser(): ?Companies
{
$user = $this->userRepository->getByEmail(new Email($this->getUser()->getUsername()));
if ($user === null) {
return null;
}

if ($this->get('security.authorization_checker')->isGranted(['ROLE_VSV', 'ROLE_ADMIN'])) {
return $this->companyRepository->getAll();
}

if ($this->get('security.authorization_checker')->isGranted(['ROLE_CONTACT'])) {
return $this->companyRepository->getAllByUser($user);
}
}

/**
* @param Companies|null $companies
* @param string|null $companyId
* @return Company
*/
private function getActiveCompany(
?Companies $companies,
?string $companyId
): ?Company {
if ($companies === null) {
return null;
}

if ($companyId === null) {
return $companies->getIterator()->current();
}

$company = $this->companyRepository->getById(Uuid::fromString($companyId));
if ($company === null) {
return null;
}

if (!$this->hasAccessRightsOnCompany($companies, $company)) {
throw new AccessDeniedHttpException();
}

return $company;
}

/**
* @param Companies $companies
* @param Company $activeCompany
* @return bool
*/
private function hasAccessRightsOnCompany(
Companies $companies,
Company $activeCompany
): bool {
/** @var Company $company */
foreach ($companies as $company) {
if ($company->getId()->equals($activeCompany->getId())) {
return true;
}
}

return false;
}
}
3 changes: 2 additions & 1 deletion templates/companies/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
{% for company in companies %}
<tr>
<td>
{{ company.id.toString }}
<a href="{{ path('dashboard', {'companyId': company.id.toString}) }}"
target="_blank" >{{ company.id.toString }}</a>
</td>
<td>
{{ company.name.toNative }}
Expand Down
21 changes: 21 additions & 0 deletions templates/dashboard/dashboard.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends 'authenticated_base.html.twig' %}

{% block title %}{% trans %}Dashboard{% endtrans %}{% endblock %}

{% block content %}

<h1 class="page-title">{% trans %}Dashboard{% endtrans %}</h1>

{% include '/includes/flash.html.twig' %}

<select class="form-control" id="company">
{% for company in companies %}
<option value="{{ company.id.toString }}">{{ company.name.toNative }}</option>
{% endfor %}
</select>

<h1>{{ company.name.toNative }}</h1>

<p>{% trans %}Dashboard.welcome{% endtrans %}</p>

{% endblock %}
48 changes: 41 additions & 7 deletions tests/Company/Repositories/CompanyDoctrineRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,12 @@ public function aliasProvider(): array
*/
public function it_can_get_all_companies(): void
{
$this->companyDoctrineRepository->save($this->company);

$alternateCompany = ModelsFactory::createAlternateCompany();
$this->userDoctrineRepository->save($alternateCompany->getUser());

$this->companyDoctrineRepository->save($alternateCompany);
$this->saveAlternateUserAndCompany();

$foundCompanies = $this->companyDoctrineRepository->getAll();

$this->assertEquals(
new Companies($this->company, $alternateCompany),
new Companies($this->company, ModelsFactory::createAlternateCompany()),
$foundCompanies
);
}
Expand All @@ -235,4 +230,43 @@ public function it_returns_null_when_no_companies_present(): void

$this->assertNull($foundCompanies);
}

/**
* @test
*/
public function it_can_get_all_companies_for_a_user(): void
{
$this->saveAlternateUserAndCompany();

$foundCompanies = $this->companyDoctrineRepository->getAllByUser(
$this->company->getUser()
);

$this->assertEquals(
new Companies($this->company),
$foundCompanies
);
}

/**
* @test
*/
public function it_returns_null_when_no_company_present_for_given_user(): void
{
$foundCompanies = $this->companyDoctrineRepository->getAllByUser(
ModelsFactory::createAlternateUser()
);

$this->assertNull($foundCompanies);
}

private function saveAlternateUserAndCompany(): void
{
$this->companyDoctrineRepository->save($this->company);

$alternateCompany = ModelsFactory::createAlternateCompany();
$this->userDoctrineRepository->save($alternateCompany->getUser());

$this->companyDoctrineRepository->save($alternateCompany);
}
}
2 changes: 2 additions & 0 deletions translations/messages.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,5 @@ Question.delete.success: La question %id% est supprimée
User.edit.not.found: Aucun utilisateur %id% n'a été trouvé
User.edit.success: L'utilisateur %id% est modifié
User.id.invalid: L'utilisateur %id% n'a pas été trouvé

Dashboard.welcome: '_Wanneer de quiz van start gaat op 15 oktober kan je hier de statistieken van je bedrijf opvolgen.'
2 changes: 2 additions & 0 deletions translations/messages.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,5 @@ Question.delete.success: 'Vraag %id% is verwijderd.'
User.edit.not.found: 'Geen gebruiker gevonden met id %id% om aan te passen.'
User.edit.success: 'Gebruiker %id% is aangepast.'
User.id.invalid: 'Geen gebruiker gevonden met id %id%'

Dashboard.welcome: 'Wanneer de quiz van start gaat op 15 oktober kan je hier de statistieken van je bedrijf opvolgen.'

0 comments on commit 1d05ce1

Please sign in to comment.