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 | """A page for site admins to create a new user group.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import logging |
| 11 | import re |
| 12 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 13 | from framework import exceptions |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 14 | from framework import framework_helpers |
| 15 | from framework import permissions |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 16 | from framework import servlet |
| 17 | from mrproto import usergroup_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 18 | from sitewide import group_helpers |
| 19 | |
| 20 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 21 | class GroupCreate(servlet.Servlet): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 22 | """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ínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 104 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 105 | def GetGroupCreate(self, **kwargs): |
| 106 | return self.handler(**kwargs) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 107 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 108 | def PostGroupCreate(self, **kwargs): |
| 109 | return self.handler(**kwargs) |