/** * Get roles from a particular user. * * @param T_User $user user * @return T_Role[] */ function getByUser(T_User $user) { $db = $this->db->slave(); $id = (int) $user->getId(); /* This process needs to recursively query the database until all roles * for a particular user have been retrieved. i.e. if the roles structure * looks like: * * role A * +---- role B * +---- role C * +---- role D * +---- role E * * The method must pick up all of roles A,B,C,D,E for the user. As there * may be an arbitrary number of children, this cannot be achieved in one * query and several must be issued to retrieve the user roles. */ $roles = array(); // array keys are role IDs // get initial roles $sql = 'SELECT id,name ' . 'FROM role JOIN person_role ON (id=role) ' . "WHERE person=?"; $result = $db->query($sql, array($id)); // recurse for sub-members of user member roles while (count($result) > 0) { foreach ($result as $row) { $roles[intval($row['id'])] = $this->toRole($row); } $existing = implode(',', array_keys($roles)); $sql = 'SELECT id,name ' . 'FROM role JOIN role_group ON (id=member) ' . "WHERE role IN ({$existing}) " . "AND member NOT IN ({$existing})"; // not already registered $result = $db->query($sql); } return $roles; }
/** * Get any auth available. * * @param T_User_Gateway $user_gw * @param T_Role_Gateway $role_gw * @return T_Auth|false auth if available or false if not */ function get($user_gw, $role_gw) { if (!$this->cookie->exists($this->key)) { return false; } // lookup token in db $token = $this->cookie->asScalar($this->key)->filter(new T_Validate_HexHash())->uncage(); if (mt_rand(1, 20) == 10) { // 1/20 clear old records $sql = 'DELETE FROM person_auth_token WHERE expiry<' . time(); $this->db->master()->query($sql); } $sql = 'SELECT person,expiry ' . 'FROM person_auth_token ' . 'WHERE expiry>? AND token=?'; $result = $this->db->slave()->query($sql, array(time(), $token)); // if the token has not been found, remove it // (it has probably expired). if (count($result) != 1) { $this->destroy(); return false; } // token has been found, so get the user and roles associated // with the token and create auth package $row = $result->fetch(); $user = $user_gw->getById($row['person']); $role = $role_gw->getCollectionByUser($user); $auth = new T_Auth(T_Auth::TOKEN, $user, $role); // delete the use once token $sql = 'DELETE FROM person_auth_token ' . 'WHERE token=?'; $this->db->master()->query($sql, array($token)); // add a new persistent login token $this->createToken($user, $row['expiry']); return $auth; }
/** * Gets all the countries. * * @return T_Geo_Country[] */ function getAll() { $sql = 'SELECT id,code,name,url FROM country ' . 'ORDER BY name ASC'; $result = $this->db->slave()->query($sql); $world = array(); foreach ($result as $row) { $world[$row['id']] = $this->fromRow($row); } return $world; }
/** * Get all users. * * @param string $order_by optional orderby clause (e.g. 'name', 'name DESC', etc.) * @return T_User[] */ function getAll($order_by = null) { $sql = $this->getSelectSql(); if ($order_by) { $sql .= ' ORDER BY ' . $order_by; } else { $sql .= ' ORDER BY ' . $this->getTable() . '.id'; } $result = $this->db->slave()->query($sql); $users = array(); foreach ($result as $row) { $users[$row['id']] = $this->toUser($row); } return $users; }