Skip to content

Commit df49c5b

Browse files
authored
fix: Automatic authorization for resource creation (#3464)
1 parent 0b27836 commit df49c5b

File tree

6 files changed

+77
-62
lines changed

6 files changed

+77
-62
lines changed

apps/application/serializers/application.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
from maxkb.conf import PROJECT_DIR
4141
from models_provider.models import Model
4242
from models_provider.tools import get_model_instance_by_model_workspace_id
43-
from system_manage.models import WorkspaceUserResourcePermission
43+
from system_manage.models import WorkspaceUserResourcePermission, AuthTargetType
44+
from system_manage.serializers.user_resource_permission import UserResourcePermissionSerializer
4445
from tools.models import Tool, ToolScope
4546
from tools.serializers.tool import ToolModelSerializer
4647
from users.models import User
@@ -430,9 +431,15 @@ class ApplicationSerializer(serializers.Serializer):
430431
def insert(self, instance: Dict):
431432
application_type = instance.get('type')
432433
if 'WORK_FLOW' == application_type:
433-
return self.insert_workflow(instance)
434+
r = self.insert_workflow(instance)
434435
else:
435-
return self.insert_simple(instance)
436+
r = self.insert_simple(instance)
437+
UserResourcePermissionSerializer(data={
438+
'workspace_id': self.data.get('workspace_id'),
439+
'user_id': self.data.get('user_id'),
440+
'auth_target_type': AuthTargetType.APPLICATION.value
441+
}).auth_resource(str(r.get('id')))
442+
return r
436443

437444
def insert_workflow(self, instance: Dict):
438445
self.is_valid(raise_exception=True)

apps/knowledge/serializers/knowledge.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from application.models import ApplicationKnowledgeMapping
2222
from common.config.embedding_config import VectorStore
2323
from common.constants.cache_version import Cache_Version
24-
from common.constants.permission_constants import ResourceAuthType, ResourcePermission
24+
from common.constants.permission_constants import ResourceAuthType, ResourcePermission, ResourcePermissionRole
2525
from common.database_model_manage.database_model_manage import DatabaseModelManage
2626
from common.db.search import native_search, get_dynamics_model, native_page_search
2727
from common.db.sql_execute import select_list
@@ -42,6 +42,7 @@
4242
from maxkb.conf import PROJECT_DIR
4343
from models_provider.models import Model
4444
from system_manage.models import WorkspaceUserResourcePermission, AuthTargetType
45+
from system_manage.serializers.user_resource_permission import UserResourcePermissionSerializer
4546
from users.serializers.user import is_workspace_manage
4647

4748

@@ -553,21 +554,12 @@ def save_base(self, instance, with_valid=True):
553554
QuerySet(ProblemParagraphMapping).bulk_create(
554555
problem_paragraph_mapping_list
555556
) if len(problem_paragraph_mapping_list) > 0 else None
556-
557-
# 自动授权给创建者
558-
WorkspaceUserResourcePermission(
559-
target=knowledge_id,
560-
auth_target_type=AuthTargetType.KNOWLEDGE,
561-
permission_list=[ResourcePermission.VIEW, ResourcePermission.MANAGE],
562-
workspace_id=self.data.get('workspace_id'),
563-
user_id=self.data.get('user_id'),
564-
auth_type=ResourceAuthType.RESOURCE_PERMISSION_GROUP
565-
).save()
566-
# 刷新缓存
567-
version = Cache_Version.PERMISSION_LIST.get_version()
568-
key = Cache_Version.PERMISSION_LIST.get_key(user_id=self.data.get('user_id'))
569-
cache.delete(key, version=version)
570-
557+
# 自动资源给授权当前用户
558+
UserResourcePermissionSerializer(data={
559+
'workspace_id': self.data.get('workspace_id'),
560+
'user_id': self.data.get('user_id'),
561+
'auth_target_type': AuthTargetType.KNOWLEDGE.value
562+
}).auth_resource(str(knowledge_id))
571563
return {
572564
**KnowledgeModelSerializer(knowledge).data,
573565
'user_id': self.data.get('user_id'),

apps/models_provider/serializers/model_serializer.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from models_provider.models import Model, Status
2727
from models_provider.tools import get_model_credential
2828
from system_manage.models import WorkspaceUserResourcePermission, AuthTargetType
29+
from system_manage.serializers.user_resource_permission import UserResourcePermissionSerializer
2930
from users.serializers.user import is_workspace_manage
3031

3132

@@ -326,19 +327,11 @@ def insert(self, workspace_id, with_valid=True):
326327
model = Model(**model_data)
327328
try:
328329
model.save()
329-
# 自动授权给创建者
330-
WorkspaceUserResourcePermission(
331-
target=model.id,
332-
auth_target_type=AuthTargetType.MODEL,
333-
permission_list=[ResourcePermission.VIEW, ResourcePermission.MANAGE],
334-
workspace_id=workspace_id,
335-
user_id=self.data.get('user_id'),
336-
auth_type=ResourceAuthType.RESOURCE_PERMISSION_GROUP
337-
).save()
338-
# 刷新缓存
339-
version = Cache_Version.PERMISSION_LIST.get_version()
340-
key = Cache_Version.PERMISSION_LIST.get_key(user_id=self.data.get('user_id'))
341-
cache.delete(key, version=version)
330+
UserResourcePermissionSerializer(data={
331+
'workspace_id': self.data.get('workspace_id'),
332+
'user_id': self.data.get('user_id'),
333+
'auth_target_type': AuthTargetType.MODEL.value
334+
}).auth_resource(str(model.id))
342335
except Exception as save_error:
343336
# 可添加日志记录
344337
raise AppApiException(500, _("Model saving failed")) from save_error

apps/system_manage/serializers/user_resource_permission.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from models_provider.models import Model
3030
from system_manage.models import WorkspaceUserResourcePermission, AuthTargetType
3131
from tools.models import Tool
32+
from users.serializers.user import is_workspace_manage
3233

3334

3435
class PermissionSerializer(serializers.Serializer):
@@ -101,6 +102,33 @@ def get_queryset(self):
101102
auth_target_type=self.data.get('auth_target_type'))
102103
}
103104

105+
def auth_resource(self, resource_id: str):
106+
self.is_valid(raise_exception=True)
107+
workspace_manage = is_workspace_manage(self.data.get('user_id'), self.data.get('workspace_id'))
108+
if not workspace_manage:
109+
auth_target_type = self.data.get('auth_target_type')
110+
workspace_id = self.data.get('workspace_id')
111+
user_id = self.data.get('user_id')
112+
wurp = QuerySet(WorkspaceUserResourcePermission).filter(auth_target_type=auth_target_type,
113+
workspace_id=workspace_id).first()
114+
auth_type = wurp.auth_type if wurp else ResourceAuthType.RESOURCE_PERMISSION_GROUP
115+
# 自动授权给创建者
116+
WorkspaceUserResourcePermission(
117+
target=resource_id,
118+
auth_target_type=auth_target_type,
119+
permission_list=[ResourcePermission.VIEW,
120+
ResourcePermission.MANAGE] if auth_type == ResourceAuthType.RESOURCE_PERMISSION_GROUP else [
121+
ResourcePermissionRole.ROLE],
122+
workspace_id=workspace_id,
123+
user_id=user_id,
124+
auth_type=auth_type
125+
).save()
126+
# 刷新缓存
127+
version = Cache_Version.PERMISSION_LIST.get_version()
128+
key = Cache_Version.PERMISSION_LIST.get_key(user_id=user_id)
129+
cache.delete(key, version=version)
130+
return True
131+
104132
def list(self, user, with_valid=True):
105133
if with_valid:
106134
self.is_valid(raise_exception=True)

apps/tools/serializers/tool.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from knowledge.models import File, FileSourceType
3030
from maxkb.const import CONFIG, PROJECT_DIR
3131
from system_manage.models import AuthTargetType, WorkspaceUserResourcePermission
32+
from system_manage.serializers.user_resource_permission import UserResourcePermissionSerializer
3233
from tools.models import Tool, ToolScope, ToolFolder, ToolType
3334
from tools.serializers.tool_folder import ToolFolderFlatSerializer
3435
from users.serializers.user import is_workspace_manage
@@ -219,20 +220,11 @@ def insert(self, instance, with_valid=True):
219220
).save()
220221

221222
# 自动授权给创建者
222-
WorkspaceUserResourcePermission(
223-
target=tool_id,
224-
auth_target_type=AuthTargetType.TOOL,
225-
permission_list=[ResourcePermission.VIEW, ResourcePermission.MANAGE],
226-
workspace_id=self.data.get('workspace_id'),
227-
user_id=self.data.get('user_id'),
228-
auth_type=ResourceAuthType.RESOURCE_PERMISSION_GROUP
229-
).save()
230-
231-
# 刷新缓存
232-
version = Cache_Version.PERMISSION_LIST.get_version()
233-
key = Cache_Version.PERMISSION_LIST.get_key(user_id=self.data.get('user_id'))
234-
cache.delete(key, version=version)
235-
223+
UserResourcePermissionSerializer(data={
224+
'workspace_id': self.data.get('workspace_id'),
225+
'user_id': self.data.get('user_id'),
226+
'auth_target_type': AuthTargetType.TOOL.value
227+
}).auth_resource(str(tool_id))
236228
return ToolSerializer.Operate(data={
237229
'id': tool_id, 'workspace_id': self.data.get('workspace_id')
238230
}).one()

ui/src/views/application/component/CreateApplicationDialog.vue

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,17 @@
8282
</el-dialog>
8383
</template>
8484
<script setup lang="ts">
85-
import {ref, watch, reactive} from 'vue'
86-
import {useRouter, useRoute} from 'vue-router'
87-
import type {ApplicationFormType} from '@/api/type/application'
88-
import type {FormInstance, FormRules} from 'element-plus'
85+
import { ref, watch, reactive } from 'vue'
86+
import { useRouter, useRoute } from 'vue-router'
87+
import type { ApplicationFormType } from '@/api/type/application'
88+
import type { FormInstance, FormRules } from 'element-plus'
8989
import applicationApi from '@/api/application/application'
90-
import {MsgSuccess, MsgAlert} from '@/utils/message'
91-
import {isWorkFlow} from '@/utils/application'
92-
import {baseNodes} from '@/workflow/common/data'
93-
import {t} from '@/locales'
94-
90+
import { MsgSuccess, MsgAlert } from '@/utils/message'
91+
import { isWorkFlow } from '@/utils/application'
92+
import { baseNodes } from '@/workflow/common/data'
93+
import { t } from '@/locales'
94+
import useStore from '@/stores'
95+
const { user } = useStore()
9596
const router = useRouter()
9697
const emit = defineEmits(['refresh'])
9798
@@ -227,17 +228,19 @@ const submitHandle = async (formEl: FormInstance | undefined) => {
227228
}
228229
console.log(applicationForm.value.type)
229230
applicationApi
230-
.postApplication(
231-
{...applicationForm.value, folder_id: currentFolder.value},
232-
loading,
233-
)
231+
.postApplication({ ...applicationForm.value, folder_id: currentFolder.value }, loading)
232+
.then((res) => {
233+
return user.profile().then(() => {
234+
return res
235+
})
236+
})
234237
.then((res) => {
235238
MsgSuccess(t('common.createSuccess'))
236239
emit('refresh')
237240
if (isWorkFlow(applicationForm.value.type)) {
238-
router.push({path: `/application/${res.data.id}/workflow`})
241+
router.push({ path: `/application/${res.data.id}/workflow` })
239242
} else {
240-
router.push({path: `/application/${res.data.id}/${res.data.type}/setting`})
243+
router.push({ path: `/application/${res.data.id}/${res.data.type}/setting` })
241244
}
242245
dialogVisible.value = false
243246
})
@@ -249,7 +252,7 @@ function selectedType(type: string) {
249252
appTemplate.value = type
250253
}
251254
252-
defineExpose({open})
255+
defineExpose({ open })
253256
</script>
254257
<style lang="scss" scoped>
255258
.radio-card {

0 commit comments

Comments
 (0)