Improved users page a bit in accordance with user rights.
[timetracker.git] / WEB-INF / lib / ttUser.class.php
index 0a10488..5321e74 100644 (file)
@@ -241,4 +241,92 @@ class ttUser {
 
     return true;
   }
+
+  // getUsers obtains users in a group, as specififed by options.
+  function getUsers($options) {
+
+    $mdb2 = getConnection();
+
+    $skipClients = !isset($options['include_clients']);
+    $includeSelf = isset($options['include_self']);
+
+    $select_part = 'select u.id, u.name';
+    if (isset($options['include_login'])) $select_part .= ', u.login';
+    if (!isset($options['include_clients'])) $select_part .= ', r.rights';
+    if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
+
+    $from_part = ' from tt_users u';
+
+    $left_joins = null;
+    if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
+        $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
+
+    $where_part = " where u.team_id = $this->team_id";
+    if (isset($options['status'])) $where_part .= ' and u.status = '.(int)$options['status'];
+    if ($includeSelf) {
+      $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
+    } else {
+      if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
+    }
+
+    $sql = $select_part.$from_part.$left_joins.$where_part;
+    $res = $mdb2->query($sql);
+    $user_list = array();
+    if (is_a($res, 'PEAR_Error'))
+      return false;
+
+    while ($val = $res->fetchRow()) {
+      if ($skipClients) {
+        $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
+        if ($isClient)
+          continue; // Skip adding clients.
+      }
+      $user_list[] = $val;
+    }
+
+    if (isset($options['self_first'])) {
+      // Put own entry at the front.
+      $cnt = count($user_list);
+      for($i = 0; $i < $cnt; $i++) {
+        if ($user_list[$i]['id'] == $this->id) {
+          $self = $user_list[$i]; // Found self.
+          array_unshift($user_list, $self); // Put own entry at the front.
+          array_splice($user_list, $i+1, 1); // Remove duplicate.
+        }
+      }
+    }
+    return $user_list;
+  }
+
+  // checkBehalfId checks whether behalf_id is appropriate.
+  // On behalf user must be active and have lower rank.
+  function checkBehalfId() {
+    $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
+    $users = $this->getUsers($options);
+    foreach($users as $one_user) {
+      if ($one_user['id'] == $this->behalf_id)
+        return true;
+    }
+    return false;
+  }
+
+  // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
+  // apropriate user.
+  //
+  // Needed for situations when user does not have do_own_something right.
+  // Example: has view_charts but does not have view_own_charts.
+  // In this case we still allow access to charts, but set behalf_id to someone else.
+  function adjustBehalfId() {
+    $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
+    $users = $this->getUsers($options);
+    foreach($users as $one_user) {
+      // Fake loop to access first element.
+      $this->behalf_id = $one_user['id'];
+      $this->behalf_name = $one_user['name'];
+      $_SESSION['behalf_id'] = $this->behalf_id;
+      $_SESSION['behalf_name'] = $this->behalf_name;
+      return true;
+    }
+    return false;
+  }
 }