public async Task <string> Update(UpdateUserGroupInput input) { input.CheckDataAnnotations().CheckValidResult(); var userGroup = await _userGroupRepository.SingleOrDefaultAsync(p => p.Id == input.Id); if (userGroup == null) { throw new BusinessException($"不存在Id为{input.Id}的用户组"); } userGroup = input.MapTo(userGroup); await _userGroupRepository.UpdateAsync(userGroup); return("更新用户组成功"); }
public async Task Update(UpdateUserGroupInput input) { var userGroup = await _userGroupRepository.SingleOrDefaultAsync(p => p.Id == input.Id); if (userGroup == null) { throw new BusinessException($"不存在Id为{input.Id}的用户组"); } userGroup = input.MapTo(userGroup); await UnitOfWorkAsync(async (conn, trans) => { await _userGroupRepository.UpdateAsync(userGroup, conn, trans); await _userGroupRoleRepository.DeleteAsync(p => p.UserGroupId == userGroup.Id, conn, trans); await _userUserGroupRelationRepository.DeleteAsync(p => p.UserGroupId == userGroup.Id, conn, trans); foreach (var userId in input.UserIds) { var userInfo = await _userRepository.SingleOrDefaultAsync(p => p.Id == userId); if (userInfo == null) { throw new BusinessException($"不存在用户Id为{userId}的用户信息"); } await _userUserGroupRelationRepository.InsertAsync(new UserUserGroupRelation() { UserGroupId = userGroup.Id, UserId = userId }, conn, trans); } foreach (var roleId in input.RoleIds) { var roleInfo = await _roleRepository.SingleOrDefaultAsync(p => p.Id == roleId); if (roleInfo == null) { throw new BusinessException($"不存在用户Id为{roleId}的角色信息"); } await _userGroupRoleRepository.InsertAsync(new UserGroupRole() { UserGroupId = userGroup.Id, RoleId = roleId }, conn, trans); } }, Connection); }
public async Task Update(UpdateUserGroupInput input) { CheckUserDefinedDataPermission(input.DataPermissionType, input.DataPermissionOrgIds); using (var locker = await _lockerProvider.CreateLockAsync("UpdateUserGroup")) { await locker.Lock(async() => { var userGroup = await _userGroupRepository.SingleOrDefaultAsync(p => p.Id == input.Id); if (userGroup == null) { throw new BusinessException($"不存在Id为{input.Id}的用户组"); } if (!userGroup.Identification.Equals(input.Identification)) { var exsitUserGroup = await _userGroupRepository.FirstOrDefaultAsync(p => p.Identification == input.Identification, false); if (exsitUserGroup != null) { throw new BusinessException($"系统中已经存在{input.Identification}的用户组"); } } userGroup = input.MapTo(userGroup); await UnitOfWorkAsync(async(conn, trans) => { var userGroupOrganizationIds = (await _userGroupOrganizationRepository.GetAllAsync(p => p.UserGroupId == userGroup.Id, conn, trans)).Select(p => p.OrgId).ToArray(); await _userGroupRepository.UpdateAsync(userGroup, conn, trans); await _userGroupRoleRepository.DeleteAsync(p => p.UserGroupId == userGroup.Id, conn, trans); await _userGroupPermissionRepository.DeleteAsync(p => p.UserGroupId == userGroup.Id, conn, trans); await _userGroupDataPermissionOrgRelationRepository.DeleteAsync( p => p.UserGroupId == userGroup.Id, conn, trans); await _userGroupOrganizationRepository.DeleteAsync(p => p.UserGroupId == userGroup.Id, conn, trans); foreach (var roleId in input.RoleIds) { await _userGroupRoleRepository.InsertAsync( new UserGroupRole { UserGroupId = userGroup.Id, RoleId = roleId }, conn, trans); } if (!input.IsAllOrg) { if (!await UpdateOrgIdsEqExistOrgIds(input.OrgIds, userGroupOrganizationIds, userGroup.Id)) { throw new BusinessException("用户组所属类型为自定义的且已经存在用户的,不允许修改用户组的所属部门"); } foreach (var orgId in input.OrgIds) { var userGroupOrg = new UserGroupOrganization() { UserGroupId = userGroup.Id, OrgId = orgId }; await _userGroupOrganizationRepository.InsertAsync(userGroupOrg, conn, trans); } } var insertSql = "INSERT INTO UserGroupPermission(PermissionId,UserGroupId,CreateTime,CreateBy,TenantId) VALUES(@PermissionId,@UserGroupId,@CreationTime,@CreatorUserId,@TenantId)"; var userGroupPermissions = new List <UserGroupPermission>(); foreach (var permissionId in input.PermissionIds) { userGroupPermissions.Add(new UserGroupPermission { PermissionId = permissionId, UserGroupId = userGroup.Id, CreationTime = DateTime.Now, CreatorUserId = _session.UserId, TenantId = _session.TenantId }); } await conn.ExecuteAsync(insertSql, userGroupPermissions, trans); if (input.DataPermissionType == DataPermissionType.UserDefined) { var insertDataPermissionOrgSql = "INSERT INTO UserGroupDataPermissionOrgRelation(UserGroupId,OrgId,CreateTime,CreateBy,TenantId) VALUES(@UserGroupId,@OrgId,@CreationTime,@CreatorUserId,@TenantId)"; var dataPermissionOrgDatas = new List <UserGroupDataPermissionOrgRelation>(); foreach (var orgId in input.DataPermissionOrgIds) { dataPermissionOrgDatas.Add(new UserGroupDataPermissionOrgRelation() { UserGroupId = userGroup.Id, OrgId = orgId, CreationTime = DateTime.Now, CreatorUserId = _session.UserId, TenantId = _session.TenantId }); } await conn.ExecuteAsync(insertDataPermissionOrgSql, dataPermissionOrgDatas, trans); } await RemoveUserGroupCheckPemissionCache(userGroup.Id); }, Connection); }); } }