1 <?php
  2 
  3 /**
  4  * Class for users.
  5  *
  6  * @author gharlan
  7  *
  8  * @package redaxo\core\login
  9  */
 10 class rex_user
 11 {
 12     /**
 13      * SQL instance.
 14      *
 15      * @var rex_sql
 16      */
 17     protected $sql;
 18 
 19     /**
 20      * User role instance.
 21      *
 22      * @var rex_user_role_interface
 23      */
 24     protected $role;
 25 
 26     /**
 27      * Class name for user roles.
 28      *
 29      * @var string
 30      */
 31     protected static $roleClass;
 32 
 33     /**
 34      * Constructor.
 35      *
 36      * @param rex_sql $sql
 37      */
 38     public function __construct(rex_sql $sql)
 39     {
 40         $this->sql = $sql;
 41     }
 42 
 43     /**
 44      * Returns the value for the given key.
 45      *
 46      * @param string $key Key
 47      *
 48      * @return string value
 49      */
 50     public function getValue($key)
 51     {
 52         return $this->sql->getValue($key);
 53     }
 54 
 55     /**
 56      * Returns the ID.
 57      *
 58      * @return int
 59      */
 60     public function getId()
 61     {
 62         return $this->sql->getValue('id');
 63     }
 64 
 65     /**
 66      * Returns the user login.
 67      *
 68      * @return string Login
 69      */
 70     public function getLogin()
 71     {
 72         return $this->sql->getValue('login');
 73     }
 74 
 75     /**
 76      * Returns the name.
 77      *
 78      * @return string Name
 79      */
 80     public function getName()
 81     {
 82         return $this->sql->getValue('name');
 83     }
 84 
 85     /**
 86      * Returns the email.
 87      *
 88      * @return string email
 89      */
 90     public function getEmail()
 91     {
 92         return $this->sql->getValue('email');
 93     }
 94 
 95     /**
 96      * Returns if the user is an admin.
 97      *
 98      * @return bool
 99      */
100     public function isAdmin()
101     {
102         return (bool) $this->sql->getValue('admin');
103     }
104 
105     /**
106      * Returns the language.
107      *
108      * @return string Language
109      */
110     public function getLanguage()
111     {
112         return $this->sql->getValue('language');
113     }
114 
115     /**
116      * Returns the start page.
117      *
118      * @return string Start page
119      */
120     public function getStartPage()
121     {
122         return $this->sql->getValue('startpage');
123     }
124 
125     /**
126      * Returns if the user has a role.
127      *
128      * @return bool
129      */
130     public function hasRole()
131     {
132         if (self::$roleClass && !is_object($this->role) && ($role = $this->sql->getValue('role'))) {
133             $class = self::$roleClass;
134             $this->role = $class::get($role);
135         }
136         return is_object($this->role);
137     }
138 
139     /**
140      * Returns if the user has the given permission.
141      *
142      * @param string $perm Perm key
143      *
144      * @return bool
145      */
146     public function hasPerm($perm)
147     {
148         if ($this->isAdmin()) {
149             return true;
150         }
151         $result = false;
152         if (strpos($perm, '/') !== false) {
153             list($complexPerm, $method) = explode('/', $perm, 2);
154             $complexPerm = $this->getComplexPerm($complexPerm);
155             return $complexPerm ? $complexPerm->$method() : false;
156         }
157         if ($this->hasRole()) {
158             $result = $this->role->hasPerm($perm);
159         }
160         if (!$result && in_array($perm, ['isAdmin', 'admin', 'admin[]'])) {
161             return $this->isAdmin();
162         }
163         return $result;
164     }
165 
166     /**
167      * Returns the complex perm for the user.
168      *
169      * @param string $key Complex perm key
170      *
171      * @return rex_complex_perm Complex perm
172      */
173     public function getComplexPerm($key)
174     {
175         if ($this->hasRole()) {
176             return $this->role->getComplexPerm($this, $key);
177         }
178         return rex_complex_perm::get($this, $key);
179     }
180 
181     /**
182      * Sets the role class.
183      *
184      * @param string $class Class name
185      */
186     public static function setRoleClass($class)
187     {
188         self::$roleClass = $class;
189     }
190 }
191