From 09bb840079b835529b298c4d84a9a2e0f8a4bdb8 Mon Sep 17 00:00:00 2001 From: Nik Okuntseff Date: Sun, 11 Mar 2018 22:04:32 +0000 Subject: [PATCH] Added code to migrate legacy role on user entry to the system. --- WEB-INF/lib/ttUser.class.php | 24 ++++++++++++++++++++++++ WEB-INF/templates/footer.tpl | 2 +- initialize.php | 6 ++++++ user_add.php | 4 ---- user_edit.php | 4 ---- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/WEB-INF/lib/ttUser.class.php b/WEB-INF/lib/ttUser.class.php index 61d88139..5ead08bc 100644 --- a/WEB-INF/lib/ttUser.class.php +++ b/WEB-INF/lib/ttUser.class.php @@ -269,4 +269,28 @@ define('ROLE_SITE_ADMIN', 1024); // Site administrator. } 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; + } } diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index 8c3205e2..84441b48 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.17.37.4053 | Copyright © Anuko | +  Anuko Time Tracker 1.17.37.4054 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/initialize.php b/initialize.php index 5d721b80..63ed88d5 100644 --- a/initialize.php +++ b/initialize.php @@ -169,6 +169,12 @@ $msg = new ActionErrors(); // Notification messages (not errrors) for user. // Create an instance of ttUser class. This gets us most of user details. import('ttUser'); $user = new ttUser(null, $auth->getUserId()); +// Temporary code to assign role_id to users who don't yet have it. +if ($user->login && !$user->role_id) { + $user->migrateLegacyRole(); + // Recycle User object, now with proper role_id. + $user = new ttUser(null, $auth->getUserId()); +} if ($user->custom_logo) { $smarty->assign('custom_logo', 'images/'.$user->team_id.'.png'); $smarty->assign('mobile_custom_logo', '../images/'.$user->team_id.'.png'); diff --git a/user_add.php b/user_add.php index ebc313a2..66d2f2d4 100644 --- a/user_add.php +++ b/user_add.php @@ -84,10 +84,6 @@ if (!$auth->isPasswordExternal()) { $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email)); $active_roles = ttTeamHelper::getActiveRolesForUser(); -//$roles[ROLE_USER] = $i18n->getKey('label.user'); -//$roles[ROLE_COMANAGER] = $i18n->getKey('form.users.comanager'); -//if ($user->isPluginEnabled('cl')) -// $roles[ROLE_CLIENT] = $i18n->getKey('label.client'); $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role,'data'=>$active_roles,'datakeys'=>array('id', 'name'))); if ($user->isPluginEnabled('cl')) $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->getKey('dropdown.select')))); diff --git a/user_edit.php b/user_edit.php index a811c00b..d8f49a78 100644 --- a/user_edit.php +++ b/user_edit.php @@ -119,10 +119,6 @@ if (!$auth->isPasswordExternal()) { $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','style'=>'width: 300px;','value'=>$cl_email)); $active_roles = ttTeamHelper::getActiveRolesForUser(); -//$roles[ROLE_USER] = $i18n->getKey('label.user'); -//$roles[ROLE_COMANAGER] = $i18n->getKey('form.users.comanager'); -//if ($user->isPluginEnabled('cl')) -// $roles[ROLE_CLIENT] = $i18n->getKey('label.client'); $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role,'data'=>$active_roles, 'datakeys'=>array('id', 'name'))); if ($user->isPluginEnabled('cl')) $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->getKey('dropdown.select')))); -- 2.20.1