public async Task <Avatar> UpdateAvatar(User targetUser, string newAvatar)
        {
            if (targetUser == null)
            {
                return(null);
            }
            var now    = DateTime.Now;
            var avatar = new Avatar()
            {
                FilePath   = newAvatar == null ? null : $"{Guid.NewGuid().ToString()}.png",
                CreateTime = now,
                Img        = newAvatar.FromBase64ToBytes(),
                User       = targetUser
            };

            if (avatar?.Img?.Length <= 1024 * 200)
            {
                if (targetUser.DiyInfo != null)
                {
                    _context.AppUserDiyAvatars.Add(avatar);
                    targetUser.DiyInfo.Avatar = avatar;
                    _context.AppUserDiyInfos.Update(targetUser.DiyInfo);
                    await _context.SaveChangesAsync().ConfigureAwait(true);
                }
            }
            else if (avatar?.Img?.Length > 1024 * 200)
            {
                throw new ActionStatusMessageException(new ApiResult(ActionStatusMessage.StaticMessage.FileSizeInvalid, $"最大支持200KB,当前:{avatar.Img.Length}", true));
            }
            return(avatar);
        }
        private ApplicationUser CreateUser(User user, string password)
        {
            if (_context.Users.Where(u => u.UserName == user.Id).FirstOrDefault() != null)
            {
                var check_removed_user = _context.AppUsersDb.Where(u => u.Id == user.Id) == null?"原用户已被移除,如需继续使用,请联系管理员恢复。":null;
                throw new ActionStatusMessageException(ActionStatusMessage.Account.Register.AccountExist, check_removed_user);
            }

            var identity = new ApplicationUser
            {
                UserName             = user.Id,
                Email                = user.Application?.Email,
                PhoneNumberConfirmed = false,
                EmailConfirmed       = false,
                NormalizedEmail      = user.Application?.Email?.ToUpper(),
                NormalizedUserName   = user.Id.ToUpper(),
                LockoutEnabled       = true,
                TwoFactorEnabled     = false,
                SecurityStamp        = user.Id.GetHashCode().ToString()
            };

            var passwordHasher = new PasswordHasher <ApplicationUser>();

            identity.PasswordHash = passwordHasher.HashPassword(identity, password);

            return(identity);
        }
        /// <summary>
        /// Async edits user.
        /// </summary>
        public async Task <bool> EditAsync(User newUser)
        {
            _context.AppUsers.Update(newUser);
            await _context.SaveChangesAsync().ConfigureAwait(true);

            return(true);
        }
        /// <summary>
        /// Async creates user.
        /// </summary>
        public async Task <ApplicationUser> CreateAsync(User user, string password, Func <User, bool> checkUserValid)
        {
            if (user == null)
            {
                throw new ActionStatusMessageException(ActionStatusMessage.Account.Register.UserInvalid);
            }
            var identity = CreateUser(user, password);

            if (identity == null)
            {
                throw new ActionStatusMessageException(ActionStatusMessage.Account.Register.IdentityFail);
            }
            var appUser = CreateAppUser(user);

            if (!checkUserValid?.Invoke(appUser) ?? true)
            {
                return(null);
            }
            await _context.Users.AddAsync(identity).ConfigureAwait(true);

            await _context.AppUsers.AddAsync(appUser).ConfigureAwait(true);

            await _context.SaveChangesAsync().ConfigureAwait(true);

            return(identity);
        }
 /// <summary>
 /// 获取用户自定义信息
 /// </summary>
 /// <param name="model"></param>
 /// <param name="user">需要传入用户本身</param>
 /// <returns></returns>
 public static UserDiyInfoDataModel ToViewModel(this UserDiyInfo model, DAL.Entities.UserInfo.User user)
 {
     return(new UserDiyInfoDataModel()
     {
         About = model.About,
         Avatar = model.Avatar?.Id.ToString(),
         ThirdpardAccounts = model.ThirdpardAccount
     });
 }
        private void RemoveUserInfo(User user)
        {
            if (user.BaseInfo != null)
            {
                _context.AppUserBaseInfos.Remove(user.BaseInfo);
            }
            if (user.Application != null)
            {
                if (user.Application.Permission != null)
                {
                    _context.Permissions.Remove(user.Application.Permission);
                }
                if (user.Application.ApplicationSetting != null)
                {
                    _context.AppUserApplicationSettings.Remove(user.Application.ApplicationSetting);
                }
            }
            if (user.CompanyInfo != null)
            {
                _context.AppUserCompanyInfos.Remove(user.CompanyInfo);
            }

            if (user.SocialInfo != null)
            {
                _context.AppUserSocialInfos.Remove(user.SocialInfo);
                if (user.SocialInfo.Settle != null)
                {
                    _context.AppUserSocialInfoSettles.Remove(user.SocialInfo.Settle);
                    if (user.SocialInfo.Settle.Self != null)
                    {
                        _context.AppUserSocialInfoSettleMoments.Remove(user.SocialInfo.Settle.Self);
                    }
                    if (user.SocialInfo.Settle.Parent != null)
                    {
                        _context.AppUserSocialInfoSettleMoments.Remove(user.SocialInfo.Settle.Parent);
                    }
                    if (user.SocialInfo.Settle.Lover != null)
                    {
                        _context.AppUserSocialInfoSettleMoments.Remove(user.SocialInfo.Settle.Lover);
                    }
                    if (user.SocialInfo.Settle.LoversParent != null)
                    {
                        _context.AppUserSocialInfoSettleMoments.Remove(user.SocialInfo.Settle.LoversParent);
                    }
                }
            }
            if (user.DiyInfo != null)
            {
                _context.AppUserDiyInfos.Remove(user.DiyInfo);
                if (user.DiyInfo.Avatar != null)
                {
                    _context.AppUserDiyAvatars.Remove(user.DiyInfo.Avatar);
                }
            }
        }
        private User CreateAppUser(User user)
        {
            var application = user.Application;

            application.Create             = DateTime.Now;
            application.AuthKey            = new Random().Next(1000, 99999).ToString().GetHashCode().ToString();
            application.ApplicationSetting = new UserApplicationSetting()
            {
                LastSubmitApplyTime = DateTime.Now
            };
            application.Permission = new Permissions()
            {
                Regions = "",
                Role    = "User"
            };
            var company = user.CompanyInfo;

            company.Company = _context.CompaniesDb.FirstOrDefault(c => c.Code == company.CompanyCode);
            company.Duties  = _context.Duties.FirstOrDefault(d => d.Name == user.CompanyInfo.Duties.Name);

            var title = user.CompanyInfo.Title;

            company.Title = _context.UserCompanyTitles.FirstOrDefault(d => d.Name == title.Name);
            var social = user.SocialInfo;

            social.Address = _context.AdminDivisions.Find(user.SocialInfo?.Address?.Code);
            var settle = social.Settle;

            if (settle != null)
            {
                if (settle.Lover?.Address != null)
                {
                    settle.Lover.Address = _context.AdminDivisions.Find(settle.Lover.Address.Code);
                }
                if (settle.Parent?.Address != null)
                {
                    settle.Parent.Address = _context.AdminDivisions.Find(settle.Parent.Address.Code);
                }
                if (settle.LoversParent?.Address != null)
                {
                    settle.LoversParent.Address = _context.AdminDivisions.Find(settle.LoversParent.Address.Code);
                }
                if (settle.Self?.Address != null)
                {
                    settle.Self.Address = _context.AdminDivisions.Find(settle.Self.Address.Code);
                }
                // if prev yealy history not set , then build new one
                if (settle.PrevYealyLengthHistory == null)
                {
                    settle.PrevYealyLengthHistory = new List <AppUsersSettleModifyRecord>();
                }
            }
            return(user);
        }
        private ApiResult CheckPermissionNodes(DAL.Entities.UserInfo.User u, IEnumerable <ApplyAuditStreamNodeAction> nodes)
        {
            var result = ActionStatusMessage.Success;

            // 获取第一个低权限的节点,如果不存在则获取任何一个节点
            foreach (var node in nodes)
            {
                result = CheckPermission(u, node.ToDtoModel(), node.RegionOnCompany, node.RegionOnCompany);
                if (result.Status != 0)
                {
                    return(result);
                }
            }
            return(result);
        }
 /// <summary>
 /// 删除用户
 /// 20201019@serfend:改为仅修改用户删除属性为已删除
 /// 20201021@serfend:保留两种删除方式
 /// </summary>
 /// <param name="user"></param>
 /// <param name="reason"></param>
 /// <param name="RemoveEntity">是否完全删除</param>
 /// <returns></returns>
 private void RemoveUser(User user, string reason, bool RemoveEntity = false)
 {
     if (!RemoveEntity)
     {
         user.AccountStatus += (int)AccountStatus.Abolish;
         user.Application.UserRemoveReason = reason;
         _context.AppUsers.Update(user);
         SetUserAppliesStatus(user.Id, true);
     }
     else
     {
         _context.AppUsers.Remove(user);
         RemoveUserInfo(user);
         var appUser = _context.Users.FirstOrDefault(u => u.UserName == user.Id);
         _context.Users.Remove(appUser);
     }
 }
        public async Task <User> ModifyAsync(User newUser, bool update)
        {
            if (newUser == null)
            {
                return(null);
            }
            var lastCreateTime = newUser?.Application?.Create;
            var appUser        = CreateAppUser(newUser);

            appUser.Application.Create = lastCreateTime;             // create time should not modify
            if (update)
            {
                _context.AppUsers.Update(appUser);
                await _context.SaveChangesAsync().ConfigureAwait(true);
            }
            return(appUser);
        }
Example #11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="model"></param>
 /// <param name="user"></param>
 /// <returns></returns>
 public static UserApplicationDataModel ToModel(this UserApplicationInfo model, DAL.Entities.UserInfo.User user)
 {
     return(new UserApplicationDataModel()
     {
         UserName = user.Id,
         Create = model.Create,
         Email = model.Email,
         InvitedBy = model.InvitedBy,
         AccountStatus = user.AccountStatus,
         StatusBeginDate = user.StatusBeginDate,
         StatusEndDate = user.StatusEndDate,
     });
 }
 /// <summary>
 /// Edits user.
 /// </summary>
 public bool Edit(User newUser)
 {
     _context.AppUsers.Update(newUser);
     _context.SaveChanges();
     return(true);
 }
 /// <summary>
 /// Creates user.
 /// </summary>
 public ApplicationUser Create(User user, string password, Func <User, bool> checkUserValid) =>
 CreateAsync(user, password, checkUserValid).Result;