Some cleanup and refactoring of role_id update.
[timetracker.git] / WEB-INF / lib / ttUser.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 class ttUser {
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 $legacy_role = null;      // Old user role (user, client, comanager, manager, admin). TODO: remove when new roles are done.
35                                 // Complete removal requires refactoring migrateLegacyRole, which is used in dbinstall.php.
36                                 // Perhaps, after doing an installer?
37
38   var $role_id = null;          // Role id.
39   var $rank = null;             // User role rank.
40   var $client_id = null;        // Client id for client user role.
41   var $behalf_id = null;        // User id, on behalf of whom we are working.
42   var $behalf_name = null;      // User name, on behalf of whom we are working.
43   var $email = null;            // User email.
44   var $lang = null;             // Language.
45   var $decimal_mark = null;     // Decimal separator.
46   var $date_format = null;      // Date format.
47   var $time_format = null;      // Time format.
48   var $week_start = 0;          // Week start day.
49   var $show_holidays = 0;       // Whether to show holidays in calendar.
50   var $tracking_mode = 0;       // Tracking mode.
51   var $project_required = 0;    // Whether project selection is required on time entires.
52   var $task_required = 0;       // Whether task selection is required on time entires.
53   var $record_type = 0;         // Record type (duration vs start and finish, or both).
54   var $punch_mode = 0;          // Whether punch mode is enabled for user.
55   var $allow_overlap = 0;       // Whether to allow overlapping time entries.
56   var $future_entries = 0;      // Whether to allow creating future entries.
57   var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
58   var $bcc_email = null;        // Bcc email.
59   var $currency = null;         // Currency.
60   var $plugins = null;          // Comma-separated list of enabled plugins.
61   var $config = null;           // Comma-separated list of miscellaneous config options.
62   var $team = null;             // Team name.
63   var $custom_logo = 0;         // Whether to use a custom logo for team.
64   var $lock_spec = null;        // Cron specification for record locking.
65   var $workday_minutes = 480;   // Number of work minutes in a regular day.
66   var $rights = array();        // An array of user rights such as 'track_own_time', etc.
67   var $is_client = false;       // Whether user is a client as determined by missing 'track_own_time' right.
68
69   // Constructor.
70   function __construct($login, $id = null) {
71     if (!$login && !$id) {
72       // nothing to initialize
73       return;
74     }
75
76     $mdb2 = getConnection();
77
78     $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,
79       t.currency, t.lang, t.decimal_mark, t.date_format, t.time_format, t.week_start,
80       t.tracking_mode, t.project_required, t.task_required, t.record_type,
81       t.bcc_email, t.plugins, t.config, t.lock_spec, t.workday_minutes, t.custom_logo
82       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     if ($id)
84       $sql .= "u.id = $id";
85     else
86       $sql .= "u.login = ".$mdb2->quote($login);
87     $sql .= " AND u.status = 1";
88
89     $res = $mdb2->query($sql);
90     if (is_a($res, 'PEAR_Error')) {
91       return;
92     }
93
94     $val = $res->fetchRow();
95     if ($val['id'] > 0) {
96       $this->login = $val['login'];
97       $this->name = $val['name'];
98       $this->id = $val['id'];
99       $this->team_id = $val['team_id'];
100       $this->legacy_role = $val['role'];
101       $this->role_id = $val['role_id'];
102       $this->rights = explode(',', $val['rights']);
103       $this->is_client = !in_array('track_own_time', $this->rights);
104       $this->rank = $val['rank'];
105       // Downgrade rank to legacy ROLE_MANAGER rank, until we have sub-groups implemented.
106       if ($this->rank > ROLE_MANAGER) $this->rank = ROLE_MANAGER;
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'];
125
126       $this->config = $val['config'];
127       $config_array = explode(',', $this->config);
128
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);
135
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'];
140       }
141     }
142   }
143
144   // The getActiveUser returns user id on behalf of whom the current user is operating.
145   function getActiveUser() {
146     return ($this->behalf_id ? $this->behalf_id : $this->id);
147   }
148
149   // can - determines whether user has a right to do something.
150   function can($do_something) {
151     return in_array($do_something, $this->rights);
152   }
153
154   // isAdmin - determines whether current user is admin (has right_administer_site).
155   function isAdmin() {
156     return $this->can('administer_site');
157   }
158
159   // isManager - determines whether current user is team manager.
160   // This is a legacy function that we are getting rid of by replacing with rights check.
161   function isManager() {
162     return $this->can('export_data'); // By default this is assigned to managers but not co-managers.
163                                       // Which is sufficient for now until we refactor all calls
164                                       // to this function and then remove it.
165   }
166
167   // isCoManager - determines whether current user is team comanager.
168   // This is a legacy function that we are getting rid of by replacing with rights check.
169   function isCoManager() {
170     return ($this->can('manage_users') && !$this->can('export_data'));
171   }
172
173   // isClient - determines whether current user is a client.
174   function isClient() {
175     return $this->is_client;
176   }
177
178   // canManageTeam - determines whether current user is manager or co-manager.
179   // This is a legacy function that we are getting rid of by replacing with rights check.
180   function canManageTeam() {
181     return $this->can('manage_users'); // By default this is assigned to co-managers (an managers).
182                                        // Which is sufficient for now until we refactor all calls
183                                        // to this function and then remove it.
184   }
185
186   // isPluginEnabled checks whether a plugin is enabled for user.
187   function isPluginEnabled($plugin)
188   {
189     return in_array($plugin, explode(',', $this->plugins));
190   }
191
192   // getAssignedProjects - returns an array of assigned projects.
193   function getAssignedProjects()
194   {
195     $result = array();
196     $mdb2 = getConnection();
197
198     // Do a query with inner join to get assigned projects.
199     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
200       inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
201       where p.team_id = $this->team_id and p.status = 1 order by p.name";
202     $res = $mdb2->query($sql);
203     if (!is_a($res, 'PEAR_Error')) {
204       while ($val = $res->fetchRow()) {
205         $result[] = $val;
206       }
207     }
208     return $result;
209   }
210
211   // isDateLocked checks whether a specifc date is locked for modifications.
212   function isDateLocked($date)
213   {
214     if ($this->isPluginEnabled('lk') && $this->lock_spec) {
215
216       // Override.
217       if ($this->can('override_date_lock')) return false;
218
219       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
220       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
221
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)) {
226         return true;
227       }
228     }
229     return false;
230   }
231 }