Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimization(acl): query role list #141

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions cmdb-api/api/lib/perm/acl/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import six
from flask import abort
from flask import current_app
from sqlalchemy import or_, and_

from api.extensions import db
from api.lib.perm.acl.app import AppCRUD
Expand Down Expand Up @@ -212,18 +213,21 @@ class RoleCRUD(object):

@staticmethod
def search(q, app_id, page=1, page_size=None, user_role=True, is_all=False, user_only=False):
query = db.session.query(Role).filter(Role.deleted.is_(False))
query1 = query.filter(Role.app_id == app_id).filter(Role.uid.is_(None))
query2 = query.filter(Role.app_id.is_(None)).filter(Role.uid.is_(None))
query = query1.union(query2)

query = None
if user_role:
query1 = db.session.query(Role).filter(Role.deleted.is_(False)).filter(Role.uid.isnot(None))
query = query.union(query1)
query = db.session.query(Role).filter(Role.deleted.is_(False)).filter(
or_(Role.uid.isnot(None), and_(Role.app_id.in_([app_id, None]), Role.uid.is_(None)))
pycook marked this conversation as resolved.
Show resolved Hide resolved
)

if user_only:
query = db.session.query(Role).filter(Role.deleted.is_(False)).filter(Role.uid.isnot(None))

if query is None:
query = db.session.query(Role).filter(
Role.deleted.is_(False), Role.uid.is_(None),
Role.app_id.in_([app_id, None])
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,in query用or替代


if not is_all:
role_ids = list(HasResourceRoleCache.get(app_id).keys())
query = query.filter(Role.id.in_(role_ids))
Expand Down
11 changes: 4 additions & 7 deletions cmdb-api/api/views/acl/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ def get(self):
page_size = get_page_size(request.values.get("page_size"))
q = request.values.get('q')
app_id = request.values.get('app_id')
is_all = request.values.get('is_all', True)
is_all = True if is_all in current_app.config.get("BOOL_TRUE") else False
user_role = request.values.get('user_role', True)
user_only = request.values.get('user_only', False)
user_role = True if user_role in current_app.config.get("BOOL_TRUE") else False
user_only = True if user_only in current_app.config.get("BOOL_TRUE") else False

is_all = request.values.get('is_all', True) in current_app.config.get("BOOL_TRUE")
user_role = request.values.get('user_role', True) in current_app.config.get("BOOL_TRUE")
user_only = request.values.get('user_only', False) in current_app.config.get("BOOL_TRUE")

numfound, roles = RoleCRUD.search(q, app_id, page, page_size, user_role, is_all, user_only)

id2parents = RoleRelationCRUD.get_parents([i.id for i in roles], app_id=app_id)
Expand Down
Loading