Ejemplo n.º 1
0
 public Task CreateAsync(User user)
 {
     ThrowIfDisposed();
     if (user == null)
     {
         throw new ArgumentNullException("user");
     }
     _entities.Create(user);
     //await SaveChanges().ConfigureAwait(false);
     return(Task.FromResult(0));
 }
Ejemplo n.º 2
0
        public async Task Handle(SendVerificationEmail command)
        {
            // create the email verification
            var createEmailVerification = new CreateEmailVerification
            {
                Commit       = false,
                EmailAddress = command.EmailAddress,
                Purpose      = command.Purpose,
            };
            await _commands.Execute(createEmailVerification);

            var verification = createEmailVerification.CreatedEntity;

            // attach the email to a user when appropriate
            if (command.Purpose == EmailVerificationPurpose.AddEmail && command.Principal != null && command.Principal.Identity.IsAuthenticated)
            {
                verification.EmailAddress.UserId = command.Principal.Identity.GetUserId <int>();
            }

            // load the templates
            var resourceFormat = string.Format("{0}.{1}.txt", verification.Purpose, "{0}");
            var assembly       = Assembly.GetExecutingAssembly();
            var subjectFormat  = assembly.GetManifestResourceText(assembly.GetManifestResourceName(string.Format(resourceFormat, "Subject")));
            var bodyFormat     = assembly.GetManifestResourceText(assembly.GetManifestResourceName(string.Format(resourceFormat, "Body")));

            // format the message body
            var formatters = new Dictionary <string, string>
            {
                { "{EmailAddress}", verification.EmailAddress.Value },
                { "{Secret}", verification.Secret },
                // don't forget to encode the token, it contains illegal querystring characters
                { "{VerificationUrl}", string.Format(command.VerifyUrlFormat ?? "",
                                                     Uri.EscapeDataString(verification.Token),
                                                     Uri.EscapeDataString(verification.Ticket)) },
                { "{SendFromUrl}", command.SendFromUrl }
            };

            // create the message
            var message = new EmailMessage
            {
                EmailAddress = verification.EmailAddress,
                From         = _appConfiguration.MailFromDefault.ToString(),
                Subject      = formatters.Format(subjectFormat),
                Body         = formatters.Format(bodyFormat),
                IsBodyHtml   = false,
                SendOnUtc    = DateTime.UtcNow,
            };

            _entities.Create(message);

            // link the message to the verification
            verification.Message = message;

            await _entities.SaveChangesAsync();

            _mail.Deliver(message.Id);
            command.CreatedTicket = verification.Ticket;
        }
        public Task Handle(CreateAddressCommand command, CancellationToken cancellationToken)
        {
            var address = _mapper.Map <CreateAddressCommand, Address>(command);

            _db.Create(address);
            command.CreatedEntity = address;

            return(Task.CompletedTask);
        }
Ejemplo n.º 4
0
        public Task Handle(CreateEmployerCommand command, CancellationToken cancellationToken)
        {
            var emp = _mapper.Map <CreateEmployerCommand, Employer>(command);

            _db.Create(emp);
            command.CreatedEntity = emp;

            return(Task.CompletedTask);
        }
Ejemplo n.º 5
0
        public Task Handle(CreateCustomerEmployerMappingCommand command, CancellationToken cancellationToken)
        {
            var cem = _mapper.Map <CreateCustomerEmployerMappingCommand, CustomerEmployerMapping>(command);

            _db.Create(cem);
            command.CreatedEntity = cem;

            return(Task.CompletedTask);
        }
Ejemplo n.º 6
0
        public Task Handle(CreateCustomerCommand command, CancellationToken cancellationToken)
        {
            var cust = _mapper.Map <CreateCustomerCommand, Customer>(command);

            cust.ManagedBy = _userService.ClaimsIdentity.Name;

            _db.Create(cust);
            command.CreatedEntity = cust;


            return(Task.CompletedTask);
        }
Ejemplo n.º 7
0
        public Task Handle(CreateUser command)
        {
            var entity = new User
            {
                Name          = command.Name,
                SecurityStamp = Guid.NewGuid().ToString(),
            };

            _entities.Create(entity);

            command.CreatedEntity = entity;
            return(Task.FromResult(0));
        }
Ejemplo n.º 8
0
        public async Task Handle(CreateEmailVerification command)
        {
            // find or create the email address
            var emailAddress = await _entities.Get <EmailAddress>().ByValueAsync(command.EmailAddress)
                               ?? new EmailAddress
            {
                Value       = command.EmailAddress.ToLower(), // TODO: allow users to change capitalization of email?
                HashedValue = await _queries.Execute(new HashedEmailValueBy(command.EmailAddress)),
            };

            // create random secret and ticket
            // note that changing the length of the secret requires updating the
            // email messages sent to the address, since those mention secret length
            var secret = _queries.Execute(new RandomSecret(10, 12));
            var ticket = _queries.Execute(new RandomSecret(20, 25));

            // make sure ticket is unique
            while (_entities.Query <EmailVerification>().ByTicket(ticket) != null)
            {
                ticket = _queries.Execute(new RandomSecret(20, 25));
            }

            // serialize a new user token to a string
            var token = await _userManager.UserTokenProvider.GenerateAsync(command.Purpose.ToString(), _userManager,
                                                                           new UserTicket
            {
                UserName = ticket,
            });

            // create the verification
            var verification = new EmailVerification
            {
                EmailAddress = emailAddress,
                Purpose      = command.Purpose,
                Secret       = secret,
                Ticket       = ticket,
                Token        = token,

                // change this, and you have to change the content of the email messages to reflect new expiration
                ExpiresOnUtc = DateTime.UtcNow.AddMinutes(30),
            };

            _entities.Create(verification);

            if (command.Commit)
            {
                await _entities.SaveChangesAsync();
            }

            command.CreatedEntity = verification;
        }
Ejemplo n.º 9
0
        public Task Handle(CreatePersonCommand command, CancellationToken cancellationToken)
        {
            var newPerson = new Person
            {
                Name        = command.Name,
                Age         = command.Age,
                Description = command.Description
            };

            _db.Create(newPerson);
            command.CreatedEntity = newPerson;

            return(Task.CompletedTask);
        }
 public void AddAlbum(Album album)
 {
     _writeEntities.Create(album);
 }