Completed
Pull Request — master (#24)
by Wachter
23:14 queued 10:42
created

CommentAdmin::getSecurityContexts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 0
cp 0
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) Sulu GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\CommentBundle\Admin;
13
14
use Sulu\Bundle\AdminBundle\Admin\Admin;
15
use Sulu\Bundle\AdminBundle\Admin\Routing\RouteBuilderFactoryInterface;
16
use Sulu\Bundle\AdminBundle\Navigation\Navigation;
17
use Sulu\Bundle\AdminBundle\Navigation\NavigationItem;
18
use Sulu\Component\Security\Authorization\PermissionTypes;
19
use Sulu\Component\Security\Authorization\SecurityCheckerInterface;
20
21
/**
22
 * Integrates sulu_comment into sulu-admin.
23
 */
24
class CommentAdmin extends Admin
25
{
26
    const COMMENT_SECURITY_CONTEXT = 'sulu.comment.comments';
27
    const COMMENT_LIST_ROUTE = 'sulu_comment.comments.list';
28
    const COMMENT_EDIT_FORM_ROUTE = 'sulu_comment.comments.edit_form';
29
    const COMMENT_EDIT_FORM_DETAILS_ROUTE = 'sulu_comment.comments.edit_form.details';
30
31
    const THREAD_SECURITY_CONTEXT = 'sulu.comment.threads';
32
    const THREAD_LIST_ROUTE = 'sulu_comment.threads.list';
33
    const THREAD_EDIT_FORM_ROUTE = 'sulu_comment.threads.edit_form';
34
    const THREAD_EDIT_FORM_DETAILS_ROUTE = 'sulu_comment.threads.edit_form.details';
35
36
    /**
37
     * @var RouteBuilderFactoryInterface
38
     */
39
    private $routeBuilderFactory;
40
41
    /**
42
     * @var SecurityCheckerInterface
43
     */
44
    private $securityChecker;
45
46
    public function __construct(RouteBuilderFactoryInterface $routeBuilderFactory, SecurityCheckerInterface $securityChecker)
47
    {
48
        $this->routeBuilderFactory = $routeBuilderFactory;
49
        $this->securityChecker = $securityChecker;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getNavigation(): Navigation
56
    {
57
        $rootNavigationItem = $this->getNavigationItemRoot();
58
59
        $commentModule = new NavigationItem('sulu_comment.comments');
60
        $commentModule->setPosition(21);
61
        $commentModule->setIcon('su-comment');
62
63 View Code Duplication
        if ($this->securityChecker->hasPermission(self::COMMENT_SECURITY_CONTEXT, PermissionTypes::VIEW)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
            $comments = new NavigationItem('sulu_comment.comments');
65
            $comments->setPosition(10);
66
            $comments->setMainRoute(static::COMMENT_LIST_ROUTE);
67
68
            $commentModule->addChild($comments);
69
        }
70
71 View Code Duplication
        if ($this->securityChecker->hasPermission(self::THREAD_SECURITY_CONTEXT, PermissionTypes::VIEW)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
            $threads = new NavigationItem('sulu_comment.threads');
73
            $threads->setPosition(20);
74
            $threads->setMainRoute(static::THREAD_LIST_ROUTE);
75
76
            $commentModule->addChild($threads);
77
        }
78
79
        if ($commentModule->hasChildren()) {
80
            $rootNavigationItem->addChild($commentModule);
81
        }
82
83
        return new Navigation($rootNavigationItem);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getRoutes(): array
90
    {
91
        $formToolbarActions = [
92
            'sulu_admin.save',
93
            'sulu_admin.delete',
94
        ];
95
96
        $listToolbarActions = [
97
            'sulu_admin.delete',
98
            'sulu_admin.export',
99
        ];
100
101
        return [
102
            $this->routeBuilderFactory->createListRouteBuilder(static::COMMENT_LIST_ROUTE, '/comments')
103
                ->setResourceKey('comments')
104
                ->setListKey('comments')
105
                ->setTitle('sulu_comment.comments')
106
                ->addListAdapters(['table'])
107
                ->setEditRoute(static::COMMENT_EDIT_FORM_ROUTE)
108
                ->enableSearching()
109
                ->addToolbarActions($listToolbarActions)
110
                ->getRoute(),
111
            $this->routeBuilderFactory->createResourceTabRouteBuilder(static::COMMENT_EDIT_FORM_ROUTE, '/comments/:id')
112
                ->setResourceKey('comments')
113
                ->setBackRoute(static::COMMENT_LIST_ROUTE)
114
                ->getRoute(),
115
            $this->routeBuilderFactory->createFormRouteBuilder(static::COMMENT_EDIT_FORM_DETAILS_ROUTE, '/details')
116
                ->setResourceKey('comments')
117
                ->setFormKey('comment_details')
118
                ->setTabTitle('sulu_admin.details')
119
                ->addToolbarActions(array_merge($formToolbarActions, ['sulu_admin.publish_toggler']))
120
                ->setParent(static::COMMENT_EDIT_FORM_ROUTE)
121
                ->getRoute(),
122
            $this->routeBuilderFactory->createListRouteBuilder(static::THREAD_LIST_ROUTE, '/threads')
123
                ->setResourceKey('threads')
124
                ->setListKey('threads')
125
                ->setTitle('sulu_comment.threads')
126
                ->addListAdapters(['table'])
127
                ->setEditRoute(static::THREAD_EDIT_FORM_ROUTE)
128
                ->enableSearching()
129
                ->addToolbarActions($listToolbarActions)
130
                ->getRoute(),
131
            $this->routeBuilderFactory->createResourceTabRouteBuilder(static::THREAD_EDIT_FORM_ROUTE, '/threads/:id')
132
                ->setResourceKey('threads')
133
                ->setBackRoute(static::THREAD_LIST_ROUTE)
134
                ->getRoute(),
135
            $this->routeBuilderFactory->createFormRouteBuilder(static::THREAD_EDIT_FORM_DETAILS_ROUTE, '/details')
136
                ->setResourceKey('threads')
137
                ->setFormKey('thread_details')
138
                ->setTabTitle('sulu_admin.details')
139
                ->addToolbarActions($formToolbarActions)
140
                ->setParent(static::THREAD_EDIT_FORM_ROUTE)
141
                ->getRoute(),
142
        ];
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function getSecurityContexts()
149
    {
150
        return [
151
            'Sulu' => [
152
                'Comment' => [
153
                    self::COMMENT_SECURITY_CONTEXT => [
154
                        PermissionTypes::VIEW,
155
                        PermissionTypes::DELETE,
156
                        PermissionTypes::LIVE,
157
                    ],
158
                    self::THREAD_SECURITY_CONTEXT => [
159
                        PermissionTypes::VIEW,
160
                        PermissionTypes::DELETE,
161
                        PermissionTypes::EDIT,
162
                    ],
163
                ],
164
            ],
165
        ];
166
    }
167
}
168