Split data_entry right into track_time and track_expenses.
[timetracker.git] / WEB-INF / lib / ttRoleHelper.class.php
index 62d2baf..bcd6605 100644 (file)
@@ -50,6 +50,118 @@ class ttRoleHelper {
     return false;
   }
 
+  // The getRoleByName looks up a role by name.
+  static function getRoleByName($role_name) {
+
+    $mdb2 = getConnection();
+    global $user;
+
+    $sql = "select id from tt_roles where team_id = $user->team_id and name = ".
+      $mdb2->quote($role_name)." and (status = 1 or status = 0)";
+    $res = $mdb2->query($sql);
+
+    if (!is_a($res, 'PEAR_Error')) {
+      $val = $res->fetchRow();
+      if ($val['id'])
+        return $val;
+    }
+    return false;
+  }
+
+  // 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 getLegacyRole obtains a legacy role value for a role_id.
+  // This is a temporary function to allow usage of both old and new roles
+  // while new role code is being written and deployed.
+  static function getLegacyRole($role_id) {
+    global $user;
+    $mdb2 = getConnection();
+
+    $sql = "select rank from tt_roles where team_id = $user->team_id and id = $role_id";
+    $res = $mdb2->query($sql);
+
+    if (!is_a($res, 'PEAR_Error')) {
+      $val = $res->fetchRow();
+      if ($val['rank']) {
+        $rank = $val['rank'];
+        if ($rank >= ROLE_MANAGER)
+          return ROLE_MANAGER;
+        else if ($rank >= ROLE_COMANAGER)
+          return ROLE_COMANAGER;
+        else if ($rank >= ROLE_CLIENT)
+          return ROLE_CLIENT;
+        else
+          return ROLE_USER;
+      }
+    }
+    return false;
+  }
+
+  // isClientRole determines if the role is a "client" role.
+  // This simply means the role has no "track_own_time" right.
+  static function isClientRole($role_id) {
+    global $user;
+    $mdb2 = getConnection();
+
+    $sql = "select rights from tt_roles where team_id = $user->team_id and id = $role_id";
+    $res = $mdb2->query($sql);
+
+    if (!is_a($res, 'PEAR_Error')) {
+      $val = $res->fetchRow();
+      if ($val['rights']) {
+        return !in_array('track_own_time', explode(',', $val['rights']));
+      }
+    }
+    return false;
+  }
+
+  // getRoleByRank looks up a role by its rank.
+  static function getRoleByRank($rank) {
+    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)";
+    $res = $mdb2->query($sql);
+
+    if (!is_a($res, 'PEAR_Error')) {
+      $val = $res->fetchRow();
+      if ($val['id'])
+        return $val['id'];
+    }
+    return false;
+  }
+
+  // update function updates a role in the database.
+  static function update($fields) {
+    global $user;
+    $mdb2 = getConnection();
+
+    $id = (int)$fields['id'];
+    if (isset($fields['name'])) $name_part = 'name = '.$mdb2->quote($fields['name']);
+    if (isset($fields['description'])) $descr_part = ', description = '.$mdb2->quote($fields['description']);
+    if (isset($fields['status'])) $status_part = ', status = '.(int)$fields['status'];
+    if (isset($fields['rights'])) $rights_part = ', rights = '.$mdb2->quote($fields['rights']);
+    $parts = trim($name_part.$descr_part.$status_part.$rights_part, ',');
+    $sql = "update tt_roles set $parts where id = $id and team_id = $user->team_id";
+    $affected = $mdb2->exec($sql);
+    return (!is_a($affected, 'PEAR_Error'));
+  }
+
   // delete - marks the role as deleted.
   static function delete($role_id) {
     global $user;
@@ -70,11 +182,12 @@ class ttRoleHelper {
     $team_id = (int) $fields['team_id'];
     $name = $fields['name'];
     $rank = (int) $fields['rank'];
+    $description = $fields['description'];
     $rights = $fields['rights'];
     $status = $fields['status'];
 
-    $sql = "insert into tt_roles (team_id, name, rank, rights, status)
-      values ($team_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
+    $sql = "insert into tt_roles (team_id, name, rank, description, rights, status)
+      values ($team_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($description).", ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error'))
       return false;
@@ -98,6 +211,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_reports,view_own_charts,view_own_invoices,manage_own_settings';
+    $rights_user = 'track_own_time,track_own_expenses,view_own_reports,view_own_charts,manage_own_settings,view_users';
+    $rights_supervisor = $rights_user.',track_time,track_expenses,view_reports,view_charts,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()
   {
@@ -105,10 +279,16 @@ class ttRoleHelper {
     global $i18n;
     global $user;
 
+    $rights_client = 'view_own_reports,view_own_charts,view_own_invoices,manage_own_settings';
+    $rights_user = 'track_own_time,track_own_expenses,view_own_reports,view_own_charts,manage_own_settings,view_users';
+    $rights_supervisor = $rights_user.',track_time,track_expenses,view_reports,view_charts,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('data_entry,view_own_data,manage_own_settings,view_users');
+    $rights = $mdb2->quote($rights_user);
     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 4, $rights, 1)";
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error'))
@@ -116,7 +296,7 @@ class ttRoleHelper {
 
     $name = $mdb2->quote($i18n->getKey('role.client.label'));
     $description = $mdb2->quote($i18n->getKey('role.client.description'));
-    $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users');  // TODO: adjust rights.
+    $rights = $mdb2->quote($rights_client);
     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 16, $rights, 1)";
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error'))
@@ -124,7 +304,7 @@ class ttRoleHelper {
 
     $name = $mdb2->quote($i18n->getKey('role.comanager.label'));
     $description = $mdb2->quote($i18n->getKey('role.comanager.description'));
-    $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users');  // TODO: adjust rights.
+    $rights = $mdb2->quote($rights_comanager);
     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 68, $rights, 1)";
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error'))
@@ -132,7 +312,7 @@ class ttRoleHelper {
 
     $name = $mdb2->quote($i18n->getKey('role.manager.label'));
     $description = $mdb2->quote($i18n->getKey('role.manager.description'));
-    $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users');  // TODO: adjust rights.
+    $rights = $mdb2->quote($rights_manager);
     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 324, $rights, 1)";
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error'))
@@ -141,7 +321,7 @@ class ttRoleHelper {
     // Inactive roles.
     $name = $mdb2->quote($i18n->getKey('role.supervisor.label'));
     $description = $mdb2->quote($i18n->getKey('role.supervisor.description'));
-    $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users'); // TODO: adjust rights.
+    $rights = $mdb2->quote($rights_supervisor);
     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 12, $rights, 0)";
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error'))