| 1 |  |  | <?php /** MicroFileACL */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Micro\Auth\Drivers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Micro\Db\IConnection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Micro\Mvc\Models\Query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * File ACL class file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * ACL security with files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author Oleg Lunegov <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @link https://github.com/linpax/microphp-framework | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @copyright Copyright (c) 2013 Oleg Lunegov | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package Micro | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @subpackage Auth\Drivers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @version 1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @since 1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class FileAcl extends Acl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var array $roles configured roles */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $roles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var array $perms configured perms */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $perms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var array $rolePermsCompare compare of permissions in roles */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $rolePermsCompare; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Configured ACL with files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param IConnection $db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param array $params configuration array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @result void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function __construct(IConnection $db, array $params = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         parent::__construct($db, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $roles = !empty($params['roles']) ? $params['roles'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->roles = !empty($roles['roles']) ? $roles['roles'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->perms = !empty($roles['perms']) ? $roles['perms'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->rolePermsCompare = !empty($roles['role_perms']) ? $roles['role_perms'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Check user access to permission | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param integer $userId user id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param string $permission checked permission | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param array $data not used, added for compatible! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @throws \Micro\Base\Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function check($userId, $permission, array $data = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $permissionId = in_array($permission, $this->perms, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         /** @var array $assigned */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $assigned = $this->assigned($userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (!$assigned) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         foreach ($assigned AS $assign) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             if ($assign['perm'] && $assign['perm'] === $permissionId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } elseif ($assign['role'] && in_array($permissionId, $this->rolePerms($assign['role']), true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * Get assigned elements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param integer $userId user ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @throws \Micro\Base\Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 94 |  | View Code Duplication |     public function assigned($userId) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $query = new Query($this->db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $query->select = '*'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $query->table = 'acl_user'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $query->addWhere('`user`='.$userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $query->single = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return $query->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * Get permissions in role | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @access private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param integer $role role name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |     protected function rolePerms($role) | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |         return $this->rolePermsCompare[$role]; | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  | 
            
                        
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.