Completed
Push — master ( 425846...6cb170 )
by Adam
07:36
created

Permission::addPermission()   C

Complexity

Conditions 11
Paths 26

Size

Total Lines 66
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 66
rs 5.9447
cc 11
eloc 29
nc 26
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Permission.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Permissions!
9
 * @subpackage     Security
10
 * @since          1.0.0
11
 *
12
 * @date           10.10.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Permissions\Security;
18
19
use Nette;
20
use Nette\Reflection;
21
use Nette\Security as NS;
22
use Nette\Utils;
23
24
use IPub;
25
use IPub\Permissions\Access;
26
use IPub\Permissions\Entities;
27
use IPub\Permissions\Exceptions;
28
use IPub\Permissions\Providers;
29
30
/**
31
 * Nette user permission
32
 *
33
 * @package        iPublikuj:Permissions!
34
 * @subpackage     Security
35
 *
36
 * @author         Adam Kadlec <[email protected]>
37
 */
38
class Permission extends NS\Permission implements NS\IAuthorizator
39
{
40
	/**
41
	 * @param Providers\IRolesProvider $rolesProvider
42
	 * @param Providers\IResourcesProvider $resourcesProvider
43
	 */
44
	public function __construct(
45
		Providers\IRolesProvider $rolesProvider,
46
		Providers\IResourcesProvider $resourcesProvider
47
	) {
48
		// Get all available resources
49
		$resources = $resourcesProvider->findAll();
50
51
		foreach ($resources as $resource) {
52
			$resourceParent = $resource->getParent();
53
54
			// Assign resource to application permission checker
55
			$this->addResource($resource->getResourceId(), $resourceParent ? $resourceParent->getResourceId() : NULL);
56
		}
57
58
		// Get all available roles
59
		$roles = $rolesProvider->findAll();
60
61
		// Register all available roles
62
		foreach ($roles as $role) {
63
			$roleParent = $role->getParent();
64
65
			// Assign role to application permission checker
66
			$this->addRole($role->getRoleId(), $roleParent ? $roleParent->getRoleId() : NULL);
67
68
			// Allow all privileges for administrator
69
			if ($role->isAdministrator()) {
70
				$this->allow($role->getRoleId(), self::ALL, self::ALL);
71
72
			// For others apply setup privileges
73
			} else {
74
				foreach ($role->getPermissions() as $permission) {
75
					$resource = $permission->getResource();
76
					$resource = $resource ? $resource->getResourceId() : NS\IAuthorizator::ALL;
77
78
					$this->allow($role->getRoleId(), $resource, $permission->getPrivilege(), $permission->getAssertion());
79
				}
80
			}
81
		}
82
	}
83
}
84