예제 #1
0
        private WorkflowTask GetWorkflowTask(WorkflowTask request)
        {
            var          id    = request?.Id;
            WorkflowTask ret   = null;
            var          query = DocQuery.ActiveQuery ?? Execute;

            DocPermissionFactory.SetVisibleFields <WorkflowTask>(currentUser, "WorkflowTask", request.VisibleFields);

            DocEntityWorkflowTask entity = null;

            if (id.HasValue)
            {
                entity = DocEntityWorkflowTask.GetWorkflowTask(id.Value);
            }
            if (null == entity)
            {
                throw new HttpError(HttpStatusCode.NotFound, $"No WorkflowTask found for Id {id.Value}");
            }

            if (!DocPermissionFactory.HasPermission(entity, currentUser, DocConstantPermission.VIEW))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have VIEW permission for this route.");
            }

            ret = entity?.ToDto();
            return(ret);
        }
예제 #2
0
        public void Delete(WorkflowTask request)
        {
            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    if (!(request?.Id > 0))
                    {
                        throw new HttpError(HttpStatusCode.NotFound, $"No Id provided for delete.");
                    }

                    var en = DocEntityWorkflowTask.GetWorkflowTask(request?.Id);
                    if (null == en)
                    {
                        throw new HttpError(HttpStatusCode.NotFound, $"No WorkflowTask could be found for Id {request?.Id}.");
                    }
                    if (en.IsRemoved)
                    {
                        return;
                    }

                    if (!DocPermissionFactory.HasPermission(en, currentUser, DocConstantPermission.DELETE))
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, "You do not have DELETE permission for this route.");
                    }

                    en.Remove();

                    DocCacheClient.RemoveSearch(DocConstantModelName.WORKFLOWTASK);
                    DocCacheClient.RemoveById(request.Id);
                });
            }
        }
예제 #3
0
        public WorkflowTask Post(WorkflowTaskCopy request)
        {
            WorkflowTask ret = null;

            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    var entity = DocEntityWorkflowTask.GetWorkflowTask(request?.Id);
                    if (null == entity)
                    {
                        throw new HttpError(HttpStatusCode.NoContent, "The COPY request did not succeed.");
                    }
                    if (!DocPermissionFactory.HasPermission(entity, currentUser, DocConstantPermission.ADD))
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
                    }

                    var pAssignee    = entity.Assignee;
                    var pData        = entity.Data;
                    var pDescription = entity.Description;
                    if (!DocTools.IsNullOrEmpty(pDescription))
                    {
                        pDescription += " (Copy)";
                    }
                    var pDueDate  = entity.DueDate;
                    var pReporter = entity.Reporter;
                    var pStatus   = entity.Status;
                    var pType     = entity.Type;
                    var pWorkflow = entity.Workflow;
                    #region Custom Before copyWorkflowTask
                    #endregion Custom Before copyWorkflowTask
                    var copy = new DocEntityWorkflowTask(ssn)
                    {
                        Hash          = Guid.NewGuid()
                        , Assignee    = pAssignee
                        , Data        = pData
                        , Description = pDescription
                        , DueDate     = pDueDate
                        , Reporter    = pReporter
                        , Status      = pStatus
                        , Type        = pType
                        , Workflow    = pWorkflow
                    };

                    #region Custom After copyWorkflowTask
                    #endregion Custom After copyWorkflowTask
                    copy.SaveChanges(DocConstantPermission.ADD);
                    ret = copy.ToDto();
                });
            }
            return(ret);
        }
예제 #4
0
        private WorkflowTask _AssignValues(WorkflowTask request, DocConstantPermission permission, Session session)
        {
            if (permission != DocConstantPermission.ADD && (request == null || request.Id <= 0))
            {
                throw new HttpError(HttpStatusCode.NotFound, $"No record");
            }

            if (permission == DocConstantPermission.ADD && !DocPermissionFactory.HasPermissionTryAdd(currentUser, "WorkflowTask"))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
            }

            request.VisibleFields = request.VisibleFields ?? new List <string>();

            WorkflowTask ret = null;

            request = _InitAssignValues <WorkflowTask>(request, permission, session);
            //In case init assign handles create for us, return it
            if (permission == DocConstantPermission.ADD && request.Id > 0)
            {
                return(request);
            }

            var cacheKey = GetApiCacheKey <WorkflowTask>(DocConstantModelName.WORKFLOWTASK, nameof(WorkflowTask), request);

            //First, assign all the variables, do database lookups and conversions
            var pAssignee                = (request.Assignee?.Id > 0) ? DocEntityUser.GetUser(request.Assignee.Id) : null;
            var pData                    = request.Data;
            var pDescription             = request.Description;
            var pDueDate                 = request.DueDate;
            var pReporter                = (request.Reporter?.Id > 0) ? DocEntityUser.GetUser(request.Reporter.Id) : null;
            DocEntityLookupTable pStatus = GetLookup(DocConstantLookupTable.WORKFLOWSTATUS, request.Status?.Name, request.Status?.Id);
            DocEntityLookupTable pType   = GetLookup(DocConstantLookupTable.WORKFLOWTASKTYPE, request.Type?.Name, request.Type?.Id);
            var pWorkflow                = (request.Workflow?.Id > 0) ? DocEntityWorkflow.GetWorkflow(request.Workflow.Id) : null;

            DocEntityWorkflowTask entity = null;

            if (permission == DocConstantPermission.ADD)
            {
                var now = DateTime.UtcNow;
                entity = new DocEntityWorkflowTask(session)
                {
                    Created = now,
                    Updated = now
                };
            }
            else
            {
                entity = DocEntityWorkflowTask.GetWorkflowTask(request.Id);
                if (null == entity)
                {
                    throw new HttpError(HttpStatusCode.NotFound, $"No record");
                }
            }

            //Special case for Archived
            var pArchived = true == request.Archived;

            if (DocPermissionFactory.IsRequestedHasPermission <bool>(currentUser, request, pArchived, permission, DocConstantModelName.WORKFLOWTASK, nameof(request.Archived)))
            {
                if (DocPermissionFactory.IsRequested(request, pArchived, entity.Archived, nameof(request.Archived)))
                {
                    if (DocResources.Metadata.IsInsertOnly(DocConstantModelName.WORKFLOWTASK, nameof(request.Archived)) && DocConstantPermission.ADD != permission)
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, $"{nameof(request.Archived)} cannot be modified once set.");
                    }
                }
                if (DocTools.IsNullOrEmpty(pArchived) && DocResources.Metadata.IsRequired(DocConstantModelName.WORKFLOWTASK, nameof(request.Archived)))
                {
                    throw new HttpError(HttpStatusCode.BadRequest, $"{nameof(request.Archived)} requires a value.");
                }
                entity.Archived = pArchived;
                if (DocPermissionFactory.IsRequested <bool>(request, pArchived, nameof(request.Archived)) && !request.VisibleFields.Matches(nameof(request.Archived), ignoreSpaces: true))
                {
                    request.VisibleFields.Add(nameof(request.Archived));
                }
            }

            if (DocPermissionFactory.IsRequestedHasPermission <DocEntityUser>(currentUser, request, pAssignee, permission, DocConstantModelName.WORKFLOWTASK, nameof(request.Assignee)))
            {
                if (DocPermissionFactory.IsRequested(request, pAssignee, entity.Assignee, nameof(request.Assignee)))
                {
                    if (DocResources.Metadata.IsInsertOnly(DocConstantModelName.WORKFLOWTASK, nameof(request.Assignee)) && DocConstantPermission.ADD != permission)
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, $"{nameof(request.Assignee)} cannot be modified once set.");
                    }
                }
                if (DocTools.IsNullOrEmpty(pAssignee) && DocResources.Metadata.IsRequired(DocConstantModelName.WORKFLOWTASK, nameof(request.Assignee)))
                {
                    throw new HttpError(HttpStatusCode.BadRequest, $"{nameof(request.Assignee)} requires a value.");
                }
                entity.Assignee = pAssignee;
                if (DocPermissionFactory.IsRequested <DocEntityUser>(request, pAssignee, nameof(request.Assignee)) && !request.VisibleFields.Matches(nameof(request.Assignee), ignoreSpaces: true))
                {
                    request.VisibleFields.Add(nameof(request.Assignee));
                }
            }
            if (DocPermissionFactory.IsRequestedHasPermission <string>(currentUser, request, pData, permission, DocConstantModelName.WORKFLOWTASK, nameof(request.Data)))
            {
                if (DocPermissionFactory.IsRequested(request, pData, entity.Data, nameof(request.Data)))
                {
                    if (DocResources.Metadata.IsInsertOnly(DocConstantModelName.WORKFLOWTASK, nameof(request.Data)) && DocConstantPermission.ADD != permission)
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, $"{nameof(request.Data)} cannot be modified once set.");
                    }
                }
                if (DocTools.IsNullOrEmpty(pData) && DocResources.Metadata.IsRequired(DocConstantModelName.WORKFLOWTASK, nameof(request.Data)))
                {
                    throw new HttpError(HttpStatusCode.BadRequest, $"{nameof(request.Data)} requires a value.");
                }
                entity.Data = pData;
                if (DocPermissionFactory.IsRequested <string>(request, pData, nameof(request.Data)) && !request.VisibleFields.Matches(nameof(request.Data), ignoreSpaces: true))
                {
                    request.VisibleFields.Add(nameof(request.Data));
                }
            }
            if (DocPermissionFactory.IsRequestedHasPermission <string>(currentUser, request, pDescription, permission, DocConstantModelName.WORKFLOWTASK, nameof(request.Description)))
            {
                if (DocPermissionFactory.IsRequested(request, pDescription, entity.Description, nameof(request.Description)))
                {
                    if (DocResources.Metadata.IsInsertOnly(DocConstantModelName.WORKFLOWTASK, nameof(request.Description)) && DocConstantPermission.ADD != permission)
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, $"{nameof(request.Description)} cannot be modified once set.");
                    }
                }
                if (DocTools.IsNullOrEmpty(pDescription) && DocResources.Metadata.IsRequired(DocConstantModelName.WORKFLOWTASK, nameof(request.Description)))
                {
                    throw new HttpError(HttpStatusCode.BadRequest, $"{nameof(request.Description)} requires a value.");
                }
                entity.Description = pDescription;
                if (DocPermissionFactory.IsRequested <string>(request, pDescription, nameof(request.Description)) && !request.VisibleFields.Matches(nameof(request.Description), ignoreSpaces: true))
                {
                    request.VisibleFields.Add(nameof(request.Description));
                }
            }
            if (DocPermissionFactory.IsRequestedHasPermission <DateTime?>(currentUser, request, pDueDate, permission, DocConstantModelName.WORKFLOWTASK, nameof(request.DueDate)))
            {
                if (DocPermissionFactory.IsRequested(request, pDueDate, entity.DueDate, nameof(request.DueDate)))
                {
                    if (DocResources.Metadata.IsInsertOnly(DocConstantModelName.WORKFLOWTASK, nameof(request.DueDate)) && DocConstantPermission.ADD != permission)
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, $"{nameof(request.DueDate)} cannot be modified once set.");
                    }
                }
                if (DocTools.IsNullOrEmpty(pDueDate) && DocResources.Metadata.IsRequired(DocConstantModelName.WORKFLOWTASK, nameof(request.DueDate)))
                {
                    throw new HttpError(HttpStatusCode.BadRequest, $"{nameof(request.DueDate)} requires a value.");
                }
                entity.DueDate = pDueDate;
                if (DocPermissionFactory.IsRequested <DateTime?>(request, pDueDate, nameof(request.DueDate)) && !request.VisibleFields.Matches(nameof(request.DueDate), ignoreSpaces: true))
                {
                    request.VisibleFields.Add(nameof(request.DueDate));
                }
            }
            if (DocPermissionFactory.IsRequestedHasPermission <DocEntityUser>(currentUser, request, pReporter, permission, DocConstantModelName.WORKFLOWTASK, nameof(request.Reporter)))
            {
                if (DocPermissionFactory.IsRequested(request, pReporter, entity.Reporter, nameof(request.Reporter)))
                {
                    if (DocResources.Metadata.IsInsertOnly(DocConstantModelName.WORKFLOWTASK, nameof(request.Reporter)) && DocConstantPermission.ADD != permission)
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, $"{nameof(request.Reporter)} cannot be modified once set.");
                    }
                }
                if (DocTools.IsNullOrEmpty(pReporter) && DocResources.Metadata.IsRequired(DocConstantModelName.WORKFLOWTASK, nameof(request.Reporter)))
                {
                    throw new HttpError(HttpStatusCode.BadRequest, $"{nameof(request.Reporter)} requires a value.");
                }
                entity.Reporter = pReporter;
                if (DocPermissionFactory.IsRequested <DocEntityUser>(request, pReporter, nameof(request.Reporter)) && !request.VisibleFields.Matches(nameof(request.Reporter), ignoreSpaces: true))
                {
                    request.VisibleFields.Add(nameof(request.Reporter));
                }
            }
            if (DocPermissionFactory.IsRequestedHasPermission <DocEntityLookupTable>(currentUser, request, pStatus, permission, DocConstantModelName.WORKFLOWTASK, nameof(request.Status)))
            {
                if (DocPermissionFactory.IsRequested(request, pStatus, entity.Status, nameof(request.Status)))
                {
                    if (DocResources.Metadata.IsInsertOnly(DocConstantModelName.WORKFLOWTASK, nameof(request.Status)) && DocConstantPermission.ADD != permission)
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, $"{nameof(request.Status)} cannot be modified once set.");
                    }
                }
                if (DocTools.IsNullOrEmpty(pStatus) && DocResources.Metadata.IsRequired(DocConstantModelName.WORKFLOWTASK, nameof(request.Status)))
                {
                    throw new HttpError(HttpStatusCode.BadRequest, $"{nameof(request.Status)} requires a value.");
                }
                entity.Status = pStatus;
                if (DocPermissionFactory.IsRequested <DocEntityLookupTable>(request, pStatus, nameof(request.Status)) && !request.VisibleFields.Matches(nameof(request.Status), ignoreSpaces: true))
                {
                    request.VisibleFields.Add(nameof(request.Status));
                }
            }
            if (DocPermissionFactory.IsRequestedHasPermission <DocEntityLookupTable>(currentUser, request, pType, permission, DocConstantModelName.WORKFLOWTASK, nameof(request.Type)))
            {
                if (DocPermissionFactory.IsRequested(request, pType, entity.Type, nameof(request.Type)))
                {
                    if (DocResources.Metadata.IsInsertOnly(DocConstantModelName.WORKFLOWTASK, nameof(request.Type)) && DocConstantPermission.ADD != permission)
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, $"{nameof(request.Type)} cannot be modified once set.");
                    }
                }
                if (DocTools.IsNullOrEmpty(pType) && DocResources.Metadata.IsRequired(DocConstantModelName.WORKFLOWTASK, nameof(request.Type)))
                {
                    throw new HttpError(HttpStatusCode.BadRequest, $"{nameof(request.Type)} requires a value.");
                }
                entity.Type = pType;
                if (DocPermissionFactory.IsRequested <DocEntityLookupTable>(request, pType, nameof(request.Type)) && !request.VisibleFields.Matches(nameof(request.Type), ignoreSpaces: true))
                {
                    request.VisibleFields.Add(nameof(request.Type));
                }
            }
            if (DocPermissionFactory.IsRequestedHasPermission <DocEntityWorkflow>(currentUser, request, pWorkflow, permission, DocConstantModelName.WORKFLOWTASK, nameof(request.Workflow)))
            {
                if (DocPermissionFactory.IsRequested(request, pWorkflow, entity.Workflow, nameof(request.Workflow)))
                {
                    if (DocResources.Metadata.IsInsertOnly(DocConstantModelName.WORKFLOWTASK, nameof(request.Workflow)) && DocConstantPermission.ADD != permission)
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, $"{nameof(request.Workflow)} cannot be modified once set.");
                    }
                }
                if (DocTools.IsNullOrEmpty(pWorkflow) && DocResources.Metadata.IsRequired(DocConstantModelName.WORKFLOWTASK, nameof(request.Workflow)))
                {
                    throw new HttpError(HttpStatusCode.BadRequest, $"{nameof(request.Workflow)} requires a value.");
                }
                entity.Workflow = pWorkflow;
                if (DocPermissionFactory.IsRequested <DocEntityWorkflow>(request, pWorkflow, nameof(request.Workflow)) && !request.VisibleFields.Matches(nameof(request.Workflow), ignoreSpaces: true))
                {
                    request.VisibleFields.Add(nameof(request.Workflow));
                }
            }

            if (request.Locked)
            {
                entity.Locked = request.Locked;
            }

            entity.SaveChanges(permission);

            DocPermissionFactory.SetVisibleFields <WorkflowTask>(currentUser, nameof(WorkflowTask), request.VisibleFields);
            ret = entity.ToDto();

            var cacheExpires = DocResources.Metadata.GetCacheExpiration(DocConstantModelName.WORKFLOWTASK);

            DocCacheClient.Set(key: cacheKey, value: ret, entityId: request.Id, entityType: DocConstantModelName.WORKFLOWTASK, cacheExpires);

            return(ret);
        }