Skip to content

Commit 3eb108a

Browse files
authored
Merge pull request #94 from bubnovKelnik/develop
Fixes #95, Fixes #96 Fix vich_config - property name, UserManager::hasRole(), hardcoded template path to prototype, UserLoad
2 parents 205535c + 08bbfbb commit 3eb108a

10 files changed

Lines changed: 67 additions & 39 deletions

File tree

EventListener/FileSubscriber.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public function postUpload(VichEvent\Event $event)
3030
{
3131
/** @var MessageAttachmentInterface $object */
3232
$object = $event->getObject();
33+
// Ignore any entity lifecycle events not relating to this bundles entities.
34+
if (!($object instanceof MessageAttachmentInterface)) {
35+
return;
36+
}
3337
$file = $object->getAttachmentFile();
3438
$object->setAttachmentSize($file->getSize());
3539
$object->setAttachmentMimeType($file->getMimeType());

EventListener/UserLoad.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Hackzilla\Bundle\TicketBundle\EventListener;
44

55
use Doctrine\ORM\Event\LifecycleEventArgs;
6-
use Hackzilla\Bundle\TicketBundle\Entity\Ticket;
7-
use Hackzilla\Bundle\TicketBundle\Entity\TicketMessage;
6+
use Hackzilla\Bundle\TicketBundle\Model\TicketInterface;
7+
use Hackzilla\Bundle\TicketBundle\Model\TicketMessageInterface;
88

99
class UserLoad
1010
{
@@ -27,20 +27,20 @@ public function postLoad(LifecycleEventArgs $args)
2727
$entity = $args->getEntity();
2828

2929
// Ignore any entity lifecycle events not relating to this bundles entities.
30-
if (!$entity instanceof Ticket && !$entity instanceof TicketMessage) {
30+
if (!$entity instanceof TicketInterface && !$entity instanceof TicketMessageInterface) {
3131
return;
3232
}
3333

3434
$userRepository = $args->getEntityManager()->getRepository($this->userRepository);
3535

36-
if ($entity instanceof Ticket) {
36+
if ($entity instanceof TicketInterface) {
3737
if (\is_null($entity->getUserCreatedObject())) {
3838
$entity->setUserCreated($userRepository->find($entity->getUserCreated()));
3939
}
4040
if (\is_null($entity->getLastUserObject())) {
4141
$entity->setLastUser($userRepository->find($entity->getLastUser()));
4242
}
43-
} elseif ($entity instanceof TicketMessage) {
43+
} elseif ($entity instanceof TicketMessageInterface) {
4444
if (\is_null($entity->getUserObject())) {
4545
$entity->setUser($userRepository->find($entity->getUser()));
4646
}

Manager/UserManager.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,38 @@
77
use Hackzilla\Bundle\TicketBundle\Model\UserInterface;
88
use Hackzilla\Bundle\TicketBundle\TicketRole;
99
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
10+
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1011

1112
class UserManager implements UserManagerInterface
1213
{
14+
/**
15+
* @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
16+
*/
1317
private $tokenStorage;
18+
19+
/**
20+
* @var \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface
21+
*/
22+
private $authorizationChecker;
23+
24+
/**
25+
* @var \Doctrine\ORM\EntityRepository
26+
*/
1427
private $userRepository;
1528

29+
/**
30+
* @param TokenStorage $tokenStorage
31+
* @param AuthorizationCheckerInterface $authorizationChecker
32+
* @param EntityRepository $userRepository
33+
*/
1634
public function __construct(
1735
TokenStorage $tokenStorage,
18-
EntityRepository $userRepository
36+
EntityRepository $userRepository,
37+
AuthorizationCheckerInterface $authorizationChecker
1938
) {
2039
$this->tokenStorage = $tokenStorage;
2140
$this->userRepository = $userRepository;
41+
$this->authorizationChecker = $authorizationChecker;
2242
}
2343

2444
/**
@@ -61,7 +81,7 @@ public function getUserById($userId)
6181
*/
6282
public function hasRole(UserInterface $user, $role)
6383
{
64-
return in_array(strtoupper($role), $user->getRoles(), true);
84+
return $this->authorizationChecker->isGranted($role);
6585
}
6686

6787
/**

Resources/config/services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ services:
1717
arguments:
1818
- '@security.token_storage'
1919
- '@hackzilla_ticket.user_repository'
20+
- '@security.authorization_checker'
2021

2122
hackzilla_ticket.user_repository:
2223
class: Doctrine\ORM\EntityRepository
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<vich_uploader class="Hackzilla\Bundle\TicketBundle\Entity\TicketMessageWithAttachment">
2-
<field mapping="ticket_message_attachment" name="file" filename_property="attachmentName" />
2+
<field mapping="ticket_message_attachment" name="attachmentFile" filename_property="attachmentName" />
33
</vich_uploader>

Resources/views/Ticket/show.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
{# <br />{{ 'LABEL_STATUS'|trans }} <span class="label label-{{ message.status ? 'success' : 'danger' }}">{{ message.statusString|trans }}</span> #}
4444

4545
<span class="pull-right">
46-
{% if message.userObject|isTicketAdmin('ROLE_TICKET_ADMIN') %}<span
46+
{% if message.userObject != ticket.userCreatedObject %}<span
4747
class="label label-danger">{{ 'LABEL_ADMIN'|trans }}</span> {% endif %}
4848
<small><i>{{ message.createdAt|date('LABEL_DATE_TIME_FORMAT'|trans) }}</i></small>
4949
</span>
@@ -66,7 +66,7 @@
6666
{% if form is defined %}
6767
<div class="well well-sm">
6868
{{ form_start(form, {'method': 'POST', 'action': path('hackzilla_ticket_reply', {'ticketId': ticket.id})}) }}
69-
{% include 'HackzillaTicketBundle:Ticket:prototype.html.twig' with {'form': form} %}
69+
{% include hackzilla_ticket.templates.prototype with {'form': form} %}
7070

7171
{{ form_rest(form) }}
7272

Tests/Form/Type/TicketMessageTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TicketMessageTypeTest extends TypeTestCase
1515

1616
protected function setUp()
1717
{
18-
$this->user = $this->createMock(UserManagerInterface::class);
18+
$this->user = $this->getMockBuilder(UserManagerInterface::class)->getMock();
1919

2020
parent::setUp();
2121
}

Tests/Form/Type/TicketTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TicketTypeTest extends TypeTestCase
1717

1818
protected function setUp()
1919
{
20-
$this->user = $this->createMock(UserManagerInterface::class);
20+
$this->user = $this->getMockBuilder(UserManagerInterface::class)->getMock();
2121

2222
parent::setUp();
2323
}

Tests/Manager/UserManagerTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@
55
use Doctrine\ORM\EntityRepository;
66
use Hackzilla\Bundle\TicketBundle\Manager\UserManager;
77
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
8+
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
9+
use Symfony\Component\Security\Core\Authentication\Provider\AnonymousAuthenticationProvider;
810
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
11+
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
12+
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
913

1014
class UserManagerTest extends WebTestCase
1115
{
1216
private $object;
1317
private $tokenStorage;
18+
private $authorizationChecker;
1419

1520
public function setUp()
1621
{
1722
$this->tokenStorage = new TokenStorage();
23+
$authenticationProviderManager = new AuthenticationProviderManager([new AnonymousAuthenticationProvider('secret')]);
24+
$accessDecisionManager = new AccessDecisionManager();
25+
$this->authorizationChecker = new AuthorizationChecker($this->tokenStorage, $authenticationProviderManager, $accessDecisionManager);
1826

1927
$this->object = new UserManager(
2028
$this->tokenStorage,
21-
$this->getMockUserRepository()
29+
$this->getMockUserRepository(),
30+
$this->authorizationChecker
2231
);
2332
}
2433

TwigExtension/TicketGlobalExtension.php

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,45 @@
44

55
use Symfony\Component\DependencyInjection\ContainerInterface;
66

7-
class TicketGlobalExtension extends \Twig_Extension {
8-
7+
class TicketGlobalExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
8+
{
99
/**
10-
*
11-
* @access protected
12-
* @var \Symfony\Component\DependencyInjection\ContainerInterface $container
10+
* @var \Symfony\Component\DependencyInjection\ContainerInterface
1311
*/
1412
protected $container;
1513

1614
/**
17-
*
18-
* @access public
1915
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
2016
*/
21-
public function __construct(ContainerInterface $container) {
17+
public function __construct(ContainerInterface $container)
18+
{
2219
$this->container = $container;
2320
}
2421

2522
/**
26-
*
27-
* @access public
2823
* @return array
2924
*/
30-
public function getGlobals() {
31-
return array(
32-
'hackzilla_ticket' => array(
33-
'templates' => array(
34-
'index' => $this->container->getParameter('hackzilla_ticket.templates.index'),
35-
'new' => $this->container->getParameter('hackzilla_ticket.templates.new'),
36-
'show' => $this->container->getParameter('hackzilla_ticket.templates.show'),
25+
public function getGlobals()
26+
{
27+
return [
28+
'hackzilla_ticket' => [
29+
'templates' => [
30+
'index' => $this->container->getParameter('hackzilla_ticket.templates.index'),
31+
'new' => $this->container->getParameter('hackzilla_ticket.templates.new'),
32+
'show' => $this->container->getParameter('hackzilla_ticket.templates.show'),
3733
'show_attachment' => $this->container->getParameter('hackzilla_ticket.templates.show_attachment'),
38-
'prototype' => $this->container->getParameter('hackzilla_ticket.templates.prototype'),
39-
'macros' => $this->container->getParameter('hackzilla_ticket.templates.macros'),
40-
),
41-
)
42-
);
34+
'prototype' => $this->container->getParameter('hackzilla_ticket.templates.prototype'),
35+
'macros' => $this->container->getParameter('hackzilla_ticket.templates.macros'),
36+
],
37+
],
38+
];
4339
}
4440

4541
/**
46-
*
47-
* @access public
4842
* @return string
4943
*/
50-
public function getName() {
44+
public function getName()
45+
{
5146
return 'ticketGlobal';
5247
}
53-
5448
}

0 commit comments

Comments
 (0)