Exemple #1
0
 public async Task <bool> UserExistsAsync(string phoneNum)
 {
     using (UserCenterDbContext ctx = new UserCenterDbContext())
     {
         return(await ctx.Users.AnyAsync(u => u.PhoneNum == phoneNum));
     }
 }
Exemple #2
0
        /// <summary>
        /// 根据组id获取组
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <UserGroupDto> GetByIdAsync(long id)
        {
            using (UserCenterDbContext ctx = new UserCenterDbContext())
            {
                var group = await ctx.UserGroups.SingleOrDefaultAsync(g => g.Id == id);

                if (group == null)
                {
                    return(null);
                }
                return(ToUserGroupDto(group));
            }
        }
Exemple #3
0
        public async Task <UserDto> GetByPhoneNumAsync(string phoneNum)
        {
            using (UserCenterDbContext ctx = new UserCenterDbContext())
            {
                var user = await ctx.Users.SingleOrDefaultAsync(u => u.PhoneNum == phoneNum);

                if (user == null)
                {
                    return(null);
                }
                return(ToUserDto(user));
            }
        }
Exemple #4
0
        public async Task <UserDto> GetByIdAsync(long id)
        {
            using (UserCenterDbContext ctx = new UserCenterDbContext())
            {
                var user = await ctx.Users.SingleOrDefaultAsync(u => u.Id == id);

                if (user == null)
                {
                    return(null);
                }
                return(ToUserDto(user));
            }
        }
Exemple #5
0
        public async Task <bool> CheckLoginAsync(string phoneNum, string password)
        {
            using (UserCenterDbContext ctx = new UserCenterDbContext())
            {
                var user = await ctx.Users.SingleOrDefaultAsync(u => u.PhoneNum == phoneNum);

                if (user == null)
                {
                    return(false);
                }
                string inputHash = MD5Helper.CalcMD5(password + user.PasswordSalt);
                return(user.PasswordHash == inputHash);
            }
        }
Exemple #6
0
        /// <summary>
        /// 根据用户id获取组
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task <UserGroupDto[]> GetGroupsAsync(long userId)
        {
            using (UserCenterDbContext ctx = new UserCenterDbContext())
            {
                var user = await ctx.Users.SingleOrDefaultAsync(u => u.Id == userId);

                if (user == null)
                {
                    return(null);
                }
                var groups = user.Groups;
                List <UserGroupDto> groupDtos = new List <UserGroupDto>();
                foreach (var item in groups)
                {
                    groupDtos.Add(ToUserGroupDto(item));
                }
                return(groupDtos.ToArray());
            }
        }
Exemple #7
0
        /// <summary>
        /// 将用户添加到组
        /// </summary>
        /// <param name="userGroupId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task AddUserToGroupAsync(long userGroupId, long userId)
        {
            using (UserCenterDbContext ctx = new UserCenterDbContext())
            {
                var group = await ctx.UserGroups.SingleOrDefaultAsync(g => g.Id == userGroupId);

                if (group == null)
                {
                    throw new AggregateException("userGroupId=" + userGroupId + "不存在");
                }
                var user = await ctx.Users.SingleOrDefaultAsync(u => u.Id == userId);

                if (user == null)
                {
                    throw new AggregateException("userId=" + userId + "不存在");
                }
                group.Users.Add(user);
                user.Groups.Add(group);
                await ctx.SaveChangesAsync();
            }
        }
Exemple #8
0
        public async Task <long> AddNewAsync(string phoneNum, string nickName, string password)
        {
            using (UserCenterDbContext ctx = new UserCenterDbContext())
            {
                if (await ctx.Users.AnyAsync(u => u.PhoneNum == phoneNum))
                {
                    throw new ApplicationException("手机号:" + phoneNum + "已经存在");
                }
                UserEntity userEntity = new UserEntity();
                userEntity.NickName = nickName;
                userEntity.PhoneNum = phoneNum;
                string salt = new Random().Next(10000, 99999).ToString();
                string hash = MD5Helper.CalcMD5(password + salt);
                userEntity.PasswordSalt = salt;
                userEntity.PasswordHash = hash;
                ctx.Users.Add(userEntity);
                await ctx.SaveChangesAsync();

                return(userEntity.Id);
            }
        }
Exemple #9
0
        public async Task <AppInfoDto> GetByAppKeyAsync(string appKey)
        {
            using (UserCenterDbContext ctx = new UserCenterDbContext())
            {
                var appInfo = await ctx.AppInfos.FirstOrDefaultAsync(a => a.AppKey == appKey);

                if (appInfo == null)
                {
                    return(null);
                }
                else
                {
                    AppInfoDto appInfoDto = new AppInfoDto();
                    appInfoDto.AppKey    = appInfo.AppKey;
                    appInfoDto.AppSecret = appInfo.AppSecret;
                    appInfoDto.Name      = appInfo.Name;
                    appInfoDto.IsEnabled = appInfo.IsEnabled;
                    appInfoDto.Id        = appInfo.Id;
                    return(appInfoDto);
                }
            }
        }
Exemple #10
0
        /// <summary>
        /// 获取某个组的所有用户
        /// </summary>
        /// <param name="userGroupId"></param>
        /// <returns></returns>
        public async Task <UserDto[]> GetGroupUsersAsync(long userGroupId)
        {
            using (UserCenterDbContext ctx = new UserCenterDbContext())
            {
                var group = await ctx.UserGroups.SingleOrDefaultAsync(g => g.Id == userGroupId);

                if (group == null)
                {
                    return(null);
                }
                var            users    = group.Users;
                List <UserDto> userDtos = new List <UserDto>();
                foreach (var item in users)
                {
                    UserDto userDto = new UserDto();
                    userDto.Id       = item.Id;
                    userDto.NickName = item.NickName;
                    userDto.PhoneNum = item.PhoneNum;
                    userDtos.Add(userDto);
                }
                return(userDtos.ToArray());
            }
        }