| 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     Entities | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @since          1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @date           13.03.14 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | namespace IPub\Permissions\Entities; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Nette; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Nette\Utils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use IPub; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use IPub\Permissions\Security; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | class Permission implements IPermission | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 	 * Implement nette smart magic | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 	use Nette\SmartObject; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 	 * Permission resource | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 	 * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 	private $resource; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 	 * Permission privilege | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 	 * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	private $privilege; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 	 * @var callable|NULL | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 	private $assertion; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 	 * Permission details | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 	 * @var Utils\ArrayHash | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 	private $details; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 	 * @param IResource|NULL $resource | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 	 * @param string|NULL $privilege | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 	 * @param array $details | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 	 * @param callable|NULL $assertion | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	public function __construct(IResource $resource = NULL, string $privilege = NULL, $details = [], callable $assertion = NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$this->resource = $resource; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		$this->privilege = $privilege; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$this->assertion = $assertion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		// Check if permission details are provided too | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		if (!empty($details)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			$this->details = Utils\ArrayHash::from($details); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 	 * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	public function getResource() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		return $this->resource; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 	 * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	public function getPrivilege() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		return $this->privilege; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 	 * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	public function getAssertion() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		return $this->assertion; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 	 * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	public function setDetails(array $details) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		$this->details = Utils\ArrayHash::from($details); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 	 * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	public function getTitle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		return $this->details->offsetExists('title') ? $this->details->offsetGet('title') : NULL; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  | 	 * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	public function getDescription() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		return $this->details->offsetExists('description') ? $this->details->offsetGet('description') : NULL; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  | 	 * Convert permission object to string | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  | 	 * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	public function __toString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		return ((string) $this->resource) . self::DELIMITER . $this->privilege; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 133 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  | 
            
                        
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.