//TODO Вынести имперсонацию отсюда
        public List <WorkflowCommandType> IsAllowedToExecuteCommand(Guid instanceUid,
                                                                    List <WorkflowCommandType> commandsToCheck)
        {
            var allowedOperations = new List <WorkflowCommandType>(commandsToCheck.Count);

            WorkflowState currentState;

            try
            {
                currentState = WorkflowStateService.GetCurrentState(instanceUid);
            }
            catch (ArgumentException)
            {
                return(allowedOperations);
            }

            WorkflowType workflowType = currentState == null
                                            ? WorkflowStateService.TryGetExpectedWorkflowType(instanceUid)
                                            : currentState.Type;

            if (workflowType == null)
            {
                return(allowedOperations);
            }

            IAuthorizationValidator validator;

            try
            {
                var validatorFactory = new AuthorizationValidatorFactory(workflowType, this);
                validator = validatorFactory.CreateValidator();
            }
            catch (ArgumentException)
            {
                return(allowedOperations);
            }

            var identities = GetAllIdentities();

            if (AuthorizeAccessAndImpersonateIfNecessary(identities, validator, currentState, instanceUid))
            {
                allowedOperations.AddRange(commandsToCheck.Where(commandToCheck => validator.IsCommandSupportsInState(currentState, commandToCheck, instanceUid)));
            }

            //Добавление дополнительных команд специфичных для вф и не привязанных к паре пользователь-состояние

            allowedOperations = validator.AddAdditionalCommand(AuthenticationService.GetCurrentIdentity(), identities, currentState, instanceUid, allowedOperations);

            return(allowedOperations);
        }
        public bool CreateWorkflowIfNotExists(WorkflowRuntime runtime, Guid instanceId, WorkflowType workflowType,Dictionary<string,object> parameters)
        {
            try
            {
                var workflow = runtime.GetWorkflow(instanceId);
                return true;
            }
            catch (Exception)
            {
            }

            WorkflowInstance instance = null;

            var wfparameters = new Dictionary<string, object>();
            
            if (parameters != null)
                wfparameters = new Dictionary<string, object>(1) {{"WorkflowPersistanceParameters", parameters}};

            if (workflowType == WorkflowType.BillDemandWorkfow)
                instance = runtime.CreateWorkflow(typeof(BillDemand), wfparameters,
                                                  instanceId);
            else if (workflowType == WorkflowType.DemandAdjustmentWorkflow)
                instance = runtime.CreateWorkflow(typeof(DemandAdjustment), wfparameters,
                                                  instanceId);
            else if (workflowType == WorkflowType.DemandWorkflow)
                instance = runtime.CreateWorkflow(typeof(Demand), wfparameters,
                                                  instanceId);
            else
                throw new InvalidOperationException("Невозможно определить тип маршрута");

            instance.Start();

            return false;

            //TODO Синхронизация и класс синхронизатор
        }
 public bool CreateWorkflowIfNotExists(WorkflowRuntime runtime, Guid instanceId, WorkflowType workflowType)
 {
     return CreateWorkflowIfNotExists(runtime, instanceId, workflowType, null);
 }
 public AuthorizationValidatorFactory (WorkflowType workflowType, AuthorizationService authorizationService)
 {
     _workflowType = workflowType;
     _authorizationService = authorizationService;
 }
        public bool CreateWorkflowIfNotExists(WorkflowRuntime runtime, Guid instanceId, WorkflowType workflowType, Dictionary <string, object> parameters)
        {
            try
            {
                var workflow = runtime.GetWorkflow(instanceId);
                return(true);
            }
            catch (Exception)
            {
            }

            WorkflowInstance instance = null;

            var wfparameters = new Dictionary <string, object>();

            if (parameters != null)
            {
                wfparameters = new Dictionary <string, object>(1)
                {
                    { "WorkflowPersistanceParameters", parameters }
                }
            }
            ;

            if (workflowType == WorkflowType.BillDemandWorkfow)
            {
                instance = runtime.CreateWorkflow(typeof(BillDemand), wfparameters,
                                                  instanceId);
            }
            else if (workflowType == WorkflowType.DemandAdjustmentWorkflow)
            {
                instance = runtime.CreateWorkflow(typeof(DemandAdjustment), wfparameters,
                                                  instanceId);
            }
            else if (workflowType == WorkflowType.DemandWorkflow)
            {
                instance = runtime.CreateWorkflow(typeof(Demand), wfparameters,
                                                  instanceId);
            }
            else
            {
                throw new InvalidOperationException("Невозможно определить тип маршрута");
            }

            instance.Start();

            return(false);

            //TODO Синхронизация и класс синхронизатор
        }
 public bool CreateWorkflowIfNotExists(WorkflowRuntime runtime, Guid instanceId, WorkflowType workflowType)
 {
     return(CreateWorkflowIfNotExists(runtime, instanceId, workflowType, null));
 }