14 require_once dirname(dirname(dirname(dirname(__DIR__)))) .
15 "/lib/php/common-db.php";
97 public function getUploads($userId, $groupId, $limit, $page = 1,
98 $uploadId =
null, $options =
null, $recursive =
true, $apiVersion=ApiVersion::V1)
101 $folderId = $options[
"folderId"];
102 if ($folderId ===
null) {
104 $folderId = $users[0]->getRootFolderId();
106 $folders = [$folderId];
108 if ($uploadId !==
null) {
111 $folderId = $users[0]->getRootFolderId();
112 $folders = [$folderId];
116 $tree = $this->folderDao->getFolderStructure($folderId);
117 $folders = array_map(
function ($folder) {
118 return $folder[$this->folderDao::FOLDER_KEY]->getId();
122 $params = [$folders];
123 $partialQuery = $uploadProxy->getFolderPartialQuery($params);
126 $statementCount = __METHOD__ .
".countAllUploads";
127 $statementGet = __METHOD__ .
".getAllUploads.$limit";
128 if ($uploadId !==
null) {
129 $params[] = $uploadId;
130 $where .=
" AND upload.upload_pk = $" . count($params);
131 $statementGet .=
".upload";
132 $statementCount .=
".upload";
134 if (! empty($options[
"name"])) {
135 $params[] = strtolower(
"%" . $options[
"name"] .
"%");
136 $where .=
" AND (LOWER(upload_desc) LIKE $" . count($params) .
137 " OR LOWER(ufile_name) LIKE $" . count($params) .
138 " OR LOWER(upload_filename) LIKE $" . count($params) .
")";
139 $statementGet .=
".name";
140 $statementCount .=
".name";
142 if (! empty($options[
"status"])) {
143 $params[] = $options[
"status"];
144 $where .=
" AND status_fk = $" . count($params);
145 $statementGet .=
".stat";
146 $statementCount .=
".stat";
148 if (! empty($options[
"assignee"])) {
149 $params[] = $options[
"assignee"];
150 $where .=
" AND assignee = $" . count($params);
151 $statementGet .=
".assi";
152 $statementCount .=
".assi";
154 if (! empty($options[
"since"])) {
155 $params[] = $options[
"since"];
156 $where .=
" AND upload_ts >= to_timestamp($" . count($params) .
")";
157 $statementGet .=
".since";
158 $statementCount .=
".since";
160 $sql =
"SELECT count(*) AS cnt FROM $partialQuery $where;";
161 $totalResult = $this->
dbManager->getSingleRow($sql, $params, $statementCount);
162 $totalResult = intval($totalResult[
'cnt']);
163 $totalResult = intval(ceil($totalResult / $limit));
165 $params[] = ($page - 1) * $limit;
168 upload.upload_pk, upload.upload_desc, upload.upload_ts, upload.upload_filename, upload_clearing.assignee
169 FROM $partialQuery $where ORDER BY upload_pk ASC LIMIT $limit OFFSET $" .
170 count($params) .
";";
171 $results = $this->
dbManager->getRows($sql, $params, $statementGet);
173 foreach ($results as $row) {
174 $uploadId = $row[
"upload_pk"];
178 $pfile_sha256 =
null;
180 if ($pfile !==
null) {
181 $pfile_size = $pfile[
'pfile_size'];
182 $pfile_sha1 = $pfile[
'pfile_sha1'];
183 $pfile_md5 = $pfile[
'pfile_md5'];
184 $pfile_sha256 = $pfile[
'pfile_sha256'];
188 if ($folder ===
null) {
191 $folderId = $folder->getId();
192 $folderName = $folder->getName();
194 $hash =
new Hash($pfile_sha1, $pfile_md5, $pfile_sha256, $pfile_size);
195 $upload =
new Upload($folderId, $folderName, $uploadId,
196 $row[
"upload_desc"], $row[
"upload_filename"], $row[
"upload_ts"], $row[
"assignee"], $hash);
197 if (! empty($row[
"assignee"]) && $row[
"assignee"] != 1) {
198 $upload->setAssigneeDate($this->uploadDao->getAssigneeDate($uploadId));
200 $upload->setClosingDate($this->uploadDao->getClosedDate($uploadId));
201 $uploads[] = $upload->getArray($apiVersion);
203 return [$totalResult, $uploads];
215 "SELECT DISTINCT ufile_name FROM uploadtree
216 WHERE uploadtree_pk=$1", [$uploadTreePk])[
"ufile_name"];
229 return (0 < (intval($this->
getDbManager()->getSingleRow(
"SELECT COUNT(*)
230 FROM $tableName WHERE $idRowName = $1", [$id],
231 __METHOD__ . $tableName . $idRowName)[
"count"])));
241 require_once dirname(dirname(__DIR__)) .
"/user-del-helper.php";
255 $usersSQL =
"SELECT user_pk, user_name, user_desc, user_email,
256 email_notify, root_folder_fk, group_fk, user_perm, user_agent_list, default_bucketpool_fk FROM users;";
257 $statement = __METHOD__ .
".getAllUsers";
259 $usersSQL =
"SELECT user_pk, user_name, user_desc, user_email,
260 email_notify, root_folder_fk, group_fk, user_perm, user_agent_list, default_bucketpool_fk FROM users
261 WHERE user_pk = $1;";
262 $statement = __METHOD__ .
".getSpecificUser";
266 $result = $this->
dbManager->getRows($usersSQL, [], $statement);
268 $result = $this->
dbManager->getRows($usersSQL, [$id], $statement);
272 foreach ($result as $row) {
275 ($row[
"user_pk"] == $currentUser)) {
276 $user =
new User($row[
"user_pk"], $row[
"user_name"], $row[
"user_desc"],
277 $row[
"user_email"], $row[
"user_perm"], $row[
"root_folder_fk"],
278 $row[
"email_notify"], $row[
"user_agent_list"], $row[
"group_fk"], $row[
"default_bucketpool_fk"]);
280 $user =
new User($row[
"user_pk"], $row[
"user_name"], $row[
"user_desc"],
281 null,
null,
null,
null,
null,
null);
298 return "WITH job_with_status_cte AS (
299 SELECT j.job_pk, j.job_queued, j.job_name, j.job_upload_fk, j.job_user_fk, j.job_group_fk,
301 WHEN COUNT(CASE WHEN jq.jq_endtext = 'Failed' THEN 1 END) > 0 THEN 'Failed'
302 WHEN COUNT(CASE WHEN jq.jq_endtext in ('Started', 'Restarted', 'Paused') THEN 1 END) > 0 THEN 'Processing'
303 WHEN COUNT(CASE WHEN jq.jq_endtext IS NULL THEN 1 END) > 0 THEN 'Queued'
307 LEFT JOIN jobqueue jq ON j.job_pk=jq.jq_job_fk
308 GROUP BY j.job_pk, j.job_queued, j.job_name, j.job_upload_fk, j.job_user_fk, j.job_group_fk
328 public function getJobs($id =
null, $status =
null, $sort =
"ASC", $limit = 0, $page = 1, $uploadId =
null, $userId =
null)
331 $jobSQL =
"$jobsWithStatusCteSQL SELECT * FROM job_with_status_cte";
332 $totalJobSql =
"$jobsWithStatusCteSQL SELECT count(*) AS cnt FROM job_with_status_cte";
337 $statement = $userId !==
null ? __METHOD__ .
".getUserJobs" : __METHOD__ .
".getJobs";
338 $countStatement = __METHOD__ .
".getJobCount";
342 $filter[] =
"job_pk = $" . count($params);
343 $statement .=
".withJobFilter";
344 $countStatement .=
".withJobFilter";
345 } elseif ($uploadId !==
null) {
346 $params[] = $uploadId;
347 $filter[] =
"job_upload_fk = $" . count($params);
348 $statement .=
".withUploadFilter";
349 $countStatement .=
".withUploadFilter";
353 if ($userId !==
null) {
355 $filter[] =
"job_user_fk = $" . count($params);
359 if ($status !==
null && in_array($status, [
"Failed",
"Processing",
"Queued",
"Completed"])) {
361 $filter[] =
"job_status = $" . count($params);
364 $filterSQL = $filter ?
"WHERE " . implode(
" AND ", $filter) :
"";
367 $result = $this->
dbManager->getSingleRow(
"$totalJobSql $filterSQL;", $params,
369 $totalResult = $result[
'cnt'];
373 $orderBy = in_array($sort, [
"ASC",
"DESC"]) ?
"ORDER BY job_queued $sort" :
"";
375 $offset = ($page - 1) * $limit;
378 $pagination =
"LIMIT $" . count($params);
380 $pagination .=
" OFFSET $" . count($params);
381 $statement .=
".withLimit";
382 $totalResult = ceil($totalResult / $limit);
389 $result = $this->
dbManager->getRows(
"$jobSQL $filterSQL $orderBy $pagination;", $params,
391 foreach ($result as $row) {
392 $job =
new Job($row[
"job_pk"]);
393 $job->setName($row[
"job_name"]);
394 $job->setQueueDate($row[
"job_queued"]);
395 $job->setUploadId($row[
"job_upload_fk"]);
396 $job->setUserId($row[
"job_user_fk"]);
397 $job->setGroupId($row[
"job_group_fk"]);
398 $job->setStatus($row[
"job_status"]);
401 return [$jobs, $totalResult];
417 public function getUserJobs($userId =
null, $status =
null, $sort =
"ASC", $limit = 0, $page = 1)
419 return $this->
getJobs(
null, $status, $sort, $limit, $page,
null, $userId);
431 $sql =
"SELECT token_key, client_id, created_on, expire_on, user_fk, active, token_scope " .
432 "FROM personal_access_tokens WHERE pat_pk = $1;";
433 return $this->
dbManager->getSingleRow($sql, [$tokenId],
434 __METHOD__ .
".getTokenSecret");
444 $sql =
"UPDATE personal_access_tokens SET active = false WHERE pat_pk = $1;";
445 $this->
dbManager->getSingleRow($sql, [$tokenId], __METHOD__ .
".invalidateToken");
467 "Already have a token with same name.", 409);
472 $sql =
"INSERT INTO personal_access_tokens " .
473 "(user_fk, created_on, expire_on, token_scope, token_name, token_key, active) " .
474 "VALUES ($1, NOW(), $2, $3, $4, $5, true) " .
475 "RETURNING pat_pk || '.' || user_fk AS jti, created_on";
476 return $this->
dbManager->getSingleRow($sql, [
477 $userId, $expire, $scope, $name, $key
478 ], __METHOD__ .
".insertNewToken");
491 $sql =
"INSERT INTO personal_access_tokens" .
492 "(user_fk, created_on, token_scope, token_name, client_id, active)" .
493 "VALUES ($1, NOW(), $2, $3, $4, true);";
495 $userId, $scope, $name, $clientId
509 $tokenIsUnique =
true;
510 $sql =
"SELECT count(*) AS cnt FROM personal_access_tokens " .
511 "WHERE user_fk = $1 AND token_name = $2;";
512 $result = $this->
dbManager->getSingleRow($sql, [$userId, $tokenName],
513 __METHOD__ .
".checkTokenNameUnique");
514 if ($result[
'cnt'] != 0) {
515 $tokenIsUnique =
false;
517 return $tokenIsUnique;
530 $tokenIsUnique =
true;
531 $sql =
"SELECT count(*) AS cnt FROM personal_access_tokens " .
532 "WHERE user_fk = $1 AND token_key = $2;";
533 $result = $this->
dbManager->getSingleRow($sql, [$userId, $tokenKey],
534 __METHOD__ .
".checkTokenKeyUnique");
535 if ($result[
'cnt'] != 0) {
536 $tokenIsUnique =
false;
538 return $tokenIsUnique;
548 $sql =
"SELECT conf_value FROM sysconfig WHERE variablename = $1;";
549 $result = $this->
dbManager->getSingleRow($sql, [
"PATMaxExipre"],
550 __METHOD__ .
".tokenMaxValidFromSysconfig");
552 if (! empty($result[
'conf_value'])) {
553 $validity = intval($result[
'conf_value']);
565 $sql =
"SELECT pfile.* FROM upload INNER JOIN pfile " .
566 "ON pfile_fk = pfile_pk WHERE upload_pk = $1;";
567 $result = $this->
dbManager->getSingleRow($sql, [$uploadId],
568 __METHOD__ .
".getPfileFromUpload");
569 if (! empty($result)) {
582 $contentId = $this->folderDao->getFolderContentsId($uploadId,
583 $this->folderDao::MODE_UPLOAD);
584 $content = $this->folderDao->getContent($contentId);
585 return $this->folderDao->getFolder($content[
'parent_fk']);
600 $statementName = __METHOD__;
601 $rfTable =
'license_all';
602 $options = [
'columns' => [
'rf_pk',
'rf_shortname',
'rf_fullname',
'rf_text',
603 'rf_url',
'rf_risk',
'group_fk']];
605 $options[
'extraCondition'] =
"rf_active = '" .
606 $this->
dbManager->booleanToDb($active) .
"'";
608 if ($kind ==
"candidate") {
609 $options[
'diff'] =
true;
610 } elseif ($kind ==
"main") {
614 $withCte = $licenseViewDao->asCTE();
616 return $this->
dbManager->getRows($withCte .
617 " SELECT * FROM $rfTable ORDER BY LOWER(rf_shortname) " .
618 "LIMIT $1 OFFSET $2;",
619 [$limit, ($page - 1) * $limit], $statementName);
631 $sql =
"SELECT sum(cnt) AS total FROM (";
632 $mainLicSql =
" SELECT count(*) AS cnt FROM ONLY license_ref ";
633 $candidateLicSql =
" SELECT count(*) AS cnt FROM license_candidate WHERE group_fk = $1";
636 if ($kind ==
"main") {
638 } elseif ($kind ==
"candidate") {
639 $sql .= $candidateLicSql;
640 $params[] = $groupId;
642 $sql .= $mainLicSql .
" UNION ALL " . $candidateLicSql;
643 $params[] = $groupId;
645 $sql .=
") as all_lic;";
647 $statement = __METHOD__ .
".getLicenseCount.$kind";
648 $result = $this->
dbManager->getSingleRow($sql, $params, $statement);
649 return intval($result[
'total']);
658 public function getTokenIdFromClientId($clientId)
660 $sql =
"SELECT pat_pk FROM personal_access_tokens " .
661 "WHERE client_id = $1;";
662 $result = $this->
dbManager->getSingleRow($sql, [$clientId], __METHOD__);
663 if (!empty($result)) {
664 return $result[
'pat_pk'];
Contains the constants and helpers for authentication of user.
static getUserId()
Get the current user's id.
static isAdmin()
Check if user is admin.
Exception when a token has duplicate key for same user.
Exception when a token has duplicate name for same user.
Provides helper methods to access database for REST api.
getJobs($id=null, $status=null, $sort="ASC", $limit=0, $page=1, $uploadId=null, $userId=null)
Get the recent jobs.
invalidateToken($tokenId)
checkTokenKeyUnique($userId, $tokenKey)
getFilenameFromUploadTree($uploadTreePk)
getLicensesPaginated($page, $limit, $kind, $groupId, $active)
doesIdExist($tableName, $idRowName, $id)
addNewClient($name, $userId, $clientId, $scope)
getLicenseCount($kind, $groupId)
getJobStatusCteSQLStatement()
getPfileInfoForUpload($uploadId)
getUserJobs($userId=null, $status=null, $sort="ASC", $limit=0, $page=1)
Get the recent jobs created by an user.
checkTokenNameUnique($userId, $tokenName)
getFolderForUpload($uploadId)
insertNewTokenKey($userId, $expire, $scope, $name, $key)
__construct(DbManager $dbManager, FolderDao $folderDao, UploadDao $uploadDao)
getUploads($userId, $groupId, $limit, $page=1, $uploadId=null, $options=null, $recursive=true, $apiVersion=ApiVersion::V1)
Hash model holding information about file like checksums and size.
Model class to hold Upload info.
Model to hold user information.
fo_dbManager * dbManager
fo_dbManager object
FUNCTION char * strtoupper(char *s)
Helper function to upper case a string.