Passed
Push — main ( cc0eef...98df1b )
by Jean-Christophe
02:33
created

AclControllerTrait::isValidRole()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.1502

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 11
c 1
b 0
f 1
nc 7
nop 2
dl 0
loc 17
ccs 9
cts 11
cp 0.8182
crap 5.1502
rs 9.6111
1
<?php
2
3
namespace Ubiquity\security\acl\controllers;
4
5
use Ubiquity\security\acl\AclManager;
6
use Ubiquity\exceptions\AclException;
7
use Ubiquity\log\Logger;
8
9
/**
10
 * To use with a controller with acls.
11
 * Ubiquity\security\acl\controllers$AclControllerTrait
12
 * This class is part of Ubiquity
13
 *
14
 * @author jc
15
 * @version 1.0.0
16
 *         
17
 */
18
trait AclControllerTrait {
19
	public abstract function _getRole();
20
21
	/**
22
	 * Returns True if access to the controller is allowed for $role.
23
	 *
24
	 * @param string $action
25
	 * @param string $role
26
	 * @return boolean
27
	 */
28 1
	protected function isValidRole($action,$role) {
29 1
		$controller = \get_class ( $this );
30 1
		$resourceController = AclManager::getPermissionMap ()->getRessourcePermission ( $controller, $action );
31 1
		if (isset ( $resourceController )) {
32
			try{
33 1
				if (AclManager::isAllowed ( $role, $resourceController ['resource'], $resourceController ['permission'] )) {
34 1
					return true;
35
				}
36
			}
37
			catch(AclException $e){
38
				Logger::alert('Router', $role.' is not allowed for this resource','Acls',[$controller,$action]);
39
			}
40
		}
41 1
		if ($action !== '*') {
42 1
			return $this->isValidRole( '*',$role );
43
		}
44 1
		return false;
45
	}
46
	/**
47
	 * Returns True if access to the controller is allowed for the role returned by _getRole method.
48
	 * To be override in sub classes
49
	 *
50
	 * @param string $action
51
	 * @return boolean
52
	 */
53 1
	public function isValid($action) {
54 1
		return $this->isValidRole($action, $this->_getRole());
55
	}
56
}
57
58