blob: 2c0ece72d95b66f33d76f52cca615030bcb87c39 [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 page for site admins to create a new user group."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import logging
12import re
13
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020014from framework import exceptions, flaskservlet
Copybara854996b2021-09-07 19:36:02 +000015from framework import framework_helpers
16from framework import permissions
Copybara854996b2021-09-07 19:36:02 +000017from proto import usergroup_pb2
18from sitewide import group_helpers
19
20
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020021class GroupCreate(flaskservlet.FlaskServlet):
Copybara854996b2021-09-07 19:36:02 +000022 """Shows a page with a simple form to create a user group."""
23
24 _PAGE_TEMPLATE = 'sitewide/group-create-page.ezt'
25
26 def AssertBasePermission(self, mr):
27 """Assert that the user has the permissions needed to view this page."""
28 super(GroupCreate, self).AssertBasePermission(mr)
29
30 if not permissions.CanCreateGroup(mr.perms):
31 raise permissions.PermissionException(
32 'User is not allowed to create a user group')
33
34 def GatherPageData(self, _mr):
35 """Build up a dictionary of data values to use when rendering the page."""
36 visibility_levels = group_helpers.BuildUserGroupVisibilityOptions()
37 initial_visibility = group_helpers.GroupVisibilityView(
38 usergroup_pb2.MemberVisibility.ANYONE)
39 group_types = group_helpers.BuildUserGroupTypeOptions()
40
41 return {
42 'groupadmin': '',
43 'group_types': group_types,
44 'import_group': '',
45 'initial_friendprojects': '',
46 'initial_group_type': '',
47 'initial_name': '',
48 'initial_visibility': initial_visibility,
49 'visibility_levels': visibility_levels,
50 }
51
52 def ProcessFormData(self, mr, post_data):
53 """Process the posted form."""
54 # 1. Gather data from the request.
55 group_name = post_data.get('groupname')
56 try:
57 existing_group_id = self.services.user.LookupUserID(mr.cnxn, group_name)
58 existing_settings = self.services.usergroup.GetGroupSettings(
59 mr.cnxn, existing_group_id)
60 if existing_settings:
61 mr.errors.groupname = 'That user group already exists'
62 except exceptions.NoSuchUserException:
63 pass
64
65 if post_data.get('import_group'):
66 vis = usergroup_pb2.MemberVisibility.OWNERS
67 ext_group_type = post_data.get('group_type')
68 friend_projects = ''
69 if not ext_group_type:
70 mr.errors.groupimport = 'Please provide external group type'
71 else:
72 ext_group_type = str(
73 usergroup_pb2.GroupType(int(ext_group_type))).lower()
74
75 if (ext_group_type == 'computed' and
76 not group_name.startswith('everyone@')):
77 mr.errors.groupimport = 'Computed groups must be named everyone@'
78
79 else:
80 vis = usergroup_pb2.MemberVisibility(int(post_data['visibility']))
81 ext_group_type = None
82 friend_projects = post_data.get('friendprojects', '')
83 who_can_view_members = str(vis).lower()
84
85 if not mr.errors.AnyErrors():
86 project_ids, error = self.services.usergroup.ValidateFriendProjects(
87 mr.cnxn, self.services, friend_projects)
88 if error:
89 mr.errors.friendprojects = error
90
91 # 2. Call services layer to save changes.
92 if not mr.errors.AnyErrors():
93 group_id = self.services.usergroup.CreateGroup(
94 mr.cnxn, self.services, group_name, who_can_view_members,
95 ext_group_type, project_ids)
96
97 # 3. Determine the next page in the UI flow.
98 if mr.errors.AnyErrors():
99 self.PleaseCorrect(mr, initial_name=group_name)
100 else:
101 # Go to the new user group's detail page.
102 return framework_helpers.FormatAbsoluteURL(
103 mr, '/g/%s/' % group_id, include_project=False)
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200104
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200105 def GetGroupCreate(self, **kwargs):
106 return self.handler(**kwargs)
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200107
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200108 def PostGroupCreate(self, **kwargs):
109 return self.handler(**kwargs)