Пример #1
0
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            stoppingToken.ThrowIfCancellationRequested();

            var consumer = new EventingBasicConsumer(_channel);

            consumer.Received += (ch, ea) =>
            {
                var content = Encoding.UTF8.GetString(ea.Body.ToArray());

                DomainDispatcher.RaiseEvent(new EventBusMessageReceivedEvent()
                {
                    QueueName = this.QueueName,
                    Message   = content
                });

                //var model = CommonService.Deserialize<IDomainEvent>(content);
                //DomainDispatcher.RaiseEvent(model);

                _channel.BasicAck(ea.DeliveryTag, false);
            };
            //consumer.Shutdown += OnConsumerShutdown;
            //consumer.Registered += OnConsumerRegistered;
            //consumer.Unregistered += OnConsumerUnregistered;
            //consumer.ConsumerCancelled += OnConsumerCancelled;

            _channel.BasicConsume(this.QueueName, false, consumer);

            return(Task.CompletedTask);
        }
Пример #2
0
        public Guid PasswordRecoveryInit(ProfilePasswordRecoveryModel model)
        {
            // validate model
            if (model == null || String.IsNullOrWhiteSpace(model.Login))
            {
                throw new CustomInputException("Password Recovery model is empty!");
            }
            if (String.IsNullOrWhiteSpace(model.Password))
            {
                throw new CustomInputException("Password is empty!");
            }
            if (!model.IsPasswordMatch())
            {
                throw new CustomInputException("Password Confirmation does not match!");
            }

            // security policy
            var securityPolicy = this.GetSecurityPolicy();

            if (!securityPolicy.CheckStrength(model.Password))
            {
                throw new CustomInputException("Password does not match Security Policy!");
            }

            // get person
            var person = this.Persons
                         .Include(t => t.User)
                         .FirstOrDefault(p => p.User.Login.ToLower() == model.Login.ToLower());

            if (person == null || person.User == null)
            {
                throw new CustomInputException($"User with login '{model.Login}' not found!");
            }

            model.PersonID = person.ID;

            // create activity
            var now      = CommonService.Now;
            var pin      = CommonService.GeneratePin(10);
            var activity = Activity.Create(now.AddHours(DEFAULT_ACTIVITY_EXPIRATION_HOURS), DEFAULT_ACTIVITY_TYPE_RECOVERY, model, pin);

            _dbContext.Set <Activity>().Add(activity);
            _dbContext.SaveChanges();

            // raise event to send email
            DomainDispatcher.RaiseEvent(new PasswordRecoveryInitDomainEvent()
            {
                Email     = person.Email,
                NameFirst = person.Name.First,
                NameLast  = person.Name.Last,
                Login     = model.Login,
                PIN       = pin
            });

            return(activity.ID);
        }
Пример #3
0
        public IActionResult GetMovie(int id)
        {
            var movie = DomainDispatcher.ExecuteQuery(new GetMovieQuery(id));

            if (movie == null)
            {
                return(NotFound());
            }

            return(Ok(movie));
        }
Пример #4
0
        public IActionResult GetRentsForMovie(int movieId)
        {
            var rents = DomainDispatcher.ExecuteQuery(new GetRentsForMovieQuery(movieId));

            if (rents == null)
            {
                return(NotFound());
            }

            return(Ok(rents));
        }
Пример #5
0
        public IActionResult DeleteMovie(int id)
        {
            var command = new DeleteMovieCommand(id);

            DomainDispatcher.ExecuteCommand(command);

            if (command.WasSuccesful)
            {
                return(NoContent());
            }
            else
            {
                return(BadRequest());
            }
        }
Пример #6
0
        public IActionResult Login([FromBody] User user)
        {
            var command = new LoginCommand(user.Email, user.Password);

            DomainDispatcher.ExecuteCommand(command);

            if (command.WasSuccesful)
            {
                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
Пример #7
0
        public IActionResult EditMovie(int id, [FromBody] Movie movie)
        {
            var command = new EditMovieCommand(movie);

            DomainDispatcher.ExecuteCommand(command);

            if (command.WasSuccesful)
            {
                return(Ok(movie));
            }
            else
            {
                return(BadRequest());
            }
        }
Пример #8
0
        public IActionResult CreateMovie([FromBody] Movie movie)
        {
            if (movie == null)
            {
                return(NotFound());
            }

            var command = new CreateMovieCommand(movie);

            DomainDispatcher.ExecuteCommand(command);

            if (command.WasSuccesful)
            {
                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
Пример #9
0
        public void Handle(EventBusMessageReceivedEvent args)
        {
            if (args == null)
            {
                return;
            }

            var emailModel = CommonService.TryDeserialize <EmailMessageModel>(args.Message);

            if (emailModel != null)
            {
                this.SendEmail(emailModel);
                return;
            }

            var domainEventModel = CommonService.TryDeserialize <IDomainEvent>(args.Message);

            if (domainEventModel != null)
            {
                DomainDispatcher.RaiseEvent(domainEventModel);
                return;
            }
        }
Пример #10
0
        public void PasswordRecoveryConfirm(Guid activityID, string pin)
        {
            var activity = _dbContext.Set <Activity>().FirstOrDefault(a => a.ID == activityID);

            if (activity == null || !activity.IsValid() || activity.Type != DEFAULT_ACTIVITY_TYPE_RECOVERY)
            {
                throw new CustomNotFoundException("Password Recovery data not found. Please, try again!");
            }

            if (!activity.CheckPin(pin))
            {
                throw new CustomNotFoundException("Password Recovery data not found. Please, try again!");
            }

            var model = activity.GetObject <ProfilePasswordRecoveryModel>();

            if (model == null || model.PersonID == null)
            {
                throw new CustomNotFoundException("Password Recovery data not found. Please, try again!");
            }

            // get person
            var person = this.Persons
                         .Include(t => t.User)
                         .FirstOrDefault(t => t.ID == model.PersonID.Value);

            // security policy to store passwords
            var securityPolicy = this.GetSecurityPolicy();

            if (securityPolicy == null)
            {
                throw new CustomArgumentException("Security Policy is not provided!");
            }

            // password to use
            var password = model.Password;

            if (String.IsNullOrWhiteSpace(password))
            {
                throw new CustomInputException("Password is empty!");
            }
            if (!securityPolicy.CheckStrength(password))
            {
                throw new CustomInputException("Password does not match securuty policy!");
            }

            // password Hash
            var passwordHash = securityPolicy.HashPassword(password);

            if (!String.IsNullOrWhiteSpace(person.User.Password) && !securityPolicy.CanReuse && passwordHash.Equals(person.User.Password, CommonService.StringComparison))
            {
                throw new CustomInputException("Password was used already!");
            }

            // change password
            person.User.ChangePassword(passwordHash);

            // discard activity
            activity.Discard();
            _dbContext.Set <Activity>().Update(activity);

            // update person
            this.Persons.Update(person);

            _dbContext.SaveChanges();

            // raise event of password change to send email
            DomainDispatcher.RaiseEvent(new PasswordChangedDomainEvent()
            {
                Login     = person.User.Login,
                NameFirst = person.Name.First,
                NameLast  = person.Name.Last,
                Email     = person.Email,
                Password  = model.Password
            });
        }
Пример #11
0
        public IActionResult GetMovies()
        {
            var movies = DomainDispatcher.ExecuteQuery(new GetAllMoviesQuery());

            return(Ok(movies));
        }
Пример #12
0
 public AuthController(DomainDispatcher domainDispatcher) : base(domainDispatcher)
 {
 }
Пример #13
0
 protected YaccLexToolsCommand()
 {
     _domain     = AppDomain.CurrentDomain;
     _dispatcher = (DomainDispatcher)_domain.GetData("yltDispatcher");
 }
Пример #14
0
 protected MigrationsDomainCommand()
 {
     _domain     = AppDomain.CurrentDomain;
     _dispatcher = (DomainDispatcher)_domain.GetData("efDispatcher");
 }
Пример #15
0
 public MoviesController(DomainDispatcher domain) : base(domain)
 {
 }
Пример #16
0
        public void ChangePassword(ProfileChangePasswordModel model)
        {
            var token = _tokenProvider.GetToken();

            if (token == null || !token.IsValid())
            {
                throw new CustomAuthenticationException("User is not authenticated!");
            }

            // password to use
            var password = model?.Password ?? "";

            // validate model
            if (model == null)
            {
                throw new CustomInputException("Change Password model is empty!");
            }
            if (String.IsNullOrWhiteSpace(password))
            {
                throw new CustomInputException("Password is empty!");
            }
            if (!model.IsPasswordMatch())
            {
                throw new CustomInputException("Password Confirmation does not match!");
            }

            // security policy
            var securityPolicy = this.GetSecurityPolicy();

            if (!securityPolicy.CheckStrength(password))
            {
                throw new CustomInputException("Password does not match securuty policy!");
            }

            // get person
            var person = this.Persons
                         .Include(t => t.User)
                         .FirstOrDefault(t => t.User.ID == token.UserID);

            if (person == null || person.User == null)
            {
                throw new CustomInputException($"User not found!");
            }

            if (!securityPolicy.VerifyPassword(model.PasswordCurrent, person.User.Password))
            {
                throw new CustomInputException("Current Password does not match!");
            }

            // password Hash
            var passwordHash = securityPolicy.HashPassword(password);

            if (!String.IsNullOrWhiteSpace(person.User.Password) && !securityPolicy.CanReuse && passwordHash.Equals(person.User.Password, CommonService.StringComparison))
            {
                throw new CustomInputException("Password was used already!");
            }

            // change password
            person.User.ChangePassword(passwordHash);
            this.Persons.Update(person);
            _dbContext.SaveChanges();

            // raise event of password change to send email
            DomainDispatcher.RaiseEvent(new PasswordChangedDomainEvent()
            {
                Login     = person.User.Login,
                NameFirst = person.Name.First,
                NameLast  = person.Name.Last,
                Email     = person.Email,
                Password  = model.Password
            });
        }
Пример #17
0
 public RentsController(DomainDispatcher domainDispatcher) : base(domainDispatcher)
 {
 }
Пример #18
0
        public Guid RegistrationInit(ProfileRegistrationModel model)
        {
            if (model == null)
            {
                throw new CustomArgumentException("Registration data is empty!");
            }

            // input validation
            if (String.IsNullOrWhiteSpace(model.Email))
            {
                throw new CustomInputException("Email is empty!");
            }
            if (String.IsNullOrWhiteSpace(model.Password))
            {
                throw new CustomInputException("Password is empty!");
            }
            if (!model.IsPasswordMatch())
            {
                throw new CustomInputException("Password confirmation is not equal to Passowrd!");
            }

            // security policy
            var securityPolicy = this.GetSecurityPolicy();

            if (!securityPolicy.CheckStrength(model.Password))
            {
                throw new CustomInputException("Password does not match Security Policy!");
            }

            // change valid login
            model.Login = model.GetValidLogin();

            // check if user already exists with login
            var persons = this.Persons
                          .Include(t => t.User)
                          .Where(p => p.User.Login.ToLower() == model.Login.ToLower())
                          .ToList();

            if (persons.Any())
            {
                throw new CustomInputException($"Person with login {model.Login} is already exist!");
            }

            // create activity
            var now      = CommonService.Now;
            var pin      = CommonService.GeneratePin(10);
            var activity = Activity.Create(now.AddHours(DEFAULT_ACTIVITY_EXPIRATION_HOURS), DEFAULT_ACTIVITY_TYPE_REGISTRATION, model, pin);

            _dbContext.Set <Activity>().Add(activity);
            _dbContext.SaveChanges();

            // raise event to send email
            DomainDispatcher.RaiseEvent(new RegistrationInitDomainEvent()
            {
                Email     = model.Email,
                NameFirst = model.NameFirst,
                NameLast  = model.NameLast,
                Login     = model.Login,
                PIN       = pin
            });

            return(activity.ID);
        }
Пример #19
0
 public BaseController(DomainDispatcher domainDispatcher)
 {
     DomainDispatcher = domainDispatcher;
 }