FOSSology  4.5.1
Open Source License Compliance by Open Source Software
AdminGroupEdit.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2025 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\UI\Page;
9 
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\HttpFoundation\Response;
15 
21 {
22 
23  const NAME = 'group_edit';
24 
25  function __construct()
26  {
27  parent::__construct(self::NAME, array(
28  self::TITLE => _("Edit Group"),
29  self::MENU_LIST => "Admin::Groups::Edit Group",
30  self::PERMISSION => Auth::PERM_ADMIN,
31  self::REQUIRES_LOGIN => TRUE
32  ));
33  }
34 
39  protected function handle(Request $request)
40  {
41  $userId = Auth::getUserId();
42  $vars = array();
43 
45  $userDao = $this->getObject('dao.user');
46  $groupMap = $userDao->getDeletableAdminGroupMap($userId,
47  $_SESSION[Auth::USER_LEVEL]);
48 
49  if (empty($groupMap)) {
50  $vars['content'] = _("You have no groups you can edit.");
51  return $this->render('include/base.html.twig', $this->mergeWithDefault($vars));
52  }
53 
54  $groupId = $request->get('grouppk');
55  $newGroupName = trim($request->get('newgroupname'));
56  if (! empty($groupId) && ! empty($newGroupName)) {
57  $validateGroup = $this->validateGroupName($newGroupName);
58  if (empty($validateGroup)) {
59  try {
60  $escapedGroupName = htmlspecialchars(strip_tags($newGroupName), ENT_QUOTES, 'UTF-8');
61  $userDao->editGroup($groupId, $escapedGroupName);
62  $vars['message'] = _("Group") . ' ' . $groupMap[$groupId] . ' ' . _("edited to ".$escapedGroupName ) . '.';
63  $groupMap[$groupId] = $escapedGroupName;
64  } catch (\Exception $e) {
65  $vars['message'] = $e->getMessage();
66  }
67  } else {
68  $vars['message'] = $validateGroup;
69  }
70  }
71 
72  $vars['groupMap'] = $groupMap;
73  $vars['uri'] = Traceback_uri() . "?mod=group_edit";
74  return $this->render('admin_group_edit.html.twig', $this->mergeWithDefault($vars));
75  }
76 
83  function validateGroupName($groupName)
84  {
85  if (empty($groupName)) {
86  return _("Invalid: Group name cannot be whitespace only");
87  } else if (preg_match('/^[\s\w_-]$/', $groupName) !== 1) {
88  return _("Invalid: Group name can only contain letters, numbers, hyphens and underscores");
89  } else if (is_numeric($groupName)) {
90  return _("Invalid: Group name cannot be numeric-only");
91  } else {
92  return "";
93  }
94  }
95 }
96 
97 register_plugin(new AdminGroupEdit());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getUserId()
Get the current user's id.
Definition: Auth.php:68
render($templateName, $vars=null, $headers=null)
validateGroupName($groupName)
validateGroupName. verify if the group is empty or numeric
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690