FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
change-license-bulk.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2014-2015,2018 Siemens AG
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
27 
29 {
30  const NAME = "change-license-bulk";
32  private $licenseDao;
34  private $dbManager;
36  private $uploadDao;
37 
38  function __construct()
39  {
40  parent::__construct(self::NAME, array(
41  self::TITLE => _("Private: schedule a bulk scan from post"),
42  self::PERMISSION => Auth::PERM_WRITE
43  ));
44 
45  $this->dbManager = $this->getObject('db.manager');
46  $this->licenseDao = $this->getObject('dao.license');
47  $this->uploadDao = $this->getObject('dao.upload');
48  }
49 
54  protected function handle(Request $request)
55  {
56  $uploadTreeId = intval($request->get('uploadTreeId'));
57  if ($uploadTreeId <= 0) {
58  return new JsonResponse(array("error" => 'bad request'), JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
59  }
60 
61  try {
62  $jobQueueId = $this->getJobQueueId($uploadTreeId, $request);
63  } catch (Exception $ex) {
64  $errorMsg = $ex->getMessage();
65  return new JsonResponse(array("error" => $errorMsg), JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
66  }
68 
69  return new JsonResponse(array("jqid" => $jobQueueId));
70  }
71 
78  private function getJobQueueId($uploadTreeId, Request $request)
79  {
80  $uploadEntry = $this->uploadDao->getUploadEntry($uploadTreeId);
81  $uploadId = intval($uploadEntry['upload_fk']);
82  $userId = Auth::getUserId();
83  $groupId = Auth::getGroupId();
84 
85  if ($uploadId <= 0 || !$this->uploadDao->isAccessible($uploadId, $groupId)) {
86  throw new Exception('permission denied');
87  }
88 
89  $bulkScope = $request->get('bulkScope');
90  switch ($bulkScope) {
91  case 'u':
92  $uploadTreeTable = $this->uploadDao->getUploadtreeTableName($uploadId);
93  $topBounds = $this->uploadDao->getParentItemBounds($uploadId, $uploadTreeTable);
94  $uploadTreeId = $topBounds->getItemId();
95  break;
96 
97  case 'f':
98  if (!Isdir($uploadEntry['ufile_mode']) &&
99  !Iscontainer($uploadEntry['ufile_mode']) &&
100  !Isartifact($uploadEntry['ufile_mode'])) {
101  $uploadTreeId = $uploadEntry['parent'] ?: $uploadTreeId;
102  }
103  break;
104 
105  default:
106  throw new InvalidArgumentException('bad scope request');
107  }
108 
109  $refText = $request->get('refText');
110  $actions = $request->get('bulkAction');
111 
112  $licenseRemovals = array();
113  foreach ($actions as $licenseAction) {
114  $licenseRemovals[$licenseAction['licenseId']] = array(($licenseAction['action']=='Remove'), $licenseAction['comment'], $licenseAction['reportinfo'], $licenseAction['acknowledgement']);
115  }
116  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId, $licenseRemovals, $refText);
117 
118  if ($bulkId <= 0) {
119  throw new Exception('cannot insert bulk reference');
120  }
121  $upload = $this->uploadDao->getUpload($uploadId);
122  $uploadName = $upload->getFilename();
123  $job_pk = JobAddJob($userId, $groupId, $uploadName, $uploadId);
125  $deciderPlugin = plugin_find("agent_deciderjob");
126  $dependecies = array(array('name' => 'agent_monk_bulk', 'args' => $bulkId));
127  $conflictStrategyId = intval($request->get('forceDecision'));
128  $errorMsg = '';
129  $jqId = $deciderPlugin->AgentAdd($job_pk, $uploadId, $errorMsg, $dependecies, $conflictStrategyId);
130 
131  if (!empty($errorMsg)) {
132  throw new Exception(str_replace('<br>', "\n", $errorMsg));
133  }
134  return $jqId;
135  }
136 }
137 
138 register_plugin(new ChangeLicenseBulk());
Iscontainer($mode)
Definition: common-dir.php:49
handle(Request $request)
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
ReportCachePurgeAll()
Purge all records from the report cache.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
Isdir($mode)
Definition: common-dir.php:31
Isartifact($mode)
Definition: common-dir.php:40
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:45