From d5fbe42ed7614391c7ab8f63845a2d19863eb2ca Mon Sep 17 00:00:00 2001 From: pycook Date: Sun, 8 Dec 2019 00:20:55 +0800 Subject: [PATCH] relation view bugfix --- api/lib/perm/acl/acl.py | 5 ++ api/lib/perm/acl/role.py | 9 ++ api/views/acl/user.py | 1 + .../views/acl/module/addRoleRelationForm.vue | 13 ++- ui/src/views/acl/module/permissionForm.vue | 3 - ui/src/views/acl/module/resourceForm.vue | 2 - ui/src/views/acl/module/resourceTypeForm.vue | 3 - ui/src/views/acl/module/roleForm.vue | 16 ++-- ui/src/views/acl/module/userForm.vue | 3 - ui/src/views/acl/permissions.vue | 2 - ui/src/views/acl/resource_types.vue | 3 - ui/src/views/acl/roles.vue | 2 - ui/src/views/acl/users.vue | 2 - ui/src/views/cmdb/relation_views/index.vue | 82 +++++++++++++++---- 14 files changed, 99 insertions(+), 47 deletions(-) diff --git a/api/lib/perm/acl/acl.py b/api/lib/perm/acl/acl.py index f8400fd..7bc183e 100644 --- a/api/lib/perm/acl/acl.py +++ b/api/lib/perm/acl/acl.py @@ -9,6 +9,7 @@ from flask import session, abort from api.lib.cmdb.const import ResourceTypeEnum as CmdbResourceType from api.lib.cmdb.const import RoleEnum from api.lib.perm.acl.cache import AppCache +from api.lib.perm.acl.cache import RoleCache from api.lib.perm.acl.cache import UserCache from api.lib.perm.acl.permission import PermissionCRUD from api.lib.perm.acl.resource import ResourceCRUD @@ -123,6 +124,10 @@ def is_app_admin(): if RoleEnum.CONFIG in session.get("acl", {}).get("parentRoles", []): return True + for role in session.get("acl", {}).get("parentRoles", []): + if RoleCache.get(role).is_app_admin: + return True + return False diff --git a/api/lib/perm/acl/role.py b/api/lib/perm/acl/role.py index f536bdf..08f58da 100644 --- a/api/lib/perm/acl/role.py +++ b/api/lib/perm/acl/role.py @@ -81,6 +81,9 @@ class RoleRelationCRUD(object): def add(parent_id, child_id): RoleRelation.get_by(parent_id=parent_id, child_id=child_id) and abort(400, "It's already existed") + RoleRelationCache.clean(parent_id) + RoleRelationCache.clean(child_id) + return RoleRelation.create(parent_id=parent_id, child_id=child_id) @classmethod @@ -91,6 +94,9 @@ class RoleRelationCRUD(object): for child_id in child_ids: role_rebuild.apply_async(args=(child_id,), queue=ACL_QUEUE) + RoleRelationCache.clean(existed.parent_id) + RoleRelationCache.clean(existed.child_id) + existed.soft_delete() @classmethod @@ -102,6 +108,9 @@ class RoleRelationCRUD(object): for child_id in child_ids: role_rebuild.apply_async(args=(child_id,), queue=ACL_QUEUE) + RoleRelationCache.clean(existed.parent_id) + RoleRelationCache.clean(existed.child_id) + existed.soft_delete() diff --git a/api/views/acl/user.py b/api/views/acl/user.py index bf3cb9a..4fb1093 100644 --- a/api/views/acl/user.py +++ b/api/views/acl/user.py @@ -20,6 +20,7 @@ class GetUserInfoView(APIView): name = session.get("CAS_USERNAME") or current_user.nickname role = dict(permissions=session.get("acl", {}).get("parentRoles", [])) avatar = current_user.avatar + return self.jsonify(result=dict(name=name, role=role, avatar=avatar)) diff --git a/ui/src/views/acl/module/addRoleRelationForm.vue b/ui/src/views/acl/module/addRoleRelationForm.vue index 5f92dd0..f2d0265 100644 --- a/ui/src/views/acl/module/addRoleRelationForm.vue +++ b/ui/src/views/acl/module/addRoleRelationForm.vue @@ -15,8 +15,10 @@ :wrapper-col="formItemLayout.wrapperCol" label="角色列表" > - - {{ role.name }} + + @@ -97,10 +99,13 @@ export default { } : {} } - }, - mounted () { }, methods: { + filterOption (input, option) { + return ( + option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0 + ) + }, onClose () { this.form.resetFields() this.drawerVisible = false diff --git a/ui/src/views/acl/module/permissionForm.vue b/ui/src/views/acl/module/permissionForm.vue index 6776491..b473175 100644 --- a/ui/src/views/acl/module/permissionForm.vue +++ b/ui/src/views/acl/module/permissionForm.vue @@ -151,7 +151,6 @@ export default { handleEdit (record) { this.drawerVisible = true - console.log(record) this.$nextTick(() => { this.form.setFieldsValue({ id: record.id, @@ -165,8 +164,6 @@ export default { e.preventDefault() this.form.validateFields((err, values) => { if (!err) { - console.log('Received values of form: ', values) - values.app_id = this.$route.name.split('_')[0] values.perms = this.perms if (values.id) { diff --git a/ui/src/views/acl/module/resourceForm.vue b/ui/src/views/acl/module/resourceForm.vue index a6b9a91..9f92fd5 100644 --- a/ui/src/views/acl/module/resourceForm.vue +++ b/ui/src/views/acl/module/resourceForm.vue @@ -138,8 +138,6 @@ export default { e.preventDefault() this.form.validateFields((err, values) => { if (!err) { - console.log('Received values of form: ', values) - values.app_id = this.$route.name.split('_')[0] if (values.id) { this.updateResource(values.id, values) diff --git a/ui/src/views/acl/module/resourceTypeForm.vue b/ui/src/views/acl/module/resourceTypeForm.vue index 151bb1b..e2b2c8d 100644 --- a/ui/src/views/acl/module/resourceTypeForm.vue +++ b/ui/src/views/acl/module/resourceTypeForm.vue @@ -132,7 +132,6 @@ export default { handleEdit (record) { this.drawerVisible = true - console.log(record) this.perms = record.perms this.$nextTick(() => { this.form.setFieldsValue({ @@ -147,8 +146,6 @@ export default { e.preventDefault() this.form.validateFields((err, values) => { if (!err) { - console.log('Received values of form: ', values) - values.app_id = this.$route.name.split('_')[0] values.perms = this.perms if (values.id) { diff --git a/ui/src/views/acl/module/roleForm.vue b/ui/src/views/acl/module/roleForm.vue index ce01ff4..ce92473 100644 --- a/ui/src/views/acl/module/roleForm.vue +++ b/ui/src/views/acl/module/roleForm.vue @@ -29,8 +29,11 @@ - {{ role.name }} + mode="multiple" + :filterOption="filterOption"> + = 0 + ) + }, handleCreate () { this.drawerTitle = '新增' this.drawerVisible = true @@ -162,7 +167,6 @@ export default { e.preventDefault() this.form.validateFields((err, values) => { if (!err) { - console.log('Received values of form: ', values) values.app_id = this.$route.name.split('_')[0] if (values.id) { this.updateRole(values.id, values) diff --git a/ui/src/views/acl/module/userForm.vue b/ui/src/views/acl/module/userForm.vue index d19d909..2612fc5 100644 --- a/ui/src/views/acl/module/userForm.vue +++ b/ui/src/views/acl/module/userForm.vue @@ -205,7 +205,6 @@ export default { handleEdit (record) { this.drawerVisible = true - console.log(record) this.$nextTick(() => { this.form.setFieldsValue({ id: record.uid, @@ -225,8 +224,6 @@ export default { e.preventDefault() this.form.validateFields((err, values) => { if (!err) { - console.log('Received values of form: ', values) - if (values.id) { this.updateUser(values.id, values) } else { diff --git a/ui/src/views/acl/permissions.vue b/ui/src/views/acl/permissions.vue index 50df3cf..f55affe 100644 --- a/ui/src/views/acl/permissions.vue +++ b/ui/src/views/acl/permissions.vue @@ -155,7 +155,6 @@ export default { delete parameter.pageNo delete parameter.pageSize Object.assign(parameter, this.queryParam) - console.log('loadData.parameter', parameter) return searchResourceType(parameter) .then(res => { @@ -165,7 +164,6 @@ export default { res.totalPage = Math.ceil(res.numfound / parameter.pageSize) res.data = res.groups this.id2perms = res.id2perms - console.log('loadData.res', res) return res }) }, diff --git a/ui/src/views/acl/resource_types.vue b/ui/src/views/acl/resource_types.vue index e19c2fb..d663e74 100644 --- a/ui/src/views/acl/resource_types.vue +++ b/ui/src/views/acl/resource_types.vue @@ -155,7 +155,6 @@ export default { delete parameter.pageNo delete parameter.pageSize Object.assign(parameter, this.queryParam) - console.log('loadData.parameter', parameter) return searchResourceType(parameter) .then(res => { @@ -165,7 +164,6 @@ export default { res.totalPage = Math.ceil(res.numfound / parameter.pageSize) res.data = res.groups this.id2perms = res.id2perms - console.log('loadData.res', res) return res }) }, @@ -249,7 +247,6 @@ export default { } } record.perms = perms - console.log(record) this.$refs.resourceTypeForm.handleEdit(record) }, handleDelete (record) { diff --git a/ui/src/views/acl/roles.vue b/ui/src/views/acl/roles.vue index efcd5c6..00a4602 100644 --- a/ui/src/views/acl/roles.vue +++ b/ui/src/views/acl/roles.vue @@ -155,7 +155,6 @@ export default { delete parameter.pageNo delete parameter.pageSize Object.assign(parameter, this.queryParam) - console.log('loadData.parameter', parameter) return searchRole(parameter) .then(res => { @@ -165,7 +164,6 @@ export default { res.totalPage = Math.ceil(res.numfound / parameter.pageSize) res.data = res.roles - console.log('loadData.res', res) this.allRoles = res.roles this.id2parents = res.id2parents return res diff --git a/ui/src/views/acl/users.vue b/ui/src/views/acl/users.vue index 6ee94f3..255654d 100644 --- a/ui/src/views/acl/users.vue +++ b/ui/src/views/acl/users.vue @@ -218,7 +218,6 @@ export default { delete parameter.pageNo delete parameter.pageSize Object.assign(parameter, this.queryParam) - console.log('loadData.parameter', parameter) return searchUser(parameter) .then(res => { @@ -228,7 +227,6 @@ export default { res.totalPage = Math.ceil(res.numfound / parameter.pageSize) res.data = res.users - console.log('loadData.res', res) this.allUsers = res.users return res }) diff --git a/ui/src/views/cmdb/relation_views/index.vue b/ui/src/views/cmdb/relation_views/index.vue index 9a3479f..afb0cf1 100644 --- a/ui/src/views/cmdb/relation_views/index.vue +++ b/ui/src/views/cmdb/relation_views/index.vue @@ -62,7 +62,9 @@ export default { relationViews: {}, levels: [], showTypeIds: [], + origShowTypeIds: [], showTypes: [], + origShowTypes: [], leaf2showTypes: {}, node2ShowTypes: {}, leaf: [], @@ -84,7 +86,7 @@ export default { console.log(parameter, 'load instances') this.parameter = parameter const params = Object.assign(parameter || {}, this.$refs.search.queryParam) - let q = `q=_type:${this.currentTypeId[0]}` + let q = '' Object.keys(params).forEach(key => { if (!['pageNo', 'pageSize', 'sortField', 'sortOrder'].includes(key) && params[key] + '' !== '') { if (typeof params[key] === 'object' && params[key].length > 1) { @@ -109,8 +111,13 @@ export default { } q += `&sort=${order}${params['sortField']}` } + if (q && q[0] === ',') { + q = q.slice(1) + } if (this.treeKeys.length === 0) { + this.judgeCITypes(q) + q = `q=_type:${this.currentTypeId[0]},` + q return searchCI(q).then(res => { const result = {} result.pageNo = res.page @@ -123,7 +130,7 @@ export default { setTimeout(() => { this.setColumnWidth() console.log('set column') - }, 300) + }, 100) } this.loadRoot() return result @@ -133,13 +140,6 @@ export default { q += `&root_id=${this.treeKeys[this.treeKeys.length - 1].split('_')[0]}` const typeId = parseInt(this.treeKeys[this.treeKeys.length - 1].split('_')[1]) - this.showTypes = this.node2ShowTypes[typeId + ''] - const showTypeIds = [] - this.showTypes.forEach(item => { - showTypeIds.push(item.id) - }) - this.showTypeIds = showTypeIds - let level = [] if (!this.leaf.includes(typeId)) { let startIdx = 0 @@ -160,9 +160,8 @@ export default { level = [1] } q += `&level=${level.join(',')}` - if (q[0] === '&') { - q = q.slice(1) - } + this.judgeCITypes(q) + q = `q=_type:${this.currentTypeId[0]},` + q return searchCIRelation(q).then(res => { const result = {} result.pageNo = res.page @@ -176,7 +175,7 @@ export default { setTimeout(() => { this.setColumnWidth() console.log('set column') - }, 300) + }, 100) this.loadNoRoot(this.treeKeys[this.treeKeys.length - 1], level) } return result @@ -212,6 +211,55 @@ export default { }, 100) }, + async judgeCITypes (q) { + const showTypeIds = [] + let _showTypes = [] + let _showTypeIds = [] + + if (this.treeKeys.length) { + const typeId = parseInt(this.treeKeys[this.treeKeys.length - 1].split('_')[1]) + + _showTypes = this.node2ShowTypes[typeId + ''] + _showTypes.forEach(item => { + _showTypeIds.push(item.id) + }) + } else { + _showTypeIds = JSON.parse(JSON.stringify(this.origShowTypeIds)) + _showTypes = JSON.parse(JSON.stringify(this.origShowTypes)) + } + + const promises = _showTypeIds.map(typeId => { + const _q = (`q=_type:${typeId},` + q).replace(/count=\d*/, 'count=1') + if (this.treeKeys.length === 0) { + return searchCI(_q).then(res => { + if (res.numfound !== 0) { + showTypeIds.push(typeId) + } + }) + } else { + return searchCIRelation(_q).then(res => { + if (res.numfound !== 0) { + showTypeIds.push(typeId) + } + }) + } + }) + await Promise.all(promises) + if (showTypeIds.length && showTypeIds.length !== this.showTypeIds.length) { + const showTypes = [] + _showTypes.forEach(item => { + if (showTypeIds.includes(item.id)) { + showTypes.push(item) + } + }) + this.showTypes = showTypes + this.showTypeIds = showTypeIds + if (this.currentTypeId.length && !this.showTypeIds.includes(this.currentTypeId[0])) { + this.currentTypeId = this.showTypeIds[0] + } + } + }, + async loadRoot () { searchCI(`q=_type:(${this.levels[0].join(';')})&count=10000`).then(async res => { const facet = [] @@ -335,17 +383,17 @@ export default { } }) this.levels = this.relationViews.views[this.viewName].topo - this.showTypes = this.relationViews.views[this.viewName].show_types + this.origShowTypes = this.relationViews.views[this.viewName].show_types const showTypeIds = [] - this.showTypes.forEach(item => { + this.origShowTypes.forEach(item => { showTypeIds.push(item.id) }) - this.showTypeIds = showTypeIds + this.origShowTypeIds = showTypeIds this.leaf2showTypes = this.relationViews.views[this.viewName].leaf2show_types this.node2ShowTypes = this.relationViews.views[this.viewName].node2show_types this.leaf = this.relationViews.views[this.viewName].leaf this.currentView = [this.viewId] - this.currentTypeId = [this.showTypeIds[0]] + this.currentTypeId = [this.origShowTypeIds[0]] this.typeId = this.levels[0][0] this.loadColumns() this.$refs.table && this.$refs.table.refresh(true)