blob: 4fe4641a7ff89bea12d7ff1c52049a04894c0e62 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2016 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Classes to list user groups."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import logging
11import time
12
13import ezt
14
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010015from framework import framework_helpers
Copybara854996b2021-09-07 19:36:02 +000016from framework import permissions
17from framework import servlet
18from framework import urls
19from framework import xsrf
20from sitewide import sitewide_views
21
22
23class GroupList(servlet.Servlet):
24 """Shows a page with a simple form to create a user group."""
25
26 _PAGE_TEMPLATE = 'sitewide/group-list-page.ezt'
27
28 def AssertBasePermission(self, mr):
29 """Assert that the user has the permissions needed to view this page."""
30 super(GroupList, self).AssertBasePermission(mr)
31
32 if not mr.perms.HasPerm(permissions.VIEW_GROUP, None, None):
33 raise permissions.PermissionException(
34 'User is not allowed to view list of user groups')
35
36 def GatherPageData(self, mr):
37 """Build up a dictionary of data values to use when rendering the page."""
38 group_views = [
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020039 sitewide_views.GroupView(*groupinfo)
40 for groupinfo in self.services.usergroup.GetAllUserGroupsInfo(mr.cnxn)
41 ]
42 group_views.sort(key=lambda gv: gv.name)
43 offer_group_deletion = mr.perms.CanUsePerm(
44 permissions.DELETE_GROUP, mr.auth.effective_ids, None, [])
45 offer_group_creation = mr.perms.CanUsePerm(
46 permissions.CREATE_GROUP, mr.auth.effective_ids, None, [])
47
48 return {
49 'form_token':
50 xsrf.GenerateToken(mr.auth.user_id, '%s.do' % urls.GROUP_DELETE),
51 'groups':
52 group_views,
53 'offer_group_deletion':
54 ezt.boolean(offer_group_deletion),
55 'offer_group_creation':
56 ezt.boolean(offer_group_creation),
57 }
58
59 def ProcessFormData(self, mr, post_data):
60 """Process the posted form."""
61 if 'removebtn' in post_data:
62 return self.ProcessDeleteGroups(mr, post_data)
63
64 def ProcessDeleteGroups(self, mr, post_data):
65 """Process request to delete groups."""
66 if not mr.perms.CanUsePerm(permissions.DELETE_GROUP, mr.auth.effective_ids,
67 None, []):
68 raise permissions.PermissionException(
69 'User is not permitted to delete groups')
70
71 remove_groups = [int(g) for g in post_data.getlist('remove')]
72
73 if not mr.errors.AnyErrors():
74 self.services.usergroup.DeleteGroups(mr.cnxn, remove_groups)
75
76 if mr.errors.AnyErrors():
77 self.PleaseCorrect(mr)
78 else:
79 return framework_helpers.FormatAbsoluteURL(
80 mr, '/g', include_project=False, saved=1, ts=int(time.time()))
81
82 def GetGroupDelete(self, **kwargs):
83 return self.handler(**kwargs)
84
85 def PostGroupDelete(self, **kwargs):
86 return self.handler(**kwargs)
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020087
88 def GetGroupList(self, **kwargs):
89 return self.handler(**kwargs)