Hawk - PHP documentation
  • Namespace
  • Class
  • Tree

Namespaces

  • Hawk
    • View
      • Plugins

Classes

  • Hawk\App
  • Hawk\ButtonInput
  • Hawk\Cache
  • Hawk\CheckboxInput
  • Hawk\ColorInput
  • Hawk\Conf
  • Hawk\Controller
  • Hawk\Crypto
  • Hawk\DatabaseSessionHandler
  • Hawk\DatetimeInput
  • Hawk\DB
  • Hawk\DBExample
  • Hawk\DeleteInput
  • Hawk\Dialogbox
  • Hawk\EmailInput
  • Hawk\ErrorHandler
  • Hawk\Event
  • Hawk\FileInput
  • Hawk\FileSystem
  • Hawk\FloatInput
  • Hawk\Form
  • Hawk\FormFieldset
  • Hawk\FormInput
  • Hawk\GenericModel
  • Hawk\GifImage
  • Hawk\HawkApi
  • Hawk\HawkUpdater
  • Hawk\HiddenInput
  • Hawk\HtmlInput
  • Hawk\HTTPRequest
  • Hawk\Icon
  • Hawk\Image
  • Hawk\IntegerInput
  • Hawk\ItemList
  • Hawk\ItemListField
  • Hawk\JpegImage
  • Hawk\Lang
  • Hawk\Language
  • Hawk\LeftSidebarTab
  • Hawk\Less
  • Hawk\Logger
  • Hawk\Mail
  • Hawk\MenuItem
  • Hawk\Model
  • Hawk\NoSidebarTab
  • Hawk\NumberInput
  • Hawk\ObjectInput
  • Hawk\Option
  • Hawk\Panel
  • Hawk\PasswordInput
  • Hawk\Permission
  • Hawk\Plugin
  • Hawk\PluginInstaller
  • Hawk\PngImage
  • Hawk\ProfileQuestion
  • Hawk\ProfileQuestionValue
  • Hawk\RadioInput
  • Hawk\Request
  • Hawk\Response
  • Hawk\RightSidebarTab
  • Hawk\Role
  • Hawk\RolePermission
  • Hawk\Route
  • Hawk\Router
  • Hawk\SelectInput
  • Hawk\Session
  • Hawk\Singleton
  • Hawk\SubmitInput
  • Hawk\Tabs
  • Hawk\TextareaInput
  • Hawk\TextInput
  • Hawk\Theme
  • Hawk\TimeInput
  • Hawk\Upload
  • Hawk\User
  • Hawk\View
  • Hawk\View\Plugins\Accordion
  • Hawk\View\Plugins\Button
  • Hawk\View\Plugins\Form
  • Hawk\View\Plugins\Icon
  • Hawk\View\Plugins\Import
  • Hawk\View\Plugins\Panel
  • Hawk\View\Plugins\Tabs
  • Hawk\View\Plugins\Text
  • Hawk\View\Plugins\Uri
  • Hawk\View\Plugins\Widget
  • Hawk\ViewPlugin
  • Hawk\Widget
  • Hawk\WysiwygInput

Traits

  • Hawk\Utils

Exceptions

  • Hawk\AppStopException
  • Hawk\DBExampleException
  • Hawk\DBException
  • Hawk\FileSystemException
  • Hawk\HawkApiException
  • Hawk\ImageException
  • Hawk\MailException
  • Hawk\UploadException
  • Hawk\ViewException
  1 <?php
  2 /**
  3  * User.php
  4  *
  5  * @author  Elvyrra SAS
  6  * @license http://rem.mit-license.org/ MIT
  7  */
  8 
  9 namespace Hawk;
 10 
 11 
 12 /**
 13  * This model describes the user data
 14  *
 15  * @package BaseModels
 16  */
 17 class User extends Model{
 18     /**
 19      * The associated table
 20      *
 21      * @var string
 22      */
 23     protected static $tablename = "User";
 24 
 25 
 26     /**
 27      * The user profile data
 28      *
 29      * @var array
 30      */
 31     private $profile,
 32 
 33     /**
 34      * The user permissions
 35      *
 36      * @var array
 37      */
 38     $permissions,
 39 
 40     /**
 41      * The user's options
 42      *
 43      * @var array
 44      */
 45     $options;
 46 
 47     /**
 48      * The id of guest users
 49      */
 50     const GUEST_USER_ID = 0;
 51 
 52     /**
 53      * The id for the first administrator user
 54      */
 55     const ROOT_USER_ID = 1;
 56 
 57     /**
 58      * Constructor
 59      *
 60      * @param array $data The data to set to the user
 61      */
 62     public function __construct($data = array()){
 63         parent::__construct($data);
 64         if(!empty($this->roleId)) {
 65             $this->role = Role::getById($this->roleId);
 66         }
 67     }
 68 
 69     /**
 70      * Get all users except guest user
 71      *
 72      * @param string $index  The field to use as key in the returned array
 73      * @param array  $fields The table fields to get
 74      * @param array  $order  The order instruction to get the users
 75      *
 76      * @return array
 77      */
 78     public static function getAll($index = null, $fields = array(), $order = array()){
 79         $example = array(
 80         'id' => array(
 81         '$ne' => self::GUEST_USER_ID
 82         )
 83         );
 84         return self::getListByExample(new DBExample($example), $index, $fields, $order);
 85     }
 86 
 87 
 88     /**
 89      * Get a user by it username
 90      *
 91      * @param string $username The username to search
 92      *
 93      * @return User
 94      */
 95     public static function getByUsername($username){
 96         return self::getByExample(new DBExample(array('username' => $username)));
 97     }
 98 
 99 
100     /**
101      * Get a user by it email address
102      *
103      * @param string $email The user email
104      *
105      * @return User
106      */
107     public static function getByEmail($email){
108         return self::getByExample(new DBExample(array('email' => $email)));
109     }
110 
111     /**
112      * Set all the permissions on the user
113      */
114     private function getPermissions(){
115         if(!isset($this->permissions)) {
116             $sql = 'SELECT P.plugin, P.key, P.id
117                     FROM ' . RolePermission::getTable() . ' RP
118                         INNER JOIN ' . Permission::getTable() . ' P ON RP.permissionId = P.id
119                         INNER JOIN ' . self::getTable() . ' U ON U.roleId = RP.roleId
120                     WHERE U.id = :id AND RP.value=1';
121 
122             $permissions = App::db()->query($sql, array('id' => $this->id), array('return' => DB::RETURN_OBJECT));
123             $this->permissions = array();
124             foreach($permissions as $permission){
125                 // Register the permission by it id
126                 $this->permissions['byId'][$permission->id] = 1;
127 
128                 // Regoster the permission by it name
129                 $this->permissions['byName'][$permission->plugin][$permission->key] = 1;
130             }
131         }
132     }
133 
134 
135     /**
136      * Get the user's profile data
137      *
138      * @param string $prop The property name to get.
139      *                     If not set, the function will return an array containing all the profile data
140      *
141      * @return mixed
142      */
143     public function getProfileData($prop = ""){
144         if(!isset($this->profile)) {
145             $sql = 'SELECT Q.name, V.value
146                     FROM ' . ProfileQuestionValue::getTable()  . ' V
147                         INNER JOIN ' . ProfileQuestion::getTable() . ' Q ON V.question = Q.name
148                     WHERE V.userId = :id';
149 
150             $data = App::db()->query(
151                 $sql,
152                 array(
153                     'id' => $this->id
154                 ),
155                 array(
156                     'return' => DB::RETURN_ARRAY,
157                     'index' => 'name'
158                 )
159             );
160 
161             $this->profile = array_map(
162                 function ($v) {
163                     return $v['value'];
164                 },
165                 $data
166             );
167         }
168         return $prop ? (isset($this->profile[$prop]) ? $this->profile[$prop] : null) : $this->profile;
169     }
170 
171 
172     /**
173      * Set the user's profile data. This method does not register the data in database, only set in the user properties
174      *
175      * @param string $prop  The property name to set
176      * @param string $value The value to set
177      */
178     public function setProfileData($prop, $value){
179         $this->profile[$prop] = $value;
180     }
181 
182 
183     /**
184      * Save the user's profile in the database
185      */
186     public function saveProfile(){
187         foreach($this->profile as $prop => $value){
188             $questionValue = new ProfileQuestionValue(
189                 array(
190                 'question' => $prop,
191                 'userId' => $this->id,
192                 'value' => $value
193                 )
194             );
195             $questionValue->save();
196         }
197     }
198 
199 
200     /**
201      * Get the user options. This function returns the option value for $name. If $name is not set,
202      * it returns the array containing all the user options
203      *
204      * @param string $name The option name, formatted like '<plugin>.<key>'
205      *
206      * @return mixed The value for the option $name, or the array contaning all the user options
207      */
208     public function getOptions($name = ''){
209         if(!isset($this->options)) {
210             $example = $this->isLogged() ? array('userId' => $this->id) : array('userIp' => App::request()->clientIp());
211 
212             $options = App::db()->select(
213                 array(
214                 'from' => DB::getFullTablename('UserOption'),
215                 'where' => new DBExample($example)
216                 )
217             );
218 
219             $this->options = array();
220             foreach($options as $option){
221                 $this->options[$option['plugin'] . '.' . $option['key']] = $option['value'];
222             }
223         }
224 
225         if($name) {
226             return isset($this->options[$name]) ? $this->options[$name] : null;
227         }
228         else{
229             return $this->options;
230         }
231     }
232 
233 
234     /**
235      * Register an option for the user.
236      * This function registers the option value in the database and in the current user options
237      *
238      * @param string $name  The option name, formatted as '<plugin>.<key>'
239      * @param mixed  $value The value to set to the option
240      */
241     public function setOption($name, $value){
242         $this->getOptions();
243         $this->options[$name] = $value;
244 
245         list($plugin, $key) = explode('.', $name, 2);
246         $data = array(
247         'plugin' => $plugin,
248         'key' => $key,
249         'value' => $value
250         );
251 
252         if($this->isLogged()) {
253             $data['userId'] = $this->id;
254         }
255         else{
256             $data['userIp'] = App::request()->clientIp();
257         }
258         App::db()->replace(DB::getFullTablename('UserOption'), $data);
259     }
260 
261 
262 
263     /**
264      *     Check if the user is allowed to perform an action
265      *
266      * @param string|int $action This parameter can represent :
267      *                                - A specific action, formatted as "<plugin>.<key>"
268      *                                - A permission id, when an integer is given
269      *
270      * @return boolean TRUE if the user is allowed to perform the action, else FALSE
271      */
272     public function isAllowed($action){
273         if($this->roleId == Role::ADMIN_ROLE_ID) {
274             // The admins can perform any action
275             return true;
276         }
277         if($action !== Permission::ALL_PRIVILEGES_ID && $action !== Permission::ALL_PRIVILEGES_NAME && $this->isAllowed(Permission::ALL_PRIVILEGES_ID)) {
278             // The user has all privileges
279             return true;
280         }
281 
282         // Get the user permissions
283         $this->getPermissions();
284 
285         if(is_numeric($action)) {
286             // $action represents the id of the action
287             return !empty($this->permissions['byId'][$action]);
288         }
289         else{
290             // The action is formatted as <plugin>.<key>
291             list($plugin, $key) = explode('.', $action);
292 
293             return !empty($this->permissions['byName'][$plugin][$key]);
294         }
295     }
296 
297 
298     /**
299      * Get the user's username
300      *
301      * @return string The user's username
302      */
303     public function getUsername(){
304         return $this->id ? $this->username : Lang::get('main.guest-username');
305     }
306 
307 
308     /**
309      * Get the user's full name. This method returns the real name if it set in the user's profile, else, it returns his username
310      *
311      * @return string
312      */
313     public function getDisplayName(){
314         return $this->getProfileData('realname') ? $this->getProfileData('realname') : $this->getUsername();
315     }
316 
317     /**
318      * Check if the user is logged or not
319      *
320      * @return bool
321      */
322     public function isLogged(){
323         return $this->id && App::session()->getData('user.id') == $this->id && $this->active;
324     }
325 
326 
327     /**
328      * Check if the user can access the application
329      *
330      * @return bool
331      */
332     public function canAccessApplication(){
333         return $this->isLogged() || Option::get('main.allow-guest');
334     }
335 
336     /**
337      * Check of the user is removable. A user is removable if he's not the one executing the current script,
338      * and if he's not a guest or the main application administrator
339      *
340      * @return bool
341      */
342     public function isRemovable(){
343         return  $this->id != App::session()->getUser()->id &&
344                 $this->id != self::ROOT_USER_ID &&
345                 $this->id != self::GUEST_USER_ID;
346     }
347 }
Hawk - PHP documentation API documentation generated by ApiGen