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 // +----------------------------------------------------------------------+
29 import('ttRoleHelper');
31 // ttAdmin class is used to perform admin tasks.
32 // Used as namespace, as it is a collection of static functions that we call
33 // from admin pages to administer the site as a whole.
36 // getSubgroups rerurns an array of subgroups for a group.
37 static function getSubgroups($group_id) {
38 $mdb2 = getConnection();
41 $sql = "select id from tt_groups where parent_id = $group_id";
42 $res = $mdb2->query($sql);
43 if (!is_a($res, 'PEAR_Error')) {
44 while ($val = $res->fetchRow()) {
51 // markGroupDeleted marks a group and everything in it as deleted.
52 // This function is called in context of a logged on admin who may
53 // operate on any group.
54 static function markGroupDeleted($group_id) {
55 $mdb2 = getConnection();
57 // Keep the logic simple by returning false on first error.
59 // Obtain subgroups and call self recursively on them.
60 $subgroups = ttAdmin::getSubgroups($group_id);
61 foreach($subgroups as $subgroup) {
62 if (!ttAdmin::markGroupDeleted($subgroup['id']))
66 // Now do actual work with all entities.
68 // Some things cannot be marked deleted as we don't have the status field for them.
69 // Just delete such things (until we have a better way to deal with them).
70 $tables_to_delete_from = array(
72 'tt_predefined_expenses',
73 'tt_client_project_binds',
74 'tt_project_task_binds'
76 foreach($tables_to_delete_from as $table) {
77 if (!ttAdmin::deleteGroupEntriesFromTable($group_id, $table))
81 // Now mark status deleted where we can.
82 // Note: we don't mark tt_log, tt_custom_field_lod, or tt_expense_items deleted here.
85 // 1) Users may mark some of them deleted during their work.
86 // If we mark all of them deleted here, we can't recover nicely
87 // as we'll lose track of what was accidentally deleted by user.
89 // 2) DB maintenance script (Clean up DB from inactive groups) should
90 // get rid of these items permanently eventually.
91 $tables_to_mark_deleted_in = array(
94 // 'tt_expense_items',
95 // 'tt_custom_field_log',
96 'tt_custom_field_options',
100 'tt_user_project_binds',
107 foreach($tables_to_mark_deleted_in as $table) {
108 if (!ttAdmin::markGroupDeletedInTable($group_id, $table))
112 // Mark group deleted.
114 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
115 $sql = "update tt_groups set status = null $modified_part where id = $group_id";
116 $affected = $mdb2->exec($sql);
117 if (is_a($affected, 'PEAR_Error')) return false;
122 // updateGroup updates a (top) group with new information.
123 static function updateGroup($fields) {
124 $group_id = (int)$fields['group_id'];
125 if (!$group_id) return false; // Nothing to update.
127 $mdb2 = getConnection();
129 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
131 // Update group name if it changed.
132 if ($fields['old_group_name'] != $fields['new_group_name']) {
133 $name_part = 'name = '.$mdb2->quote($fields['new_group_name']);
134 $sql = 'update tt_groups set '.$name_part.$modified_part.' where id = '.$group_id;
135 $affected = $mdb2->exec($sql);
136 if (is_a($affected, 'PEAR_Error')) return false;
139 // Update group manager.
140 $user_id = $fields['user_id'];
141 $login_part = 'login = '.$mdb2->quote($fields['new_login']);
142 if ($fields['password1'])
143 $password_part = ', password = md5('.$mdb2->quote($fields['password1']).')';
144 $name_part = ', name = '.$mdb2->quote($fields['user_name']);
145 $email_part = ', email = '.$mdb2->quote($fields['email']);
146 $sql = 'update tt_users set '.$login_part.$password_part.$name_part.$email_part.$modified_part.' where id = '.$user_id;
147 $affected = $mdb2->exec($sql);
148 if (is_a($affected, 'PEAR_Error')) return false;
153 // updateSelf updates admin account with new information.
154 static function updateSelf($fields) {
156 $mdb2 = getConnection();
159 $user_id = $user->id;
160 $login_part = 'login = '.$mdb2->quote($fields['login']);
161 if ($fields['password1'])
162 $password_part = ', password = md5('.$mdb2->quote($fields['password1']).')';
163 $name_part = ', name = '.$mdb2->quote($fields['name']);
164 $email_part = ', email = '.$mdb2->quote($fields['email']);
165 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
166 $sql = 'update tt_users set '.$login_part.$password_part.$name_part.$email_part.$modified_part.' where id = '.$user_id;
167 $affected = $mdb2->exec($sql);
168 return (!is_a($affected, 'PEAR_Error'));
171 // getGroupName obtains group name.
172 static function getGroupName($group_id) {
174 $mdb2 = getConnection();
176 $sql = "select name from tt_groups where id = $group_id";
178 $res = $mdb2->query($sql);
179 if (!is_a($res, 'PEAR_Error')) {
180 $val = $res->fetchRow();
187 // getOrgDetails obtains group name and its top manager details.
188 static function getOrgDetails($group_id) {
190 $mdb2 = getConnection();
192 // Note: current code works with properly set top manager (rank 512).
193 // However, we now allow export and import of subgroups, which seems to work well.
194 // In this situation, imported role is no longer "Top manager", and this call fails.
195 // Setting role id manually in database for top user to Top manager resolves the issue.
197 // TODO: assess whether it is safe / reasonable to promote role during export or import.
198 // The problem is that user having 'export_data' right is not necessarily top user.
199 // And if we do it by rank, what to do for multiple managers situation? First found?
200 // Leaving to manual fixing for now.
201 $sql = "select g.name as group_name, u.id as manager_id, u.name as manager_name, u.login as manager_login, u.email as manager_email".
203 " inner join tt_users u on (u.group_id = g.id)".
204 " inner join tt_roles r on (r.id = u.role_id and r.rank = 512)". // Fails for partially imported org. See comment above.
205 " where g.id = $group_id";
207 $res = $mdb2->query($sql);
208 if (!is_a($res, 'PEAR_Error')) {
209 $val = $res->fetchRow();
216 // deleteGroupEntriesFromTable is a generic helper function for markGroupDeleted.
217 // It deletes entries in ONE table belonging to a given group.
218 static function deleteGroupEntriesFromTable($group_id, $table_name) {
219 $mdb2 = getConnection();
221 $sql = "delete from $table_name where group_id = $group_id";
222 $affected = $mdb2->exec($sql);
223 return (!is_a($affected, 'PEAR_Error'));
226 // markGroupDeletedInTable is a generic helper function for markGroupDeleted.
227 // It updates ONE table by setting status to NULL for all records belonging to a group.
228 static function markGroupDeletedInTable($group_id, $table_name) {
229 $mdb2 = getConnection();
231 // Add modified info to sql for some tables, depending on table name.
232 if ($table_name == 'tt_users') {
234 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
237 $sql = "update $table_name set status = null $modified_part where group_id = $group_id";
238 $affected = $mdb2->exec($sql);
239 return (!is_a($affected, 'PEAR_Error'));
242 // createGroup creates a new top group and returns its id.
243 // It is a helper function for createOrg.
244 static function createGroup($fields) {
246 $mdb2 = getConnection();
248 $name = $mdb2->quote($fields['group_name']);
249 $currency = $mdb2->quote($fields['currency']);
250 $lang = $mdb2->quote($fields['lang']);
252 $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
253 $created_by = $user->id;
255 $sql = "insert into tt_groups (name, currency, lang, created, created_ip, created_by)".
256 " values($name, $currency, $lang, $created, $created_ip, $created_by)";
257 $affected = $mdb2->exec($sql);
258 if (is_a($affected, 'PEAR_Error')) return false;
260 $group_id = $mdb2->lastInsertID('tt_groups', 'id');
262 // Update org_id with group_id.
263 $sql = "update tt_groups set org_id = $group_id where org_id is NULL and id = $group_id";
264 $affected = $mdb2->exec($sql);
265 if (is_a($affected, 'PEAR_Error')) return false;
270 // createOrgManager creates a new user (top manager role) in a group.
271 // It is a helper function for createOrg.
272 static function createOrgManager($fields) {
274 $mdb2 = getConnection();
276 $role_id = ttRoleHelper::getTopManagerRoleID();
277 $login = $mdb2->quote($fields['login']);
278 $password = 'md5('.$mdb2->quote($fields['password']).')';
279 $name = $mdb2->quote($fields['user_name']);
280 $group_id = (int) $fields['group_id'];
282 $email = $mdb2->quote($fields['email']);
284 $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
285 $created_by = $user->id;
287 $columns = '(login, password, name, group_id, org_id, role_id, email, created, created_ip, created_by)';
288 $values = "values($login, $password, $name, $group_id, $org_id, $role_id, $email, $created, $created_ip, $created_by)";
290 $sql = "insert into tt_users $columns $values";
291 $affected = $mdb2->exec($sql);
292 return (!is_a($affected, 'PEAR_Error'));
295 // The createOrg function creates an organization in Time Tracker.
296 static function createOrg($fields) {
297 // There are 3 steps that we need to 2 when creating a new organization.
298 // 1. Create a new group with null parent_id.
299 // 2. Create pre-defined roles in it.
300 // 3. Create a top manager account for new group.
302 // Create a new group.
303 $group_id = ttAdmin::createGroup($fields);
304 if (!$group_id) return false;
306 // Create predefined roles.
307 if (!ttRoleHelper::createPredefinedRoles($group_id, $fields['lang']))
311 $fields['group_id'] = $group_id;
312 if (!ttAdmin::createOrgManager($fields))