Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Classes to list user groups.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import logging |
| 11 | import time |
| 12 | |
| 13 | import ezt |
| 14 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 15 | from framework import framework_helpers |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | from framework import permissions |
| 17 | from framework import servlet |
| 18 | from framework import urls |
| 19 | from framework import xsrf |
| 20 | from sitewide import sitewide_views |
| 21 | |
| 22 | |
| 23 | class 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ínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 39 | 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 87 | |
| 88 | def GetGroupList(self, **kwargs): |
| 89 | return self.handler(**kwargs) |