Skip to content
Merged
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
7 changes: 4 additions & 3 deletions apps/users/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,10 @@ class UserInstance(serializers.Serializer):
)
source = serializers.CharField(required=False, label=_("Source"), max_length=20, default="LOCAL")

def is_valid(self, *, raise_exception=True):
super().is_valid(raise_exception=True)
self._check_unique_username_and_email()
def is_valid(self, *, raise_exception=True, check_unique=True):
super().is_valid(raise_exception=raise_exception)
if check_unique:
self._check_unique_username_and_email()

def _check_unique_username_and_email(self):
username = self.data.get("username")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,16 @@ function handleFileRemove() {
nextTick(() => importUsersFormRef.value?.validateField('files'))
}

function conflictTypeLabel(type: string) {
if (type === 'username') return '用户名已存在'
if (type === 'email') return '邮箱已存在'
if (type === 'nick_name') return '姓名已存在'
return `${type} 冲突`
}

function formatConflictMessage(conflicts: ChatUserSyncConflict[]) {
return conflicts
.map((conflict) => {
const conflictLabel = conflict.type === 'username' ? '用户名已存在' : conflict.type === 'nick_name' ? '姓名已存在' : `${conflict.type} 冲突`
return `${conflictLabel}:${conflict.users.join('、')}`
})
.map((conflict) => `${conflictTypeLabel(conflict.type)}:${conflict.users.join('、')}`)
.join(';')
}

Expand All @@ -116,8 +120,11 @@ function submitImportUsers() {
syncFile,
)
.then((result) => {
const conflictMessage = formatConflictMessage(result.conflict_users ?? [])
MsgSuccess(`成功导入 ${result.success_count} 个用户${conflictMessage ? `,${conflictMessage}` : ''}`)
const conflictUsers = result.conflict_users ?? []
const skippedCount = conflictUsers.reduce((sum, conflict) => sum + conflict.users.length, 0)
const conflictMessage = formatConflictMessage(conflictUsers)
const skipText = skippedCount > 0 ? `,跳过 ${skippedCount} 个用户` : ''
MsgSuccess(`成功导入 ${result.success_count} 个用户${skipText}${conflictMessage ? `,${conflictMessage}` : ''}`)
emit('refresh')
dialogVisible.value = false
})
Expand Down
18 changes: 16 additions & 2 deletions ui/src/views/system/identity/users/UserFromDrawer.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, nextTick, reactive, ref } from 'vue'
import { computed, nextTick, reactive, ref, watch } from 'vue'
import JSEncrypt from 'jsencrypt'
import CommonSystemApi from '@/api/admin/system/common'
import CurrentUserApi from '@/api/admin/auth/current-user'
Expand Down Expand Up @@ -62,6 +62,10 @@ function isAdminRole(roleId: string) {
return roleOptions.value.find(({ id }) => id === roleId)?.type === ROLE_TYPE.ADMIN
}

function isUserRole(roleId: string) {
return roleOptions.value.find(({ id }) => id === roleId)?.type === ROLE_TYPE.USER
}

function handleRoleChange(roleAssignment: SystemUserRoleAssignment, index: number) {
if (isAdminRole(roleAssignment.role_id)) {
roleAssignment.workspace_ids = []
Expand Down Expand Up @@ -99,6 +103,16 @@ const selectedWorkspaceIds = computed(() => {
return [...new Set(userForm.role_setting.flatMap(({ workspace_ids }) => workspace_ids))]
})

// 用户组仅在选择包含普通用户角色时展示。
const showUserGroup = computed(() => userForm.role_setting.some(({ role_id }) => isUserRole(role_id)))

// 移除普通用户角色后清空已选用户组,避免随角色变更残留提交。
watch(showUserGroup, (visible) => {
if (!visible) {
userForm.user_group_ids = []
}
})

// 默认密码
function loadDefaultPassword() {
return CommonSystemApi.getDefaultPassword().then(({ password }) => {
Expand Down Expand Up @@ -292,7 +306,7 @@ defineExpose({ open })
</template>
</MkFormList>
</section>
<section v-if="selectedWorkspaceIds.length">
<section v-if="showUserGroup">
<h4 class="mk-title-decoration mb-4 mt-4">用户组</h4>
<UserGroupSetting v-model="userForm.user_group_ids" :workspace-ids="selectedWorkspaceIds" :workspace-options="workspaceOptions" />
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,16 @@ function handleDownloadTemplate() {
UserManageApi.getUserManageImportTemplate()
}

function conflictTypeLabel(type: ChatUserSyncConflict['type']) {
if (type === 'username') return '用户名已存在'
if (type === 'email') return '邮箱已存在'
if (type === 'nick_name') return '姓名已存在'
return `${type} 冲突`
}

function formatConflictMessage(conflicts: ChatUserSyncConflict[]) {
return conflicts
.map((conflict) => {
const conflictLabel = conflict.type === 'username' ? '用户名已存在' : conflict.type === 'nick_name' ? '姓名已存在' : `${conflict.type} 冲突`
return `${conflictLabel}:${conflict.users.join('、')}`
})
.map((conflict) => `${conflictTypeLabel(conflict.type)}:${conflict.users.join('、')}`)
.join(';')
}

Expand All @@ -209,8 +213,11 @@ function submitImportUsers() {
importUsersForm.userGroupIds[0],
)
.then((result) => {
const conflictMessage = formatConflictMessage(result.conflict_users ?? [])
MsgSuccess(`成功导入 ${result.success_count} 个用户${conflictMessage ? `,${conflictMessage}` : ''}`)
const conflictUsers = result.conflict_users ?? []
const skippedCount = conflictUsers.reduce((sum, conflict) => sum + conflict.users.length, 0)
const conflictMessage = formatConflictMessage(conflictUsers)
const skipText = skippedCount > 0 ? `,跳过 ${skippedCount} 个用户` : ''
MsgSuccess(`成功导入 ${result.success_count} 个用户${skipText}${conflictMessage ? `,${conflictMessage}` : ''}`)
emit('refresh')
dialogVisible.value = false
})
Expand Down
Loading