예제 #1
0
        public void ModifyInfo(ModifyAccountInfoInput input)
        {
            if (input.AccountName.IsNotNullOrEmpty())
            {
                input.AccountName = input.AccountName.Trim();
            }

            input.Validate();

            var session = this.Session;

            Sys_User user = this.DbContext.Query <Sys_User>().FilterDeleted().Where(a => a.Id == session.UserId).AsTracking().First();

            string accountName = user.AccountName;

            if (user.AccountName.IsNullOrEmpty())
            {
                //用户名设置后不能修改
                if (input.AccountName.IsNotNullOrEmpty())
                {
                    accountName = input.AccountName.ToLower();
                    AceUtils.EnsureAccountNameLegal(accountName);
                    bool exists = this.DbContext.Query <Sys_User>().Where(a => a.AccountName == accountName).Any();
                    if (exists)
                    {
                        throw new InvalidInputException("用户名[{0}]已存在".ToFormat(input.AccountName));
                    }
                }
            }

            user.AccountName = accountName;
            user.Name        = input.Name;
            user.Gender      = input.Gender;
            user.Birthday    = input.Birthday;
            user.WeChat      = input.WeChat;
            user.Description = input.Description;

            this.DbContext.Update <Sys_User>(user);
        }
예제 #2
0
        public UserModel Register(RegisterInput input)
        {
            AceUtils.EnsureAccountNameLegal(input.AccountName);

            PasswordHelper.EnsurePasswordLegal(input.Password);

            if (input.NickName.IsNullOrEmpty() || input.NickName.Length < 2 || input.NickName.Length > 15)
            {
                throw new InvalidInputException("昵称太短或太长");
            }

            string accountName = input.AccountName.ToLower();

            AceUtils.EnsureAccountNameLegal(accountName);
            bool exists = this._userRepository.Query().Where(a => a.AccountName == accountName).Any();

            if (exists)
            {
                throw new InvalidInputException("用户名[{0}]已存在".ToFormat(input.AccountName));
            }

            UserDetail user = new UserDetail();

            user.AccountName = accountName;
            user.NickName    = input.NickName;

            string userSecretkey     = UserHelper.GenUserSecretkey();
            string encryptedPassword = PasswordHelper.Encrypt(input.Password, userSecretkey);

            user.SecretKey = userSecretkey;
            user.Password  = encryptedPassword;

            user.HeadPhoto    = "/content/images/avatar.png";
            user.RegisterTime = DateTime.Now;

            this._userRepository.AddUser(user);

            return(UserModel.Create(user));
        }
예제 #3
0
        public void Add(AddUserInput input)
        {
            this.Trim(input);

            input.Validate();

            if (input.AccountName.IsNullOrEmpty() && input.MobilePhone.IsNullOrEmpty() && input.Email.IsNullOrEmpty())
            {
                throw new InvalidInputException("用户名/手机号码/邮箱至少填一个");
            }

            string accountName = null;

            if (input.AccountName.IsNotNullOrEmpty())
            {
                accountName = input.AccountName.ToLower();
                AceUtils.EnsureAccountNameLegal(accountName);
                bool exists = this.DbContext.Query <SysUser>().Where(a => a.AccountName == accountName).Any();
                if (exists)
                {
                    throw new InvalidInputException("用户名[{0}]已存在".ToFormat(input.AccountName));
                }
            }

            string mobilePhone = null;

            if (input.MobilePhone.IsNotNullOrEmpty())
            {
                mobilePhone = input.MobilePhone;
                if (AceUtils.IsMobilePhone(mobilePhone) == false)
                {
                    throw new InvalidInputException("请输入正确的手机号码");
                }

                bool exists = this.DbContext.Query <SysUser>().Where(a => a.MobilePhone == mobilePhone).Any();
                if (exists)
                {
                    throw new InvalidInputException("手机号码[{0}]已存在".ToFormat(mobilePhone));
                }
            }

            string email = null;

            if (input.Email.IsNotNullOrEmpty())
            {
                email = input.Email.ToLower();
                if (AceUtils.IsEmail(email) == false)
                {
                    throw new InvalidInputException("请输入正确的邮箱地址");
                }

                bool exists = this.DbContext.Query <SysUser>().Where(a => a.Email == email).Any();
                if (exists)
                {
                    throw new InvalidInputException("邮箱地址[{0}]已存在".ToFormat(input.Email));
                }
            }

            SysUser user = this.CreateEntity <SysUser>(null, input.CreatorId);

            user.AccountName = accountName;
            user.Name        = input.Name;
            user.Gender      = input.Gender;
            user.MobilePhone = mobilePhone;
            user.Birthday    = input.Birthday;
            user.WeChat      = input.WeChat;
            user.Email       = email;
            user.Description = input.Description;
            user.State       = AccountState.Normal;

            string userSecretkey     = UserHelper.GenUserSecretkey();
            string encryptedPassword = PasswordHelper.Encrypt(input.Password, userSecretkey);

            SysUserLogOn logOnEntity = new SysUserLogOn();

            logOnEntity.Id            = IdHelper.CreateStringSnowflakeId();
            logOnEntity.UserId        = user.Id;
            logOnEntity.UserSecretkey = userSecretkey;
            logOnEntity.UserPassword  = encryptedPassword;

            List <string>      roleIds   = input.GetRoles();
            List <SysUserRole> userRoles = roleIds.Select(a =>
            {
                return(new SysUserRole()
                {
                    Id = IdHelper.CreateStringSnowflakeId(),
                    UserId = user.Id,
                    RoleId = a,
                });
            }).ToList();

            user.RoleIds = string.Join(",", roleIds);

            List <string>     orgIds   = input.GetOrgs();
            List <SysUserOrg> userOrgs = orgIds.Select(a =>
            {
                return(new SysUserOrg()
                {
                    Id = IdHelper.CreateStringSnowflakeId(),
                    UserId = user.Id,
                    OrgId = a,
                    DisablePermission = false
                });
            }).ToList();

            user.OrgIds = string.Join(",", orgIds);

            List <string>      postIds   = input.GetPosts();
            List <SysUserPost> userPosts = postIds.Select(a =>
            {
                return(new SysUserPost()
                {
                    Id = IdHelper.CreateStringSnowflakeId(),
                    UserId = user.Id,
                    PostId = a
                });
            }).ToList();

            user.PostIds = string.Join(",", postIds);

            this.DbContext.DoWithTransaction(() =>
            {
                this.DbContext.Insert(user);
                this.DbContext.Insert(logOnEntity);
                this.DbContext.InsertRange(userRoles);
                this.DbContext.InsertRange(userOrgs);
                this.DbContext.InsertRange(userPosts);
            });
        }
예제 #4
0
        public void Update(UpdateUserInput input)
        {
            this.Trim(input);

            input.Validate();

            SysUser user = this.Query.Where(a => a.Id == input.Id).AsTracking().First();

            user.EnsureIsNotAdmin();
            if (user.State == AccountState.Closed)
            {
                throw new InvalidInputException("无法修改已注销用户");
            }

            string accountName = null;

            if (user.AccountName.IsNullOrEmpty())
            {
                //用户名设置后不能修改
                if (input.AccountName.IsNotNullOrEmpty())
                {
                    accountName = input.AccountName.ToLower();
                    AceUtils.EnsureAccountNameLegal(accountName);
                    bool exists = this.DbContext.Query <SysUser>().Where(a => a.AccountName == accountName).Any();
                    if (exists)
                    {
                        throw new InvalidInputException("用户名[{0}]已存在".ToFormat(input.AccountName));
                    }
                }
            }
            else
            {
                accountName = user.AccountName;
            }

            string mobilePhone = null;

            if (user.MobilePhone.IsNotNullOrEmpty() && input.MobilePhone.IsNullOrEmpty())
            {
                //手机号码设置后不能再改为空
                throw new InvalidInputException("请输入手机号码");
            }
            if (input.MobilePhone.IsNotNullOrEmpty())
            {
                mobilePhone = input.MobilePhone;
                if (AceUtils.IsMobilePhone(mobilePhone) == false)
                {
                    throw new InvalidInputException("请输入正确的手机号码");
                }

                if (user.MobilePhone != mobilePhone)//不等说明手机号码有变
                {
                    bool exists = this.DbContext.Query <SysUser>().Where(a => a.MobilePhone == mobilePhone).Any();
                    if (exists)
                    {
                        throw new InvalidInputException("手机号码[{0}]已存在".ToFormat(mobilePhone));
                    }
                }
            }

            string email = null;

            if (user.Email.IsNotNullOrEmpty() && input.Email.IsNullOrEmpty())
            {
                //邮箱地址设置后不能再改为空
                throw new InvalidInputException("请输入邮箱地址");
            }
            if (input.Email.IsNotNullOrEmpty())
            {
                email = input.Email.ToLower();
                if (AceUtils.IsEmail(email) == false)
                {
                    throw new InvalidInputException("请输入正确的邮箱地址");
                }

                if (user.Email != email)//不等说明邮箱有变
                {
                    bool exists = this.DbContext.Query <SysUser>().Where(a => a.Email == email).Any();
                    if (exists)
                    {
                        throw new InvalidInputException("邮箱地址[{0}]已存在".ToFormat(input.Email));
                    }
                }
            }

            user.AccountName = accountName;
            user.Name        = input.Name;
            user.Gender      = input.Gender;
            user.MobilePhone = mobilePhone;
            user.Birthday    = input.Birthday;
            user.WeChat      = input.WeChat;
            user.Email       = email;
            user.Description = input.Description;

            List <string>      roleIds           = input.GetRoles();
            List <SysUserRole> userRoles         = this.DbContext.Query <SysUserRole>().Where(a => a.UserId == input.Id).ToList();
            List <string>      userRolesToDelete = userRoles.Where(a => !roleIds.Contains(a.Id)).Select(a => a.Id).ToList();
            List <SysUserRole> userRolesToAdd    = roleIds.Where(a => !userRoles.Any(r => r.Id == a)).Select(a =>
            {
                return(new SysUserRole()
                {
                    Id = IdHelper.CreateStringSnowflakeId(),
                    UserId = input.Id,
                    RoleId = a,
                });
            }).ToList();

            user.RoleIds = string.Join(",", roleIds);

            List <string>     orgIds           = input.GetOrgs();
            List <SysUserOrg> userOrgs         = this.DbContext.Query <SysUserOrg>().Where(a => a.UserId == input.Id).ToList();
            List <string>     userOrgsToDelete = userOrgs.Where(a => !orgIds.Contains(a.Id)).Select(a => a.Id).ToList();
            List <SysUserOrg> userOrgsToAdd    = orgIds.Where(a => !userOrgs.Any(r => r.Id == a)).Select(a =>
            {
                return(new SysUserOrg()
                {
                    Id = IdHelper.CreateStringSnowflakeId(),
                    UserId = input.Id,
                    OrgId = a,
                    DisablePermission = false
                });
            }).ToList();

            user.OrgIds = string.Join(",", orgIds);

            List <string>      postIds   = input.GetPosts();
            List <SysUserPost> userPosts = postIds.Select(a =>
            {
                return(new SysUserPost()
                {
                    Id = IdHelper.CreateStringSnowflakeId(),
                    UserId = input.Id,
                    PostId = a
                });
            }).ToList();

            user.PostIds = string.Join(",", postIds);

            this.DbContext.DoWithTransaction(() =>
            {
                this.DbContext.Delete <SysUserRole>(a => a.Id.In(userRolesToDelete));
                this.DbContext.InsertRange(userRolesToAdd);

                this.DbContext.Delete <SysUserOrg>(a => a.Id.In(userOrgsToDelete));
                this.DbContext.InsertRange(userOrgsToAdd);

                this.DbContext.Delete <SysUserPost>(a => a.UserId == input.Id);
                this.DbContext.InsertRange(userPosts);

                this.DbContext.Update <SysUser>(user);
            });
        }