Exemplo n.º 1
0
        public override async Task <IList <MangoUser> > GetUsersInRoleAsync(string roleName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (roleName == null)
            {
                throw new ArgumentNullException(nameof(roleName));
            }
            IList <MangoUser> mangoUsers = new List <MangoUser>();
            var userEntities             = await _userDbContext.MangoUsers.Join(_userDbContext.User2Roles, ue => ue.Id, u2r => u2r.UserId, (ue, u2r) => new UserEntity
            {
                Id            = ue.Id,
                Email         = ue.Email,
                LoginName     = ue.LoginName,
                UserName      = ue.UserName,
                CreateDate    = ue.CreateDate,
                LastLoginDate = ue.LastLoginDate,
                Password      = ue.Password
            }).ToListAsync();

            foreach (UserEntity userEntity in userEntities)
            {
                mangoUsers.Add(MEConversion.UserE2M(userEntity));
            }

            return(mangoUsers);
        }
Exemplo n.º 2
0
        public override async Task <IdentityResult> CreateAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            try
            {
                await _userDbContext.MangoUsers.AddAsync(MEConversion.UserM2E(user));

                await _userDbContext.SaveChangesAsync();

                _userDbContext.ChangeTracker.Entries <UserEntity>().First().State = EntityState.Detached;
            }
            catch (DbUpdateConcurrencyException e)
            {
                return(IdentityResult.Failed(new IdentityError {
                    Description = e.Message
                }));
            }
            return(IdentityResult.Success);
        }
Exemplo n.º 3
0
        public override async Task <IList <UserLoginInfo> > GetLoginsAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            IList <UserLoginInfo> userLoginInfos;
            UserEntity            userEntity = await FindByNameEntityAsync(user.UserName);

            if (userEntity == null)
            {
                return(null);
            }
            var logins = await _userDbContext.ExternalLogins.Where(ex => ex.UserId == userEntity.Id).ToListAsync();

            userLoginInfos = new List <UserLoginInfo>();
            foreach (ExternalLoginEntity externalLogin in logins)
            {
                userLoginInfos.Add(MEConversion.ExternalLoginE2M(externalLogin));
            }

            return(userLoginInfos);
        }
Exemplo n.º 4
0
        public override async Task <MangoUser> FindByLoginAsync(string loginProvider, string providerKey, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (loginProvider == null)
            {
                throw new ArgumentNullException(nameof(loginProvider));
            }
            if (providerKey == null)
            {
                throw new ArgumentNullException(nameof(providerKey));
            }

            ExternalLoginEntity exl = await _userDbContext.ExternalLogins.FirstOrDefaultAsync(ex => ex.LoginProvider == loginProvider && ex.ProviderKey == providerKey);

            if (exl == null)
            {
                return(null);
            }
            UserEntity userEntity = await _userDbContext.MangoUsers.FirstOrDefaultAsync(ex => ex.Id == exl.UserId);

            if (userEntity == null)
            {
                throw new UserNotFoundException($"method:{nameof(FindByLoginAsync)},没有找到id为:{exl.UserId}的用户");
            }
            return(MEConversion.UserE2M(userEntity));
        }
Exemplo n.º 5
0
		public override async Task<MangoUserRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken)
		{
			var userrole = await _userDbContext.MangoUserRoles.Where(r => r.RoleName == normalizedRoleName).SingleOrDefaultAsync();
			if (userrole == null)
			{
				return null;
			}
			return MEConversion.UserRoleE2M(userrole);
		}
Exemplo n.º 6
0
		public override async Task<MangoUserRole> FindByIdAsync(int roleId, CancellationToken cancellationToken)
		{
			var userrole = await _userDbContext.MangoUserRoles.Where(r => r.Id == roleId).SingleOrDefaultAsync();
			if (userrole == null)
			{
				return null;
			}
			return MEConversion.UserRoleE2M(userrole);
		}
Exemplo n.º 7
0
        /// <summary>
        /// 输入参数的username经过UserManger的格式化处理
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override async Task <MangoUser> FindByNameAsync(string username, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            UserEntity userEntity = await _userDbContext.MangoUsers.FirstOrDefaultAsync(u => u.LoginName == username);

            if (userEntity == null)
            {
                return(null);
            }
            return(MEConversion.UserE2M(userEntity));
        }
Exemplo n.º 8
0
        public override async Task <MangoUser> FindByIdAsync(int userId, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            UserEntity userEntity = await _userDbContext.MangoUsers.FirstOrDefaultAsync(u => u.Id == userId);

            if (userEntity == null)
            {
                return(null);
            }
            return(MEConversion.UserE2M(userEntity));
        }
Exemplo n.º 9
0
        public override async Task <MangoUser> FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (normalizedEmail == null)
            {
                throw new ArgumentNullException(nameof(normalizedEmail));
            }

            UserEntity userEntity = await _userDbContext.MangoUsers.FirstOrDefaultAsync(u => u.NormalizedEmail == normalizedEmail);

            if (userEntity == null)
            {
                return(null);
            }
            return(MEConversion.UserE2M(userEntity));
        }
Exemplo n.º 10
0
		public override async Task<IdentityResult> CreateAsync(MangoUserRole role, CancellationToken cancellationToken)
		{
			if (role == null)
			{
				throw new ArgumentNullException(nameof(role));
			}

			try
			{
				UserRoleEntity userRoleEntity = MEConversion.UserRoleM2E(role);
				await _userDbContext.MangoUserRoles.AddAsync(userRoleEntity);
				await _userDbContext.SaveChangesAsync();
			}
			catch (DbUpdateException e)
			{
				return IdentityResult.Failed(new IdentityError { Description = e.Message });
			}
			return IdentityResult.Success;
		}
Exemplo n.º 11
0
        public override async Task <IdentityResult> UpdateAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            _userDbContext.Update(MEConversion.UserM2E(user));

            try
            {
                await _userDbContext.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                return(IdentityResult.Failed(new IdentityError {
                    Description = e.Message
                }));
            }
            return(IdentityResult.Success);
        }
Exemplo n.º 12
0
        public override async Task AddLoginAsync(MangoUser user, UserLoginInfo login, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (login == null)
            {
                throw new ArgumentNullException(nameof(login));
            }

            var mangouser = await FindByNameEntityAsync(user.UserName);

            if (mangouser != null)
            {
                ExternalLoginEntity externalLogin = MEConversion.ExternalLoginM2E(login);
                externalLogin.UserId = mangouser.Id;

                await _userDbContext.ExternalLogins.AddAsync(externalLogin);
            }
        }