Is there a way to set content permission on Confluence 8?

Hi there,

We have a plugin that provides REST API for setting content permissions.
Previously, with Confluence 7 we used ContentEntityManagerInternal in conjunction with ContentPermissionManager:

        ContentEntityObject contentObject = contentEntityManager.getById(ContentId.of(pageId));
        ...
        contentPermissionManager.setContentPermissions(resultingPermissionsMap, contentObject);
        

Now we are getting ready to upgrade to Confluence 8.5. The problem we faced is, ContentEntityManagerInternal seems to no longer be available, failing with:

Unsatisfied requirement: (&(package=com.atlassian.confluence.internal))

Probably, due to this:

com.atlassian.confluence.internal (Atlassian Confluence 6.2.0 API) - Internal interfaces that are NOT exposed to the plugin classpath, by being excluded in the packageScanningConfiguration of bootstrapContext.xml. This allows us to slowly reduce the surface area of confluence-core as an API, now that we are intentionally exposing an official API in confluence-java-api.

The suggested way of getting content by ID seems to be using the ContentService, so we figured this would do the job:

        Content content = contentService.find()
                .withStatus(ContentStatus.CURRENT)
                .withId(ContentId.of(pageId))
                .fetch()
                .orElseThrow(notFound("No content found with id : " + pageId));

And it does! The problem is, it returns the com.atlassian.confluence.api.model.content.Content object, while ContentPermissionManaget expects com.atlassian.confluence.core.ContentEntityObject.

So, we need either a way of getting a ContentEntityObject some other way, or set permissions some other way.

I would appreciate any suggestions on how to do this using the current Confluence APIs.

1 Like

Is there any solution to this?

Yes, we’ve found a solution that works for us.

Looks essentially like this:

Content content = contentService
    .find(new Expansion(SPACE))
    .withStatus(ContentStatus.CURRENT)
    .withId(ContentId.of(contentId))
    .fetch().orElse(null);

Map<SubjectType, PageResponse<Subject>> restrictionsMap = new LinkedHashMap<>();

Collection<Subject> userSubjects = permissionsUserSet.stream().map(userName -> asUser(userName))
    .collect(Collectors.toSet());
PageResponse<Subject> someUsers = PageResponseImpl.<Subject> builder().addAll(userSubjects)
    .build();

restrictionsMap.put(SubjectType.USER, someUsers);

ContentRestriction contentRestriction = 
    ContentRestriction.builder().operation(OperationKey.UPDATE).restrictions(restrictionsMap).build();

contentRestrictionService.updateRestrictions(content.getId(), restrictions,
    Expansions.of("update.restrictions.user"));