-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add readonly REST API endpoint for roles and permissions (#14664)
Co-authored-by: Ash Berlin-Taylor <ash_github@firemirror.com>
- Loading branch information
1 parent
a85d840
commit 0e13458
Showing
8 changed files
with
581 additions
and
5 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
airflow/api_connexion/endpoints/role_and_permission_endpoint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
from flask import current_app | ||
from flask_appbuilder.security.sqla.models import Permission, Role | ||
from sqlalchemy import func | ||
|
||
from airflow.api_connexion import security | ||
from airflow.api_connexion.exceptions import NotFound | ||
from airflow.api_connexion.parameters import check_limit, format_parameters | ||
from airflow.api_connexion.schemas.role_and_permission_schema import ( | ||
ActionCollection, | ||
RoleCollection, | ||
action_collection_schema, | ||
role_collection_schema, | ||
role_schema, | ||
) | ||
from airflow.security import permissions | ||
|
||
|
||
@security.requires_access([(permissions.ACTION_CAN_SHOW, permissions.RESOURCE_ROLE_MODEL_VIEW)]) | ||
def get_role(role_name): | ||
"""Get role""" | ||
ab_security_manager = current_app.appbuilder.sm | ||
role = ab_security_manager.find_role(name=role_name) | ||
if not role: | ||
raise NotFound(title="Role not found", detail=f"The Role with name `{role_name}` was not found") | ||
return role_schema.dump(role) | ||
|
||
|
||
@security.requires_access([(permissions.ACTION_CAN_LIST, permissions.RESOURCE_ROLE_MODEL_VIEW)]) | ||
@format_parameters({'limit': check_limit}) | ||
def get_roles(limit=None, offset=None): | ||
"""Get roles""" | ||
appbuilder = current_app.appbuilder | ||
session = appbuilder.get_session | ||
total_entries = session.query(func.count(Role.id)).scalar() | ||
query = session.query(Role) | ||
|
||
roles = query.order_by(Role.name).offset(offset).limit(limit).all() | ||
|
||
return role_collection_schema.dump(RoleCollection(roles=roles, total_entries=total_entries)) | ||
|
||
|
||
@security.requires_access([(permissions.ACTION_CAN_LIST, permissions.RESOURCE_PERMISSION_MODEL_VIEW)]) | ||
@format_parameters({'limit': check_limit}) | ||
def get_permissions(limit=None, offset=None): | ||
"""Get permissions""" | ||
session = current_app.appbuilder.get_session | ||
total_entries = session.query(func.count(Permission.id)).scalar() | ||
query = session.query(Permission) | ||
actions = query.offset(offset).limit(limit).all() | ||
return action_collection_schema.dump(ActionCollection(actions=actions, total_entries=total_entries)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
airflow/api_connexion/schemas/role_and_permission_schema.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from typing import List, NamedTuple | ||
|
||
from flask_appbuilder.security.sqla.models import Permission, PermissionView, Role, ViewMenu | ||
from marshmallow import Schema, fields | ||
from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field | ||
|
||
|
||
class ActionSchema(SQLAlchemySchema): | ||
"""Permission Action Schema""" | ||
|
||
class Meta: | ||
"""Meta""" | ||
|
||
model = Permission | ||
|
||
name = auto_field() | ||
|
||
|
||
class ResourceSchema(SQLAlchemySchema): | ||
"""View menu Schema""" | ||
|
||
class Meta: | ||
"""Meta""" | ||
|
||
model = ViewMenu | ||
|
||
name = auto_field() | ||
|
||
|
||
class ActionCollection(NamedTuple): | ||
"""Permission Action Collection""" | ||
|
||
actions: List[Permission] | ||
total_entries: int | ||
|
||
|
||
class ActionCollectionSchema(Schema): | ||
"""Permissions list schema""" | ||
|
||
actions = fields.List(fields.Nested(ActionSchema)) | ||
total_entries = fields.Int() | ||
|
||
|
||
class ActionResourceSchema(SQLAlchemySchema): | ||
"""Permission View Schema""" | ||
|
||
class Meta: | ||
"""Meta""" | ||
|
||
model = PermissionView | ||
|
||
permission = fields.Nested(ActionSchema, data_key="action") | ||
view_menu = fields.Nested(ResourceSchema, data_key="resource") | ||
|
||
|
||
class RoleSchema(SQLAlchemySchema): | ||
"""Role item schema""" | ||
|
||
class Meta: | ||
"""Meta""" | ||
|
||
model = Role | ||
|
||
name = auto_field() | ||
permissions = fields.List(fields.Nested(ActionResourceSchema), data_key='actions') | ||
|
||
|
||
class RoleCollection(NamedTuple): | ||
"""List of roles""" | ||
|
||
roles: List[Role] | ||
total_entries: int | ||
|
||
|
||
class RoleCollectionSchema(Schema): | ||
"""List of roles""" | ||
|
||
roles = fields.List(fields.Nested(RoleSchema)) | ||
total_entries = fields.Int() | ||
|
||
|
||
role_schema = RoleSchema() | ||
role_collection_schema = RoleCollectionSchema() | ||
action_collection_schema = ActionCollectionSchema() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.