예제 #1
0
        public override bool Execute(IConfiguration configuration)
        {
            Configuration = configuration;

            using (var connector = new MySQLAdapter(Configuration))
            {
                connector.SwitchDatabase(configuration.Database);
            }

            var adminUser = new SystemUser
            {
                Username   = "******",
                Passhash   = "test",
                CreatedAt  = DateTime.Now,
                ModifiedAt = DateTime.Now,
            };

            var repository = new SystemUserRepository(new MySQLAdapter(Configuration));

            try
            {
                repository.Add(adminUser);
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                return(false);
            }

            return(true);
        }
예제 #2
0
        public RedirectResult SignUp(string login, string password)
        {
            var systemUser = _systemUserRepository.Add(new SystemUser
            {
                Login    = login,
                Password = password
            });

            if (systemUser == null)
            {
                return(Redirect("/signup"));
            }

            HttpContext.Session.SetString("userId", systemUser.Id.ToString());

            return(Redirect("/bots"));
        }
예제 #3
0
        public void TestSetItem()
        {
            var systemUser = new SystemUser
            {
                Id              = 1,
                Name            = "Teste",
                LastName        = "Teste",
                Email           = "*****@*****.**",
                Password        = "******",
                RegisterDate    = DateTime.Now,
                Status          = true,
                SystemProfile   = null,
                SystemProfileId = 1
            };

            _systemUserRepository.Add(systemUser);
        }
예제 #4
0
        public async Task <UpdateSystemUserCommandResponse> Handle(UpdateSystemUserCommand request, CancellationToken cancellationToken)
        {
            var response = new UpdateSystemUserCommandResponse()
            {
                IsSuccessful = false
            };

            var userExists = _systemUserRepository.UserExists(request.Email, request.SystemUserId);

            if (!userExists)
            {
                throw new RulesException("email", $"User with SystemuserId: {request.SystemUserId} and email address: {request.Email} does not exists");
            }

            //var user = _mapper.Map<SystemUsers>(request);
            var user = (await _systemUserRepository.GetSystemUsersByIds(new List <int> {
                request.SystemUserId
            })).FirstOrDefault();
            var countriesToDelete = new List <int>();

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                user.FirstName         = request.FirstName;
                user.LastName          = request.LastName;
                user.Level             = request.Level;
                user.Role              = request.Role;
                user.WorkPhoneNumber   = request.WorkPhoneNumber;
                user.MobilePhoneNumber = request.MobilePhoneNumber;
                user.Location          = request.Location;
                user.Email             = request.Email;
                user.UpdatedBy         = "CMS Admin";
                user.UpdatedDate       = DateTime.UtcNow;

                var homeCountry = user.SystemUserAssociatedCountries.FirstOrDefault(c => c.CountryId == request.HomeCountry);
                if (homeCountry == null)
                {
                    homeCountry = new SystemUserAssociatedCountries
                    {
                        CountryId    = request.HomeCountry,
                        IsPrimary    = true,
                        SystemUserId = user.SystemUserId
                    };
                    _systemUserRepository.Add(homeCountry);
                }
                else
                {
                    homeCountry.IsPrimary    = true;
                    homeCountry.SystemUserId = user.SystemUserId;
                    homeCountry.CountryId    = request.HomeCountry;
                    _systemUserRepository.Update(homeCountry);
                }

                foreach (var country in request.Countries.Where(x => !x.Equals(request.HomeCountry)))
                {
                    if (user.SystemUserAssociatedCountries.FirstOrDefault(c => c.CountryId == country) == null)
                    {
                        var associatedCountries = new SystemUserAssociatedCountries
                        {
                            CountryId    = country,
                            SystemUserId = user.SystemUserId,
                            IsPrimary    = false
                        };
                        user.SystemUserAssociatedCountries.Add(associatedCountries);
                        _systemUserRepository.Add(associatedCountries);
                    }
                }
                foreach (var associatedCountry in user.SystemUserAssociatedCountries.ToList())
                {
                    if (request.Countries.Where(c => c == associatedCountry.CountryId).Count() == 0)
                    {
                        countriesToDelete.Add(associatedCountry.SystemUserAssociatedCountryId);
                        user.SystemUserAssociatedCountries.Remove(associatedCountry);
                        _systemUserRepository.Delete(associatedCountry);
                    }
                }

                await _systemUserRepository.UnitOfWork.SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var systemuserDocs = _context.GetAll(Constants.SystemUsersDiscriminator);
                foreach (var content in user.SystemUserAssociatedCountries)
                {
                    var doc = systemuserDocs.FirstOrDefault(d => d.GetPropertyValue <int>("SystemUserId") == user.SystemUserId &&
                                                            d.GetPropertyValue <int>("SystemUserAssociatedCountryId") == content.SystemUserAssociatedCountryId);
                    var eventSourcing = new SystemUserCommandEvent()
                    {
                        id = doc != null?doc.GetPropertyValue <Guid>("id") : Guid.NewGuid(),
                                 EventType         = doc != null ? ServiceBusEventType.Update : ServiceBusEventType.Create,
                                 Discriminator     = Constants.SystemUsersDiscriminator,
                                 SystemUserId      = user.SystemUserId,
                                 CreatedBy         = user.CreatedBy ?? string.Empty,
                                 CreatedDate       = user.CreatedDate,
                                 UpdatedBy         = user.UpdatedBy,
                                 UpdatedDate       = user.UpdatedDate,
                                 FirstName         = user.FirstName,
                                 LastName          = user.LastName,
                                 WorkPhoneNumber   = user.WorkPhoneNumber,
                                 MobilePhoneNumber = user.MobilePhoneNumber,
                                 Level             = user.Level,
                                 Role      = user.Role,
                                 Location  = user.Location,
                                 Email     = user.Email,
                                 TimeZone  = user.TimeZone,
                                 CountryId = content.CountryId,
                                 IsPrimary = content.IsPrimary,
                                 SystemUserAssociatedCountryId = content.SystemUserAssociatedCountryId,
                                 PartitionKey = ""
                    };
                    await _eventcontext.PublishThroughEventBusAsync(eventSourcing);
                }
                foreach (int i in countriesToDelete)
                {
                    var deleteEvt = new SystemUserCommandEvent()
                    {
                        id = systemuserDocs.FirstOrDefault(d => d.GetPropertyValue <int>("SystemUserId") == user.SystemUserId &&
                                                           d.GetPropertyValue <int>("SystemUserAssociatedCountryId") == i).GetPropertyValue <Guid>("id"),
                        EventType     = ServiceBusEventType.Delete,
                        Discriminator = Constants.SystemUsersDiscriminator,
                        PartitionKey  = ""
                    };
                    await _eventcontext.PublishThroughEventBusAsync(deleteEvt);
                }
                scope.Complete();
            }
            return(response);
        }
        public async Task <CreateSystemUserCommandResponse> Handle(CreateSystemUserCommand request, CancellationToken cancellationToken)
        {
            var response = new CreateSystemUserCommandResponse()
            {
                IsSuccessful = false
            };

            // Check User Exists
            var userExists = _systemUserRepository.UserExists(request.Email, 0);

            if (userExists)
            {
                throw new RulesException("email", $"This email address: {request.Email} already exists");
            }

            var user = _mapper.Map <SystemUsers>(request);

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                user.CreatedBy   = "CMS Admin";
                user.CreatedDate = DateTime.UtcNow;
                user.UpdatedBy   = "CMS Admin";
                user.UpdatedDate = DateTime.UtcNow;


                _systemUserRepository.Add(user);
                await _systemUserRepository.UnitOfWork.SaveEntitiesAsync();


                var homeCountry = new SystemUserAssociatedCountries();
                homeCountry.CountryId    = request.HomeCountry;
                homeCountry.IsPrimary    = true;
                homeCountry.SystemUserId = user.SystemUserId;
                _systemUserRepository.Add(homeCountry);

                var countryPermissionList = request.Countries.Distinct();
                foreach (var country in countryPermissionList.Where(x => !x.Equals(request.HomeCountry)))
                {
                    var associatedCountry = new SystemUserAssociatedCountries();
                    associatedCountry.CountryId    = country;
                    associatedCountry.SystemUserId = user.SystemUserId;
                    associatedCountry.IsPrimary    = false;
                    _systemUserRepository.Add(associatedCountry);
                }

                await _systemUserRepository.UnitOfWork.SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                foreach (var content in user.SystemUserAssociatedCountries)
                {
                    var eventSourcing = new SystemUserCommandEvent()
                    {
                        EventType         = ServiceBusEventType.Create,
                        Discriminator     = Constants.SystemUsersDiscriminator,
                        SystemUserId      = user.SystemUserId,
                        CreatedBy         = user.CreatedBy,
                        CreatedDate       = user.CreatedDate,
                        UpdatedBy         = user.UpdatedBy,
                        UpdatedDate       = user.UpdatedDate,
                        FirstName         = user.FirstName,
                        LastName          = user.LastName,
                        WorkPhoneNumber   = user.WorkPhoneNumber,
                        MobilePhoneNumber = user.MobilePhoneNumber,
                        Level             = user.Level,
                        Role      = user.Role,
                        Location  = user.Location,
                        Email     = user.Email,
                        TimeZone  = user.TimeZone,
                        CountryId = content.CountryId,
                        IsPrimary = content.IsPrimary,
                        SystemUserAssociatedCountryId = content.SystemUserAssociatedCountryId,
                        PartitionKey = ""
                    };
                    await _eventcontext.PublishThroughEventBusAsync(eventSourcing);
                }
                scope.Complete();
            }
            return(response);
        }