Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # Copyright 2020 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 | from __future__ import print_function |
| 6 | from __future__ import division |
| 7 | from __future__ import absolute_import |
| 8 | |
| 9 | from framework import permissions |
| 10 | from framework import exceptions |
| 11 | from api.v3.api_proto import permission_objects_pb2 |
| 12 | |
| 13 | # Global dictionaries to map backend permission strings to |
| 14 | # API Permission enum values. |
| 15 | |
| 16 | HOTLIST_PERMISSIONS_MAP = { |
| 17 | permissions.EDIT_HOTLIST: |
| 18 | permission_objects_pb2.Permission.Value('HOTLIST_EDIT'), |
| 19 | permissions.ADMINISTER_HOTLIST: |
| 20 | permission_objects_pb2.Permission.Value('HOTLIST_ADMINISTER') |
| 21 | } |
| 22 | |
| 23 | FIELDDEF_PERMISSIONS_MAP = { |
| 24 | permissions.EDIT_FIELD_DEF: |
| 25 | permission_objects_pb2.Permission.Value('FIELD_DEF_EDIT'), |
| 26 | permissions.EDIT_FIELD_DEF_VALUE: |
| 27 | permission_objects_pb2.Permission.Value('FIELD_DEF_VALUE_EDIT') |
| 28 | } |
| 29 | |
| 30 | # TODO(crbug/monorail/7339): Create a common _ConvertPermissions(permissions, |
| 31 | # permissions_map) |
| 32 | |
| 33 | |
| 34 | def ConvertHotlistPermissions(hotlist_permissions): |
| 35 | # type: (Sequence[str]) -> Sequence[permission_objects_pb2.Permission] |
| 36 | """Converts hotlist permission strings into protoc Permission enum values.""" |
| 37 | api_permissions = [] |
| 38 | for permission in hotlist_permissions: |
| 39 | api_permission = HOTLIST_PERMISSIONS_MAP.get(permission) |
| 40 | if not api_permission: |
| 41 | raise exceptions.InputException( |
| 42 | 'Unrecognized hotlist permission: %s' % permission) |
| 43 | api_permissions.append(api_permission) |
| 44 | |
| 45 | return api_permissions |
| 46 | |
| 47 | |
| 48 | def ConvertFieldDefPermissions(field_permissions): |
| 49 | # type: (Sequence[str]) -> Sequence[permission_objects_pb2.Permission] |
| 50 | """Converts field permission strings into protoc Permission enum values.""" |
| 51 | api_permissions = [] |
| 52 | for permission in field_permissions: |
| 53 | api_permission = FIELDDEF_PERMISSIONS_MAP.get(permission) |
| 54 | if not api_permission: |
| 55 | raise exceptions.InputException( |
| 56 | 'Unrecognized field permission: %s' % permission) |
| 57 | api_permissions.append(api_permission) |
| 58 | |
| 59 | return api_permissions |
| 60 | |
| 61 | |
| 62 | # TODO(crbug/monorail/7339): Implement all ConvertFooPermissions methods. |