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