/** * * */ function getUserResByTag(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks) { $r = new folksoResponse(); try { $u = $fks->userSession(null); if (!$u instanceof folksoUser && !$q->is_param('user')) { return $r->setError(404, 'No user'); } elseif ($q->is_param('user')) { $u = new folksoUser($dbc); // we create a user object anyway $u->setUid($q->get_param('user')); if (!$u->exists($q->get_param('user'))) { return $r->setError(404, 'Missing or invalid user'); } } $i = new folksoDBinteract($dbc); $uq = new folksoUserQuery(); $sql = $uq->resourcesByTag($q->tag, $u->userid); $i->query($sql); /* these are inside the try block because exists() hits the DB */ if ($i->rowCount == 0) { if (isset($u->nick) || $u->exists()) { return $r->setOk(204, 'User has no resources with this tag'); } else { // no longer necessary return $r->setError(404, 'Unknown user'); } } } catch (dbException $e) { return $r->handleDBexception($e); } catch (badUseridException $e) { return $r->handleDBexception($e); // TODO: update this with new class } $r->setOk(200, 'Found'); $df = new folksoDisplayFactory(); if ($q->content_type() == 'json') { $dd = new folksoDataJson('resid', 'url', 'title'); } else { $dd = $df->ResourceList('xml'); } $r->t($dd->startform()); while ($row = $i->result->fetch_object()) { $r->t($dd->line($row->id, htmlspecialchars($row->uri_raw), htmlspecialchars($row->title))); } $r->t($dd->endform()); return $r; }
function testExists() { $u = new folksoUser($this->dbc); $this->assertTrue($u->exists('marcelp-2010-001')); $this->assertEqual($u->userid, 'marcelp-2010-001'); }
/** * Load user data from session id (cookie). Retuns folksoUser * obj. Caches the fkUser object. We might consider a "force reload" * option if there were a reason for it. This also means that if the * arguments (sid) change, the data returned will not. This should * not be a problem though. * * @param $sid Session ID. * @return folksoUser obj or false if user not found */ public function userSession($sid = null, $service = null, $right = null) { if ($this->user instanceof folksoUser) { return $this->user; } $sid = $sid ? $sid : $this->sessionId; if ($this->validateSid($sid) === false) { return false; // exception? } $i = new folksoDBinteract($this->dbc); $sql = ''; if (is_null($service) || is_null($right)) { $sql = 'select u.nick as nick, u.firstname as firstname, ' . ' u.lastname as lastname, u.email as email, u.userid as userid' . ' from sessions s ' . ' join users u on u.userid = s.userid ' . " where s.token = '" . $sid . "'" . " and s.started > now() - 1209600 "; } else { $sql = 'select u.nick as nick, u.firstname as firstname, ' . ' u.lastname as lastname, u.email as email, u.userid as userid, ' . ' dr.rightid, dr.service ' . ' from sessions s ' . ' join users u on u.userid = s.userid ' . ' left join users_rights ur on ur.userid = s.userid ' . ' left join rights dr on dr.rightid = ur.rightid ' . " where s.token = '" . $i->dbescape($sid) . "' " . " and dr.rightid = '" . $i->dbescape($right) . "' " . " and s.started > now() - 1209600 "; } $this->debug = $sql; $i->query($sql); if ($i->result_status == 'OK') { $u = new folksoUser($this->dbc); $res = $i->result->fetch_object(); $u->loadUser(array('nick' => $res->nick, 'firstname' => $res->firstname, 'lastname' => $res->lastname, 'email' => $res->email, 'userid' => $res->userid)); if ($right && $service && $res->rightid == $right && $res->service == $service) { $this->debug2 = 'we r here'; $u->rights->addRight(new folksoRight($res->service, $res->rightid)); } return $u; } else { return false; } }