* @license PHP License * @package WB * @subpackage content */ /** * User * * * * @version 0.5.0 * @package WB * @subpackage content */ class WBUser extends WBStdClass { /** * id of current user * @var string */ protected $id = null; /** * current user's primary data * @var array */ protected $data = array(); /** * groups the current user is member * @var array */ protected $group = array(); /** * storage module * @var WBUser_Storage */ protected $storage; /** * switch to show whether config was loaded * @var bool */ private $configLoaded = false; /** * basic config for user data * * @var array */ protected $config = array( 'fields' => array( 'editable' => array( 'nickname', 'password', 'email', 'forename', 'surname', 'lang', 'country' ), 'required' => array( 'nickname', 'password', 'email' ) ) ); /** * get current user object * * @return WBUser_Auth */ static public function getCurrent() { WBClass::load('WBUser_Auth'); return WBUser_Auth::getCurrent(); } /** * Load user system settings * * Make sure config is loaded only once */ protected function loadConfig() { if ($this->configLoaded) { return; } $this->configLoaded = true; $config = WBClass::create('WBConfig'); if (!$config->load('user/config', true)) { return; } $this->config['fields']['editable'] = $config->get('fields/editable', $this->config['fields']['editable']); $this->config['fields']['required'] = $config->get('fields/required', $this->config['fields']['required']); } /** * get user's id * * @return string */ public function getId() { return $this->id; } /** * get user's nickname * * @return string */ public function getNickname() { return trim($this->data['nickname']); } /** * get user's data * * @return array */ public function getData() { return $this->data; } /** * get user's groups * * @return array */ public function getGroups() { return $this->group; } /** * Check group membership * * Checks if user is in specified group. If group name is an array, * user must be on one or more groups * * @param string|array $name group name to validate * @param bool $id whether $name is the group's id * @return bool true in case user is member of given group */ public function isInGroup($name, $id = false) { if (empty($this->id)) { return false; } // in case there is no group name given, a loaded user is sufficiant if (empty($name)) { return true; } if (!is_array($name)) { $name = array($name); } $groups = $this->getGroups(); if ($id) { foreach ($name as $n) { if (isset($groups[$n])) { return true; } } return false; } foreach ($name as $n) { if (in_array($n, $groups)) { return true; } } return false; } /** * Is User Enabled? * * Check enabled bit * * @return bool true if enabled */ public function isEnabled() { if (1 == intval($this->data['enabled'])) { return true; } return false; } /** * load user's data by id * * Call storage module to load user's data * * @param string $id user's id * @return true on success, false otherwise */ public function load($id) { if ($this->id == $id && !empty($this->data)) { return true; } $this->id = null; $this->data = array(); $this->group = array(); $storage = $this->getStorageModule(); if (!$storage->load($id)) { return false; } $this->id = $storage->getId(); $this->data = $storage->get(); $this->group = $storage->getGroup(); return true; } /** * Reaload User Data * */ public function reload() { $this->data = array(); $this->load($this->id); } /** * enable or disable user * * @param bool $enable */ private function doEnable($enable) { // load user first if (!$this->id) { return; } if (intval($this->data['enabled']) == intval($enable)) { return; } $data = array('enabled' => 0); $event = 'user:disabled'; $msg = 'Disabled user account {NICKNAME}'; if ($enable) { $data['enabled'] = 1; $event = 'user:enabled'; $msg = 'Enabled user account {NICKNAME}'; } if (!$this->storage->set($data)) { return; } $data = $this->storage->get(); $data['id'] = $this->id; WBEvent::trigger($event, $msg, $data); return; } /** * enable user * * Set enabled flag to loaded user. */ public function enable() { return $this->doEnable(true); } /** * disable user * * Remove enabled flag from loaded user. */ public function disable() { return $this->doEnable(false); } /** * save user information * * Load user by id first! * * @param array $data */ public function set($data) { // load user first if (!$this->id) { return; } $data = $this->getEditableData($data); /** @var WBUser_Storage */ $storage = $this->getStorageModule(); $storage->set($data); // trigger event $this->data = $this->storage->get(); $data = $this->data; $data['id'] = $this->id; WBEvent::trigger('user:set', 'User data saved for {EMAIL} ({ID})', $data); } /** * Get date with editable fields * * Filter everything besides editable fields and * return savealble data * * @see $config * @param array $data * @return array */ protected function getEditableData($data) { $this->loadConfig(); $save = array(); foreach ($this->config['fields']['editable'] as $f) { if (isset($data[$f])) { $save[$f] = $data[$f]; } } return $save; } /** * save group memebership * * Load user by id first! * * @param array $groupIds */ public function setGroups($groupIds) { // load user first if (!$this->id) { return; } $this->storage->setGroups($groupIds); // trigger event $data = $this->storage->get(); $data['id'] = $this->id; $data['groups'] = $groupIds; WBEvent::trigger('user:setgroups', 'User\'s ({ID}) groups set.', $data); } /** * start storage module only once * * @return WBUser_Storage */ public function getStorageModule() { if ($this->storage) { return $this->storage; } $storage = WBParam::get('wb/user/storage', 'Table'); $this->storage = WBClass::create('WBUser_Storage_' . $storage); return $this->storage; } /** * Get list of required fields * * @return array */ public function getFieldsRequired() { $this->loadConfig(); return $this->config['fields']['required']; } /** * Get list of editable fields * * @return array */ public function getFieldsEditable() { $this->loadConfig(); return $this->config['fields']['editable']; } }