2 // +----------------------------------------------------------------------+
 
   3 // | Anuko Time Tracker
 
   4 // +----------------------------------------------------------------------+
 
   5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
 
   6 // +----------------------------------------------------------------------+
 
   7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
 
   8 // | by anyone for any purpose, and freely redistributed alone or in
 
   9 // | combination with other software, provided that the license is obeyed.
 
  11 // | There are only two ways to violate the license:
 
  13 // | 1. To redistribute this code in source form, with the copyright
 
  14 // |    notice or license removed or altered. (Distributing in compiled
 
  15 // |    forms without embedded copyright notices is permitted).
 
  17 // | 2. To redistribute modified versions of this code in *any* form
 
  18 // |    that bears insufficient indications that the modifications are
 
  19 // |    not the work of the original author(s).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  30   var $login = null;            // User login.
 
  31   var $name = null;             // User name.
 
  32   var $id = null;               // User id.
 
  33   var $team_id = null;          // Team id.
 
  34   var $role = null;             // User role (user, client, comanager, manager, admin). TODO: remove when new roles are done.
 
  35   var $role_id = null;          // Role id.
 
  36   var $rank = null;             // User role rank.
 
  37   var $client_id = null;        // Client id for client user role.
 
  38   var $behalf_id = null;        // User id, on behalf of whom we are working.
 
  39   var $behalf_name = null;      // User name, on behalf of whom we are working.
 
  40   var $email = null;            // User email.
 
  41   var $lang = null;             // Language.
 
  42   var $decimal_mark = null;     // Decimal separator.
 
  43   var $date_format = null;      // Date format.
 
  44   var $time_format = null;      // Time format.
 
  45   var $week_start = 0;          // Week start day.
 
  46   var $show_holidays = 0;       // Whether to show holidays in calendar.
 
  47   var $tracking_mode = 0;       // Tracking mode.
 
  48   var $project_required = 0;    // Whether project selection is required on time entires.
 
  49   var $task_required = 0;       // Whether task selection is required on time entires.
 
  50   var $record_type = 0;         // Record type (duration vs start and finish, or both).
 
  51   var $punch_mode = 0;          // Whether punch mode is enabled for user.
 
  52   var $allow_overlap = 0;       // Whether to allow overlapping time entries.
 
  53   var $future_entries = 0;      // Whether to allow creating future entries.
 
  54   var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
 
  55   var $bcc_email = null;        // Bcc email.
 
  56   var $currency = null;         // Currency.
 
  57   var $plugins = null;          // Comma-separated list of enabled plugins.
 
  58   var $config = null;           // Comma-separated list of miscellaneous config options.
 
  59   var $team = null;             // Team name.
 
  60   var $custom_logo = 0;         // Whether to use a custom logo for team.
 
  61   var $lock_spec = null;        // Cron specification for record locking.
 
  62   var $workday_minutes = 480;   // Number of work minutes in a regular day.
 
  63   var $rights_mask = 0;         // A mask of user rights. TODO: remove after roles revamp.
 
  64   var $rights = array();        // An array of user rights, planned replacement of $rights_mask.
 
  67   function __construct($login, $id = null) {
 
  68     if (!$login && !$id) {
 
  69       // nothing to initialize
 
  73     $mdb2 = getConnection();
 
  75     $sql = "SELECT u.id, u.login, u.name, u.team_id, u.role, u.role_id, r.rank, r.rights, u.client_id, u.email, t.name as team_name,
 
  76       t.currency, t.lang, t.decimal_mark, t.date_format, t.time_format, t.week_start,
 
  77       t.tracking_mode, t.project_required, t.task_required, t.record_type,
 
  78       t.bcc_email, t.plugins, t.config, t.lock_spec, t.workday_minutes, t.custom_logo
 
  79       FROM tt_users u LEFT JOIN tt_teams t ON (u.team_id = t.id) LEFT JOIN tt_roles r on (r.id = u.role_id) WHERE ";
 
  83       $sql .= "u.login = ".$mdb2->quote($login);
 
  84     $sql .= " AND u.status = 1";
 
  86     $res = $mdb2->query($sql);
 
  87     if (is_a($res, 'PEAR_Error')) {
 
  91     $val = $res->fetchRow();
 
  93       $this->login = $val['login'];
 
  94       $this->name = $val['name'];
 
  95       $this->id = $val['id'];
 
  96       $this->team_id = $val['team_id'];
 
  97       $this->role = $val['role'];
 
  98       $this->role_id = $val['role_id'];
 
  99       $this->rights = explode(',', $val['rights']);
 
 100       $this->rank = $val['rank'];
 
 101       // Downgrade rank to legacy role, if it is still in use.
 
 102       if ($this->role > 0 && $this->rank > $this->role)
 
 103         $this->rank = $this->role; // TODO: remove after roles revamp.
 
 104       // Upgrade rank from legacy role, for user who does not yet have a role_id.
 
 105       if (!$this->rank && !$this->role_id && $this->role > 0)
 
 106         $this->rank = $this->role; // TODO: remove after roles revamp.
 
 107       $this->client_id = $val['client_id'];
 
 108       $this->email = $val['email'];
 
 109       $this->lang = $val['lang'];
 
 110       $this->decimal_mark = $val['decimal_mark'];
 
 111       $this->date_format = $val['date_format'];
 
 112       $this->time_format = $val['time_format'];
 
 113       $this->week_start = $val['week_start'];
 
 114       $this->tracking_mode = $val['tracking_mode'];
 
 115       $this->project_required = $val['project_required'];
 
 116       $this->task_required = $val['task_required'];
 
 117       $this->record_type = $val['record_type'];
 
 118       $this->bcc_email = $val['bcc_email'];
 
 119       $this->team = $val['team_name'];
 
 120       $this->currency = $val['currency'];
 
 121       $this->plugins = $val['plugins'];
 
 122       $this->lock_spec = $val['lock_spec'];
 
 123       $this->workday_minutes = $val['workday_minutes'];
 
 124       $this->custom_logo = $val['custom_logo'];
 
 126       $this->config = $val['config'];
 
 127       $config_array = explode(',', $this->config);
 
 129       // Set user config options.
 
 130       $this->show_holidays = in_array('show_holidays', $config_array);
 
 131       $this->punch_mode = in_array('punch_mode', $config_array);
 
 132       $this->allow_overlap = in_array('allow_overlap', $config_array);
 
 133       $this->future_entries = in_array('future_entries', $config_array);
 
 134       $this->uncompleted_indicators = in_array('uncompleted_indicators', $config_array);
 
 136       // Set "on behalf" id and name.
 
 137       if (isset($_SESSION['behalf_id'])) {
 
 138           $this->behalf_id = $_SESSION['behalf_id'];
 
 139           $this->behalf_name = $_SESSION['behalf_name'];
 
 142       // Set user rights. TODO: remove during roles revamp, whe we redo access checks.
 
 143       if ($this->role == ROLE_USER) {
 
 144         $this->rights_mask = right_data_entry|right_view_charts|right_view_reports;
 
 145       } elseif ($this->role == ROLE_CLIENT) {
 
 146         $this->rights_mask = right_view_reports|right_view_invoices;
 
 147       } elseif ($this->role == ROLE_COMANAGER) {
 
 148         $this->rights_mask = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team;
 
 149       } elseif ($this->role == ROLE_MANAGER) {
 
 150         $this->rights_mask = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team|right_assign_roles|right_export_team;
 
 151       } elseif ($this->role == ROLE_SITE_ADMIN) {
 
 152         $this->rights_mask = right_administer_site;
 
 157   // The getActiveUser returns user id on behalf of whom current user is operating.
 
 158   function getActiveUser() {
 
 159     return ($this->behalf_id ? $this->behalf_id : $this->id);
 
 162   // isAdmin - determines whether current user is admin (has right_administer_site).
 
 164     return (right_administer_site & $this->role);
 
 167   // isManager - determines whether current user is team manager.
 
 168   function isManager() {
 
 169     return (ROLE_MANAGER == $this->role);
 
 172   // isCoManager - determines whether current user is team comanager.
 
 173   function isCoManager() {
 
 174     return (ROLE_COMANAGER == $this->role);
 
 177   // isClient - determines whether current user is a client.
 
 178   function isClient() {
 
 179     return (ROLE_CLIENT == $this->role);
 
 182   // canManageTeam - determines whether current user is manager or co-manager.
 
 183   function canManageTeam() {
 
 184     return (right_manage_team & $this->role);
 
 187   // isPluginEnabled checks whether a plugin is enabled for user.
 
 188   function isPluginEnabled($plugin)
 
 190     return in_array($plugin, explode(',', $this->plugins));
 
 193   // getAssignedProjects - returns an array of assigned projects.
 
 194   function getAssignedProjects()
 
 197     $mdb2 = getConnection();
 
 199     // Do a query with inner join to get assigned projects.
 
 200     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
 
 201       inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
 
 202       where p.team_id = $this->team_id and p.status = 1 order by p.name";
 
 203     $res = $mdb2->query($sql);
 
 204     if (!is_a($res, 'PEAR_Error')) {
 
 205       while ($val = $res->fetchRow()) {
 
 212   // isDateLocked checks whether a specifc date is locked for modifications.
 
 213   function isDateLocked($date)
 
 215     if ($this->isPluginEnabled('lk') && $this->lock_spec) {
 
 216       // Override for managers.
 
 217       if ($this->canManageTeam()) return false;
 
 219       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
 
 220       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
 
 222       // Calculate the last occurrence of a lock.
 
 223       $last = tdCron::getLastOccurrence($this->lock_spec, time());
 
 224       $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
 
 225       if ($date->before($lockdate)) {
 
 232   // migrateLegacyRole makes changes to user database record and assigns a user to
 
 233   // one of pre-defined roles, which are created if necessary.
 
 234   // No changes to $this instance are done.
 
 235   function migrateLegacyRole() {
 
 236     // Do nothing if we already have a role_id.
 
 237     if ($this->role_id) return false;
 
 239     // Create default roles if necessary.
 
 240     import ('ttRoleHelper');
 
 241     if (!ttRoleHelper::rolesExist()) ttRoleHelper::createDefaultRoles(); // TODO: refactor or remove after roles revamp.
 
 243     // Obtain new role id based on legacy role.
 
 244     $role_id = ttRoleHelper::getRoleByRank($this->role);
 
 245     if (!$role_id) return false; // Role not found, nothing to do.
 
 247     $mdb2 = getConnection();
 
 248     $sql = "update tt_users set role_id = $role_id where id = $this->id and team_id = $this->team_id";
 
 249     $affected = $mdb2->exec($sql);
 
 250     if (is_a($affected, 'PEAR_Error'))