Test Failed
Push — master ( d32ce5...6bd554 )
by Simon
54s
created

AccessControl::getAllSecuredRoutes()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 35
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
rs 4.909
cc 9
eloc 16
nc 7
nop 0
1
<?php
2
/**
3
 * User: Simon Libaud
4
 * Date: 19/03/2017
5
 * Email: [email protected].
6
 */
7
namespace Sil\RouteSecurityBundle\Security;
8
9
use Sil\RouteSecurityBundle\Interfaces\NamingStrategyInterface;
10
use Symfony\Component\Routing\RouterInterface;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
13
/**
14
 * Class AccessControl.
15
 */
16
class AccessControl
17
{
18
    private $router;
19
    private $routeToRoleConverter;
20
    private $is_access_control_enable;
21
    private $secured_routes;
22
    private $secured_routes_format;
23
    private $ignored_routes;
24
    private $ignored_routes_format;
25
    private $all_secured_routes;
26
27
    public function __construct(RouterInterface $router, NamingStrategyInterface $routeToRoleConverter, $configuration)
28
    {
29
        $this->router = $router;
30
        $this->routeToRoleConverter = $routeToRoleConverter;
31
32
        $this->is_access_control_enable = $configuration['enable_access_control'];
33
        $this->secured_routes = $configuration['secured_routes'];
34
        $this->secured_routes_format = $configuration['secured_routes_format'];
35
        $this->ignored_routes = $configuration['ignored_routes'];
36
        $this->ignored_routes_format = $configuration['ignored_routes_format'];
37
38
        $this->all_secured_routes = null;
39
    }
40
41
    /**
42
     * Verify if  user has access to a specific route.
43
     *
44
     * @param UserInterface $user
45
     * @param string        $route
46
     *
47
     * @return bool
48
     */
49
    public function hasUserAccessToRoute(UserInterface $user, $route)
50
    {
51
        if (false === $this->is_access_control_enable || false === $this->isRouteSecure($route)) {
52
            return true;
53
        }
54
55
        $role = $this->routeToRoleConverter->generateRoleForRoute($route);
56
57
        return in_array($role, $user->getRoles());
58
    }
59
60
    /**
61
     * Verify if user has access to all routes.
62
     *
63
     * @param UserInterface $user
64
     * @param array         $routes
65
     *
66
     * @return bool
67
     */
68 View Code Duplication
    public function hasUserAccessToRoutes(UserInterface $user, $routes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
    {
70
        foreach ($routes as $route) {
71
            if (false === $this->hasUserAccessToRoute($user, $route)) {
72
                return false;
73
            }
74
        }
75
76
        return true;
77
    }
78
79
    /**
80
     * Verify if user has access to one of routes.
81
     *
82
     * @param UserInterface $user
83
     * @param $routes
84
     *
85
     * @return bool
86
     */
87 View Code Duplication
    public function hasUserAccessAtLeastOneRoute(UserInterface $user, $routes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
88
    {
89
        foreach ($routes as $route) {
90
            if (true === $this->hasUserAccessToRoute($user, $route)) {
91
                return true;
92
            }
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * Check if the given route is manage by the bundle depending of the configuration.
100
     *
101
     * @param string $route
102
     *
103
     * @return bool
104
     */
105
    public function isRouteSecure($route)
106
    {
107
        return in_array($route, $this->getAllSecuredRoutes());
108
    }
109
110
    /**
111
     * Return the secured routes depending of the bundle configuration.
112
     *
113
     * @return array $secured_routes
114
     */
115
    public function getAllSecuredRoutes()
116
    {
117
        if (true === is_array($this->all_secured_routes)) {
118
            return $this->all_secured_routes;
119
        }
120
121
        $this->all_secured_routes = [];
122
        $configured_routes = array_keys($this->router->getRouteCollection()->all());
123
124
        foreach ($configured_routes as $route) {
125
126
            // Ignored routes
127
            if (in_array($route, $this->ignored_routes)) {
128
                continue;
129
            }
130
131
            // Ignored routes format
132
            if (null !== $this->ignored_routes_format && 1 === preg_match($this->ignored_routes_format, $route)) {
133
                continue;
134
            }
135
136
            // Secured routes
137
            if (true === in_array($route, $this->secured_routes)) {
138
                $this->all_secured_routes[] = $route;
139
                continue;
140
            }
141
142
            // Secured routes format
143
            if (null !== $this->secured_routes_format && 1 === preg_match($this->secured_routes_format, $route)) {
144
                $this->all_secured_routes[] = $route;
145
                continue;
146
            }
147
        }
148
149
        return $this->all_secured_routes;
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    public function isEnable()
156
    {
157
        return $this->is_access_control_enable;
158
    }
159
}
160