Beispiel #1
0
        private Client GetClient(Client request)
        {
            var    id    = request?.Id;
            Client ret   = null;
            var    query = DocQuery.ActiveQuery ?? Execute;

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

            DocEntityClient entity = null;

            if (id.HasValue)
            {
                entity = DocEntityClient.Get(id.Value);
            }
            if (null == entity)
            {
                throw new HttpError(HttpStatusCode.NotFound, $"No Client 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);
        }
Beispiel #2
0
        public void Delete(Client request)
        {
            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    if (!(request?.Id > 0))
                    {
                        throw new HttpError(HttpStatusCode.NotFound, $"No Id provided for delete.");
                    }

                    var en = DocEntityClient.Get(request?.Id);
                    if (null == en)
                    {
                        throw new HttpError(HttpStatusCode.NotFound, $"No Client 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.CLIENT);
                    DocCacheClient.RemoveById(request.Id);
                });
            }
        }
Beispiel #3
0
        private Division _AssignValues(Division 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, "Division"))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
            }

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

            Division ret = null;

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

            //First, assign all the variables, do database lookups and conversions
            var pClient        = DocEntityClient.Get(request.Client?.Id, true, Execute) ?? DocEntityClient.Get(request.ClientId, true, Execute);
            var pDefaultLocale = DocEntityLocale.Get(request.DefaultLocale?.Id, true, Execute) ?? DocEntityLocale.Get(request.DefaultLocaleId, true, Execute);
            var pDocumentSets  = GetVariable <Reference>(request, nameof(request.DocumentSets), request.DocumentSets?.ToList(), request.DocumentSetsIds?.ToList());
            var pName          = request.Name;
            var pRole          = DocEntityRole.Get(request.Role?.Id, true, Execute) ?? DocEntityRole.Get(request.RoleId, true, Execute);
            var pSettings      = (DocTools.IsNullOrEmpty(request.Settings)) ? null : DocSerialize <DivisionSettings> .ToString(request.Settings);

            var pUsers    = GetVariable <Reference>(request, nameof(request.Users), request.Users?.ToList(), request.UsersIds?.ToList());
            var pArchived = true == request.Archived;
            var pLocked   = request.Locked;

            var entity = InitEntity <DocEntityDivision, Division>(request, permission, session);

            if (AllowPatchValue <Division, bool>(request, DocConstantModelName.DIVISION, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived))
            {
                entity.Archived = pArchived;
            }
            if (AllowPatchValue <Division, DocEntityClient>(request, DocConstantModelName.DIVISION, pClient, permission, nameof(request.Client), pClient != entity.Client))
            {
                entity.Client = pClient;
            }
            if (AllowPatchValue <Division, DocEntityLocale>(request, DocConstantModelName.DIVISION, pDefaultLocale, permission, nameof(request.DefaultLocale), pDefaultLocale != entity.DefaultLocale))
            {
                entity.DefaultLocale = pDefaultLocale;
            }
            if (AllowPatchValue <Division, string>(request, DocConstantModelName.DIVISION, pName, permission, nameof(request.Name), pName != entity.Name))
            {
                entity.Name = pName;
            }
            if (AllowPatchValue <Division, DocEntityRole>(request, DocConstantModelName.DIVISION, pRole, permission, nameof(request.Role), pRole != entity.Role))
            {
                entity.Role = pRole;
            }
            if (AllowPatchValue <Division, string>(request, DocConstantModelName.DIVISION, pSettings, permission, nameof(request.Settings), pSettings != entity.Settings))
            {
                entity.Settings = pSettings;
            }
            if (request.Locked && AllowPatchValue <Division, bool>(request, DocConstantModelName.DIVISION, pArchived, permission, nameof(request.Locked), pLocked != entity.Locked))
            {
                entity.Archived = pArchived;
            }
            entity.SaveChanges(permission);

            var idsToInvalidate = new List <int>();

            idsToInvalidate.AddRange(PatchCollection <Division, DocEntityDivision, Reference, DocEntityDocumentSet>(request, entity, pDocumentSets, permission, nameof(request.DocumentSets)));
            idsToInvalidate.AddRange(PatchCollection <Division, DocEntityDivision, Reference, DocEntityUser>(request, entity, pUsers, permission, nameof(request.Users)));
            if (idsToInvalidate.Any())
            {
                idsToInvalidate.Add(entity.Id);
                DocCacheClient.RemoveByEntityIds(idsToInvalidate);
                DocCacheClient.RemoveSearch(DocConstantModelName.DIVISION);
            }

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

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

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

            return(ret);
        }
Beispiel #4
0
        private Project _AssignValues(Project 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, "Project"))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
            }

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

            Project ret = null;

            request = _InitAssignValues <Project>(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 <Project>(DocConstantModelName.PROJECT, nameof(Project), 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 pClient              = DocEntityClient.Get(request.Client?.Id, true, Execute) ?? DocEntityClient.Get(request.ClientId, true, Execute);
            var pDatabaseDeadline    = request.DatabaseDeadline;
            var pDatabaseName        = request.DatabaseName;
            var pDataset             = DocEntityDataSet.Get(request.Dataset?.Id, true, Execute) ?? DocEntityDataSet.Get(request.DatasetId, true, Execute);
            var pDeliverableDeadline = request.DeliverableDeadline;
            var pFqId                  = request.FqId;
            var pFqWorkflow            = DocEntityWorkflow.Get(request.FqWorkflow?.Id, true, Execute) ?? DocEntityWorkflow.Get(request.FqWorkflowId, true, Execute);
            var pLegacyPackageId       = request.LegacyPackageId;
            var pLibrary               = DocEntityLibrarySet.Get(request.Library?.Id, true, Execute) ?? DocEntityLibrarySet.Get(request.LibraryId, true, Execute);
            var pLibraryPackageId      = request.LibraryPackageId;
            var pLibraryPackageName    = request.LibraryPackageName;
            var pNumber                = request.Number;
            var pOperationsDeliverable = request.OperationsDeliverable;
            var pOpportunityId         = request.OpportunityId;
            var pOpportunityName       = request.OpportunityName;
            var pParent                = DocEntityProject.Get(request.Parent?.Id, true, Execute) ?? DocEntityProject.Get(request.ParentId, true, Execute);
            var pPICO                  = request.PICO;
            var pProjectId             = request.ProjectId;
            var pProjectName           = request.ProjectName;
            var pStatus                = request.Status;
            var pTimeCards             = GetVariable <Reference>(request, nameof(request.TimeCards), request.TimeCards?.ToList(), request.TimeCardsIds?.ToList());
            var pArchived              = true == request.Archived;
            var pLocked                = request.Locked;

            var entity = InitEntity <DocEntityProject, Project>(request, permission, session);

            if (AllowPatchValue <Project, bool>(request, DocConstantModelName.PROJECT, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived))
            {
                entity.Archived = pArchived;
            }
            if (AllowPatchValue <Project, DocEntityClient>(request, DocConstantModelName.PROJECT, pClient, permission, nameof(request.Client), pClient != entity.Client))
            {
                entity.Client = pClient;
            }
            if (AllowPatchValue <Project, DateTime?>(request, DocConstantModelName.PROJECT, pDatabaseDeadline, permission, nameof(request.DatabaseDeadline), pDatabaseDeadline != entity.DatabaseDeadline))
            {
                entity.DatabaseDeadline = pDatabaseDeadline;
            }
            if (AllowPatchValue <Project, string>(request, DocConstantModelName.PROJECT, pDatabaseName, permission, nameof(request.DatabaseName), pDatabaseName != entity.DatabaseName))
            {
                entity.DatabaseName = pDatabaseName;
            }
            if (AllowPatchValue <Project, DocEntityDataSet>(request, DocConstantModelName.PROJECT, pDataset, permission, nameof(request.Dataset), pDataset != entity.Dataset))
            {
                entity.Dataset = pDataset;
            }
            if (AllowPatchValue <Project, DateTime?>(request, DocConstantModelName.PROJECT, pDeliverableDeadline, permission, nameof(request.DeliverableDeadline), pDeliverableDeadline != entity.DeliverableDeadline))
            {
                entity.DeliverableDeadline = pDeliverableDeadline;
            }
            if (AllowPatchValue <Project, int?>(request, DocConstantModelName.PROJECT, pFqId, permission, nameof(request.FqId), pFqId != entity.FqId))
            {
                entity.FqId = pFqId;
            }
            if (AllowPatchValue <Project, DocEntityWorkflow>(request, DocConstantModelName.PROJECT, pFqWorkflow, permission, nameof(request.FqWorkflow), pFqWorkflow != entity.FqWorkflow))
            {
                entity.FqWorkflow = pFqWorkflow;
            }
            if (AllowPatchValue <Project, int?>(request, DocConstantModelName.PROJECT, pLegacyPackageId, permission, nameof(request.LegacyPackageId), pLegacyPackageId != entity.LegacyPackageId))
            {
                entity.LegacyPackageId = pLegacyPackageId;
            }
            if (AllowPatchValue <Project, DocEntityLibrarySet>(request, DocConstantModelName.PROJECT, pLibrary, permission, nameof(request.Library), pLibrary != entity.Library))
            {
                entity.Library = pLibrary;
            }
            if (AllowPatchValue <Project, int?>(request, DocConstantModelName.PROJECT, pLibraryPackageId, permission, nameof(request.LibraryPackageId), pLibraryPackageId != entity.LibraryPackageId))
            {
                entity.LibraryPackageId = pLibraryPackageId;
            }
            if (AllowPatchValue <Project, string>(request, DocConstantModelName.PROJECT, pLibraryPackageName, permission, nameof(request.LibraryPackageName), pLibraryPackageName != entity.LibraryPackageName))
            {
                entity.LibraryPackageName = pLibraryPackageName;
            }
            if (AllowPatchValue <Project, string>(request, DocConstantModelName.PROJECT, pNumber, permission, nameof(request.Number), pNumber != entity.Number))
            {
                entity.Number = pNumber;
            }
            if (AllowPatchValue <Project, string>(request, DocConstantModelName.PROJECT, pOperationsDeliverable, permission, nameof(request.OperationsDeliverable), pOperationsDeliverable != entity.OperationsDeliverable))
            {
                entity.OperationsDeliverable = pOperationsDeliverable;
            }
            if (AllowPatchValue <Project, string>(request, DocConstantModelName.PROJECT, pOpportunityId, permission, nameof(request.OpportunityId), pOpportunityId != entity.OpportunityId))
            {
                entity.OpportunityId = pOpportunityId;
            }
            if (AllowPatchValue <Project, string>(request, DocConstantModelName.PROJECT, pOpportunityName, permission, nameof(request.OpportunityName), pOpportunityName != entity.OpportunityName))
            {
                entity.OpportunityName = pOpportunityName;
            }
            if (AllowPatchValue <Project, DocEntityProject>(request, DocConstantModelName.PROJECT, pParent, permission, nameof(request.Parent), pParent != entity.Parent))
            {
                entity.Parent = pParent;
            }
            if (AllowPatchValue <Project, string>(request, DocConstantModelName.PROJECT, pPICO, permission, nameof(request.PICO), pPICO != entity.PICO))
            {
                entity.PICO = pPICO;
            }
            if (AllowPatchValue <Project, string>(request, DocConstantModelName.PROJECT, pProjectId, permission, nameof(request.ProjectId), pProjectId != entity.ProjectId))
            {
                entity.ProjectId = pProjectId;
            }
            if (AllowPatchValue <Project, string>(request, DocConstantModelName.PROJECT, pProjectName, permission, nameof(request.ProjectName), pProjectName != entity.ProjectName))
            {
                entity.ProjectName = pProjectName;
            }
            if (AllowPatchValue <Project, ForeignKeyStatusEnm?>(request, DocConstantModelName.PROJECT, pStatus, permission, nameof(request.Status), pStatus != entity.Status))
            {
                entity.Status = pStatus;
            }
            if (request.Locked && AllowPatchValue <Project, bool>(request, DocConstantModelName.PROJECT, pArchived, permission, nameof(request.Locked), pLocked != entity.Locked))
            {
                entity.Archived = pArchived;
            }
            entity.SaveChanges(permission);

            var idsToInvalidate = new List <int>();

            idsToInvalidate.AddRange(PatchCollection <Project, DocEntityProject, Reference, DocEntityProject>(request, entity, pChildren, permission, nameof(request.Children)));
            idsToInvalidate.AddRange(PatchCollection <Project, DocEntityProject, Reference, DocEntityTimeCard>(request, entity, pTimeCards, permission, nameof(request.TimeCards)));
            if (idsToInvalidate.Any())
            {
                idsToInvalidate.Add(entity.Id);
                DocCacheClient.RemoveByEntityIds(idsToInvalidate);
                DocCacheClient.RemoveSearch(DocConstantModelName.PROJECT);
            }

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

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

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

            return(ret);
        }
Beispiel #5
0
        private Scope _AssignValues(Scope 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, "Scope"))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
            }

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

            Scope ret = null;

            request = _InitAssignValues <Scope>(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 <Scope>(DocConstantModelName.SCOPE, nameof(Scope), 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 pBindings       = GetVariable <Reference>(request, nameof(request.Bindings), request.Bindings?.ToList(), request.BindingsIds?.ToList());
            var pBroadcasts     = GetVariable <Reference>(request, nameof(request.Broadcasts), request.Broadcasts?.ToList(), request.BroadcastsIds?.ToList());
            var pClient         = DocEntityClient.Get(request.Client?.Id, true, Execute) ?? DocEntityClient.Get(request.ClientId, true, Execute);
            var pDelete         = request.Delete;
            var pDocumentSet    = DocEntityDocumentSet.Get(request.DocumentSet?.Id, true, Execute) ?? DocEntityDocumentSet.Get(request.DocumentSetId, true, Execute);
            var pEdit           = request.Edit;
            var pHelp           = GetVariable <Reference>(request, nameof(request.Help), request.Help?.ToList(), request.HelpIds?.ToList());
            var pIsGlobal       = request.IsGlobal;
            var pScopedComments = GetVariable <Reference>(request, nameof(request.ScopedComments), request.ScopedComments?.ToList(), request.ScopedCommentsIds?.ToList());
            var pScopedFiles    = GetVariable <Reference>(request, nameof(request.ScopedFiles), request.ScopedFiles?.ToList(), request.ScopedFilesIds?.ToList());
            var pScopedTags     = GetVariable <Reference>(request, nameof(request.ScopedTags), request.ScopedTags?.ToList(), request.ScopedTagsIds?.ToList());
            var pSynonyms       = GetVariable <Reference>(request, nameof(request.Synonyms), request.Synonyms?.ToList(), request.SynonymsIds?.ToList());
            var pTeam           = DocEntityTeam.Get(request.Team?.Id, true, Execute) ?? DocEntityTeam.Get(request.TeamId, true, Execute);
            var pType           = request.Type;
            var pUser           = DocEntityUser.Get(request.User?.Id, true, Execute) ?? DocEntityUser.Get(request.UserId, true, Execute);
            var pVariableRules  = GetVariable <Reference>(request, nameof(request.VariableRules), request.VariableRules?.ToList(), request.VariableRulesIds?.ToList());
            var pView           = request.View;
            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 <DocEntityScope, Scope>(request, permission, session);

            if (AllowPatchValue <Scope, bool>(request, DocConstantModelName.SCOPE, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived))
            {
                entity.Archived = pArchived;
            }
            if (AllowPatchValue <Scope, DocEntityApp>(request, DocConstantModelName.SCOPE, pApp, permission, nameof(request.App), pApp != entity.App))
            {
                entity.App = pApp;
            }
            if (AllowPatchValue <Scope, DocEntityClient>(request, DocConstantModelName.SCOPE, pClient, permission, nameof(request.Client), pClient != entity.Client))
            {
                entity.Client = pClient;
            }
            if (AllowPatchValue <Scope, bool>(request, DocConstantModelName.SCOPE, pDelete, permission, nameof(request.Delete), pDelete != entity.Delete))
            {
                entity.Delete = pDelete;
            }
            if (AllowPatchValue <Scope, DocEntityDocumentSet>(request, DocConstantModelName.SCOPE, pDocumentSet, permission, nameof(request.DocumentSet), pDocumentSet != entity.DocumentSet))
            {
                entity.DocumentSet = pDocumentSet;
            }
            if (AllowPatchValue <Scope, bool>(request, DocConstantModelName.SCOPE, pEdit, permission, nameof(request.Edit), pEdit != entity.Edit))
            {
                entity.Edit = pEdit;
            }
            if (AllowPatchValue <Scope, bool>(request, DocConstantModelName.SCOPE, pIsGlobal, permission, nameof(request.IsGlobal), pIsGlobal != entity.IsGlobal))
            {
                entity.IsGlobal = pIsGlobal;
            }
            if (AllowPatchValue <Scope, DocEntityTeam>(request, DocConstantModelName.SCOPE, pTeam, permission, nameof(request.Team), pTeam != entity.Team))
            {
                entity.Team = pTeam;
            }
            if (AllowPatchValue <Scope, ScopeEnm?>(request, DocConstantModelName.SCOPE, pType, permission, nameof(request.Type), pType != entity.Type))
            {
                if (null != pType)
                {
                    entity.Type = pType.Value;
                }
            }
            if (AllowPatchValue <Scope, DocEntityUser>(request, DocConstantModelName.SCOPE, pUser, permission, nameof(request.User), pUser != entity.User))
            {
                entity.User = pUser;
            }
            if (AllowPatchValue <Scope, bool>(request, DocConstantModelName.SCOPE, pView, permission, nameof(request.View), pView != entity.View))
            {
                entity.View = pView;
            }
            if (request.Locked && AllowPatchValue <Scope, bool>(request, DocConstantModelName.SCOPE, pArchived, permission, nameof(request.Locked), pLocked != entity.Locked))
            {
                entity.Archived = pArchived;
            }
            entity.SaveChanges(permission);

            var idsToInvalidate = new List <int>();

            idsToInvalidate.AddRange(PatchCollection <Scope, DocEntityScope, Reference, DocEntityLookupTableBinding>(request, entity, pBindings, permission, nameof(request.Bindings)));
            idsToInvalidate.AddRange(PatchCollection <Scope, DocEntityScope, Reference, DocEntityBroadcast>(request, entity, pBroadcasts, permission, nameof(request.Broadcasts)));
            idsToInvalidate.AddRange(PatchCollection <Scope, DocEntityScope, Reference, DocEntityHelp>(request, entity, pHelp, permission, nameof(request.Help)));
            idsToInvalidate.AddRange(PatchCollection <Scope, DocEntityScope, Reference, DocEntityComment>(request, entity, pScopedComments, permission, nameof(request.ScopedComments)));
            idsToInvalidate.AddRange(PatchCollection <Scope, DocEntityScope, Reference, DocEntityFile>(request, entity, pScopedFiles, permission, nameof(request.ScopedFiles)));
            idsToInvalidate.AddRange(PatchCollection <Scope, DocEntityScope, Reference, DocEntityTag>(request, entity, pScopedTags, permission, nameof(request.ScopedTags)));
            idsToInvalidate.AddRange(PatchCollection <Scope, DocEntityScope, Reference, DocEntityTermSynonym>(request, entity, pSynonyms, permission, nameof(request.Synonyms)));
            idsToInvalidate.AddRange(PatchCollection <Scope, DocEntityScope, Reference, DocEntityVariableRule>(request, entity, pVariableRules, permission, nameof(request.VariableRules)));
            idsToInvalidate.AddRange(PatchCollection <Scope, DocEntityScope, Reference, DocEntityWorkflow>(request, entity, pWorkflows, permission, nameof(request.Workflows)));
            if (idsToInvalidate.Any())
            {
                idsToInvalidate.Add(entity.Id);
                DocCacheClient.RemoveByEntityIds(idsToInvalidate);
                DocCacheClient.RemoveSearch(DocConstantModelName.SCOPE);
            }

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

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

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

            return(ret);
        }
Beispiel #6
0
        public Client Post(ClientCopy request)
        {
            Client ret = null;

            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    var entity = DocEntityClient.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 pDefaultLocale = entity.DefaultLocale;
                    var pDivisions     = entity.Divisions.ToList();
                    var pDocumentSets  = entity.DocumentSets.ToList();
                    var pName          = entity.Name;
                    if (!DocTools.IsNullOrEmpty(pName))
                    {
                        pName += " (Copy)";
                    }
                    var pProjects            = entity.Projects.ToList();
                    var pRole                = entity.Role;
                    var pSalesforceAccountId = entity.SalesforceAccountId;
                    if (!DocTools.IsNullOrEmpty(pSalesforceAccountId))
                    {
                        pSalesforceAccountId += " (Copy)";
                    }
                    var pScopes   = entity.Scopes.ToList();
                    var pSettings = entity.Settings;
                    var copy      = new DocEntityClient(ssn)
                    {
                        Hash                  = Guid.NewGuid()
                        , DefaultLocale       = pDefaultLocale
                        , Name                = pName
                        , Role                = pRole
                        , SalesforceAccountId = pSalesforceAccountId
                        , Settings            = pSettings
                    };
                    foreach (var item in pDivisions)
                    {
                        entity.Divisions.Add(item);
                    }

                    foreach (var item in pDocumentSets)
                    {
                        entity.DocumentSets.Add(item);
                    }

                    foreach (var item in pProjects)
                    {
                        entity.Projects.Add(item);
                    }

                    foreach (var item in pScopes)
                    {
                        entity.Scopes.Add(item);
                    }

                    copy.SaveChanges(DocConstantPermission.ADD);
                    ret = copy.ToDto();
                });
            }
            return(ret);
        }