From f1c11908a996c1a0d9582ef6d32490e5ce15f02d Mon Sep 17 00:00:00 2001 From: Nik Okuntseff Date: Sat, 10 Mar 2018 21:38:08 +0000 Subject: [PATCH] Put code to create default roles upon team creation. --- WEB-INF/lib/ttRoleHelper.class.php | 78 +++++++++++++++++++++++++++- WEB-INF/lib/ttUserHelper.class.php | 4 +- WEB-INF/templates/admin_team_add.tpl | 4 ++ WEB-INF/templates/footer.tpl | 2 +- WEB-INF/templates/register.tpl | 4 ++ admin_team_add.php | 32 +++++++++++- register.php | 35 +++++++++++-- 7 files changed, 150 insertions(+), 9 deletions(-) diff --git a/WEB-INF/lib/ttRoleHelper.class.php b/WEB-INF/lib/ttRoleHelper.class.php index 0cf5f446..7e0f5f69 100644 --- a/WEB-INF/lib/ttRoleHelper.class.php +++ b/WEB-INF/lib/ttRoleHelper.class.php @@ -68,7 +68,22 @@ class ttRoleHelper { return false; } - // The getRoleByRank looks up a role by its rank. + // The getTopManagerRoleID obtains an ID for top manager role. + static function getTopManagerRoleID() { + $mdb2 = getConnection(); + + $sql = "select id from tt_roles where team_id = 0 and rank = 512"; + $res = $mdb2->query($sql); + + if (!is_a($res, 'PEAR_Error')) { + $val = $res->fetchRow(); + if ($val['id']) + return $val['id']; + } + return false; + } + + // The getRoleID looks up a role by its rank. static function getRoleByRank($rank) { global $user; $mdb2 = getConnection(); @@ -151,6 +166,67 @@ class ttRoleHelper { return false; } + // createPredefinedRoles - creates a set of predefined roles for the team to use. + static function createPredefinedRoles($team_id, $lang) + { + // We need localized role names and a new I18n object to obtain them. + import('I18n'); + $i18n = new I18n(); + $i18n->load($lang); + + $mdb2 = getConnection(); + + $rights_client = 'view_own_data,manage_own_settings'; + $rights_user = 'data_entry,view_own_data,manage_own_settings,view_users'; + $rights_supervisor = $rights_user.',on_behalf_data_entry,view_data,override_punch_mode,swap_roles,approve_timesheets'; + $rights_comanager = $rights_supervisor.',manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices'; + $rights_manager = $rights_comanager.',manage_features,manage_basic_settings,manage_advanced_settings,manage_roles,export_data,manage_subgroups'; + + // Active roles. + $name = $mdb2->quote($i18n->getKey('role.user.label')); + $description = $mdb2->quote($i18n->getKey('role.user.description')); + $rights = $mdb2->quote($rights_user); + $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 4, $rights, 1)"; + $affected = $mdb2->exec($sql); + if (is_a($affected, 'PEAR_Error')) + return false; + + $name = $mdb2->quote($i18n->getKey('role.client.label')); + $description = $mdb2->quote($i18n->getKey('role.client.description')); + $rights = $mdb2->quote($rights_client); + $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 16, $rights, 1)"; + $affected = $mdb2->exec($sql); + if (is_a($affected, 'PEAR_Error')) + return false; + + $name = $mdb2->quote($i18n->getKey('role.comanager.label')); + $description = $mdb2->quote($i18n->getKey('role.comanager.description')); + $rights = $mdb2->quote($rights_comanager); + $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 68, $rights, 1)"; + $affected = $mdb2->exec($sql); + if (is_a($affected, 'PEAR_Error')) + return false; + + $name = $mdb2->quote($i18n->getKey('role.manager.label')); + $description = $mdb2->quote($i18n->getKey('role.manager.description')); + $rights = $mdb2->quote($rights_manager); + $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 324, $rights, 1)"; + $affected = $mdb2->exec($sql); + if (is_a($affected, 'PEAR_Error')) + return false; + + // Inactive roles. + $name = $mdb2->quote($i18n->getKey('role.supervisor.label')); + $description = $mdb2->quote($i18n->getKey('role.supervisor.description')); + $rights = $mdb2->quote($rights_supervisor); + $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 12, $rights, 0)"; + $affected = $mdb2->exec($sql); + if (is_a($affected, 'PEAR_Error')) + return false; + + return true; + } + // createDefaultRoles - creates a set of predefined roles for the team to use. static function createDefaultRoles() { diff --git a/WEB-INF/lib/ttUserHelper.class.php b/WEB-INF/lib/ttUserHelper.class.php index cc8b90de..f88f195d 100644 --- a/WEB-INF/lib/ttUserHelper.class.php +++ b/WEB-INF/lib/ttUserHelper.class.php @@ -125,9 +125,9 @@ class ttUserHelper { $status_v = ', '.$mdb2->quote($fields['status']); } - $sql = "insert into tt_users (name, login, password, team_id, role, client_id, rate, email $status_f) values (". + $sql = "insert into tt_users (name, login, password, team_id, role, role_id, client_id, rate, email $status_f) values (". $mdb2->quote($fields['name']).", ".$mdb2->quote($fields['login']). - ", $password, $team_id, $role, ".$mdb2->quote($fields['client_id']).", $rate, ".$mdb2->quote($email)." $status_v)"; + ", $password, $team_id, $role, ".$mdb2->quote($fields['role_id']).", ".$mdb2->quote($fields['client_id']).", $rate, ".$mdb2->quote($email)." $status_v)"; $affected = $mdb2->exec($sql); // Now deal with project assignment. diff --git a/WEB-INF/templates/admin_team_add.tpl b/WEB-INF/templates/admin_team_add.tpl index bf48a4e8..97cd94da 100644 --- a/WEB-INF/templates/admin_team_add.tpl +++ b/WEB-INF/templates/admin_team_add.tpl @@ -7,6 +7,10 @@ {$i18n.label.team_name}: {$forms.teamForm.team_name.control} + + {$i18n.label.language}: + {$forms.teamForm.lang.control} +   {$i18n.label.manager_name} (*): diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index 5107af54..f9f60818 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
- + + + + diff --git a/admin_team_add.php b/admin_team_add.php index de18950b..e88da19a 100644 --- a/admin_team_add.php +++ b/admin_team_add.php @@ -29,6 +29,7 @@ require_once('initialize.php'); import('form.Form'); import('ttUserHelper'); +import('ttRoleHelper'); // Access check. if (!ttAccessCheck(right_administer_site)) { @@ -38,6 +39,7 @@ if (!ttAccessCheck(right_administer_site)) { if ($request->isPost()) { $cl_team_name = trim($request->getParameter('team_name')); + $cl_lang = $request->getParameter('lang'); $cl_manager_name = trim($request->getParameter('manager_name')); $cl_manager_login = trim($request->getParameter('manager_login')); if (!$auth->isPasswordExternal()) { @@ -45,10 +47,30 @@ if ($request->isPost()) { $cl_password2 = $request->getParameter('password2'); } $cl_manager_email = trim($request->getParameter('manager_email')); -} +} else + $cl_lang = $i18n->lang; // Browser setting from initialize.php. $form = new Form('teamForm'); $form->addInput(array('type'=>'text','maxlength'=>'200','name'=>'team_name','value'=>$cl_team_name)); + +// Prepare an array of available languages. +$lang_files = I18n::getLangFileList(); +foreach ($lang_files as $lfile) { + $content = file(RESOURCE_DIR."/".$lfile); + $lname = ''; + foreach ($content as $line) { + if (strstr($line, 'i18n_language')) { + $a = explode('=', $line); + $lname = trim(str_replace(';','',str_replace("'","",$a[1]))); + break; + } + } + unset($content); + $longname_lang[] = array('id'=>I18n::getLangFromFilename($lfile),'name'=>$lname); +} +$longname_lang = mu_sort($longname_lang, 'name'); +$form->addInput(array('type'=>'combobox','name'=>'lang','style'=>'width: 200px','data'=>$longname_lang,'datakeys'=>array('id','name'),'value'=>$cl_lang)); + $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_name','value'=>$cl_manager_name)); $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_login','value'=>$cl_manager_login)); if (!$auth->isPasswordExternal()) { @@ -75,12 +97,18 @@ if ($request->isPost()) { if (!ttUserHelper::getUserByLogin($cl_manager_login)) { // Create a new team. if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$'); - $team_id = ttTeamHelper::insert(array('name'=>$cl_team_name,'currency'=>CURRENCY_DEFAULT)); + $team_id = ttTeamHelper::insert(array('name'=>$cl_team_name,'currency'=>CURRENCY_DEFAULT,'lang'=>$cl_lang)); if ($team_id) { + if (!ttRoleHelper::createPredefinedRoles($team_id, $cl_lang)) + $err->add($i18n->getKey('error.db')); + + $role_id = ttRoleHelper::getTopManagerRoleID(); + // Team created, now create a team manager. $user_id = ttUserHelper::insert(array( 'team_id' => $team_id, 'role' => ROLE_MANAGER, + 'role_id' => $role_id, 'name' => $cl_manager_name, 'login' => $cl_manager_login, 'password' => $cl_password1, diff --git a/register.php b/register.php index 99e9539b..13cf5574 100644 --- a/register.php +++ b/register.php @@ -29,6 +29,7 @@ require_once('initialize.php'); import('form.Form'); import('ttUserHelper'); +import('ttRoleHelper'); if (!isTrue(MULTITEAM_MODE) || $auth->isPasswordExternal()) { header('Location: login.php'); @@ -43,17 +44,39 @@ if ($request->isPost()) { $cl_team_name = trim($request->getParameter('team_name')); $cl_currency = trim($request->getParameter('currency')); if (!$cl_currency) $cl_currency = CURRENCY_DEFAULT; + $cl_lang = $request->getParameter('lang'); $cl_manager_name = trim($request->getParameter('manager_name')); $cl_manager_login = trim($request->getParameter('manager_login')); $cl_password1 = $request->getParameter('password1'); $cl_password2 = $request->getParameter('password2'); $cl_manager_email = trim($request->getParameter('manager_email')); -} else +} else { $cl_currency = CURRENCY_DEFAULT; + $cl_lang = $i18n->lang; // Browser setting from initialize.php. +} $form = new Form('profileForm'); $form->addInput(array('type'=>'text','maxlength'=>'200','name'=>'team_name','value'=>$cl_team_name)); $form->addInput(array('type'=>'text','maxlength'=>'7','name'=>'currency','value'=>$cl_currency)); + +// Prepare an array of available languages. +$lang_files = I18n::getLangFileList(); +foreach ($lang_files as $lfile) { + $content = file(RESOURCE_DIR."/".$lfile); + $lname = ''; + foreach ($content as $line) { + if (strstr($line, 'i18n_language')) { + $a = explode('=', $line); + $lname = trim(str_replace(';','',str_replace("'","",$a[1]))); + break; + } + } + unset($content); + $longname_lang[] = array('id'=>I18n::getLangFromFilename($lfile),'name'=>$lname); +} +$longname_lang = mu_sort($longname_lang, 'name'); +$form->addInput(array('type'=>'combobox','name'=>'lang','style'=>'width: 200px','data'=>$longname_lang,'datakeys'=>array('id','name'),'value'=>$cl_lang)); + $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_name','value'=>$cl_manager_name)); $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_login','value'=>$cl_manager_login)); $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password1','value'=>$cl_password1)); @@ -76,12 +99,18 @@ if ($request->isPost()) { if ($err->no()) { if (!ttUserHelper::getUserByLogin($cl_manager_login)) { // Create a new team. - $team_id = ttTeamHelper::insert(array('name'=>$cl_team_name,'currency'=>$cl_currency)); + $team_id = ttTeamHelper::insert(array('name'=>$cl_team_name,'currency'=>$cl_currency,'lang'=>$cl_lang)); if ($team_id) { + if (!ttRoleHelper::createPredefinedRoles($team_id, $cl_lang)) + $err->add($i18n->getKey('error.db')); + + $role_id = ttRoleHelper::getTopManagerRoleID(); + // Team created, now create a team manager. $user_id = ttUserHelper::insert(array( 'team_id' => $team_id, - 'role' => ROLE_MANAGER, + 'role' => ROLE_MANAGER, // TODO: this is now out of sync (324 vs 512 for new role id). Work in progress. + 'role_id' => $role_id, 'name' => $cl_manager_name, 'login' => $cl_manager_login, 'password' => $cl_password1, -- 2.20.1
 Anuko Time Tracker 1.17.36.4048 | Copyright © Anuko | +  Anuko Time Tracker 1.17.36.4049 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/WEB-INF/templates/register.tpl b/WEB-INF/templates/register.tpl index ba1e50e0..bc77a6a4 100644 --- a/WEB-INF/templates/register.tpl +++ b/WEB-INF/templates/register.tpl @@ -11,6 +11,10 @@ {$i18n.label.currency}: {$forms.profileForm.currency.control}
{$i18n.label.language}:{$forms.profileForm.lang.control}
 
{$i18n.label.manager_name} (*):