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 | """Helper functions used in user group modules.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | from framework import framework_views |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 11 | from mrproto import usergroup_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | class GroupVisibilityView(object): |
| 15 | """Object for group visibility information that can be easily used in EZT.""" |
| 16 | |
| 17 | VISIBILITY_NAMES = { |
| 18 | usergroup_pb2.MemberVisibility.ANYONE: 'Anyone on the Internet', |
| 19 | usergroup_pb2.MemberVisibility.MEMBERS: 'Group Members', |
| 20 | usergroup_pb2.MemberVisibility.OWNERS: 'Group Owners'} |
| 21 | |
| 22 | def __init__(self, group_visibility_enum): |
| 23 | self.key = int(group_visibility_enum) |
| 24 | self.name = self.VISIBILITY_NAMES[group_visibility_enum] |
| 25 | |
| 26 | |
| 27 | class GroupTypeView(object): |
| 28 | """Object for group type information that can be easily used in EZT.""" |
| 29 | |
| 30 | TYPE_NAMES = { |
| 31 | usergroup_pb2.GroupType.CHROME_INFRA_AUTH: 'Chrome-infra-auth', |
| 32 | usergroup_pb2.GroupType.MDB: 'MDB', |
| 33 | usergroup_pb2.GroupType.BAGGINS: 'Baggins', |
| 34 | usergroup_pb2.GroupType.COMPUTED: 'Computed', |
| 35 | } |
| 36 | |
| 37 | def __init__(self, group_type_enum): |
| 38 | self.key = int(group_type_enum) |
| 39 | self.name = self.TYPE_NAMES[group_type_enum] |
| 40 | |
| 41 | |
| 42 | class GroupMemberView(framework_views.UserView): |
| 43 | """Wrapper class to display basic group member information in a template.""" |
| 44 | |
| 45 | def __init__(self, user, group_id, role): |
| 46 | assert role in ['member', 'owner'] |
| 47 | super(GroupMemberView, self).__init__(user) |
| 48 | self.group_id = group_id |
| 49 | self.role = role |
| 50 | |
| 51 | |
| 52 | def BuildUserGroupVisibilityOptions(): |
| 53 | """Return a list of user group visibility values for use in an HTML menu. |
| 54 | |
| 55 | Returns: |
| 56 | A list of GroupVisibilityView objects that can be used in EZT. |
| 57 | """ |
| 58 | vis_levels = [usergroup_pb2.MemberVisibility.OWNERS, |
| 59 | usergroup_pb2.MemberVisibility.MEMBERS, |
| 60 | usergroup_pb2.MemberVisibility.ANYONE] |
| 61 | |
| 62 | return [GroupVisibilityView(vis) for vis in vis_levels] |
| 63 | |
| 64 | |
| 65 | def BuildUserGroupTypeOptions(): |
| 66 | """Return a list of user group types for use in an HTML menu. |
| 67 | |
| 68 | Returns: |
| 69 | A list of GroupTypeView objects that can be used in EZT. |
| 70 | """ |
| 71 | group_types = [usergroup_pb2.GroupType.CHROME_INFRA_AUTH, |
| 72 | usergroup_pb2.GroupType.MDB, |
| 73 | usergroup_pb2.GroupType.BAGGINS, |
| 74 | usergroup_pb2.GroupType.COMPUTED] |
| 75 | |
| 76 | return sorted([GroupTypeView(gt) for gt in group_types], |
| 77 | key=lambda gtv: gtv.name) |