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);
302 public function getJobs($id =
null, $limit = 0, $page = 1, $uploadId =
null)
304 $jobSQL =
"SELECT job_pk, job_queued, job_name, job_upload_fk," .
305 " job_user_fk, job_group_fk FROM job";
306 $totalJobSql =
"SELECT count(*) AS cnt FROM job";
312 $statement = __METHOD__ .
".getJobs";
313 $countStatement = __METHOD__ .
".getJobCount";
315 if ($uploadId !==
null) {
316 $params[] = $uploadId;
317 $filter =
"WHERE job_upload_fk = $" . count($params);
318 $statement .=
".withUploadFilter";
319 $countStatement .=
".withUploadFilter";
323 $filter =
"WHERE job_pk = $" . count($params);
324 $statement .=
".withJobFilter";
325 $countStatement .=
".withJobFilter";
328 $result = $this->
dbManager->getSingleRow(
"$totalJobSql $filter;", $params,
331 $totalResult = $result[
'cnt'];
333 $offset = ($page - 1) * $limit;
336 $pagination =
"LIMIT $" . count($params);
338 $pagination .=
" OFFSET $" . count($params);
339 $statement .=
".withLimit";
340 $totalResult = ceil($totalResult / $limit);
346 $result = $this->
dbManager->getRows(
"$jobSQL $filter $pagination;", $params,
348 foreach ($result as $row) {
349 $job =
new Job($row[
"job_pk"]);
350 $job->setName($row[
"job_name"]);
351 $job->setQueueDate($row[
"job_queued"]);
352 $job->setUploadId($row[
"job_upload_fk"]);
353 $job->setUserId($row[
"job_user_fk"]);
354 $job->setGroupId($row[
"job_group_fk"]);
357 return [$jobs, $totalResult];
374 public function getUserJobs($id =
null, $uid=
null, $limit = 0, $page = 1, $uploadId =
null)
376 $jobSQL =
"SELECT job_pk, job_queued, job_name, job_upload_fk," .
377 " job_user_fk, job_group_fk FROM job WHERE job_user_fk=$1";
378 $totalJobSql =
"SELECT count(*) AS cnt FROM job WHERE job_user_fk=$1";
383 $statement = __METHOD__ .
".getUserJobs";
384 $countStatement = __METHOD__ .
".getJobCount";
386 if ($uploadId !==
null) {
387 $params[] = $uploadId;
388 $filter =
"WHERE job_upload_fk = $" . count($params);
389 $statement .=
".withUploadFilter";
390 $countStatement .=
".withUploadFilter";
394 $filter =
"WHERE job_pk = $" . count($params);
395 $statement .=
".withJobFilter";
396 $countStatement .=
".withJobFilter";
399 $result = $this->
dbManager->getSingleRow(
"$totalJobSql $filter;", $params,
402 $totalResult = $result[
'cnt'];
404 $offset = ($page - 1) * $limit;
407 $pagination =
"LIMIT $" . count($params);
409 $pagination .=
" OFFSET $" . count($params);
410 $statement .=
".withLimit";
411 $totalResult = ceil($totalResult / $limit);
417 $result = $this->
dbManager->getRows(
"$jobSQL $filter $pagination;", $params,
419 foreach ($result as $row) {
420 $job =
new Job($row[
"job_pk"]);
421 $job->setName($row[
"job_name"]);
422 $job->setQueueDate($row[
"job_queued"]);
423 $job->setUploadId($row[
"job_upload_fk"]);
424 $job->setUserId($row[
"job_user_fk"]);
425 $job->setGroupId($row[
"job_group_fk"]);
428 return [$jobs, $totalResult];
440 $sql =
"SELECT token_key, client_id, created_on, expire_on, user_fk, active, token_scope " .
441 "FROM personal_access_tokens WHERE pat_pk = $1;";
442 return $this->
dbManager->getSingleRow($sql, [$tokenId],
443 __METHOD__ .
".getTokenSecret");
453 $sql =
"UPDATE personal_access_tokens SET active = false WHERE pat_pk = $1;";
454 $this->
dbManager->getSingleRow($sql, [$tokenId], __METHOD__ .
".invalidateToken");
476 "Already have a token with same name.", 409);
481 $sql =
"INSERT INTO personal_access_tokens " .
482 "(user_fk, created_on, expire_on, token_scope, token_name, token_key, active) " .
483 "VALUES ($1, NOW(), $2, $3, $4, $5, true) " .
484 "RETURNING pat_pk || '.' || user_fk AS jti, created_on";
485 return $this->
dbManager->getSingleRow($sql, [
486 $userId, $expire, $scope, $name, $key
487 ], __METHOD__ .
".insertNewToken");
500 $sql =
"INSERT INTO personal_access_tokens" .
501 "(user_fk, created_on, token_scope, token_name, client_id, active)" .
502 "VALUES ($1, NOW(), $2, $3, $4, true);";
504 $userId, $scope, $name, $clientId
518 $tokenIsUnique =
true;
519 $sql =
"SELECT count(*) AS cnt FROM personal_access_tokens " .
520 "WHERE user_fk = $1 AND token_name = $2;";
521 $result = $this->
dbManager->getSingleRow($sql, [$userId, $tokenName],
522 __METHOD__ .
".checkTokenNameUnique");
523 if ($result[
'cnt'] != 0) {
524 $tokenIsUnique =
false;
526 return $tokenIsUnique;
539 $tokenIsUnique =
true;
540 $sql =
"SELECT count(*) AS cnt FROM personal_access_tokens " .
541 "WHERE user_fk = $1 AND token_key = $2;";
542 $result = $this->
dbManager->getSingleRow($sql, [$userId, $tokenKey],
543 __METHOD__ .
".checkTokenKeyUnique");
544 if ($result[
'cnt'] != 0) {
545 $tokenIsUnique =
false;
547 return $tokenIsUnique;
557 $sql =
"SELECT conf_value FROM sysconfig WHERE variablename = $1;";
558 $result = $this->
dbManager->getSingleRow($sql, [
"PATMaxExipre"],
559 __METHOD__ .
".tokenMaxValidFromSysconfig");
561 if (! empty($result[
'conf_value'])) {
562 $validity = intval($result[
'conf_value']);
574 $sql =
"SELECT pfile.* FROM upload INNER JOIN pfile " .
575 "ON pfile_fk = pfile_pk WHERE upload_pk = $1;";
576 $result = $this->
dbManager->getSingleRow($sql, [$uploadId],
577 __METHOD__ .
".getPfileFromUpload");
578 if (! empty($result)) {
591 $contentId = $this->folderDao->getFolderContentsId($uploadId,
592 $this->folderDao::MODE_UPLOAD);
593 $content = $this->folderDao->getContent($contentId);
594 return $this->folderDao->getFolder($content[
'parent_fk']);
609 $statementName = __METHOD__;
610 $rfTable =
'license_all';
611 $options = [
'columns' => [
'rf_pk',
'rf_shortname',
'rf_fullname',
'rf_text',
612 'rf_url',
'rf_risk',
'group_fk']];
614 $options[
'extraCondition'] =
"rf_active = '" .
615 $this->
dbManager->booleanToDb($active) .
"'";
617 if ($kind ==
"candidate") {
618 $options[
'diff'] =
true;
619 } elseif ($kind ==
"main") {
623 $withCte = $licenseViewDao->asCTE();
625 return $this->
dbManager->getRows($withCte .
626 " SELECT * FROM $rfTable ORDER BY LOWER(rf_shortname) " .
627 "LIMIT $1 OFFSET $2;",
628 [$limit, ($page - 1) * $limit], $statementName);
640 $sql =
"SELECT sum(cnt) AS total FROM (";
641 $mainLicSql =
" SELECT count(*) AS cnt FROM ONLY license_ref ";
642 $candidateLicSql =
" SELECT count(*) AS cnt FROM license_candidate WHERE group_fk = $1";
645 if ($kind ==
"main") {
647 } elseif ($kind ==
"candidate") {
648 $sql .= $candidateLicSql;
649 $params[] = $groupId;
651 $sql .= $mainLicSql .
" UNION ALL " . $candidateLicSql;
652 $params[] = $groupId;
654 $sql .=
") as all_lic;";
656 $statement = __METHOD__ .
".getLicenseCount.$kind";
657 $result = $this->
dbManager->getSingleRow($sql, $params, $statement);
658 return intval($result[
'total']);
667 public function getTokenIdFromClientId($clientId)
669 $sql =
"SELECT pat_pk FROM personal_access_tokens " .
670 "WHERE client_id = $1;";
671 $result = $this->
dbManager->getSingleRow($sql, [$clientId], __METHOD__);
672 if (!empty($result)) {
673 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.
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)
getPfileInfoForUpload($uploadId)
getUserJobs($id=null, $uid=null, $limit=0, $page=1, $uploadId=null)
Get the recent jobs created by an user.
getJobs($id=null, $limit=0, $page=1, $uploadId=null)
Get the recent jobs.
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