ソースを参照

经沟通账号重复为非必要需求,恢复限制用户账号唯一

Signed-off-by: Lzh666 <422235757@qq.com>
Lzh666 1 年間 前
コミット
cce25d4ae9

+ 8 - 12
Admin.NET/Admin.NET.Core/Service/Auth/SysAuthService.cs

@@ -116,23 +116,19 @@ public class SysAuthService : IDynamicApiController, ITransient
     [NonAction]
     public async Task<(SysTenant tenant, SysUser user)> GetLoginUserAndTenant(long? tenantId, string account = null, string phone = null)
     {
-        // 如果租户为空或为-1,则使用默认租户
-        if (tenantId is null or -1) tenantId = SqlSugarConst.DefaultTenantId;
-
-        // 租户是否存在或已禁用
-        var tenant = await _sysUserRep.ChangeRepository<SqlSugarRepository<SysTenant>>().GetFirstAsync(u => u.Id == tenantId);
-        if (tenant?.Status != StatusEnum.Enable) throw Oops.Oh(ErrorCodeEnum.Z1003);
-
-        // 判断账号是否存在
+        // 账号是否存在
         var user = await _sysUserRep.AsQueryable().Includes(t => t.SysOrg).ClearFilter()
-            .Where(u => u.AccountType == AccountTypeEnum.SuperAdmin || u.TenantId == tenantId)
             .WhereIF(!string.IsNullOrWhiteSpace(account), u => u.Account.Equals(account))
-            .WhereIF(!string.IsNullOrWhiteSpace(phone), u => u.Phone.Equals(phone))
-            .FirstAsync();
+            .WhereIF(!string.IsNullOrWhiteSpace(phone), u => u.Phone.Equals(phone)).FirstAsync();
         _ = user ?? throw Oops.Oh(ErrorCodeEnum.D0009);
 
+        // 租户是否存在或已禁用
+        var tenant = await _sysUserRep.ChangeRepository<SqlSugarRepository<SysTenant>>().GetFirstAsync(u => u.Id == user.TenantId);
+        if (tenant?.Status != StatusEnum.Enable) throw Oops.Oh(ErrorCodeEnum.Z1003);
+
         // 如果是超级管理员,则引用登录选择的租户进入系统
-        if (user.AccountType == AccountTypeEnum.SuperAdmin) user.TenantId = tenantId;
+        if (tenantId > 0)
+            if (user.AccountType == AccountTypeEnum.SuperAdmin) user.TenantId = tenantId;
 
         return (tenant, user);
     }

+ 12 - 11
Admin.NET/Admin.NET.Core/Service/User/SysUserService.cs

@@ -102,10 +102,11 @@ public class SysUserService : IDynamicApiController, ITransient
     [DisplayName("增加用户")]
     public virtual async Task<long> AddUser(AddUserInput input)
     {
-        var query = _sysUserRep.AsQueryable().ClearFilter().Where(u => u.TenantId == _userManager.TenantId || u.AccountType == AccountTypeEnum.SuperAdmin);
+        var isExist = await _sysUserRep.AsQueryable().ClearFilter().AnyAsync(u => u.Account == input.Account);
+        if (isExist) throw Oops.Oh(ErrorCodeEnum.D1003);
 
-        if (await query.AnyAsync(u => u.Account == input.Account)) throw Oops.Oh(ErrorCodeEnum.D1003);
-        if (!string.IsNullOrWhiteSpace(input.Phone) && await query.AnyAsync(u => u.Phone == input.Phone)) throw Oops.Oh(ErrorCodeEnum.D1032);
+        if (!string.IsNullOrWhiteSpace(input.Phone) && await _sysUserRep.AsQueryable().ClearFilter().AnyAsync(u => u.Phone == input.Phone))
+            throw Oops.Oh(ErrorCodeEnum.D1032);
 
         // 禁止越权新增超级管理员和系统管理员
         if (_userManager.AccountType != AccountTypeEnum.SuperAdmin && input.AccountType is AccountTypeEnum.SuperAdmin or AccountTypeEnum.SysAdmin) throw Oops.Oh(ErrorCodeEnum.D1038);
@@ -141,10 +142,11 @@ public class SysUserService : IDynamicApiController, ITransient
     [NonAction]
     public virtual async Task<long> RegisterUser(AddUserInput input)
     {
-        var query = _sysUserRep.AsQueryable().ClearFilter().Where(u => u.TenantId == _userManager.TenantId || u.AccountType == AccountTypeEnum.SuperAdmin);
+        var isExist = await _sysUserRep.AsQueryable().ClearFilter().AnyAsync(u => u.Account == input.Account);
+        if (isExist) throw Oops.Oh(ErrorCodeEnum.D1003);
 
-        if (await query.AnyAsync(u => u.Account == input.Account)) throw Oops.Oh(ErrorCodeEnum.D1003);
-        if (!string.IsNullOrWhiteSpace(input.Phone) && await query.AnyAsync(u => u.Phone == input.Phone)) throw Oops.Oh(ErrorCodeEnum.D1032);
+        if (!string.IsNullOrWhiteSpace(input.Phone) && await _sysUserRep.AsQueryable().ClearFilter().AnyAsync(u => u.Phone == input.Phone))
+            throw Oops.Oh(ErrorCodeEnum.D1032);
 
         // 禁止越权注册
         if (input.AccountType is AccountTypeEnum.SuperAdmin or AccountTypeEnum.SysAdmin) throw Oops.Oh(ErrorCodeEnum.D1038);
@@ -185,12 +187,11 @@ public class SysUserService : IDynamicApiController, ITransient
     [DisplayName("更新用户")]
     public virtual async Task UpdateUser(UpdateUserInput input)
     {
-        // 是否租户隔离登录验证
-        var query = _sysUserRep.AsQueryable().ClearFilter()
-            .Where(u => u.Id != input.Id && (u.TenantId == _userManager.TenantId || u.AccountType == AccountTypeEnum.SuperAdmin));
+        if (await _sysUserRep.AsQueryable().ClearFilter().AnyAsync(u => u.Account == input.Account && u.Id != input.Id))
+            throw Oops.Oh(ErrorCodeEnum.D1003);
 
-        if (await query.AnyAsync(u => u.Account == input.Account)) throw Oops.Oh(ErrorCodeEnum.D1003);
-        if (!string.IsNullOrWhiteSpace(input.Phone) && await query.AnyAsync(u => u.Phone == input.Phone)) throw Oops.Oh(ErrorCodeEnum.D1032);
+        if (!string.IsNullOrWhiteSpace(input.Phone) && await _sysUserRep.AsQueryable().ClearFilter().AnyAsync(u => u.Phone == input.Phone && u.Id != input.Id))
+            throw Oops.Oh(ErrorCodeEnum.D1032);
 
         // 禁止越权更新超级管理员或系统管理员信息
         if (_userManager.AccountType != AccountTypeEnum.SuperAdmin && input.AccountType is AccountTypeEnum.SuperAdmin or AccountTypeEnum.SysAdmin) throw Oops.Oh(ErrorCodeEnum.D1038);