25 require_once dirname(dirname(dirname(dirname(__DIR__)))) .
26 "/lib/php/common-db.php";
82 if ($uploadId == null) {
84 upload.upload_pk, upload.upload_desc, upload.upload_ts, upload.upload_filename, 85 folder.folder_pk, folder.folder_name, pfile.pfile_size, pfile.pfile_sha1 87 INNER JOIN folderlist ON folderlist.upload_pk = upload.upload_pk 88 INNER JOIN folder ON folder.folder_pk = folderlist.parent 89 INNER JOIN pfile ON pfile.pfile_pk = upload.pfile_fk 90 WHERE upload.user_fk = $1 91 ORDER BY upload.upload_pk;";
92 $statementName = __METHOD__ .
".getAllUploads";
96 upload.upload_pk, upload.upload_desc, upload.upload_ts, upload.upload_filename, 97 folder.folder_pk, folder.folder_name, pfile.pfile_size, pfile.pfile_sha1 99 INNER JOIN folderlist ON folderlist.upload_pk = upload.upload_pk 100 INNER JOIN folder ON folder.folder_pk = folderlist.parent 101 INNER JOIN pfile ON pfile.pfile_pk = upload.pfile_fk 102 WHERE upload.user_fk = $1 103 AND upload.upload_pk = $2 104 ORDER BY upload.upload_pk;";
105 $statementName = __METHOD__ .
".getSpecificUpload";
106 $params = [$userId,$uploadId];
108 $result = $this->
dbManager->getRows($sql, $params, $statementName);
110 foreach ($result as $row) {
111 $upload =
new Upload($row[
"folder_pk"], $row[
"folder_name"],
112 $row[
"upload_pk"], $row[
"upload_desc"], $row[
"upload_filename"],
113 $row[
"upload_ts"], $row[
"pfile_size"], $row[
"pfile_sha1"]);
114 array_push($uploads, $upload->getArray());
128 'SELECT DISTINCT ufile_name FROM uploadtree 129 WHERE uploadtree_pk=' . pg_escape_string($uploadTreePk))[
"ufile_name"];
142 return (0 < (intval($this->
getDbManager()->getSingleRow(
"SELECT COUNT(*) 143 FROM $tableName WHERE $idRowName= " . pg_escape_string($id))[
"count"])));
153 require_once dirname(dirname(__DIR__)) .
"/user-del-helper.php";
167 $usersSQL =
"SELECT user_pk, user_name, user_desc, user_email, 168 email_notify, root_folder_fk, user_perm, user_agent_list FROM users;";
169 $statement = __METHOD__ .
".getAllUsers";
171 $usersSQL =
"SELECT user_pk, user_name, user_desc, user_email, 172 email_notify, root_folder_fk, user_perm, user_agent_list FROM users 173 WHERE user_pk = $1;";
174 $statement = __METHOD__ .
".getSpecificUser";
178 $result = $result = $this->
dbManager->getRows($usersSQL, [], $statement);
180 $result = $result = $this->
dbManager->getRows($usersSQL, [$id],
185 foreach ($result as $row) {
188 ($row[
"user_pk"] == $currentUser)) {
189 $user =
new User($row[
"user_pk"], $row[
"user_name"], $row[
"user_desc"],
190 $row[
"user_email"], $row[
"user_perm"], $row[
"root_folder_fk"],
191 $row[
"email_notify"], $row[
"user_agent_list"]);
193 $user =
new User($row[
"user_pk"], $row[
"user_name"], $row[
"user_desc"],
194 null, null, null, null, null);
196 $users[] = $user->getArray();
215 public function getJobs($id = null, $limit = 0, $page = 1, $uploadId = null)
217 $jobSQL =
"SELECT job_pk, job_queued, job_name, job_upload_fk," .
218 " job_user_fk, job_group_fk FROM job";
219 $totalJobSql =
"SELECT count(*) AS cnt FROM job";
225 $statement = __METHOD__ .
".getJobs";
226 $countStatement = __METHOD__ .
".getJobCount";
228 if ($uploadId !== null) {
229 $params[] = $uploadId;
230 $filter =
"WHERE job_upload_fk = $" . count($params);
231 $statement .=
".withUploadFilter";
232 $countStatement .=
".withUploadFilter";
236 $filter =
"WHERE job_pk = $" . count($params);
237 $statement .=
".withJobFilter";
238 $countStatement .=
".withJobFilter";
241 $result = $this->
dbManager->getSingleRow(
"$totalJobSql $filter;", $params,
244 $totalResult = $result[
'cnt'];
246 $offset = ($page - 1) * $limit;
249 $pagination =
"LIMIT $" . count($params);
251 $pagination .=
" OFFSET $" . count($params);
252 $statement .=
".withLimit";
253 $totalResult = floor($totalResult / $limit) + 1;
259 $result = $this->
dbManager->getRows(
"$jobSQL $filter $pagination;", $params,
261 foreach ($result as $row) {
262 $job =
new Job($row[
"job_pk"]);
263 $job->setName($row[
"job_name"]);
264 $job->setQueueDate($row[
"job_queued"]);
265 $job->setUploadId($row[
"job_upload_fk"]);
266 $job->setUserId($row[
"job_user_fk"]);
267 $job->setGroupId($row[
"job_group_fk"]);
270 return [$jobs, $totalResult];
282 $sql =
"SELECT token_key, created_on, expire_on, user_fk, active, token_scope " .
283 "FROM personal_access_tokens WHERE pat_pk = $1;";
284 return $this->
dbManager->getSingleRow($sql, [$tokenId],
285 __METHOD__ .
".getTokenSecret");
295 $sql =
"UPDATE personal_access_tokens SET active = false WHERE pat_pk = $1;";
296 $this->
dbManager->getSingleRow($sql, [$tokenId], __METHOD__ .
".invalidateToken");
318 "Already have a token with same name.", 409);
323 $sql =
"INSERT INTO personal_access_tokens " .
324 "(user_fk, created_on, expire_on, token_scope, token_name, token_key, active) " .
325 "VALUES ($1, NOW(), $2, $3, $4, $5, true) " .
326 "RETURNING pat_pk || '.' || user_fk AS jti, created_on";
327 return $this->
dbManager->getSingleRow($sql, [
328 $userId, $expire, $scope, $name, $key
329 ], __METHOD__ .
".insertNewToken");
342 $tokenIsUnique =
true;
343 $sql =
"SELECT count(*) AS cnt FROM personal_access_tokens " .
344 "WHERE user_fk = $1 AND token_name = $2;";
345 $result = $this->
dbManager->getSingleRow($sql, [$userId, $tokenName],
346 __METHOD__ .
".checkTokenNameUnique");
347 if ($result[
'cnt'] != 0) {
348 $tokenIsUnique =
false;
350 return $tokenIsUnique;
363 $tokenIsUnique =
true;
364 $sql =
"SELECT count(*) AS cnt FROM personal_access_tokens " .
365 "WHERE user_fk = $1 AND token_key = $2;";
366 $result = $this->
dbManager->getSingleRow($sql, [$userId, $tokenKey],
367 __METHOD__ .
".checkTokenKeyUnique");
368 if ($result[
'cnt'] != 0) {
369 $tokenIsUnique =
false;
371 return $tokenIsUnique;
381 $sql =
"SELECT conf_value FROM sysconfig WHERE variablename = $1;";
382 $result = $this->
dbManager->getSingleRow($sql, [
"PATMaxExipre"],
383 __METHOD__ .
".tokenMaxValidFromSysconfig");
385 if (! empty($result[
'conf_value'])) {
386 $validity = intval($result[
'conf_value']);
Exception when a token has duplicate name for same user.
Model class to hold Upload info.
checkTokenKeyUnique($userId, $tokenKey)
static getUserId()
Get the current user's id.
static isAdmin()
Check if user is admin.
getFilenameFromUploadTree($uploadTreePk)
doesIdExist($tableName, $idRowName, $id)
invalidateToken($tokenId)
Exception when a token has duplicate key for same user.
insertNewTokenKey($userId, $expire, $scope, $name, $key)
__construct(DbManager $dbManager)
getUploads($userId, $uploadId=null)
checkTokenNameUnique($userId, $tokenName)
getJobs($id=null, $limit=0, $page=1, $uploadId=null)
Get the recent jobs.
fo_dbManager * dbManager
fo_dbManager object
Model to hold user information.
Provides helper methods to access database for REST api.