FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AdminGroupUsers.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2014-2015, 2018 Siemens AG
4  Author: Steffen Weber
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 
20 namespace Fossology\UI\Page;
21 
27 
33 {
34  var $groupPermissions = array(-1 => "None", UserDao::USER => "User",
35  UserDao::ADMIN => "Admin", UserDao::ADVISOR => "Advisor");
36  const NAME = 'group_manage_users';
37 
38  function __construct()
39  {
40  parent::__construct(self::NAME, array(
41  self::TITLE => _("Manage Group Users"),
42  self::MENU_LIST => "Admin::Groups::Manage Group Users",
43  self::PERMISSION => Auth::PERM_WRITE,
44  self::REQUIRES_LOGIN => TRUE
45  ));
46  }
47 
52  protected function handle(Request $request)
53  {
54  $userId = Auth::getUserId();
56  $userDao = $this->getObject('dao.user');
57  $groupMap = $userDao->getAdminGroupMap($userId, $_SESSION[Auth::USER_LEVEL]);
58  if (empty($groupMap)) {
59  $text = _("You have no permission to manage any group.");
60  return $this->render('include/base.html.twig', $this->mergeWithDefault(array('message' => $text)));
61  }
63  $dbManager = $this->getObject('db.manager');
64  $group_pk = intval($request->get('group'));
65  if (empty($group_pk) || !array_key_exists($group_pk, $groupMap)) {
66  $group_pk = key($groupMap);
67  }
68 
69  $gum_pk = intval($request->get('gum_pk'));
70  $text = "";
71  if ($gum_pk) {
72  $perm = intval($request->get('perm'));
73  $atleastOneUserShouldBePart = $dbManager->getSingleRow("SELECT count(*) cnt FROM group_user_member WHERE group_fk = (SELECT group_fk FROM group_user_member WHERE group_user_member_pk = $1)",
74  array($gum_pk), $stmt = __METHOD__ . ".atleastOneUserShouldBePart");
75  if ($atleastOneUserShouldBePart['cnt'] <= 1) {
76  $text = _("Error: atleast one user should be part of a group.");
77  } else {
78  $this->updateGUMPermission($gum_pk, $perm);
79  }
80  $groupMap = $userDao->getAdminGroupMap($userId,
81  $_SESSION[Auth::USER_LEVEL]);
82  }
83 
84  $newuser = intval($request->get('newuser'));
85  $newperm = intval($request->get('newperm'));
86 
87  if ($newuser && $group_pk) {
88  // do not produce duplicate
89  $dbManager->prepare($stmt = __METHOD__ . ".delByGroupAndUser",
90  "delete from group_user_member where group_fk=$1 and user_fk=$2");
91  $dbManager->freeResult(
92  $dbManager->execute($stmt, array($group_pk, $newuser)));
93  if ($newperm >= 0) {
94  $dbManager->prepare($stmt = __METHOD__ . ".insertGUP",
95  "insert into group_user_member (group_fk, user_fk, group_perm) values ($1,$2,$3)");
96  $dbManager->freeResult(
97  $dbManager->execute($stmt, array($group_pk, $newuser, $newperm)));
98  }
99  if ($newuser == $userId) {
100  $groupMap = $userDao->getAdminGroupMap($userId, $_SESSION[Auth::USER_LEVEL]);
101  }
102  $newperm = $newuser = 0;
103  }
104 
105  natcasesort($groupMap);
106  $baseUrl = Traceback_uri() . "?mod=" . $this->getName() . '&group=';
107  $onchange = "onchange=\"js_url(this.value, '$baseUrl')\"";
108  $baseUrl .= $group_pk;
109  $vars = array('groupMap' => $groupMap,
110  'groupId' => $group_pk,
111  'permissionMap' => $this->groupPermissions,
112  'baseUrl' => $baseUrl,
113  'groupMapAction' => $onchange);
114 
115  $stmt = __METHOD__ . "getUsersWithGroup";
116  $dbManager->prepare($stmt, "select user_pk, user_name, user_desc, group_user_member_pk, group_perm
117  FROM users LEFT JOIN group_user_member gum ON gum.user_fk=users.user_pk AND gum.group_fk=$1
118  ORDER BY user_name");
119  $result = $dbManager->execute($stmt, array($group_pk));
120  $vars['usersWithGroup'] = $dbManager->fetchAll($result);
121  $dbManager->freeResult($result);
122 
123  $otherUsers = array('0' => '');
124  foreach ($vars['usersWithGroup'] as $row) {
125  if ($row['group_user_member_pk']) {
126  continue;
127  }
128  $otherUsers[$row['user_pk']] = !empty($row['user_desc']) ? $row['user_desc']. ' ('. $row['user_name'] .')' : $row['user_name'];
129  }
130 
131  $vars['existsOtherUsers'] = count($otherUsers) - 1;
132  if ($vars['existsOtherUsers']) {
133  $vars['newPermissionMap'] = $this->groupPermissions;
134  unset($vars['newPermissionMap'][-1]);
135  $script = "var newpermurl;
136  function setNewPermUrl(newperm){
137  newpermurl='" . $baseUrl . "&newperm='+newperm+'&newuser=';
138  }
139  setNewPermUrl($newperm);";
140  $scripts = js_url() . '<script type="text/javascript"> ' . $script . '</script>';
141  $vars['otherUsers'] = $otherUsers;
142  } else {
143  $scripts = js_url();
144  }
145 
146  $vars['scripts'] = $scripts;
147  if (!empty($text)) {
148  $vars['message'] .= $text;
149  }
150  return $this->render('admin_group_users.html.twig', $this->mergeWithDefault($vars));
151  }
152 
153  private function updateGUMPermission($gum_pk, $perm)
154  {
155  $dbManager = $this->getObject('db.manager');
156  if ($perm === -1) {
157  $dbManager->prepare($stmt = __METHOD__ . ".delByGUM",
158  "DELETE FROM group_user_member WHERE group_user_member_pk=$1 RETURNING user_fk, group_fk");
159  $deletedEntry = $dbManager->execute($stmt, array($gum_pk));
160  $effectedUser = $dbManager->fetchArray($deletedEntry);
161  $isEffected = $dbManager->getSingleRow("SELECT count(*) cnt FROM users WHERE user_pk=$1 AND group_fk = $2",
162  array($effectedUser['user_fk'], $effectedUser['group_fk']), $stmt = __METHOD__ . ".isUserEffectedFromRemoval");
163  if ($isEffected['cnt'] == 1) {
164  $dbManager->getSingleRow("UPDATE users SET group_fk = (
165  SELECT group_fk FROM group_user_member WHERE user_fk = $1 AND group_perm >= 0 LIMIT 1)
166  WHERE user_pk = $1",
167  array($effectedUser['user_fk']), $stmt = __METHOD__ . ".setNewGroupId");
168  }
169  $dbManager->freeResult($deletedEntry);
170  } else if (array_key_exists($perm, $this->groupPermissions)) {
171  $dbManager->getSingleRow("UPDATE group_user_member SET group_perm=$1 WHERE group_user_member_pk=$2",
172  array($perm, $gum_pk), $stmt = __METHOD__ . ".updatePermInGUM");
173  }
174  }
175 }
176 
177 register_plugin(new AdminGroupUsers());
Traceback_uri()
Get the URI without query to this location.
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
render($templateName, $vars=null, $headers=null)
js_url()
Load a new url.