Esempio n. 1
0
        public DTO.Restriction Save(bool isRKAdmin, Guid clientId, DTO.Restriction restriction)
        {
            restriction.IsValid();

            var _restriction = restriction.Transfer();

            var _role = roleRepository.Get(_restriction.RoleCode, null);

            if (_role.IsNull())
            {
                throw new ArgumentException("Grupo não encontrado");
            }

            if (!isRKAdmin)
            {
                var appStore = applicationStoreRepository.GetByClientId(clientId);

                if (appStore.StoreCode != _role.Store.Code)
                {
                    throw new ArgumentException("Usuário não possui permissão para realizar essa operação");
                }
            }

            _restriction.Role     = null;
            _restriction.RoleCode = _role.Code;

            Restriction result;

            using (var transaction = Connection.BeginTransaction())
                result = restrictionRepository.Save(_restriction);

            return(result.GetResult());
        }
Esempio n. 2
0
        public DTO.SearchResult Search(bool isRKAdmin, Guid clientId, string name = null, Guid?application_code = null, int?skip = null, int?take = null)
        {
            DTO.SearchFilterResource searchFilter = new DTO.SearchFilterResource();

            if (!name.IsNullOrWhiteSpace())
            {
                searchFilter.Name = name;
            }

            if (skip.HasValue)
            {
                searchFilter.Skip = skip;
            }

            if (take.HasValue)
            {
                searchFilter.Take = take;
            }

            if (isRKAdmin)
            {
                searchFilter.ApplicationCode = application_code;
            }
            else
            {
                var appStore = appStoreRepository.GetByClientId(clientId);
                searchFilter.ApplicationCode = appStore.Application.Code;
            }

            var result = repository.Search(searchFilter);

            var resources = result.Results.Select(r => new DTO.Resource(r, includeApp: true));

            return(new DTO.SearchResult(resources).SetResult <Resource>(result));
        }
Esempio n. 3
0
        public Store Create(DO.Store store, Guid?clientId = null, bool storeExists = false, bool onlyApplicationMain = false, bool userExists = false, bool syncInApplication = false)
        {
            Store newStore = new Store();

            if (storeExists)
            {
                var _store = storeService.Get(store.Code);

                if (!_store.IsNull() && !_store.Code.IsEmpty())
                {
                    throw new ArgumentException("Esta loja já possui cadastro com o nome '{0}' e código {1}".ToFormat(_store.Name, _store.Code));
                }
            }

            if (onlyApplicationMain)
            {
                if (!clientId.HasValue)
                {
                    throw new ArgumentException("Não é possível efetuar essa operação");
                }

                var applicationStore = applicationStoreRepository.GetByClientId(clientId.Value);

                if (!applicationStore.Store.IsMain)
                {
                    throw new ArgumentException("Não é possível efetuar essa operação");
                }
            }

            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    newStore = storeService.CreateStore(store);
                    applicationStoreService.CreateAppStore(newStore);
                    roleService.SaveAll(newStore.Code);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

            return(newStore);
        }
Esempio n. 4
0
        public DTO.Permission Save(bool isRKAdmin, Guid clientId, DTO.Permission permission)
        {
            permission.IsValid();
            Permission result;

            var _permission = permission.Transfer(includeRole: true);

            var _resource = resourceRepository.GetByCode(_permission.ResourceCode, null);

            if (_resource.IsNull())
            {
                throw new ArgumentException("Módulo não encontrado");
            }

            var _role = roleRepository.Get(_permission.RoleCode, null);

            if (_role.IsNull())
            {
                throw new ArgumentException("Grupo não encontrado");
            }

            if (!isRKAdmin)
            {
                var appStore = appStoreRepository.GetByClientId(clientId);

                if (appStore.StoreCode != _role.StoreCode || appStore.Application.Code != _resource.ApplicationCode)
                {
                    throw new ArgumentException("Usuário não possui permissão para realizar essa operação");
                }
            }

            _permission.Role         = null;
            _permission.RoleCode     = _role.Code;
            _permission.Resource     = null;
            _permission.ResourceCode = _resource.Code;

            using (var transaction = Connection.BeginTransaction())
            {
                result = repository.Save(_permission);
            }

            return(new DTO.Permission(result));
        }
Esempio n. 5
0
        public bool Exists(string clientId)
        {
            Guid clientCode;

            if (Guid.TryParse(clientId, out clientCode))
            {
                var client = applicationStoreRepository.GetByClientId(clientCode);
                return(!client.IsNull() && !client.Code.IsEmpty());
            }

            return(false);
        }
Esempio n. 6
0
 public ApplicationStore GetByClientId(Guid clientId)
 {
     return(applicationStoreRepository.GetByClientId(clientId));
 }
Esempio n. 7
0
        public ChangePasswordToken CreateChangePasswordToken(Guid accountCode, Guid clientId, string urlBack, bool showMessage)
        {
            var appSto = applicationStoreRepository.GetByClientId(clientId);

            return(new ChangePasswordToken(accountCode, appSto.Code, urlBack, showMessage));
        }
Esempio n. 8
0
        public override string GenerateResetPasswordToken(string email, Guid clientId, string urlBack = "", int emailTemplateCode = 0)
        {
            if (email.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("E-mail não informado");
            }

            ApplicationStore appSto;
            Account          account;
            CustomerImport   customerImport = null;
            string           _tokenCode     = string.Empty;

            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    appSto = applicationStoreRepository.GetByClientId(clientId);
                    var accounts = accountRepository.Get(email, appSto, true);

                    accountService.lockedUpMemberPolicy = lockedUpMemberPolicy;
                    accountService.lockMemberPolicy     = lockMemberPolicy;
                    accountService.passwordPolicy       = passwordPolicy;

                    account = accountService.Authenticate(accounts, appSto, false);

                    if (account != null)
                    {
                        var resetPasswordTokenService = resetPasswordTokenFactory.GetResetPasswordTokenService(account);

                        resetPasswordTokenService.lockedUpMemberPolicy = lockedUpMemberPolicy;

                        var token = resetPasswordTokenService.GenerateResetPasswordToken(account, appSto, urlBack);

                        _tokenCode = token.Code.EncodeURIComponent();

                        svcEmail.SendPasswordRecoveryEmailAsync(account, appSto.Store, _tokenCode, token.UrlBack, emailTemplateCode);
                    }
                    else
                    {
                        customerImport = customerImportService.Get(email, appSto.Store.Code);

                        if (customerImport != null)
                        {
                            var resetPasswordTokenService = resetPasswordTokenFactory.GetResetPasswordTokenService(customerImport);

                            var token = resetPasswordTokenService.GenerateResetPasswordToken(customerImport, appSto, urlBack);

                            _tokenCode = token.Code.EncodeURIComponent();

                            customerImport.HandleCustomer();
                            svcEmail.SendPasswordRecoveryEmailAsync(customerImport, appSto.Store, _tokenCode, urlBack, emailTemplateCode);
                        }
                    }

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

            if (!appSto.IsNull() && (!account.IsNull() || !customerImport.IsNull()))
            {
                using (var transaction = Connection.BeginTransaction())
                {
                    try
                    {
                        var code = !account.IsNull() ? account.Code : customerImport.AccountCode;

                        passwordLogRepository.Save(new PasswordLog(code, PasswordEventLog.RequestRecovery, appSto.Store.Code));

                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                    }
                }
            }

            return(_tokenCode);
        }
Esempio n. 9
0
        public void CreatePassword(Guid memberCode, string tokenCode, string newPassword, Guid?clientId = null)
        {
            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    var customerImport = customerImportRepository.Get(memberCode);

                    if (passwordPolicy == null || passwordPolicy.Validate(customerImport.Email, newPassword))
                    {
                        customerImport.Password = newPassword.Encrypt();
                        customerImport.HandleCustomer();

                        var listAppStore     = new List <ApplicationStore>();
                        var applicationStore = applicationStoreRepository.GetByClientId(clientId.Value);

                        if (applicationStore.IsNull())
                        {
                            throw new ArgumentException("Aplicação inválida");
                        }

                        listAppStore.Add(applicationStore);

                        if (customerImport.StoreCode.HasValue)
                        {
                            var appsStore = applicationStoreRepository.GetByStore(customerImport.StoreCode.Value);

                            if (appsStore.IsNull())
                            {
                                throw new ArgumentException("Loja sem aplicações");
                            }

                            var appECStore = appsStore.FirstOrDefault(s => s.Application.Name.ToLower() == "ec-loja");

                            if (appECStore.IsNull())
                            {
                                throw new ArgumentException("Aplicação EC-Loja não encontrada");
                            }

                            listAppStore.Add(appECStore);
                        }
                        else
                        {
                            throw new ArgumentException("Código da loja não preenchido");
                        }

                        Customer customer;
                        if (customerImport is CompanyImport)
                        {
                            customer = new Company(customerImport);
                        }
                        else
                        {
                            customer = new Person(customerImport);
                        }

                        var _accounts = new Account()
                        {
                            Email    = customerImport.Email,
                            Login    = customerImport.Email,
                            Document = customerImport.DisplayDocument,
                            Password = newPassword,
                            Customer = customer,
                            Status   = customerImport.Status,
                            Removed  = false
                        };

                        listAppStore.ForEach(x =>
                        {
                            _accounts.ConnectApp(x);
                        });

                        Role _role;
                        listAppStore.ForEach(x =>
                        {
                            _role = roleRepository.GetByApplication(x.ApplicationCode, x.StoreCode);

                            if (!_role.IsNull())
                            {
                                _accounts.ConnectRole(_role);
                            }
                        });

                        var accountDO = accountService.Get(customerImport.Email, customerImport.DisplayDocument, applicationStore, true);

                        if (accountDO.IsNull())
                        {
                            accountService.Add(_accounts, applicationStore, customerImport.StoreCode.Value);
                        }
                        else
                        {
                            accountService.Update(_accounts);
                        }

                        customerImport.Password   = string.Empty;
                        customerImport.Removed    = true;
                        customerImport.UpdateDate = DateTime.Now;
                        customerImportRepository.Update(customerImport);
                    }
                    else
                    {
                        throw new ArgumentException("senha inválida.");
                    }

                    resetPasswordTokenRepository.Delete(tokenCode);

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

            if (clientId.HasValue && !clientId.Value.IsEmpty())
            {
                using (var transaction = Connection.BeginTransaction())
                {
                    try
                    {
                        var store = applicationStoreRepository.GetByClientId(clientId.Value).Store;

                        passwordLogRepository.Save(new PasswordLog(memberCode, PasswordEventLog.RecoveryCustomerImport, store.Code));

                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                    }
                }
            }
        }
Esempio n. 10
0
        public Account AuthenticateTransferToken(Guid accountCode, Guid clientId)
        {
            var _account = accountRepository.Get(accountCode, true, true);

            if (_account.IsNull())
            {
                return(null);
            }

            accountService.lockedUpMemberPolicy = lockedUpMemberPolicy;
            accountService.lockMemberPolicy     = lockMemberPolicy;
            accountService.passwordPolicy       = passwordPolicy;

            var applicationStore = applicationStoreRepository.GetByClientId(clientId);
            var accounts         = accountRepository.Get(_account.Email, applicationStore);

            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    var account = accountService.Authenticate(accounts, applicationStore);

                    transaction.Commit();
                    return(account);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    return(null);
                }
            }
        }