private VariableRule GetVariableRule(VariableRule request) { var id = request?.Id; VariableRule ret = null; var query = DocQuery.ActiveQuery ?? Execute; DocPermissionFactory.SetSelect <VariableRule>(currentUser, "VariableRule", request.Select); DocEntityVariableRule entity = null; if (id.HasValue) { entity = DocEntityVariableRule.Get(id.Value); } if (null == entity) { throw new HttpError(HttpStatusCode.NotFound, $"No VariableRule 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); }
public VariableRule Post(VariableRuleCopy request) { VariableRule ret = null; using (Execute) { Execute.Run(ssn => { var entity = DocEntityVariableRule.Get(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 pChildren = entity.Children.ToList(); var pDefinition = entity.Definition; var pInstances = entity.Instances.ToList(); var pName = entity.Name; if (!DocTools.IsNullOrEmpty(pName)) { pName += " (Copy)"; } var pOwner = entity.Owner; var pRule = entity.Rule; var pScopes = entity.Scopes.ToList(); var pType = entity.Type; var copy = new DocEntityVariableRule(ssn) { Hash = Guid.NewGuid() , Definition = pDefinition , Name = pName , Owner = pOwner , Rule = pRule , Type = pType }; foreach (var item in pChildren) { entity.Children.Add(item); } foreach (var item in pInstances) { entity.Instances.Add(item); } foreach (var item in pScopes) { entity.Scopes.Add(item); } copy.SaveChanges(DocConstantPermission.ADD); ret = copy.ToDto(); }); } return(ret); }
public void Delete(VariableRule request) { using (Execute) { Execute.Run(ssn => { if (!(request?.Id > 0)) { throw new HttpError(HttpStatusCode.NotFound, $"No Id provided for delete."); } var en = DocEntityVariableRule.Get(request?.Id); if (null == en) { throw new HttpError(HttpStatusCode.NotFound, $"No VariableRule 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.VARIABLERULE); DocCacheClient.RemoveById(request.Id); }); } }
private VariableInstance _AssignValues(VariableInstance 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, "VariableInstance")) { throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route."); } request.Select = request.Select ?? new List <string>(); VariableInstance ret = null; request = _InitAssignValues <VariableInstance>(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 <VariableInstance>(DocConstantModelName.VARIABLEINSTANCE, nameof(VariableInstance), request); //First, assign all the variables, do database lookups and conversions var pData = request.Data; var pDocument = DocEntityDocument.Get(request.Document?.Id, true, Execute) ?? DocEntityDocument.Get(request.DocumentId, true, Execute); var pRule = DocEntityVariableRule.Get(request.Rule?.Id, true, Execute) ?? DocEntityVariableRule.Get(request.RuleId, true, Execute); var pWorkflows = GetVariable <Reference>(request, nameof(request.Workflows), request.Workflows?.ToList(), request.WorkflowsIds?.ToList()); var pArchived = true == request.Archived; var pLocked = request.Locked; var entity = InitEntity <DocEntityVariableInstance, VariableInstance>(request, permission, session); if (AllowPatchValue <VariableInstance, bool>(request, DocConstantModelName.VARIABLEINSTANCE, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived)) { entity.Archived = pArchived; } if (AllowPatchValue <VariableInstance, string>(request, DocConstantModelName.VARIABLEINSTANCE, pData, permission, nameof(request.Data), pData != entity.Data)) { entity.Data = pData; } if (AllowPatchValue <VariableInstance, DocEntityDocument>(request, DocConstantModelName.VARIABLEINSTANCE, pDocument, permission, nameof(request.Document), pDocument != entity.Document)) { entity.Document = pDocument; } if (AllowPatchValue <VariableInstance, DocEntityVariableRule>(request, DocConstantModelName.VARIABLEINSTANCE, pRule, permission, nameof(request.Rule), pRule != entity.Rule)) { entity.Rule = pRule; } if (request.Locked && AllowPatchValue <VariableInstance, bool>(request, DocConstantModelName.VARIABLEINSTANCE, pArchived, permission, nameof(request.Locked), pLocked != entity.Locked)) { entity.Archived = pArchived; } entity.SaveChanges(permission); var idsToInvalidate = new List <int>(); idsToInvalidate.AddRange(PatchCollection <VariableInstance, DocEntityVariableInstance, Reference, DocEntityWorkflow>(request, entity, pWorkflows, permission, nameof(request.Workflows))); if (idsToInvalidate.Any()) { idsToInvalidate.Add(entity.Id); DocCacheClient.RemoveByEntityIds(idsToInvalidate); DocCacheClient.RemoveSearch(DocConstantModelName.VARIABLEINSTANCE); } entity.SaveChanges(permission); DocPermissionFactory.SetSelect <VariableInstance>(currentUser, nameof(VariableInstance), request.Select); ret = entity.ToDto(); var cacheExpires = DocResources.Metadata.GetCacheExpiration(DocConstantModelName.VARIABLEINSTANCE); DocCacheClient.Set(key: cacheKey, value: ret, entityId: request.Id, entityType: DocConstantModelName.VARIABLEINSTANCE, cacheExpires); return(ret); }
private VariableRule _AssignValues(VariableRule 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, "VariableRule")) { throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route."); } request.Select = request.Select ?? new List <string>(); VariableRule ret = null; request = _InitAssignValues <VariableRule>(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 <VariableRule>(DocConstantModelName.VARIABLERULE, nameof(VariableRule), request); //First, assign all the variables, do database lookups and conversions var pChildren = GetVariable <Reference>(request, nameof(request.Children), request.Children?.ToList(), request.ChildrenIds?.ToList()); var pDefinition = request.Definition; var pInstances = GetVariable <Reference>(request, nameof(request.Instances), request.Instances?.ToList(), request.InstancesIds?.ToList()); var pName = request.Name; var pOwner = DocEntityVariableRule.Get(request.Owner?.Id, true, Execute) ?? DocEntityVariableRule.Get(request.OwnerId, true, Execute); DocEntityLookupTable pRule = GetLookup(DocConstantLookupTable.VARIABLERULE, request.Rule?.Name, request.Rule?.Id); var pScopes = GetVariable <Reference>(request, nameof(request.Scopes), request.Scopes?.ToList(), request.ScopesIds?.ToList()); DocEntityLookupTable pType = GetLookup(DocConstantLookupTable.VARIABLETYPE, request.Type?.Name, request.Type?.Id); var pArchived = true == request.Archived; var pLocked = request.Locked; var entity = InitEntity <DocEntityVariableRule, VariableRule>(request, permission, session); if (AllowPatchValue <VariableRule, bool>(request, DocConstantModelName.VARIABLERULE, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived)) { entity.Archived = pArchived; } if (AllowPatchValue <VariableRule, string>(request, DocConstantModelName.VARIABLERULE, pDefinition, permission, nameof(request.Definition), pDefinition != entity.Definition)) { entity.Definition = pDefinition; } if (AllowPatchValue <VariableRule, string>(request, DocConstantModelName.VARIABLERULE, pName, permission, nameof(request.Name), pName != entity.Name)) { entity.Name = pName; } if (AllowPatchValue <VariableRule, DocEntityVariableRule>(request, DocConstantModelName.VARIABLERULE, pOwner, permission, nameof(request.Owner), pOwner != entity.Owner)) { entity.Owner = pOwner; } if (AllowPatchValue <VariableRule, DocEntityLookupTable>(request, DocConstantModelName.VARIABLERULE, pRule, permission, nameof(request.Rule), pRule != entity.Rule)) { entity.Rule = pRule; } if (AllowPatchValue <VariableRule, DocEntityLookupTable>(request, DocConstantModelName.VARIABLERULE, pType, permission, nameof(request.Type), pType != entity.Type)) { entity.Type = pType; } if (request.Locked && AllowPatchValue <VariableRule, bool>(request, DocConstantModelName.VARIABLERULE, pArchived, permission, nameof(request.Locked), pLocked != entity.Locked)) { entity.Archived = pArchived; } entity.SaveChanges(permission); var idsToInvalidate = new List <int>(); idsToInvalidate.AddRange(PatchCollection <VariableRule, DocEntityVariableRule, Reference, DocEntityVariableRule>(request, entity, pChildren, permission, nameof(request.Children))); idsToInvalidate.AddRange(PatchCollection <VariableRule, DocEntityVariableRule, Reference, DocEntityVariableInstance>(request, entity, pInstances, permission, nameof(request.Instances))); idsToInvalidate.AddRange(PatchCollection <VariableRule, DocEntityVariableRule, Reference, DocEntityScope>(request, entity, pScopes, permission, nameof(request.Scopes))); if (idsToInvalidate.Any()) { idsToInvalidate.Add(entity.Id); DocCacheClient.RemoveByEntityIds(idsToInvalidate); DocCacheClient.RemoveSearch(DocConstantModelName.VARIABLERULE); } entity.SaveChanges(permission); DocPermissionFactory.SetSelect <VariableRule>(currentUser, nameof(VariableRule), request.Select); ret = entity.ToDto(); var cacheExpires = DocResources.Metadata.GetCacheExpiration(DocConstantModelName.VARIABLERULE); DocCacheClient.Set(key: cacheKey, value: ret, entityId: request.Id, entityType: DocConstantModelName.VARIABLERULE, cacheExpires); return(ret); }