Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | """A class to display user group admin page.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import logging |
| 12 | import time |
| 13 | |
| 14 | import ezt |
| 15 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 16 | from framework import flaskservlet |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 17 | from framework import framework_helpers |
| 18 | from framework import permissions |
| 19 | from framework import servlet |
| 20 | from framework import urls |
| 21 | from proto import usergroup_pb2 |
| 22 | from services import usergroup_svc |
| 23 | from sitewide import group_helpers |
| 24 | |
| 25 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 26 | class GroupAdmin(flaskservlet.FlaskServlet): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 27 | """The group admin page.""" |
| 28 | |
| 29 | _PAGE_TEMPLATE = 'sitewide/group-admin-page.ezt' |
| 30 | |
| 31 | def AssertBasePermission(self, mr): |
| 32 | """Assert that the user has the permissions needed to view this page.""" |
| 33 | super(GroupAdmin, self).AssertBasePermission(mr) |
| 34 | |
| 35 | _, owner_ids_dict = self.services.usergroup.LookupMembers( |
| 36 | mr.cnxn, [mr.viewed_user_auth.user_id]) |
| 37 | owner_ids = owner_ids_dict[mr.viewed_user_auth.user_id] |
| 38 | if not permissions.CanEditGroup( |
| 39 | mr.perms, mr.auth.effective_ids, owner_ids): |
| 40 | raise permissions.PermissionException( |
| 41 | 'User is not allowed to edit a user group') |
| 42 | |
| 43 | def GatherPageData(self, mr): |
| 44 | """Build up a dictionary of data values to use when rendering the page.""" |
| 45 | group_id = mr.viewed_user_auth.user_id |
| 46 | group_settings = self.services.usergroup.GetGroupSettings( |
| 47 | mr.cnxn, group_id) |
| 48 | visibility_levels = group_helpers.BuildUserGroupVisibilityOptions() |
| 49 | initial_visibility = group_helpers.GroupVisibilityView( |
| 50 | group_settings.who_can_view_members) |
| 51 | group_types = group_helpers.BuildUserGroupTypeOptions() |
| 52 | import_group = bool(group_settings.ext_group_type) |
| 53 | if import_group: |
| 54 | initial_group_type = group_helpers.GroupTypeView( |
| 55 | group_settings.ext_group_type) |
| 56 | else: |
| 57 | initial_group_type = '' |
| 58 | |
| 59 | if group_settings.friend_projects: |
| 60 | initial_friendprojects = ', '.join( |
| 61 | list(self.services.project.LookupProjectNames( |
| 62 | mr.cnxn, group_settings.friend_projects).values())) |
| 63 | else: |
| 64 | initial_friendprojects = '' |
| 65 | |
| 66 | return { |
| 67 | 'admin_tab_mode': 'st2', |
| 68 | 'groupadmin': True, |
| 69 | 'groupid': group_id, |
| 70 | 'groupname': mr.viewed_username, |
| 71 | 'group_types': group_types, |
| 72 | 'import_group': import_group or '', |
| 73 | 'initial_friendprojects': initial_friendprojects, |
| 74 | 'initial_group_type': initial_group_type, |
| 75 | 'initial_visibility': initial_visibility, |
| 76 | 'offer_membership_editing': True, |
| 77 | 'visibility_levels': visibility_levels, |
| 78 | } |
| 79 | |
| 80 | def ProcessFormData(self, mr, post_data): |
| 81 | """Process the posted form.""" |
| 82 | # 1. Gather data from the request. |
| 83 | group_name = mr.viewed_username |
| 84 | group_id = mr.viewed_user_auth.user_id |
| 85 | |
| 86 | if post_data.get('import_group'): |
| 87 | vis_level = usergroup_pb2.MemberVisibility.OWNERS |
| 88 | ext_group_type = post_data.get('group_type') |
| 89 | friend_projects = '' |
| 90 | if not ext_group_type: |
| 91 | mr.errors.groupimport = 'Please provide external group type' |
| 92 | else: |
| 93 | ext_group_type = usergroup_pb2.GroupType(int(ext_group_type)) |
| 94 | else: |
| 95 | vis_level = post_data.get('visibility') |
| 96 | ext_group_type = None |
| 97 | friend_projects = post_data.get('friendprojects', '') |
| 98 | if vis_level: |
| 99 | vis_level = usergroup_pb2.MemberVisibility(int(vis_level)) |
| 100 | else: |
| 101 | mr.errors.groupimport = 'Cannot update settings for imported group' |
| 102 | |
| 103 | if not mr.errors.AnyErrors(): |
| 104 | project_ids, error = self.services.usergroup.ValidateFriendProjects( |
| 105 | mr.cnxn, self.services, friend_projects) |
| 106 | if error: |
| 107 | mr.errors.friendprojects = error |
| 108 | |
| 109 | # 2. Call services layer to save changes. |
| 110 | if not mr.errors.AnyErrors(): |
| 111 | group_settings = usergroup_pb2.UserGroupSettings( |
| 112 | who_can_view_members=vis_level, |
| 113 | ext_group_type=ext_group_type, |
| 114 | friend_projects=project_ids) |
| 115 | self.services.usergroup.UpdateSettings( |
| 116 | mr.cnxn, group_id, group_settings) |
| 117 | |
| 118 | # 3. Determine the next page in the UI flow. |
| 119 | if mr.errors.AnyErrors(): |
| 120 | self.PleaseCorrect(mr, initial_name=group_name) |
| 121 | else: |
| 122 | return framework_helpers.FormatAbsoluteURL( |
| 123 | mr, '/g/%s%s' % (group_name, urls.GROUP_ADMIN), |
| 124 | include_project=False, saved=1, ts=int(time.time())) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 125 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 126 | def GetGroupAdmin(self, **kwargs): |
| 127 | return self.handler(**kwargs) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 128 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 129 | def PostGroupAdmin(self, **kwargs): |
| 130 | return self.handler(**kwargs) |