Esempio n. 1
0
        public Account Get(string login, ApplicationStore applicationStore, string password = "")
        {
            Account account  = accPermissionService.Get(login, applicationStore).FirstOrDefault();
            Account _account = null;

            if (!password.IsNullOrWhiteSpace() && !account.IsNull() && !account.Password.Equals(password.Encrypt()))
            {
                _account = account;
                account  = null;
            }

            //dont has connect
            if (account.IsNull() &&
                (applicationStore.Application.MemberType == Domain.Enums.MemberType.Consumer || applicationStore.Application.Name.ToLower() == "mp-accounts"))
            {
                var accounts = accPermissionService.Get(login, applicationStore);

                account = CreateConnect(accounts, applicationStore, password);

                if (!password.IsNullOrWhiteSpace() && accounts.Count() > 0 && account.IsNull())
                {
                    accounts.ToList().ForEach(acc =>
                    {
                        if (acc.Code != _account.Code && lockedUpMemberPolicy.Validate(acc, false) && lockedUpMemberPolicy.Validate(acc, applicationStore, false))
                        {
                            acc.WrongLoginAttempt(lockedUpMemberPolicy, lockMemberPolicy);
                            Update(acc);
                        }
                    });
                }
            }

            if (account.IsNull() && !_account.IsNull())
            {
                account = _account;
            }

            return(account);
        }
Esempio n. 2
0
        public void UpdatePermission(DTO.AccountPermission accountPermission, PermissionType type, Guid clientId, Guid currentAccount)
        {
            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    var applicationStore = applicationStoreRepository.GetByClientId(clientId);
                    var storeCode        = applicationStore.GetStoreCodeByAuthType(accountPermission.StoreCode);

                    if (!currentAccount.IsEmpty())
                    {
                        Account accountCurrent = accPermissionService.Get(currentAccount, applicationStore);

                        if (accountCurrent.IsNull())
                        {
                            throw new ArgumentException("Você não possui permissão para executar essa ação");
                        }
                    }
                    else if (!applicationStore.Store.IsMain)
                    {
                        throw new ArgumentException("Você não possui permissão para executar essa ação");
                    }

                    Account account;
                    Role    role;
                    foreach (var accountCode in accountPermission.AccountCodes)
                    {
                        account = accountRepository.Get(accountCode, false, includeRole: true, includeBlacklist: true, includeApplication: true);

                        if (account.IsNull())
                        {
                            throw new ArgumentException("Usuário não encontrado");
                        }

                        if (account.BlacklistCollection.Count > 0)
                        {
                            throw new ArgumentException("Usuário na blacklist");
                        }

                        bool hasPendingChanges = false;

                        if (type == PermissionType.Add)
                        {
                            accountPermission.Applications.ForEach(applicationName =>
                            {
                                role = roleRepository.GetByApplication(applicationName, storeCode);

                                if (role.IsNull())
                                {
                                    throw new ArgumentException("Não será possível atribuir a permissão");
                                }

                                var exist = role.AccountRoles.Any(a => a.Status && a.AccountCode == account.Code && account.AccountRoles.Any(r => r.RoleCode == a.RoleCode));

                                if (!exist)
                                {
                                    account.ConnectRole(role);
                                    hasPendingChanges = true;
                                }
                            });
                        }
                        else if (type == PermissionType.Remove)
                        {
                            accountPermission.Applications.ForEach(applicationName =>
                            {
                                var accountRole = account.AccountRoles
                                                  .Where(x => x.Status &&
                                                         x.Role.Permissions.Any(a => a.Resource.Application.Name.ToLower() == applicationName.ToLower() &&
                                                                                x.Role.StoreCode == storeCode)).FirstOrDefault();

                                if (!accountRole.IsNull())
                                {
                                    accountRole.Status     = false;
                                    accountRole.UpdateDate = DateTime.Now;
                                    account.AccountRoles.Append(accountRole);
                                    hasPendingChanges = true;
                                }
                            });
                        }

                        if (hasPendingChanges)
                        {
                            accountService.Update(account);
                        }
                    }
                }
                catch
                {
                    transaction.Rollback();

                    throw;
                }
            }
        }