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

AclControllerTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 13
c 3
b 0
f 2
dl 0
loc 37
ccs 11
cts 13
cp 0.8462
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidRole() 0 17 5
A isValid() 0 2 1
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