FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UploadPermissionPage.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015, 2020 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\Response;
18 
20 {
21  const NAME = 'upload_permissions';
22  const MOD_REUSE = 16;
23 
25  private $uploadPermDao;
26 
28  private $dbManager;
29 
31  private $folderDao;
32 
33  function __construct()
34  {
35  parent::__construct(self::NAME, array(
36  self::TITLE => _("Edit Uploaded File Permissions"),
37  self::MENU_LIST => "Admin::Upload Permissions",
38  self::PERMISSION => Auth::PERM_WRITE,
39  self::REQUIRES_LOGIN => true
40  ));
41  $this->uploadPermDao = $this->getObject('dao.upload.permission');
42  $this->folderDao = $this->getObject('dao.folder');
43  $this->dbManager = $this->getObject('db.manager');
44  }
45 
50  protected function handle(Request $request)
51  {
52  /* Get array of groups that this user is an admin of */
53  $groupsWhereUserIsAdmin = GetGroupArray(Auth::getUserId());
54  if (empty($groupsWhereUserIsAdmin)) {
55  $text = _("You have no permission to manage any group.");
56  return $this->render('include/base.html.twig',$this->mergeWithDefault(array('content'=>$text)));
57  }
58 
59  $folder_pk = intval($request->get('folder'));
60  $upload_pk = intval($request->get('upload'));
61  $allUploadsPerm = ($request->get('alluploadsperm') == 1) ? 1 : 0;
62  $perm_upload_pk = intval($request->get('permupk'));
63  $perm = intval($request->get('perm'));
64  $newgroup = intval($request->get('newgroup'));
65  $newperm = intval($request->get('newperm'));
66  $public_perm = $request->get('public', -1);
67 
68  $commu_status = fo_communicate_with_scheduler('status', $response_from_scheduler, $error_info);
69  if ($commu_status) {
70  $response_from_scheduler = "";
71  } else {
72  $response_from_scheduler = "Error: Scheduler is not running!";
73  $error_info = null;
74  }
75 
76  $res = $this->editPermissionsForUpload($commu_status, $folder_pk, $upload_pk, $allUploadsPerm, $perm_upload_pk, $perm,$newgroup, $newperm, $public_perm);
77  $vars = array(
78  'folderStructure' => $this->folderDao->getFolderStructure($res['root_folder_pk']),
79  'groupArray' => $groupsWhereUserIsAdmin,
80  'uploadId' => $res['upload_pk'],
81  'allUploadsPerm' => $res['allUploadsPerm'],
82  'folderId' => $res['folder_pk'],
83  'baseUri' => Traceback_uri() . '?mod=upload_permissions',
84  'newPerm' => $res['newperm'],
85  'newGroup' => $res['newgroup'],
86  'uploadList' => $res['UploadList'],
87  'permNames' => $GLOBALS['PERM_NAMES'],
88  'message' => $response_from_scheduler
89  );
90 
91  if (!empty($vars['uploadList'])) {
92  $vars['publicPerm'] = $this->uploadPermDao->getPublicPermission($vars['uploadId']);
93  $permGroups = $this->uploadPermDao->getPermissionGroups($vars['uploadId']);
94  $vars['permGroups'] = $permGroups;
95  $additableGroups = array(0 => '-- select group --');
96  foreach ($groupsWhereUserIsAdmin as $gId=>$gName) {
97  if (!array_key_exists($gId, $permGroups)) {
98  $additableGroups[$gId] = $gName;
99  }
100  }
101  $vars['additableGroups'] = $additableGroups;
102  }
103  $vars['gumJson'] = json_encode($this->getGroupMembers($groupsWhereUserIsAdmin));
104 
105  if (!empty($vars['uploadId'])) {
106  $vars['permNamesWithReuse'] = $this->getPermNamesWithReuse($vars['uploadId']);
107  }
108 
109  return $this->render('upload_permissions.html.twig', $this->mergeWithDefault($vars));
110  }
111 
112  function editPermissionsForUpload($commu_status, $folder_pk,$upload_pk,$allUploadsPerm,$perm_upload_pk,$perm,$newgroup,$newperm,$public_perm)
113  {
114  $root_folder_pk = $this->folderDao->getRootFolder(Auth::getUserId())->getId();
115  if (empty($folder_pk)) {
116  $folder_pk = $root_folder_pk;
117  }
118 
119  $UploadList = FolderListUploads_perm($folder_pk, Auth::PERM_WRITE);
120  if (empty($allUploadsPerm)) {
121  if (empty($upload_pk) && !empty($UploadList)) {
122  $upload_pk = $UploadList[0]['upload_pk'];
123  }
124  if (!empty($perm_upload_pk)) {
125  $this->uploadPermDao->updatePermissionId($perm_upload_pk, $perm);
126  } else if (!empty($newgroup) && !empty($newperm)) {
127  if ($commu_status) {
128  $this->insertPermission($newgroup,$upload_pk,$newperm,$UploadList);
129  }
130  $newperm = $newgroup = 0;
131  } else if ($public_perm >= 0) {
132  $this->uploadPermDao->setPublicPermission($upload_pk, $public_perm);
133  }
134  } else {
135  foreach ($UploadList as $uploadDetails) {
136  $upload_pk = $uploadDetails['upload_pk'];
137  if (!empty($newgroup) && !empty($newperm)) {
138  if ($commu_status) {
139  $this->insertPermission($newgroup, $upload_pk, $newperm, $UploadList);
140  }
141  } else if ($public_perm >= 0) {
142  $this->uploadPermDao->setPublicPermission($upload_pk, $public_perm);
143  }
144  }
145  }
146 
147  return array(
148  'root_folder_pk' => $root_folder_pk,
149  'upload_pk' => $upload_pk,
150  'allUploadsPerm' => $allUploadsPerm,
151  'folder_pk' => $folder_pk,
152  'newperm' => $newperm,
153  'newgroup' => $newgroup,
154  'UploadList' => $UploadList,
155  );
156  }
157 
158  private function getPermNamesWithReuse($uploadId)
159  {
160  $permNamesWithReuse = $GLOBALS['PERM_NAMES'];
161  try {
162  $uploadBrowseProxy = new UploadBrowseProxy(Auth::getGroupId(), Auth::PERM_READ, $this->dbManager);
163  $uploadStatus = $uploadBrowseProxy->getStatus($uploadId);
164  } catch(\Exception $e) {
165  return $permNamesWithReuse;
166  }
167  if ($uploadStatus==UploadStatus::IN_PROGRESS || $uploadStatus==UploadStatus::CLOSED) {
168  foreach ($GLOBALS['PERM_NAMES'] as $perm=>$name) {
169  $permNamesWithReuse[$perm|self::MOD_REUSE] = $name._(' with reuse');
170  }
171  }
172  return $permNamesWithReuse;
173  }
174 
175  function insertPermission($groupId,$uploadId,$permission,$uploadList)
176  {
177  $fileName = false;
178  foreach ($uploadList as $uploadEntry) {
179  if ($uploadEntry['upload_pk']) {
180  $fileName = $uploadEntry['name'];
181  }
182  }
183  if (empty($fileName)) {
184  throw new \Exception('This upload is missing or inaccessible');
185  }
186 
187  $reuseBit = $permission&self::MOD_REUSE;
188  if ($reuseBit) {
189  $jobId = \JobAddJob(Auth::getUserId(), $groupId, $fileName, $uploadId);
190  $reuserAgent = \plugin_find('agent_reuser');
191  $request = new Request(array('uploadToReuse'=>"$uploadId,".Auth::getGroupId(),'groupId'=>$groupId));
192  $reuserAgent->scheduleAgent($jobId, $uploadId, $errorMsg, $request);
193  if (!empty($errorMsg)) {
194  throw new Exception($errorMsg);
195  }
196  $permission ^= $reuseBit;
197  }
198  $this->uploadPermDao->insertPermission($uploadId, $groupId, $permission);
199  }
200 
201  private function getGroupMembers($groupsWhereUserIsAdmin)
202  {
203  $this->dbManager->prepare($stmt=__METHOD__,
204  "SELECT user_name,gum.group_fk FROM group_user_member gum, users WHERE user_fk=user_pk");
205  $res = $this->dbManager->execute($stmt);
206  $gum = array();
207  while ($row = $this->dbManager->fetchArray($res)) {
208  if (array_key_exists($row['group_fk'], $groupsWhereUserIsAdmin)) {
209  $gum[] = array($row['user_name'], $row['group_fk']);
210  }
211  }
212  $this->dbManager->freeResult($res);
213  return $gum;
214  }
215 }
216 
217 register_plugin(new UploadPermissionPage());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
render($templateName, $vars=null, $headers=null)
handle(Request $request)
FolderListUploads_perm($ParentFolder, $perm)
Returns an array of uploads in a folder.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
GetGroupArray($user_pk)
Get array of groups that this user has admin access to @depricated use UserDao::getAdminGroupMap()
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:33
#define PERM_READ
Read-only permission.
Definition: libfossology.h:32
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16