Passed
Push — master ( 3c6fac...a2ff31 )
by Anthony
01:54
created

AccessRights::testRight()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 9.6666
1
<?php
2
3
namespace Ribs\RibsAdminBundle\Service;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
use Symfony\Component\HttpFoundation\Session\Session;
8
use Symfony\Component\Routing\RouterInterface;
9
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
10
11
class AccessRights
12
{
13
    private $em;
14
    private $router;
15
    private $session;
16
    private $request;
17
18
    /**
19
     * AccessRights constructor.
20
     * @param ContainerInterface $em
21
     * @param RouterInterface $router
22
     * @param Session $session
23
     * @param RequestStack $request
24
     */
25
    public function __construct(ContainerInterface $em, RouterInterface $router, Session $session, RequestStack $request)
26
    {
27
        $this->em = $em;
28
        $this->router = $router;
29
        $this->session = $session;
30
        $this->request = $request;
31
    }
32
33
    public function onKernelController()
34
    {
35
        $route = $this->request->getCurrentRequest()->get("_route");
36
        $admin_page = explode("_", $route)[0];
37
38
        //comment because it cause errore redirect
39
        /*if ($route == "fos_user_security_login" || $route == "fos_user_registration_register") {
40
            $this->session->clear();
41
            $this->em->get("security.token_storage")->setToken(null);
42
        }*/
43
44
        //to show admin panel
45
        if (in_array($route, ["_profiler", "_profiler_search_bar", "_wdt"])) {
46
            return;
47
        }
48
49
        $ribs_admin_rights = json_decode(file_get_contents($this->em->get('kernel')->getRootDir() . "/../src/Ribs/RibsAdminBundle/Resources/json/ribsadmin_rights.json"));
50
51
        if ($admin_page == "ribsadmin" && ($route !== 404) && ($route !== null)) {
52
            $route_right = $this->in_array_recursive($route, $ribs_admin_rights);
53
            $user_rights = $this->getUserRights();
54
55
            if ($route_right === false) {
56
                throw new AccessDeniedException("No access");
57
            }
58
59
            foreach ($user_rights as $right) {
60
                if (in_array($right, $route_right)) {
61
                    return;
62
                }
63
            }
64
65
            throw new AccessDeniedException("No access");
66
        }
67
    }
68
69
    /**
70
     * @param $needle
71
     * @param $haystack
72
     * @return bool|mixed
73
     * fonction that search if the right contain an url or more
74
     */
75
    private function in_array_recursive($needle, $haystack)
76
    {
77
        $rights = [];
78
        $it = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($haystack));
79
80
        foreach ($it AS $element => $value) {
81
            if ($value == $needle) {
82
                $rights[] = $it->getInnerIterator()["right"];
83
            }
84
        }
85
86
        if (count($rights) === 0) {
87
            return false;
88
        }
89
90
        return $rights;
91
    }
92
93
94
    /**
95
     * @return array function that retun a array that contain all user rights or empty array if no right found
96
     */
97
    private function getUserRights(): array
98
    {
99
        $user_rights = $this->em->get("security.token_storage")->getToken()->getUser()->getUser()->getAccessRights();
100
101
        if ($user_rights) {
102
            return explode(",", $user_rights);
103
        }
104
105
        return [""];
106
107
    }
108
109
    /**
110
     * @param string $right
111
     * @return bool
112
     * function that allow to test a right directly in the view
113
     */
114
    public function testRight(string $right): bool
115
    {
116
        $user_rights = $this->getUserRights();
117
118
        if (in_array($right, $user_rights)) {
119
            return true;
120
        }
121
122
        return false;
123
    }
124
}