FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UploadPermissionPage.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2015,2020, Siemens AG
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
29 
31 {
32  const NAME = 'upload_permissions';
33  const MOD_REUSE = 16;
34 
36  private $uploadPermDao;
37 
39  private $dbManager;
40 
42  private $folderDao;
43 
44  function __construct()
45  {
46  parent::__construct(self::NAME, array(
47  self::TITLE => _("Edit Uploaded File Permissions"),
48  self::MENU_LIST => "Admin::Upload Permissions",
49  self::PERMISSION => Auth::PERM_WRITE,
50  self::REQUIRES_LOGIN => TRUE
51  ));
52  $this->uploadPermDao = $this->getObject('dao.upload.permission');
53  $this->folderDao = $this->getObject('dao.folder');
54  $this->dbManager = $this->getObject('db.manager');
55  }
56 
61  protected function handle(Request $request)
62  {
63  /* Get array of groups that this user is an admin of */
64  $groupsWhereUserIsAdmin = GetGroupArray(Auth::getUserId());
65  if (empty($groupsWhereUserIsAdmin)) {
66  $text = _("You have no permission to manage any group.");
67  return $this->render('include/base.html.twig',$this->mergeWithDefault(array('content'=>$text)));
68  }
69 
70  $folder_pk = intval($request->get('folder'));
71  $upload_pk = intval($request->get('upload'));
72  $allUploadsPerm = ($request->get('alluploadsperm') == 1) ? 1 : 0;
73  $perm_upload_pk = intval($request->get('permupk'));
74  $perm = intval($request->get('perm'));
75  $newgroup = intval($request->get('newgroup'));
76  $newperm = intval($request->get('newperm'));
77  $public_perm = $request->get('public', -1);
78 
79  $root_folder_pk = $this->folderDao->getRootFolder(Auth::getUserId())->getId();
80  if (empty($folder_pk)) {
81  $folder_pk = $root_folder_pk;
82  }
83 
84  $UploadList = FolderListUploads_perm($folder_pk, Auth::PERM_WRITE);
85  if (empty($allUploadsPerm)) {
86  if (empty($upload_pk) && !empty($UploadList)) {
87  $upload_pk = $UploadList[0]['upload_pk'];
88  }
89  if (!empty($perm_upload_pk)) {
90  $this->uploadPermDao->updatePermissionId($perm_upload_pk, $perm);
91  } else if (!empty($newgroup) && !empty($newperm)) {
92  $this->insertPermission($newgroup,$upload_pk,$newperm,$UploadList);
93  $newperm = $newgroup = 0;
94  } else if ($public_perm >= 0) {
95  $this->uploadPermDao->setPublicPermission($upload_pk, $public_perm);
96  }
97  } else {
98  foreach ($UploadList as $uploadDetails) {
99  $upload_pk = $uploadDetails['upload_pk'];
100  if (!empty($newgroup) && !empty($newperm)) {
101  $this->insertPermission($newgroup, $upload_pk, $newperm, $UploadList);
102  } else if ($public_perm >= 0) {
103  $this->uploadPermDao->setPublicPermission($upload_pk, $public_perm);
104  }
105  }
106  }
107  $vars = array(
108  'folderStructure' => $this->folderDao->getFolderStructure($root_folder_pk),
109  'groupArray' => $groupsWhereUserIsAdmin,
110  'uploadId' => $upload_pk,
111  'allUploadsPerm' => $allUploadsPerm,
112  'folderId' => $folder_pk,
113  'baseUri' => Traceback_uri() . '?mod=upload_permissions',
114  'newPerm' => $newperm,
115  'newGroup' => $newgroup,
116  'uploadList' => $UploadList,
117  'permNames' => $GLOBALS['PERM_NAMES']
118  );
119 
120  if (!empty($UploadList)) {
121  $vars['publicPerm'] = $this->uploadPermDao->getPublicPermission($upload_pk);
122  $permGroups = $this->uploadPermDao->getPermissionGroups($upload_pk);
123  $vars['permGroups'] = $permGroups;
124  $additableGroups = array(0 => '-- select group --');
125  foreach ($groupsWhereUserIsAdmin as $gId=>$gName) {
126  if (!array_key_exists($gId, $permGroups)) {
127  $additableGroups[$gId] = $gName;
128  }
129  }
130  $vars['additableGroups'] = $additableGroups;
131  }
132  $vars['gumJson'] = json_encode($this->getGroupMembers($groupsWhereUserIsAdmin));
133 
134  if (!empty($upload_pk)) {
135  $vars['permNamesWithReuse'] = $this->getPermNamesWithReuse($upload_pk);
136  }
137 
138  return $this->render('upload_permissions.html.twig', $this->mergeWithDefault($vars));
139  }
140 
141  private function getPermNamesWithReuse($uploadId)
142  {
143  $permNamesWithReuse = $GLOBALS['PERM_NAMES'];
144  try {
145  $uploadBrowseProxy = new UploadBrowseProxy(Auth::getGroupId(), Auth::PERM_READ, $this->dbManager);
146  $uploadStatus = $uploadBrowseProxy->getStatus($uploadId);
147  } catch(\Exception $e) {
148  return $permNamesWithReuse;
149  }
150  if ($uploadStatus==UploadStatus::IN_PROGRESS || $uploadStatus==UploadStatus::CLOSED) {
151  foreach ($GLOBALS['PERM_NAMES'] as $perm=>$name) {
152  $permNamesWithReuse[$perm|self::MOD_REUSE] = $name._(' with reuse');
153  }
154  }
155  return $permNamesWithReuse;
156  }
157 
158  private function insertPermission($groupId,$uploadId,$permission,$uploadList)
159  {
160  $fileName = false;
161  foreach ($uploadList as $uploadEntry) {
162  if ($uploadEntry['upload_pk']) {
163  $fileName = $uploadEntry['name'];
164  }
165  }
166  if (empty($fileName)) {
167  throw new \Exception('This upload is missing or inaccessible');
168  }
169 
170  $reuseBit = $permission&self::MOD_REUSE;
171  if ($reuseBit) {
172  $jobId = \JobAddJob(Auth::getUserId(), $groupId, $fileName, $uploadId);
173  $reuserAgent = \plugin_find('agent_reuser');
174  $request = new Request(array('uploadToReuse'=>"$uploadId,".Auth::getGroupId(),'groupId'=>$groupId));
175  $reuserAgent->scheduleAgent($jobId, $uploadId, $errorMsg, $request);
176  if (!empty($errorMsg)) {
177  throw new Exception($errorMsg);
178  }
179  $permission ^= $reuseBit;
180  }
181  $this->uploadPermDao->insertPermission($uploadId, $groupId, $permission);
182  }
183 
184  private function getGroupMembers($groupsWhereUserIsAdmin)
185  {
186  $this->dbManager->prepare($stmt=__METHOD__,
187  "SELECT user_name,gum.group_fk FROM group_user_member gum, users WHERE user_fk=user_pk");
188  $res = $this->dbManager->execute($stmt);
189  $gum = array();
190  while ($row = $this->dbManager->fetchArray($res)) {
191  if (array_key_exists($row['group_fk'], $groupsWhereUserIsAdmin)) {
192  $gum[] = array($row['user_name'], $row['group_fk']);
193  }
194  }
195  $this->dbManager->freeResult($res);
196  return $gum;
197  }
198 }
199 
200 register_plugin(new UploadPermissionPage());
Traceback_uri()
Get the URI without query to this location.
FolderListUploads_perm($ParentFolder, $perm)
Returns an array of uploads in a folder.
render($templateName, $vars=null, $headers=null)
handle(Request $request)
GetGroupArray($user_pk)
Get array of groups that this user has admin access to use UserDao::getAdminGroupMap() ...
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
#define PERM_READ
Read-only permission.
Definition: libfossology.h:44
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:45