Пример #1
0
        private UserSession GetUserSession(UserSession request)
        {
            var         id    = request?.Id;
            UserSession ret   = null;
            var         query = DocQuery.ActiveQuery ?? Execute;

            DocPermissionFactory.SetSelect <UserSession>(currentUser, "UserSession", request.Select);

            DocEntityUserSession entity = null;

            if (id.HasValue)
            {
                entity = DocEntityUserSession.Get(id.Value);
            }
            if (null == entity)
            {
                throw new HttpError(HttpStatusCode.NotFound, $"No UserSession 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
        private History _AssignValues(History 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, "History"))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
            }

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

            History ret = null;

            request = _InitAssignValues <History>(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 <History>(DocConstantModelName.HISTORY, nameof(History), request);

            //First, assign all the variables, do database lookups and conversions
            var pApp           = DocEntityApp.Get(request.App?.Id, true, Execute) ?? DocEntityApp.Get(request.AppId, true, Execute);
            var pDocumentSet   = DocEntityDocumentSet.Get(request.DocumentSet?.Id, true, Execute) ?? DocEntityDocumentSet.Get(request.DocumentSetId, true, Execute);
            var pImpersonation = DocEntityImpersonation.Get(request.Impersonation?.Id, true, Execute) ?? DocEntityImpersonation.Get(request.ImpersonationId, true, Execute);
            var pPage          = DocEntityPage.Get(request.Page?.Id, true, Execute) ?? DocEntityPage.Get(request.PageId, true, Execute);
            var pURL           = request.URL;
            var pUser          = DocEntityUser.Get(request.User?.Id, true, Execute) ?? DocEntityUser.Get(request.UserId, true, Execute);
            var pUserSession   = DocEntityUserSession.Get(request.UserSession?.Id, true, Execute) ?? DocEntityUserSession.Get(request.UserSessionId, true, Execute);
            var pWorkflow      = DocEntityWorkflow.Get(request.Workflow?.Id, true, Execute) ?? DocEntityWorkflow.Get(request.WorkflowId, true, Execute);
            var pArchived      = true == request.Archived;
            var pLocked        = request.Locked;

            var entity = InitEntity <DocEntityHistory, History>(request, permission, session);

            if (AllowPatchValue <History, bool>(request, DocConstantModelName.HISTORY, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived))
            {
                entity.Archived = pArchived;
            }
            if (AllowPatchValue <History, DocEntityApp>(request, DocConstantModelName.HISTORY, pApp, permission, nameof(request.App), pApp != entity.App))
            {
                entity.App = pApp;
            }
            if (AllowPatchValue <History, DocEntityDocumentSet>(request, DocConstantModelName.HISTORY, pDocumentSet, permission, nameof(request.DocumentSet), pDocumentSet != entity.DocumentSet))
            {
                entity.DocumentSet = pDocumentSet;
            }
            if (AllowPatchValue <History, DocEntityImpersonation>(request, DocConstantModelName.HISTORY, pImpersonation, permission, nameof(request.Impersonation), pImpersonation != entity.Impersonation))
            {
                entity.Impersonation = pImpersonation;
            }
            if (AllowPatchValue <History, DocEntityPage>(request, DocConstantModelName.HISTORY, pPage, permission, nameof(request.Page), pPage != entity.Page))
            {
                entity.Page = pPage;
            }
            if (AllowPatchValue <History, string>(request, DocConstantModelName.HISTORY, pURL, permission, nameof(request.URL), pURL != entity.URL))
            {
                entity.URL = pURL;
            }
            if (AllowPatchValue <History, DocEntityUser>(request, DocConstantModelName.HISTORY, pUser, permission, nameof(request.User), pUser != entity.User))
            {
                entity.User = pUser;
            }
            if (AllowPatchValue <History, DocEntityUserSession>(request, DocConstantModelName.HISTORY, pUserSession, permission, nameof(request.UserSession), pUserSession != entity.UserSession))
            {
                entity.UserSession = pUserSession;
            }
            if (AllowPatchValue <History, DocEntityWorkflow>(request, DocConstantModelName.HISTORY, pWorkflow, permission, nameof(request.Workflow), pWorkflow != entity.Workflow))
            {
                entity.Workflow = pWorkflow;
            }
            if (request.Locked && AllowPatchValue <History, bool>(request, DocConstantModelName.HISTORY, pArchived, permission, nameof(request.Locked), pLocked != entity.Locked))
            {
                entity.Archived = pArchived;
            }
            entity.SaveChanges(permission);

            var idsToInvalidate = new List <int>();

            if (idsToInvalidate.Any())
            {
                idsToInvalidate.Add(entity.Id);
                DocCacheClient.RemoveByEntityIds(idsToInvalidate);
                DocCacheClient.RemoveSearch(DocConstantModelName.HISTORY);
            }

            entity.SaveChanges(permission);
            DocPermissionFactory.SetSelect <History>(currentUser, nameof(History), request.Select);
            ret = entity.ToDto();

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

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

            return(ret);
        }