From: Nik Okuntseff Date: Thu, 15 Mar 2018 15:47:23 +0000 (+0000) Subject: Some cleanup and refactoring of role_id update. X-Git-Tag: timetracker_1.19-1~1031 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=8d68ecdd7b1c45cb2a5cf592b8d504d7e4614911;p=timetracker.git Some cleanup and refactoring of role_id update. --- diff --git a/WEB-INF/lib/ttRoleHelper.class.php b/WEB-INF/lib/ttRoleHelper.class.php index 3e6e73e1..329e0a82 100644 --- a/WEB-INF/lib/ttRoleHelper.class.php +++ b/WEB-INF/lib/ttRoleHelper.class.php @@ -129,13 +129,13 @@ class ttRoleHelper { } // getRoleByRank looks up a role by its rank. - static function getRoleByRank($rank) { + static function getRoleByRank($rank, $team_id) { global $user; $mdb2 = getConnection(); $rank = (int) $rank; // Cast to int just in case for better security. - $sql = "select id from tt_roles where team_id = $user->team_id and rank = $rank and (status = 1 or status = 0)"; + $sql = "select id from tt_roles where team_id = $team_id and rank = $rank and (status = 1 or status = 0)"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { @@ -195,22 +195,6 @@ class ttRoleHelper { return true; } - // rolesExist - checks whether roles for team already exist. - static function rolesExist() - { - $mdb2 = getConnection(); - global $user; - - $sql = "select count(*) as count from tt_roles where team_id = $user->team_id"; - $res = $mdb2->query($sql); - if (!is_a($res, 'PEAR_Error')) { - $val = $res->fetchRow(); - if ($val['count'] > 0) - return true; // Roles for team exist. - } - return false; - } - // createPredefinedRoles - creates a set of predefined roles for the team to use. static function createPredefinedRoles($team_id, $lang) { diff --git a/WEB-INF/lib/ttUser.class.php b/WEB-INF/lib/ttUser.class.php index 61680b95..9a4dee48 100644 --- a/WEB-INF/lib/ttUser.class.php +++ b/WEB-INF/lib/ttUser.class.php @@ -228,28 +228,4 @@ class ttUser { } 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->legacy_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; - } } diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index 7960c6ea..8f8fae69 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.17.45.4080 | Copyright © Anuko | +  Anuko Time Tracker 1.17.45.4081 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/dbinstall.php b/dbinstall.php index 1648a827..1c95fc28 100644 --- a/dbinstall.php +++ b/dbinstall.php @@ -31,7 +31,7 @@ require_once('WEB-INF/lib/common.lib.php'); require_once('initialize.php'); import('ttUserHelper'); import('ttTaskHelper'); -import('ttUser'); +import('ttRoleHelper'); // setChange - executes an sql statement. TODO: rename this function to something better. // Better yet, redo the entire thing and make an installer. @@ -771,40 +771,34 @@ if ($_POST) { $mdb2 = getConnection(); - $sql = "select u.id, u.status from tt_users u inner join `tt_site_config` sc on (sc.param_name = 'version_db' and sc.param_value = '1.17.44') where u.role_id is NULL and u.status is NOT NULL"; + $sql = "select u.id, u.team_id, u.role, u.status, t.lang from tt_users u inner join `tt_site_config` sc on (sc.param_name = 'version_db' and sc.param_value = '1.17.44') left join tt_teams t on (u.team_id = t.id) where u.role_id is NULL and u.status is NOT NULL"; $res = $mdb2->query($sql); - if (is_a($res, 'PEAR_Error')) { - die($res->getMessage()); - } + if (is_a($res, 'PEAR_Error')) die($res->getMessage()); $users_updated = 0; // Iterate through users. while ($val = $res->fetchRow()) { $user_id = $val['id']; + $team_id = $val['team_id']; + $lang = $val['lang']; + $legacy_role = $val['role']; + + $sql = "select count(*) as count from tt_roles where team_id = $team_id"; + $result = $mdb2->query($sql); + if (is_a($result, 'PEAR_Error')) die($result->getMessage()); + $row = $result->fetchRow(); + if ($row['count'] == 0) + ttRoleHelper::createPredefinedRoles($team_id, $lang); - // Code only works on active users. Temporarily activate a user. - $deactivate = false; - if ($val['status'] == 0) { - $deactivate = true; // To deactivate later. - $sql = "update tt_users set status = 1 where id = $user_id"; - $mdb2->exec($sql); - } - - $user = new ttUser(null, $user_id); - $i18n = new I18n(); - $i18n->load($val['lang']); - if ($user->login) - $user->migrateLegacyRole(); + // Obtain new role id based on legacy role. + $role_id = ttRoleHelper::getRoleByRank($legacy_role, $team_id); + if (!$role_id) continue; // Role not found, nothing to do. - if ($deactivate) { - // Deactivate temporarily activated user back. - $sql = "update tt_users set status = 0 where id = $user_id"; - $mdb2->exec($sql); - } + $sql = "update tt_users set role_id = $role_id where id = $user_id and team_id = $team_id"; + $affected = $mdb2->exec($sql); + if (is_a($affected, 'PEAR_Error')) die($affected->getMessage()); - unset($user); - unset($i18n); $users_updated++; // if ($users_updated >= 1000) break; // TODO: uncomment for large user sets to run multiple times. } diff --git a/role_add.php b/role_add.php index 420b44d4..bd9f3e46 100644 --- a/role_add.php +++ b/role_add.php @@ -58,7 +58,7 @@ if ($request->isPost()) { if (!ttValidString($cl_description, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.description')); if ($err->no()) { - $existing_role = ttRoleHelper::getRoleByRank($cl_rank); + $existing_role = ttRoleHelper::getRoleByRank($cl_rank, $user->team_id); if (!$existing_role) { // Insert a role with default user rights. if (ttRoleHelper::insert(array( diff --git a/roles.php b/roles.php index efeb4957..b2629be8 100644 --- a/roles.php +++ b/roles.php @@ -37,9 +37,6 @@ if (!ttAccessAllowed('manage_roles')) { exit(); } -// If there are no roles in team, introduce default ones. -if (!ttRoleHelper::rolesExist()) ttRoleHelper::createDefaultRoles(); // TODO: refactor or remove after roles revamp. - $smarty->assign('active_roles', ttTeamHelper::getActiveRoles($user->team_id)); $smarty->assign('inactive_roles', ttTeamHelper::getInactiveRoles($user->team_id)); $smarty->assign('title', $i18n->getKey('title.roles')); diff --git a/user_edit.php b/user_edit.php index 92d27eaa..8e68583c 100644 --- a/user_edit.php +++ b/user_edit.php @@ -95,11 +95,6 @@ if ($request->isPost()) { $cl_email = $user_details['email']; $cl_rate = str_replace('.', $user->decimal_mark, $user_details['rate']); $cl_role = $user_details['role_id']; - - // In case role_id is not yet assigned... - if (!$cl_role && $user_details['role']) - $cl_role = ttRoleHelper::getRoleByRank($user_details['role']); // TODO: remove after roles revamp. - $cl_client_id = $user_details['client_id']; $cl_status = $user_details['status']; $cl_projects = array(); diff --git a/users.php b/users.php index 79f2df3e..28a37227 100644 --- a/users.php +++ b/users.php @@ -41,12 +41,6 @@ if (!ttAccessAllowed('view_users')) { // Get users. $active_users = ttTeamHelper::getActiveUsers(array('getAllFields'=>true)); if($user->canManageTeam()) { - - // If there are no roles in team, introduce default ones. - if (!ttRoleHelper::rolesExist()) ttRoleHelper::createDefaultRoles(); // TODO: refactor or remove after roles revamp. - // This is here temporarily so that we have roles to work with to manage users. - // Normally, this should be done during an upgrade step (not yet implemented). - $can_delete_manager = (1 == count($active_users)); $inactive_users = ttTeamHelper::getInactiveUsers($user->team_id, true); }