Search in sources :

Example 16 with ProjectName

use of com.enonic.xp.project.ProjectName in project xp by enonic.

the class ProjectAccessSiteProcessor method processUpdate.

@Override
public ProcessUpdateResult processUpdate(final ProcessUpdateParams params) {
    final Site editedSite = (Site) params.getEditedContent();
    final SiteConfigs editedSiteConfigs = editedSite.getSiteConfigs();
    final Site originalSite = (Site) params.getOriginalContent();
    final SiteConfigs originalSiteConfigs = originalSite.getSiteConfigs();
    final Context context = ContextAccessor.current();
    final AuthenticationInfo authenticationInfo = context.getAuthInfo();
    final ProjectName projectName = ProjectName.from(context.getRepositoryId());
    if (!Objects.equals(originalSiteConfigs, editedSiteConfigs)) {
        if (!ProjectAccessHelper.hasAdminAccess(authenticationInfo)) {
            if (ProjectConstants.DEFAULT_PROJECT_NAME.equals(projectName)) {
                throw new ProjectAccessRequiredException(authenticationInfo.getUser().getKey());
            } else if (!this.projectPermissionsContextManager.hasAnyProjectRole(authenticationInfo, projectName, Set.of(ProjectRole.OWNER))) {
                throw new ProjectAccessRequiredException(authenticationInfo.getUser().getKey(), ProjectRole.OWNER);
            }
        }
    }
    return null;
}
Also used : Site(com.enonic.xp.site.Site) Context(com.enonic.xp.context.Context) ProjectName(com.enonic.xp.project.ProjectName) SiteConfigs(com.enonic.xp.site.SiteConfigs) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo)

Example 17 with ProjectName

use of com.enonic.xp.project.ProjectName in project xp by enonic.

the class SyncContentServiceImplTest method testWorkflowInfo.

@Test
public void testWorkflowInfo() throws Exception {
    final Content source = sourceContext.callWith(() -> createContent(ContentPath.ROOT));
    syncCreated(source.getId());
    targetContext.runWith(() -> {
        contentService.update(new UpdateContentParams().contentId(source.getId()).editor(edit -> {
            edit.workflowInfo = WorkflowInfo.ready();
            edit.data = new PropertyTree();
        }));
    });
    syncContentService.resetInheritance(ResetContentInheritParams.create().contentId(source.getId()).inherit(EnumSet.of(ContentInheritType.CONTENT)).projectName(targetProject.getName()).build());
    targetContext.runWith(() -> {
        final Content changed = contentService.getById(source.getId());
        assertTrue(changed.getInherit().contains(ContentInheritType.CONTENT));
        assertTrue(changed.getData().hasProperty("stringField"));
    });
}
Also used : ChildOrder(com.enonic.xp.index.ChildOrder) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) WorkflowInfo(com.enonic.xp.content.WorkflowInfo) ContentPath(com.enonic.xp.content.ContentPath) ProjectSyncParams(com.enonic.xp.content.ProjectSyncParams) ContentInheritType(com.enonic.xp.content.ContentInheritType) Content(com.enonic.xp.content.Content) DeleteContentParams(com.enonic.xp.content.DeleteContentParams) UpdateContentParams(com.enonic.xp.content.UpdateContentParams) ContentSyncEventType(com.enonic.xp.core.impl.content.ContentSyncEventType) Test(org.junit.jupiter.api.Test) ParentContentSynchronizer(com.enonic.xp.core.impl.content.ParentContentSynchronizer) SyncContentServiceImpl(com.enonic.xp.core.impl.content.SyncContentServiceImpl) ContentId(com.enonic.xp.content.ContentId) ResetContentInheritParams(com.enonic.xp.content.ResetContentInheritParams) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ProjectName(com.enonic.xp.project.ProjectName) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SetContentChildOrderParams(com.enonic.xp.content.SetContentChildOrderParams) ContentEventsSyncParams(com.enonic.xp.core.impl.content.ContentEventsSyncParams) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EnumSet(java.util.EnumSet) PropertyTree(com.enonic.xp.data.PropertyTree) UpdateContentParams(com.enonic.xp.content.UpdateContentParams) Content(com.enonic.xp.content.Content) PropertyTree(com.enonic.xp.data.PropertyTree) Test(org.junit.jupiter.api.Test)

Example 18 with ProjectName

use of com.enonic.xp.project.ProjectName in project xp by enonic.

the class ProjectContentEventListener method doHandleContentEvent.

private void doHandleContentEvent(final List<Map<String, String>> nodes, final String type) {
    createAdminContext().runWith(() -> {
        final Branch branch = Branch.from(nodes.stream().findAny().orElseThrow(() -> new IllegalArgumentException("nodes cannot be empty")).get("branch"));
        if (!ContentConstants.BRANCH_DRAFT.equals(branch)) {
            return;
        }
        final List<ContentId> contentIds = nodes.stream().map(map -> ContentId.from(map.get("id"))).collect(Collectors.toList());
        final List<String> repoNames = nodes.stream().map(map -> map.get("repo")).distinct().collect(Collectors.toList());
        if (repoNames.size() != 1) {
            throw new IllegalArgumentException(repoNames.size() > 1 ? "An event cannot contain nodes from different repositories" : "An event must contain 'repo' property");
        }
        final ProjectName currentProjectName = ProjectName.from(RepositoryId.from(repoNames.get(0)));
        if (currentProjectName == null) {
            return;
        }
        final Project sourceProject = this.projectService.list().stream().filter(project -> currentProjectName.equals(project.getName())).findAny().orElseThrow(() -> new ProjectNotFoundException(currentProjectName));
        this.projectService.list().stream().filter(project -> currentProjectName.equals(project.getParent())).forEach(targetProject -> {
            final ContentEventsSyncParams.Builder paramsBuilder = ContentEventsSyncParams.create().addContentIds(contentIds).sourceProject(sourceProject.getName()).targetProject(targetProject.getName());
            switch(type) {
                case "node.created":
                case "node.duplicated":
                    paramsBuilder.syncEventType(ContentSyncEventType.CREATED);
                    break;
                case "node.updated":
                case "node.pushed":
                    paramsBuilder.syncEventType(ContentSyncEventType.UPDATED);
                    break;
                case "node.manualOrderUpdated":
                    paramsBuilder.syncEventType(ContentSyncEventType.MANUAL_ORDER_UPDATED);
                    break;
                case "node.sorted":
                    paramsBuilder.syncEventType(ContentSyncEventType.SORTED);
                    break;
                case "node.renamed":
                    paramsBuilder.syncEventType(ContentSyncEventType.RENAMED);
                    break;
                case "node.moved":
                    paramsBuilder.syncEventType(ContentSyncEventType.MOVED);
                    break;
                case "node.deleted":
                    paramsBuilder.syncEventType(ContentSyncEventType.DELETED);
                    break;
                default:
                    LOG.debug("Ignoring node type: {}", type);
                    break;
            }
            final ContentEventsSyncParams params = paramsBuilder.build();
            if (params.getSyncType() != null) {
                contentSynchronizer.sync(params);
            }
        });
        if (sourceProject.getParent() != null && "node.deleted".equals(type)) {
            this.projectService.list().stream().filter(project -> project.getName().equals(sourceProject.getParent())).forEach(parentProject -> contentSynchronizer.sync(ContentSyncParams.create().addContentIds(contentIds).sourceProject(parentProject.getName()).targetProject(sourceProject.getName()).build()));
        }
    });
}
Also used : EventListener(com.enonic.xp.event.EventListener) ContentConstants(com.enonic.xp.content.ContentConstants) LoggerFactory(org.slf4j.LoggerFactory) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId) Component(org.osgi.service.component.annotations.Component) RepositoryId(com.enonic.xp.repository.RepositoryId) ContextAccessor(com.enonic.xp.context.ContextAccessor) Duration(java.time.Duration) Map(java.util.Map) ProjectName(com.enonic.xp.project.ProjectName) Activate(org.osgi.service.component.annotations.Activate) ContextBuilder(com.enonic.xp.context.ContextBuilder) Event(com.enonic.xp.event.Event) User(com.enonic.xp.security.User) ProjectService(com.enonic.xp.project.ProjectService) Logger(org.slf4j.Logger) Deactivate(org.osgi.service.component.annotations.Deactivate) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Project(com.enonic.xp.project.Project) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) ProjectNotFoundException(com.enonic.xp.project.ProjectNotFoundException) PrincipalKey(com.enonic.xp.security.PrincipalKey) SimpleExecutor(com.enonic.xp.core.internal.concurrent.SimpleExecutor) RoleKeys(com.enonic.xp.security.RoleKeys) Context(com.enonic.xp.context.Context) Reference(org.osgi.service.component.annotations.Reference) Project(com.enonic.xp.project.Project) ProjectNotFoundException(com.enonic.xp.project.ProjectNotFoundException) ProjectName(com.enonic.xp.project.ProjectName) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId)

Example 19 with ProjectName

use of com.enonic.xp.project.ProjectName in project xp by enonic.

the class BaseProjectHandlerTest method mockProject.

protected void mockProject() {
    final Map<ProjectName, Project> projects = new HashMap<>();
    when(this.projectService.create(any(CreateProjectParams.class))).thenAnswer(mock -> {
        final Project project = createProject((CreateProjectParams) mock.getArguments()[0]);
        Mockito.when(projectService.get(project.getName())).thenReturn(project);
        Mockito.when(projectService.delete(project.getName())).thenReturn(true);
        projects.put(project.getName(), project);
        return project;
    });
    when(this.projectService.modify(any(ModifyProjectParams.class))).thenAnswer(mock -> {
        final Project project = createProject((ModifyProjectParams) mock.getArguments()[0]);
        Mockito.when(projectService.get(project.getName())).thenReturn(project);
        projects.put(project.getName(), project);
        return project;
    });
    when(this.projectService.list()).thenAnswer(mock -> Projects.create().addAll(projects.values()).build());
    when(this.projectService.getPermissions(any(ProjectName.class))).thenReturn(ProjectPermissions.create().build());
    when(this.projectService.modifyPermissions(any(ProjectName.class), any(ProjectPermissions.class))).thenAnswer(mock -> {
        final ProjectName projectName = (ProjectName) mock.getArguments()[0];
        final ProjectPermissions projectPermissions = (ProjectPermissions) mock.getArguments()[1];
        Mockito.when(projectService.getPermissions(projectName)).thenReturn(projectPermissions);
        return projectPermissions;
    });
}
Also used : Project(com.enonic.xp.project.Project) ModifyProjectParams(com.enonic.xp.project.ModifyProjectParams) ProjectName(com.enonic.xp.project.ProjectName) HashMap(java.util.HashMap) ProjectPermissions(com.enonic.xp.project.ProjectPermissions) CreateProjectParams(com.enonic.xp.project.CreateProjectParams)

Example 20 with ProjectName

use of com.enonic.xp.project.ProjectName in project xp by enonic.

the class ProjectsSyncTask method sortProjects.

private List<Project> sortProjects(final Projects projects) {
    final List<Project> result = new ArrayList<>();
    final Queue<Project> queue = new ArrayDeque<>(projects.getList());
    ProjectName currentParent = null;
    int currentParentCounter = 0;
    int loopSize = queue.size();
    while (!queue.isEmpty()) {
        if (loopSize == 0) {
            if (currentParentCounter < result.size()) {
                currentParent = result.get(currentParentCounter).getName();
                currentParentCounter++;
            } else {
                // projects with invalid parent in queue
                currentParent = queue.peek().getParent();
            }
            loopSize = queue.size();
        }
        loopSize--;
        final Project current = queue.poll();
        if (Objects.equals(current.getParent(), currentParent)) {
            result.add(current);
        } else {
            queue.offer(current);
        }
    }
    return result;
}
Also used : Project(com.enonic.xp.project.Project) ProjectName(com.enonic.xp.project.ProjectName) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque)

Aggregations

ProjectName (com.enonic.xp.project.ProjectName)20 Test (org.junit.jupiter.api.Test)13 Project (com.enonic.xp.project.Project)7 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)7 RepositoryId (com.enonic.xp.repository.RepositoryId)6 Context (com.enonic.xp.context.Context)5 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)5 EnumSet (java.util.EnumSet)5 ContentId (com.enonic.xp.content.ContentId)4 ContextAccessor (com.enonic.xp.context.ContextAccessor)4 ContextBuilder (com.enonic.xp.context.ContextBuilder)4 PropertyTree (com.enonic.xp.data.PropertyTree)4 ProjectService (com.enonic.xp.project.ProjectService)4 RoleKeys (com.enonic.xp.security.RoleKeys)4 Collectors (java.util.stream.Collectors)4 Content (com.enonic.xp.content.Content)3 ContentInheritType (com.enonic.xp.content.ContentInheritType)3 ResetContentInheritParams (com.enonic.xp.content.ResetContentInheritParams)3 UpdateContentParams (com.enonic.xp.content.UpdateContentParams)3 WorkflowInfo (com.enonic.xp.content.WorkflowInfo)3