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