Skip to content

Commit

Permalink
feat(api): acl supports channel
Browse files Browse the repository at this point in the history
  • Loading branch information
pycook committed Sep 9, 2024
1 parent 8ec0d61 commit bf05ea2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
6 changes: 3 additions & 3 deletions cmdb-api/api/lib/perm/acl/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def add_trigger_log(cls, app_id, trigger_id, operate_type: AuditOperateType,
origin=origin, current=current, extra=extra, source=source.value)

@classmethod
def add_login_log(cls, username, is_ok, description, _id=None, logout_at=None):
def add_login_log(cls, username, is_ok, description, _id=None, logout_at=None, ip=None, browser=None):
if _id is not None:
existed = AuditLoginLog.get_by_id(_id)
if existed is not None:
Expand All @@ -387,8 +387,8 @@ def add_login_log(cls, username, is_ok, description, _id=None, logout_at=None):
is_ok=is_ok,
description=description,
logout_at=logout_at,
ip=request.headers.get('X-Real-IP') or request.remote_addr,
browser=request.headers.get('User-Agent'),
ip=ip or request.headers.get('X-Real-IP') or request.remote_addr,
browser=browser or request.headers.get('User-Agent'),
channel=request.values.get('channel', 'web'),
)

Expand Down
22 changes: 16 additions & 6 deletions cmdb-api/api/views/acl/login.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding:utf-8 -*-

import datetime

import jwt
import six
from flask import abort
Expand All @@ -17,10 +16,12 @@
from api.lib.decorator import args_validate
from api.lib.perm.acl.acl import ACLManager
from api.lib.perm.acl.audit import AuditCRUD
from api.lib.perm.acl.cache import AppCache
from api.lib.perm.acl.cache import RoleCache
from api.lib.perm.acl.cache import User
from api.lib.perm.acl.cache import UserCache
from api.lib.perm.acl.resp_format import ErrFormat
from api.lib.perm.acl.role import RoleRelationCRUD
from api.lib.perm.auth import auth_abandoned
from api.lib.perm.auth import auth_with_app_token
from api.models.acl import Role
Expand Down Expand Up @@ -124,10 +125,17 @@ def post(self):
if not user.get('username'):
user['username'] = user.get('name')

return self.jsonify(user=user,
authenticated=authenticated,
rid=role and role.id,
can_proxy=can_proxy)
result = dict(user=user,
authenticated=authenticated,
rid=role and role.id,
can_proxy=can_proxy)

if request.values.get('need_parentRoles') in current_app.config.get('BOOL_TRUE'):
app_id = AppCache.get(request.values.get('app_id'))
parent_ids = RoleRelationCRUD.recursive_parent_ids(role and role.id, app_id and app_id.id)
result['user']['parentRoles'] = [RoleCache.get(rid).name for rid in set(parent_ids) if RoleCache.get(rid)]

return self.jsonify(result)


class AuthWithTokenView(APIView):
Expand Down Expand Up @@ -184,6 +192,8 @@ class LogoutView(APIView):
def post(self):
logout_user()

AuditCRUD.add_login_log(None, None, None, _id=session.get('LOGIN_ID'), logout_at=datetime.datetime.now())
AuditCRUD.add_login_log(None, None, None,
_id=session.get('LOGIN_ID') or request.values.get('LOGIN_ID'),
logout_at=datetime.datetime.now())

self.jsonify(code=200)
8 changes: 8 additions & 0 deletions cmdb-api/api/views/acl/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from api.lib.decorator import args_required
from api.lib.decorator import args_validate
from api.lib.perm.acl.acl import ACLManager
from api.lib.perm.acl.acl import AuditCRUD
from api.lib.perm.acl.acl import role_required
from api.lib.perm.acl.cache import AppCache
from api.lib.perm.acl.cache import UserCache
Expand Down Expand Up @@ -48,6 +49,13 @@ def get(self):
role=dict(permissions=user_info.get('parents')),
avatar=user_info.get('avatar'))

if request.values.get('channel'):
_id = AuditCRUD.add_login_log(name, True, ErrFormat.login_succeed,
ip=request.values.get('ip'),
browser=request.values.get('browser'))
session['LOGIN_ID'] = _id
result['LOGIN_ID'] = _id

current_app.logger.info("get user info for3: {}".format(result))
return self.jsonify(result=result)

Expand Down

0 comments on commit bf05ea2

Please sign in to comment.