Passed
Push — php8 ( bd75dc...d8afb4 )
by Fabio
08:15
created

setPermissionsManager()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * TPermissionsConfigurationBehavior class file.
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 */
9
10
namespace Prado\Security\Permissions;
11
12
use Prado\Util\TBehavior;
13
14
/**
15
 * TPermissionsConfigurationBehavior class.
16
 *
17
 * TPermissionsConfigurationBehavior is designed specifically to attach to the
18
 * {@link TPageConfiguration} class objects.  It reads and parses the
19
 * permissions role hierarchy and permissions rules from a page configuration
20
 * file.  Within the config.xml for a page, for example, add the following:
21
 * <code>
22
 * 		<permissions>
23
 *			<role name="pageRole" children="otherRole, permission_name" />
24
 *			<permissionrule name="permission_name" action="allow" roles="manager"/>
25
 *		</permissions>
26
 * </code>
27
 *
28
 * See <@link TPermissionsManager> for information on php configurations.
29
 *
30
 * @author Brad Anderson <[email protected]>
31
 * @since 4.2.0
32
 */
33
class TPermissionsConfigurationBehavior extends TBehavior
34
{
35
	/** @var \Prado\Security\Permissions\TPermissionsManager manager object for the behavior */
36
	private $_manager;
37
38
	/** @var array|\Prado\Xml\TXmlElement permissions data to parse */
39
	private $_permissions = [];
40
41
	/**
42
	 * @param null|\Prado\Security\Permissions\TPermissionsManager $manager
43
	 */
44
	public function __construct($manager = null)
45
	{
46
		if ($manager) {
47
			$this->setPermissionsManager($manager);
48
		}
49
		parent::__construct();
50
	}
51
52
	/**
53
	 * Loads the configuration specific for page service. This may be called multiple
54
	 * times.
55
	 * @param array $config config array
56
	 * @param string $configPath base path corresponding to this php element
57
	 * @param string $configPagePath the page path that the config php is associated with. The page path doesn't include the page name.
58
	 * @param \Prado\Util\TCallChain $callchain
59
	 */
60
	public function dyLoadPageConfigurationFromPhp($config, $configPath, $configPagePath, $callchain)
61
	{
62
		// authorization
63
		if (isset($config['permissions']) && is_array($config['permissions'])) {
64
			$this->_permissions[] = $config['permissions'];
65
		}
66
		return $callchain->dyLoadPageConfigurationFromPhp($config, $configPath, $configPagePath);
0 ignored issues
show
Bug introduced by
The method dyLoadPageConfigurationFromPhp() does not exist on Prado\Util\TCallChain. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
		return $callchain->/** @scrutinizer ignore-call */ dyLoadPageConfigurationFromPhp($config, $configPath, $configPagePath);
Loading history...
67
	}
68
69
	/**
70
	 * Loads the configuration specific for page service. This may be called multiple
71
	 * times.
72
	 * @param \Prado\Xml\TXmlElement $dom config xml element
73
	 * @param string $configPath base path corresponding to this xml element
74
	 * @param string $configPagePath the page path that the config XML is associated with. The page path doesn't include the page name.
75
	 * @param \Prado\Util\TCallChain $callchain
76
	 */
77
	public function dyLoadPageConfigurationFromXml($dom, $configPath, $configPagePath, $callchain)
78
	{
79
		// authorization
80
		if (($permissionsNode = $dom->getElementByTagName('permissions')) !== null) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $permissionsNode is correct as $dom->getElementByTagName('permissions') targeting Prado\Xml\TXmlElement::getElementByTagName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
introduced by
The condition $permissionsNode = $dom-...'permissions') !== null is always false.
Loading history...
81
			$this->_permissions[] = $permissionsNode;
82
		}
83
		return $callchain->dyLoadPageConfigurationFromXml($dom, $configPath, $configPagePath);
0 ignored issues
show
Bug introduced by
The method dyLoadPageConfigurationFromXml() does not exist on Prado\Util\TCallChain. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
		return $callchain->/** @scrutinizer ignore-call */ dyLoadPageConfigurationFromXml($dom, $configPath, $configPagePath);
Loading history...
84
	}
85
86
	/**
87
	 * Applies the permissions hierarchy and permission rules
88
	 * @param \Prado\Util\TCallChain $callchain
89
	 */
90
	public function dyApplyConfiguration($callchain)
91
	{
92
		$manager = $this->getPermissionsManager();
93
		foreach ($this->_permissions as $permission) {
94
			$manager->loadPermissionsData($permission);
95
		}
96
		return $callchain->dyApplyConfiguration();
0 ignored issues
show
Bug introduced by
The method dyApplyConfiguration() does not exist on Prado\Util\TCallChain. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
		return $callchain->/** @scrutinizer ignore-call */ dyApplyConfiguration();
Loading history...
97
	}
98
99
	/**
100
	 * @return \Prado\Security\Permissions\TPermissionsManager manages application permissions
101
	 */
102
	public function getPermissionsManager()
103
	{
104
		return $this->_manager;
105
	}
106
107
	/**
108
	 * @param \Prado\Security\Permissions\TPermissionsManager|\WeakReference $manager manages application permissions
109
	 */
110
	public function setPermissionsManager($manager)
111
	{
112
		if (class_exists('\WeakReference', false) && $manager instanceof \WeakReference) {
113
			$manager = $manager->get();
114
		}
115
		$this->_manager = $manager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $manager can also be of type WeakReference. However, the property $_manager is declared as type Prado\Security\Permissions\TPermissionsManager. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
116
	}
117
}
118