Passed
Push — master ( fcabc1...64f2a1 )
by Simon
02:24
created

AccessControl::getSecuredRoutes()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 5.3846
cc 8
eloc 14
nc 6
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
26
    public function __construct(RouterInterface $router, NamingStrategyInterface $routeToRoleConverter, $configuration)
27
    {
28
        $this->router = $router;
29
        $this->routeToRoleConverter = $routeToRoleConverter;
30
31
        $this->is_access_control_enable = $configuration['enable_access_control'];
32
        $this->secured_routes = $configuration['secured_routes'];
33
        $this->secured_routes_format = $configuration['secured_routes_format'];
34
        $this->ignored_routes = $configuration['ignored_routes'];
35
        $this->ignored_routes_format = $configuration['ignored_routes_format'];
36
    }
37
38
    /**
39
     * Verify if  user has access to a specific route.
40
     *
41
     * @param UserInterface $user
42
     * @param string        $route
43
     *
44
     * @return bool
45
     */
46
    public function hasUserAccessToRoute(UserInterface $user, $route)
47
    {
48
        if (false === $this->is_access_control_enable || false === $this->isRouteSecure($route)) {
49
            return true;
50
        }
51
52
        $role = $this->routeToRoleConverter->generateRoleForRoute($route);
53
54
        return in_array($role, $user->getRoles());
55
    }
56
57
    /**
58
     * Verify if user has access to all routes.
59
     *
60
     * @param UserInterface $user
61
     * @param array         $routes
62
     *
63
     * @return bool
64
     */
65 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...
66
    {
67
        foreach ($routes as $route) {
68
            if (false === $this->hasUserAccessToRoute($user, $route)) {
69
                return false;
70
            }
71
        }
72
73
        return true;
74
    }
75
76
    /**
77
     * Verify if user has access to one of routes.
78
     *
79
     * @param UserInterface $user
80
     * @param $routes
81
     *
82
     * @return bool
83
     */
84 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...
85
    {
86
        foreach ($routes as $route) {
87
            if (true === $this->hasUserAccessToRoute($user, $route)) {
88
                return true;
89
            }
90
        }
91
92
        return false;
93
    }
94
95
    /**
96
     * Check if the given route is manage by the bundle depending of the configuration.
97
     *
98
     * @param string $route
99
     *
100
     * @return bool
101
     */
102
    public function isRouteSecure($route)
103
    {
104
        return in_array($route, $this->getSecuredRoutes());
105
    }
106
107
    /**
108
     * Return the secured routes depending of the bundle configuration.
109
     *
110
     * @return array $secured_routes
111
     */
112
    public function getSecuredRoutes()
113
    {
114
        $configured_routes = array_keys($this->router->getRouteCollection()->all());
115
        $secured_routes = [];
116
117
        foreach ($configured_routes as $route) {
118
119
            // Ignored routes
120
            if (in_array($route, $this->ignored_routes)) {
121
                continue;
122
            }
123
124
            // Ignored routes format
125
            if (null !== $this->ignored_routes_format && 1 === preg_match($this->ignored_routes_format, $route)) {
126
                continue;
127
            }
128
129
            // Secured routes
130
            if (true === in_array($route, $this->secured_routes)) {
131
                $secured_routes[] = $route;
132
                continue;
133
            }
134
135
            // Secured routes format
136
            if (null !== $this->secured_routes_format && 1 === preg_match($this->secured_routes_format, $route)) {
137
                $secured_routes[] = $route;
138
                continue;
139
            }
140
        }
141
142
        return $secured_routes;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function isEnable()
149
    {
150
        return $this->is_access_control_enable;
151
    }
152
}
153