quote($login); $sql .= " AND u.status = 1"; $res = $mdb2->query($sql); if (is_a($res, 'PEAR_Error')) { return; } $val = $res->fetchRow(); if ($val['id'] > 0) { $this->login = $val['login']; $this->name = $val['name']; $this->id = $val['id']; $this->team_id = $val['team_id']; $this->role = $val['role']; $this->role_id = $val['role_id']; $this->rights = explode(',', $val['rights']); $this->is_client = !in_array('track_own_time', $this->rights); $this->rank = $val['rank']; // Downgrade rank to legacy role, if it is still in use. if ($this->role > 0 && $this->rank > $this->role) $this->rank = $this->role; // TODO: remove after roles revamp. // Upgrade rank from legacy role, for user who does not yet have a role_id. if (!$this->rank && !$this->role_id && $this->role > 0) $this->rank = $this->role; // TODO: remove after roles revamp. $this->client_id = $val['client_id']; $this->email = $val['email']; $this->lang = $val['lang']; $this->decimal_mark = $val['decimal_mark']; $this->date_format = $val['date_format']; $this->time_format = $val['time_format']; $this->week_start = $val['week_start']; $this->tracking_mode = $val['tracking_mode']; $this->project_required = $val['project_required']; $this->task_required = $val['task_required']; $this->record_type = $val['record_type']; $this->bcc_email = $val['bcc_email']; $this->team = $val['team_name']; $this->currency = $val['currency']; $this->plugins = $val['plugins']; $this->lock_spec = $val['lock_spec']; $this->workday_minutes = $val['workday_minutes']; $this->custom_logo = $val['custom_logo']; $this->config = $val['config']; $config_array = explode(',', $this->config); // Set user config options. $this->show_holidays = in_array('show_holidays', $config_array); $this->punch_mode = in_array('punch_mode', $config_array); $this->allow_overlap = in_array('allow_overlap', $config_array); $this->future_entries = in_array('future_entries', $config_array); $this->uncompleted_indicators = in_array('uncompleted_indicators', $config_array); // Set "on behalf" id and name. if (isset($_SESSION['behalf_id'])) { $this->behalf_id = $_SESSION['behalf_id']; $this->behalf_name = $_SESSION['behalf_name']; } } } // The getActiveUser returns user id on behalf of whom the current user is operating. function getActiveUser() { return ($this->behalf_id ? $this->behalf_id : $this->id); } // can - determines whether user has a right to do something. function can($do_something) { return in_array($do_something, $this->rights); } // isAdmin - determines whether current user is admin (has right_administer_site). function isAdmin() { return $this->can('administer_site'); } // isManager - determines whether current user is team manager. function isManager() { return (ROLE_MANAGER == $this->role); } // isCoManager - determines whether current user is team comanager. function isCoManager() { return (ROLE_COMANAGER == $this->role); } // isClient - determines whether current user is a client. function isClient() { return $this->is_client; } // canManageTeam - determines whether current user is manager or co-manager. function canManageTeam() { return (right_manage_team & $this->role); } // isPluginEnabled checks whether a plugin is enabled for user. function isPluginEnabled($plugin) { return in_array($plugin, explode(',', $this->plugins)); } // getAssignedProjects - returns an array of assigned projects. function getAssignedProjects() { $result = array(); $mdb2 = getConnection(); // Do a query with inner join to get assigned projects. $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1) where p.team_id = $this->team_id and p.status = 1 order by p.name"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { while ($val = $res->fetchRow()) { $result[] = $val; } } return $result; } // isDateLocked checks whether a specifc date is locked for modifications. function isDateLocked($date) { if ($this->isPluginEnabled('lk') && $this->lock_spec) { // Override for managers. if ($this->canManageTeam()) return false; require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php'); require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php'); // Calculate the last occurrence of a lock. $last = tdCron::getLastOccurrence($this->lock_spec, time()); $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last)); if ($date->before($lockdate)) { return true; } } return false; } // migrateLegacyRole makes changes to user database record and assigns a user to // one of pre-defined roles, which are created if necessary. // No changes to $this instance are done. function migrateLegacyRole() { // Do nothing if we already have a role_id. if ($this->role_id) return false; // Create default roles if necessary. import ('ttRoleHelper'); if (!ttRoleHelper::rolesExist()) ttRoleHelper::createDefaultRoles(); // TODO: refactor or remove after roles revamp. // Obtain new role id based on legacy role. $role_id = ttRoleHelper::getRoleByRank($this->role); if (!$role_id) return false; // Role not found, nothing to do. $mdb2 = getConnection(); $sql = "update tt_users set role_id = $role_id where id = $this->id and team_id = $this->team_id"; $affected = $mdb2->exec($sql); if (is_a($affected, 'PEAR_Error')) return false; return true; } }