diff --git a/src/lib/php/Dao/ShowJobsDao.php b/src/lib/php/Dao/ShowJobsDao.php old mode 100755 new mode 100644 index 5bf877b410..0a0ce521f3 --- a/src/lib/php/Dao/ShowJobsDao.php +++ b/src/lib/php/Dao/ShowJobsDao.php @@ -43,6 +43,13 @@ function __construct(DbManager $dbManager, UploadDao $uploadDao) function uploads2Jobs($upload_pks, $page = 0) { $jobArray = array(); + + // only use the uploads the user / group has access to + $upload_pks = array_filter($upload_pks, function($upload_pk) { + return $upload_pk !== null && $this->uploadDao->isAccessible($upload_pk, Auth::getGroupId()); + }); + + // get count of upload pks, return empty array if count equals 0 $jobCount = count($upload_pks); if ($jobCount == 0) { return $jobArray; @@ -101,19 +108,12 @@ public function myJobs($allusers, $page = 0) $allusers_str = ($allusers == 0) ? "job_user_fk='" . Auth::getUserId() . "' and " : ""; - $statementName = __METHOD__ . ".countJobs." . $allusers_str; - $sql = "SELECT count(*) AS cnt FROM job WHERE $allusers_str " . - "job_queued >= (now() - interval '" . $this->nhours . " hours');"; - - $countJobs = $this->dbManager->getSingleRow($sql, [], $statementName)['cnt']; - $totalPages = floor($countJobs / $this->maxJobsPerPage); - $statementName = __METHOD__ . "." . $allusers_str; $this->dbManager->prepare($statementName, "SELECT job_pk, job_upload_fk FROM job " . "WHERE $allusers_str " . "job_queued >= (now() - interval '" . $this->nhours . " hours') " . - "ORDER BY job_queued DESC OFFSET $1 LIMIT " . $this->maxJobsPerPage); - $result = $this->dbManager->execute($statementName, [$offset]); + "ORDER BY job_queued DESC"); + $result = $this->dbManager->execute($statementName); while ($row = $this->dbManager->fetchArray($result)) { if (! empty($row['job_upload_fk'])) { $uploadIsAccessible = $this->uploadDao->isAccessible( @@ -124,9 +124,16 @@ public function myJobs($allusers, $page = 0) } $jobArray[] = $row['job_pk']; } + + // calculate total pages for jobs accessible to current group + $totalPages = floor(count($jobArray) / $this->maxJobsPerPage); + + // get jobs for current page only + $pageJobs = array_slice($jobArray, $offset, $this->maxJobsPerPage); + $this->dbManager->freeResult($result); - return array($jobArray, $totalPages); + return array($pageJobs, $totalPages); } /* myJobs() */ /** diff --git a/src/lib/php/Dao/test/ShowJobsDaoTest.php b/src/lib/php/Dao/test/ShowJobsDaoTest.php index cfbd1076b4..526e77251e 100644 --- a/src/lib/php/Dao/test/ShowJobsDaoTest.php +++ b/src/lib/php/Dao/test/ShowJobsDaoTest.php @@ -77,38 +77,53 @@ protected function tearDown() : void public function testUploads2Jobs() { + $groupId = 2; + $GLOBALS['SysConf']['auth'][Auth::GROUP_ID] = $groupId; + $GLOBALS['SysConf']['auth'][Auth::USER_ID] = 1; + $this->uploadPermissionDao->shouldReceive('isAccessible')->withArgs(array(anything(),$groupId)) + ->andReturnUsing(function($upload,$group) + { + return ($upload==1 || $upload==2 || $upload==3 || $upload==4 || $upload==5); + }); + $jobs = array(3=>2, 4=>3, 5=>5, 6=>8%6, 7=>13%6, 8=>21%6); foreach ($jobs as $jobId => $jobUpload) { $this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload)); } - $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao'); - $showJobDao = new ShowJobsDao($this->dbManager,$uploadDao); - $jobsWithoutUpload = $showJobDao->uploads2Jobs(array()); + $jobsWithoutUpload = $this->showJobsDao->uploads2Jobs(array()); assertThat($jobsWithoutUpload, is(emptyArray())); - $jobsWithUploadIdOne = $showJobDao->uploads2Jobs(array(1)); + $jobsWithUploadIdOne = $this->showJobsDao->uploads2Jobs(array(1)); assertThat($jobsWithUploadIdOne, equalTo(array(array(1,7),0))); - $jobsAtAll = $showJobDao->uploads2Jobs(array(1,2,3,4,5)); + $jobsAtAll = $this->showJobsDao->uploads2Jobs(array(1,2,3,4,5)); assertThat($jobsAtAll, equalTo(array(array(1,7, 2,3,6, 4,8, 5),0))); - $jobsWithUploadFour = $showJobDao->uploads2Jobs(array(4)); + $jobsWithUploadFour = $this->showJobsDao->uploads2Jobs(array(4)); assertThat($jobsWithUploadFour[0], is(emptyArray())); } public function testUploads2JobsPaged() { + $groupId = 2; + $GLOBALS['SysConf']['auth'][Auth::GROUP_ID] = $groupId; + $GLOBALS['SysConf']['auth'][Auth::USER_ID] = 1; + + $this->uploadPermissionDao->shouldReceive('isAccessible')->withArgs(array(anything(),$groupId)) + ->andReturnUsing(function($upload,$group) + { + return range(1, 17); + }); + $jobs = array_combine(range(3,13),range(3,13)); foreach ($jobs as $jobId => $jobUpload) { $this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload)); } - $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao'); - $showJobDao = new ShowJobsDao($this->dbManager,$uploadDao); - $jobsPage1 = $showJobDao->uploads2Jobs(range(1,17),0); + $jobsPage1 = $this->showJobsDao->uploads2Jobs(range(1,17),0); assertThat($jobsPage1[0], arrayWithSize(10)); assertThat($jobsPage1[1], is(1)); - $jobsPage2 = $showJobDao->uploads2Jobs(array_combine(range(10,16),range(11,17)),1); + $jobsPage2 = $this->showJobsDao->uploads2Jobs(array_combine(range(10,16),range(11,17)),1); assertThat($jobsPage2[0], arrayWithSize(3)); assertThat($jobsPage2[1], is(0)); - $jobsPage3 = $showJobDao->uploads2Jobs(array(),2); + $jobsPage3 = $this->showJobsDao->uploads2Jobs(array(),2); assertThat($jobsPage3, arrayWithSize(0)); }