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