Ejemplo n.º 1
0
        public async Task <UserDto> Create(CreateOrUpdateUserInputDto input)
        {
            if (string.IsNullOrEmpty(input.Password))
            {
                throw new AbpValidationException("Password required", new List <ValidationResult>()
                {
                    new ValidationResult("Password required", new List <string>()
                    {
                        "Password"
                    })
                });
            }
            if (Repository.GetAll().Any(m => m.UserName == input.UserName))
            {
                throw new AbpValidationException("User name already exists", new List <ValidationResult>()
                {
                    new ValidationResult("User name already exists", new List <string>()
                    {
                        "UserName"
                    })
                });
            }
            if (Repository.GetAll().Any(m => m.EmailAddress == input.EmailAddress))
            {
                throw new AbpValidationException("Email Address already exists", new List <ValidationResult>()
                {
                    new ValidationResult("Email Address already exists", new List <string>()
                    {
                        "EmailAddress"
                    })
                });
            }

            input.Id = null;
            var user = ObjectMapper.Map <User>(input);

            user.TenantId         = AbpSession.TenantId;
            user.Password         = _passwordHash.HashPassword(user, input.Password);
            user.IsEmailConfirmed = true;


            CheckErrors(await UserManager.CreateAsync(user));

            if (input.RoleNames != null)
            {
                CheckErrors(await _userManager.SetRoles(user, input.RoleNames));
            }

            await CurrentUnitOfWork.SaveChangesAsync(); //To get new user's Id.


            return(ObjectMapper.Map <UserDto>(user));
        }
Ejemplo n.º 2
0
        public async Task <UserDto> Update(CreateOrUpdateUserInputDto input)
        {
            if (input.Id == null)
            {
                throw new AbpException("user not exits");
            }

            if (Repository.GetAll().Any(m => m.UserName == input.UserName && m.Id != input.Id))
            {
                throw new AbpValidationException("User name already exists", new List <ValidationResult>()
                {
                    new ValidationResult("User name already exists", new List <string>()
                    {
                        "UserName"
                    })
                });
            }
            if (Repository.GetAll().Any(m => m.EmailAddress == input.EmailAddress && m.Id != input.Id))
            {
                throw new AbpValidationException("Email Address already exists", new List <ValidationResult>()
                {
                    new ValidationResult("Email Address already exists", new List <string>()
                    {
                        "EmailAddress"
                    })
                });
            }


            var user = await Repository.FirstOrDefaultAsync(input.Id.Value);

            if (user == null)
            {
                throw new AbpException("user not exits");
            }

            ObjectMapper.Map(input, user);

            CheckErrors(await UserManager.UpdateAsync(user));

            if (input.RoleNames != null)
            {
                CheckErrors(await _userManager.SetRoles(user, input.RoleNames));
            }

            return(ObjectMapper.Map <UserDto>(user));
        }