MCPcopy Create free account
hub / github.com/53AI/53AIHub / RegisterUserToInternal

Method RegisterUserToInternal

api/service/user_service.go:296–370  ·  view source on GitHub ↗
(eid int64, mappings []UserDepartmentMapping)

Source from the content-addressed store, hash-verified

294}
295
296func (s *UserService) RegisterUserToInternal(eid int64, mappings []UserDepartmentMapping) (*RegisterUserToInternalResult, error) {
297 tx := model.DB.Begin()
298 if tx.Error != nil {
299 return nil, tx.Error
300 }
301
302 result := &RegisterUserToInternalResult{
303 SuccessCount: 0,
304 FailedUsers: []string{},
305 Total: len(mappings),
306 }
307
308 for _, mapping := range mappings {
309 user, err := model.GetUserByID(mapping.UserID)
310 if err != nil {
311 result.FailedUsers = append(result.FailedUsers, fmt.Sprintf("User ID %d not found", mapping.UserID))
312 continue
313 }
314
315 if user.Eid != eid {
316 result.FailedUsers = append(result.FailedUsers, fmt.Sprintf("User ID %d does not belong to this enterprise", mapping.UserID))
317 continue
318 }
319
320 // 转换为内部用户:更新类型并清空主分组
321 if err := tx.Model(&model.User{}).Where("user_id = ?", mapping.UserID).Updates(map[string]interface{}{
322 "type": model.UserTypeInternal,
323 "group_id": 0,
324 }).Error; err != nil {
325 result.FailedUsers = append(result.FailedUsers, fmt.Sprintf("Failed to update user type for user ID %d", mapping.UserID))
326 continue
327 }
328
329 // 清理用户在 ResourcePermission 表中的分组关联(GroupIds 计算来源)
330 if err := tx.Where("resource_id = ? AND resource_type = ?", mapping.UserID, model.ResourceTypeUser).Delete(&model.ResourcePermission{}).Error; err != nil {
331 result.FailedUsers = append(result.FailedUsers, fmt.Sprintf("Failed to clear group permissions for user ID %d", mapping.UserID))
332 continue
333 }
334
335 for _, did := range mapping.DIDs {
336 if did > 0 {
337 relation := model.MemberDepartmentRelation{
338 BID: mapping.UserID,
339 DID: did,
340 EID: eid,
341 }
342
343 var count int64
344 tx.Model(&model.MemberDepartmentRelation{}).
345 Where("bid = ? AND did = ? AND eid = ?", mapping.UserID, did, eid).
346 Count(&count)
347
348 if count == 0 {
349 if err := tx.Create(&relation).Error; err != nil {
350 result.FailedUsers = append(result.FailedUsers, fmt.Sprintf("Failed to create department relation for user ID %d and department ID %d", mapping.UserID, did))
351 continue
352 }
353 }

Callers 1

RegisterUserToInternalFunction · 0.95

Calls 3

DeleteMethod · 0.65
CreateMethod · 0.45

Tested by

no test coverage detected