Exemplo n.º 1
0
        public async Task Add(UserActivationRabbitMq activation)
        {
            var activationDto = _mapper.Map <ActivationDto>(activation);

            if (!ActivationModelValid(activationDto))
            {
                throw new UnprocessableException();
            }

            await _activationDal.Add(activationDto);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves the user in the database
        /// </summary>
        /// <param name="user">The form data the user send</param>
        public async Task Register(User user)
        {
            if (!await UserModelValid(user) || user.Password.Length < 8)
            {
                throw new UnprocessableException();
            }

            bool usernameOrEmailInUse = await _userDal.Exists(user.Username, user.Email);

            if (usernameOrEmailInUse)
            {
                throw new DuplicateNameException();
            }

            bool databaseContainsUsers = await _userDal.Any();

            var userDto = _mapper.Map <UserDto>(user);

            userDto.AccountRole = databaseContainsUsers ? AccountRole.User : AccountRole.SiteAdmin;
            userDto.Uuid        = Guid.NewGuid();

            var disabledUserDto = new DisabledUserDto
            {
                Reason   = DisableReason.EmailVerificationRequired,
                UserUuid = userDto.Uuid,
                Uuid     = Guid.NewGuid()
            };

            var activationDto = new ActivationDto
            {
                Code     = Guid.NewGuid().ToString(),
                UserUuid = userDto.Uuid,
                Uuid     = Guid.NewGuid()
            };

            await _disabledUserDal.Add(disabledUserDto);

            await _activationDal.Add(activationDto);

            var userRabbitMq = _mapper.Map <UserRabbitMqSensitiveInformation>(user);

            userRabbitMq.Uuid        = userDto.Uuid;
            userRabbitMq.AccountRole = databaseContainsUsers ? AccountRole.User : AccountRole.SiteAdmin;

            _publisher.Publish(userRabbitMq, RabbitMqRouting.AddUser, RabbitMqExchange.AuthenticationExchange);

            await _userDal.Add(userDto);

            SendActivationEmail(userDto, activationDto);
        }