Passed
Push — master ( b3073b...104510 )
by Julito
11:41
created

denyAccessUnlessValidResource()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Traits;
6
7
use Chamilo\CoreBundle\Entity\AbstractResource;
8
use Chamilo\CoreBundle\Entity\ResourceInterface;
9
use Chamilo\CoreBundle\Entity\ResourceNode;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CoreBundle\Repository\ResourceFactory;
12
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
13
use Chamilo\CoreBundle\Repository\ResourceRepository;
14
use Doctrine\ORM\EntityNotFoundException;
15
use Psr\Container\ContainerInterface;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
18
use Symfony\Component\Security\Core\User\UserInterface;
19
20
trait ResourceControllerTrait
21
{
22
    /** @var ContainerInterface */
23
    protected $container;
24
25
    public function getRepositoryFromRequest(Request $request): ResourceRepository
26
    {
27
        $tool = $request->get('tool');
28
        $type = $request->get('type');
29
30
        return $this->getRepository($tool, $type);
31
    }
32
33
    public function getResourceNodeRepository(): ResourceNodeRepository
34
    {
35
        return $this->container->get(ResourceNodeRepository::class);
36
    }
37
38
    public function getResourceRepositoryFactory(): ResourceFactory
39
    {
40
        return $this->container->get(ResourceFactory::class);
41
    }
42
43
    public function getRepository($tool, $type): ResourceRepository
44
    {
45
        $name = $this->getResourceRepositoryFactory()->getRepositoryService($tool, $type);
46
47
        return $this->container->get($name);
48
    }
49
50
    public function denyAccessUnlessValidResource(AbstractResource $resource = null)
51
    {
52
        if (null === $resource) {
53
            throw new EntityNotFoundException($this->trans('Resource doesn\'t exists.'));
0 ignored issues
show
Bug introduced by
It seems like trans() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
            throw new EntityNotFoundException($this->/** @scrutinizer ignore-call */ trans('Resource doesn\'t exists.'));
Loading history...
54
        }
55
56
        $resourceNode = $resource->getResourceNode();
57
58
        if (null === $resourceNode) {
59
            throw new EntityNotFoundException($this->trans('Resource doesn\'t have a node.'));
60
        }
61
    }
62
63
    public function getResourceParams(Request $request): array
64
    {
65
        $tool = $request->get('tool');
66
        $type = $request->get('type');
67
        $id = (int) $request->get('id');
68
69
        $courseId = null;
70
        $sessionId = null;
71
72
        if ($this->hasCourse()) {
0 ignored issues
show
Bug introduced by
It seems like hasCourse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        if ($this->/** @scrutinizer ignore-call */ hasCourse()) {
Loading history...
73
            $courseId = $this->getCourse()->getId();
0 ignored issues
show
Bug introduced by
It seems like getCourse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            $courseId = $this->/** @scrutinizer ignore-call */ getCourse()->getId();
Loading history...
74
            $session = $this->getCourseSession();
0 ignored issues
show
Bug introduced by
It seems like getCourseSession() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
            /** @scrutinizer ignore-call */ 
75
            $session = $this->getCourseSession();
Loading history...
75
            $sessionId = $session ? $session->getId() : 0;
76
        }
77
78
        return [
79
            'id' => $id,
80
            'tool' => $tool,
81
            'type' => $type,
82
            'cid' => $courseId,
83
            'sid' => $sessionId,
84
        ];
85
    }
86
87
    protected function getParentResourceNode(Request $request): ResourceNode
88
    {
89
        $parentNodeId = $request->get('id');
90
91
        $parentResourceNode = null;
92
        if (empty($parentNodeId)) {
93
            if ($this->hasCourse()) {
94
                $parentResourceNode = $this->getCourse()->getResourceNode();
95
            } else {
96
                if ($this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
97
                    /** @var User $user */
98
                    $user = $this->getUser();
99
                    if ($user) {
0 ignored issues
show
introduced by
$user is of type Chamilo\CoreBundle\Entity\User, thus it always evaluated to true.
Loading history...
100
                        $parentResourceNode = $user->getResourceNode();
101
                    }
102
                }
103
            }
104
        } else {
105
            $repo = $this->container->get('doctrine')->getRepository(ResourceNode::class);
106
            $parentResourceNode = $repo->find($parentNodeId);
107
        }
108
109
        if (null === $parentResourceNode) {
110
            throw new AccessDeniedException();
111
        }
112
113
        return $parentResourceNode;
114
    }
115
116
    protected function getUser(): ?UserInterface
117
    {
118
        /*if (!$this->container->has('security.token_storage')) {
119
            throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
120
        }*/
121
122
        if (null === $token = $this->container->get('security.token_storage')->getToken()) {
123
            return null;
124
        }
125
126
        if (!\is_object($user = $token->getUser())) {
127
            // e.g. anonymous authentication
128
            return null;
129
        }
130
131
        return $user;
132
    }
133
134
    private function setBreadCrumb(Request $request, ResourceNode $resourceNode)
135
    {
136
        return false;
137
        /*$tool = $request->get('tool');
138
        $resourceNodeId = $request->get('id');
139
        $routeParams = $this->getResourceParams($request);
140
        $baseNodeId = $this->getCourse()->getResourceNode()->getId();
141
142
        if (!empty($resourceNodeId)) {
143
            $breadcrumb = $this->getBreadCrumb();
144
            $toolParams = $routeParams;
145
            $toolParams['id'] = null;
146
147
            // Root tool link
148
            $breadcrumb->addChild(
149
                $this->trans($tool),
150
                [
151
                    'uri' => $this->generateUrl('chamilo_core_resource_index', $toolParams),
152
                ]
153
            );
154
155
            $repo = $this->getRepositoryFromRequest($request);
156
            $settings = $repo->getResourceSettings();
157
158
            //$originalResource = $repo->findOneBy(['resourceNode' => $resourceNodeId]);
159
            if (null === $resourceNode) {
160
                return;
161
            }
162
            $parentList = $resourceNode->getPathForDisplayToArray($baseNodeId);*/
163
//            var_dump($originalParent->getPath(), $originalParent->getPathForDisplay());
164
//            $parentList = [];
165
            /*          while (null !== $parent) {
166
                          if ($type !== $parent->getResourceType()->getName()) {
167
                              break;
168
                          }
169
                          $parent = $parent->getParent();
170
                          if ($parent) {
171
                              $resource = $repo->findOneBy(['resourceNode' => $parent->getId()]);
172
                              if ($resource) {
173
                                  $parentList[] = $resource;
174
                              }
175
                          }
176
                      }
177
                      $parentList = array_reverse($parentList);
178
                      foreach ($parentList as $item) {
179
                          $params = $routeParams;
180
                          $params['id'] = $item->getResourceNode()->getId();
181
                          $breadcrumb->addChild(
182
                              $item->getResourceName(),
183
                              [
184
                                  'uri' => $this->generateUrl('chamilo_core_resource_list', $params),
185
                              ]
186
                          );
187
                      }*/
188
189
            /*foreach ($parentList as $id => $title) {
190
                $params = $routeParams;
191
                $params['id'] = $id;
192
                $breadcrumb->addChild(
193
                    $title,
194
                    [
195
                        'uri' => $this->generateUrl('chamilo_core_resource_list', $params),
196
                    ]
197
                );
198
            }
199
200
            $params = $routeParams;
201
            $params['id'] = $resourceNode->getId();
202
            if (false === $settings->isAllowNodeCreation() || $resourceNode->hasResourceFile()) {
203
                $breadcrumb->addChild($resourceNode->getTitle());
204
            } else {
205
                $breadcrumb->addChild(
206
                    $resourceNode->getTitle(),
207
                    [
208
                        'uri' => $this->generateUrl('chamilo_core_resource_list', $params),
209
                    ]
210
                );
211
            }
212
        }*/
213
    }
214
}
215