mirror of https://github.com/veops/cmdb.git
fix(api): refresh rid after create and import employee (#328)
This commit is contained in:
parent
20f3e917fe
commit
72b2f8b6de
|
@ -15,10 +15,13 @@ from wtforms import validators
|
||||||
|
|
||||||
from api.extensions import db
|
from api.extensions import db
|
||||||
from api.lib.common_setting.acl import ACLManager
|
from api.lib.common_setting.acl import ACLManager
|
||||||
from api.lib.common_setting.const import COMMON_SETTING_QUEUE, OperatorType
|
from api.lib.common_setting.const import OperatorType
|
||||||
|
from api.lib.cmdb.const import CMDB_QUEUE
|
||||||
from api.lib.common_setting.resp_format import ErrFormat
|
from api.lib.common_setting.resp_format import ErrFormat
|
||||||
from api.models.common_setting import Employee, Department
|
from api.models.common_setting import Employee, Department
|
||||||
|
|
||||||
|
from api.tasks.common_setting import refresh_employee_acl_info, edit_employee_department_in_acl
|
||||||
|
|
||||||
acl_user_columns = [
|
acl_user_columns = [
|
||||||
'email',
|
'email',
|
||||||
'mobile',
|
'mobile',
|
||||||
|
@ -137,7 +140,9 @@ class EmployeeCRUD(object):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add(**kwargs):
|
def add(**kwargs):
|
||||||
try:
|
try:
|
||||||
return CreateEmployee().create_single(**kwargs)
|
res = CreateEmployee().create_single(**kwargs)
|
||||||
|
refresh_employee_acl_info.apply_async(args=(), queue=CMDB_QUEUE)
|
||||||
|
return res
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
abort(400, str(e))
|
abort(400, str(e))
|
||||||
|
|
||||||
|
@ -164,10 +169,9 @@ class EmployeeCRUD(object):
|
||||||
existed.update(**kwargs)
|
existed.update(**kwargs)
|
||||||
|
|
||||||
if len(e_list) > 0:
|
if len(e_list) > 0:
|
||||||
from api.tasks.common_setting import edit_employee_department_in_acl
|
|
||||||
edit_employee_department_in_acl.apply_async(
|
edit_employee_department_in_acl.apply_async(
|
||||||
args=(e_list, new_department_id, current_user.uid),
|
args=(e_list, new_department_id, current_user.uid),
|
||||||
queue=COMMON_SETTING_QUEUE
|
queue=CMDB_QUEUE
|
||||||
)
|
)
|
||||||
|
|
||||||
return existed
|
return existed
|
||||||
|
@ -291,7 +295,7 @@ class EmployeeCRUD(object):
|
||||||
employees = []
|
employees = []
|
||||||
for r in pagination.items:
|
for r in pagination.items:
|
||||||
d = r.Employee.to_dict()
|
d = r.Employee.to_dict()
|
||||||
d['department_name'] = r.Department.department_name
|
d['department_name'] = r.Department.department_name if r.Department else ''
|
||||||
employees.append(d)
|
employees.append(d)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -437,7 +441,7 @@ class EmployeeCRUD(object):
|
||||||
employees = []
|
employees = []
|
||||||
for r in pagination.items:
|
for r in pagination.items:
|
||||||
d = r.Employee.to_dict()
|
d = r.Employee.to_dict()
|
||||||
d['department_name'] = r.Department.department_name
|
d['department_name'] = r.Department.department_name if r.Department else ''
|
||||||
employees.append(d)
|
employees.append(d)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -571,6 +575,7 @@ class EmployeeCRUD(object):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def import_employee(employee_list):
|
def import_employee(employee_list):
|
||||||
res = CreateEmployee().batch_create(employee_list)
|
res = CreateEmployee().batch_create(employee_list)
|
||||||
|
refresh_employee_acl_info.apply_async(args=(), queue=CMDB_QUEUE)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -1,24 +1,24 @@
|
||||||
# -*- coding:utf-8 -*-
|
# -*- coding:utf-8 -*-
|
||||||
import requests
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
|
|
||||||
from api.extensions import celery
|
from api.extensions import celery
|
||||||
from api.extensions import db
|
|
||||||
from api.lib.common_setting.acl import ACLManager
|
from api.lib.common_setting.acl import ACLManager
|
||||||
from api.lib.common_setting.const import COMMON_SETTING_QUEUE
|
from api.lib.cmdb.const import CMDB_QUEUE
|
||||||
from api.lib.common_setting.resp_format import ErrFormat
|
from api.lib.common_setting.resp_format import ErrFormat
|
||||||
from api.models.common_setting import Department
|
from api.models.common_setting import Department, Employee
|
||||||
|
from api.lib.decorator import flush_db
|
||||||
|
from api.lib.decorator import reconnect_db
|
||||||
|
|
||||||
|
|
||||||
@celery.task(name="common_setting.edit_employee_department_in_acl", queue=COMMON_SETTING_QUEUE)
|
@celery.task(name="common_setting.edit_employee_department_in_acl", queue=CMDB_QUEUE)
|
||||||
|
@flush_db
|
||||||
|
@reconnect_db
|
||||||
def edit_employee_department_in_acl(e_list, new_d_id, op_uid):
|
def edit_employee_department_in_acl(e_list, new_d_id, op_uid):
|
||||||
"""
|
"""
|
||||||
:param e_list:{acl_rid: 11, department_id: 22}
|
:param e_list:{acl_rid: 11, department_id: 22}
|
||||||
:param new_d_id
|
:param new_d_id
|
||||||
:param op_uid
|
:param op_uid
|
||||||
"""
|
"""
|
||||||
db.session.remove()
|
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
new_department = Department.get_by(
|
new_department = Department.get_by(
|
||||||
first=True, department_id=new_d_id, to_dict=False)
|
first=True, department_id=new_d_id, to_dict=False)
|
||||||
|
@ -75,3 +75,41 @@ def edit_employee_department_in_acl(e_list, new_d_id, op_uid):
|
||||||
result.append(ErrFormat.acl_add_user_to_role_failed.format(str(e)))
|
result.append(ErrFormat.acl_add_user_to_role_failed.format(str(e)))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@celery.task(name="common_setting.refresh_employee_acl_info", queue=CMDB_QUEUE)
|
||||||
|
@flush_db
|
||||||
|
@reconnect_db
|
||||||
|
def refresh_employee_acl_info():
|
||||||
|
acl = ACLManager('acl')
|
||||||
|
role_map = {role['name']: role for role in acl.get_all_roles()}
|
||||||
|
|
||||||
|
criterion = [
|
||||||
|
Employee.deleted == 0
|
||||||
|
]
|
||||||
|
query = Employee.query.filter(*criterion).order_by(
|
||||||
|
Employee.created_at.desc()
|
||||||
|
)
|
||||||
|
|
||||||
|
for em in query.all():
|
||||||
|
if em.acl_uid and em.acl_rid:
|
||||||
|
continue
|
||||||
|
role = role_map.get(em.username, None)
|
||||||
|
if not role:
|
||||||
|
continue
|
||||||
|
|
||||||
|
params = dict()
|
||||||
|
if not em.acl_uid:
|
||||||
|
params['acl_uid'] = role.get('uid', 0)
|
||||||
|
|
||||||
|
if not em.acl_rid:
|
||||||
|
params['acl_rid'] = role.get('id', 0)
|
||||||
|
|
||||||
|
try:
|
||||||
|
em.update(**params)
|
||||||
|
current_app.logger.info(
|
||||||
|
f"refresh_employee_acl_info success, employee_id: {em.employee_id}, uid: {em.acl_uid}, "
|
||||||
|
f"rid: {em.acl_rid}")
|
||||||
|
except Exception as e:
|
||||||
|
current_app.logger.error(str(e))
|
||||||
|
continue
|
||||||
|
|
Loading…
Reference in New Issue