|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +"""Remove can_read permission on config resource for User and Viewer role |
| 20 | +
|
| 21 | +Revision ID: 82b7c48c147f |
| 22 | +Revises: 449b4072c2da |
| 23 | +Create Date: 2021-02-04 12:45:58.138224 |
| 24 | +
|
| 25 | +""" |
| 26 | + |
| 27 | +from airflow.security import permissions |
| 28 | +from airflow.www.app import create_app |
| 29 | + |
| 30 | +# revision identifiers, used by Alembic. |
| 31 | +revision = '82b7c48c147f' |
| 32 | +down_revision = '449b4072c2da' |
| 33 | +branch_labels = None |
| 34 | +depends_on = None |
| 35 | + |
| 36 | + |
| 37 | +def upgrade(): |
| 38 | + """Remove can_read permission on config resource for User and Viewer role""" |
| 39 | + appbuilder = create_app(config={'FAB_UPDATE_PERMS': False}).appbuilder |
| 40 | + roles_to_modify = [role for role in appbuilder.sm.get_all_roles() if role.name in ["User", "Viewer"]] |
| 41 | + can_read_on_config_perm = appbuilder.sm.find_permission_view_menu( |
| 42 | + permissions.ACTION_CAN_READ, permissions.RESOURCE_CONFIG |
| 43 | + ) |
| 44 | + |
| 45 | + for role in roles_to_modify: |
| 46 | + if appbuilder.sm.exist_permission_on_roles( |
| 47 | + permissions.RESOURCE_CONFIG, permissions.ACTION_CAN_READ, [role.id] |
| 48 | + ): |
| 49 | + appbuilder.sm.del_permission_role(role, can_read_on_config_perm) |
| 50 | + |
| 51 | + |
| 52 | +def downgrade(): |
| 53 | + """Add can_read permission on config resource for User and Viewer role""" |
| 54 | + appbuilder = create_app(config={'FAB_UPDATE_PERMS': False}).appbuilder |
| 55 | + roles_to_modify = [role for role in appbuilder.sm.get_all_roles() if role.name in ["User", "Viewer"]] |
| 56 | + can_read_on_config_perm = appbuilder.sm.find_permission_view_menu( |
| 57 | + permissions.ACTION_CAN_READ, permissions.RESOURCE_CONFIG |
| 58 | + ) |
| 59 | + |
| 60 | + for role in roles_to_modify: |
| 61 | + if not appbuilder.sm.exist_permission_on_roles( |
| 62 | + permissions.RESOURCE_CONFIG, permissions.ACTION_CAN_READ, [role.id] |
| 63 | + ): |
| 64 | + appbuilder.sm.add_permission_role(role, can_read_on_config_perm) |
0 commit comments