use of com.enonic.xp.context.Context in project xp by enonic.
the class JsonExceptionMapper method createContextJson.
private static ObjectNode createContextJson() {
final Context context = ContextAccessor.current();
final AuthenticationInfo authInfo = context.getAuthInfo();
final ObjectNode node = JsonNodeFactory.instance.objectNode();
node.put("authenticated", (authInfo != null) && authInfo.isAuthenticated());
final ArrayNode principals = node.putArray("principals");
if (authInfo != null) {
for (final PrincipalKey principal : authInfo.getPrincipals()) {
principals.add(principal.toString());
}
}
return node;
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class UnpublishContentHandler method execute.
public List<String> execute() {
final Branch targetBranch = ContentConstants.BRANCH_MASTER;
final Context context = ContextBuilder.from(ContextAccessor.current()).branch(targetBranch).build();
final List<ContentId> contentIds = context.callWith(this::resolveContentIds);
final UnpublishContentParams unpublishContentParams = UnpublishContentParams.create().contentIds(ContentIds.from(contentIds)).unpublishBranch(targetBranch).includeChildren(true).build();
final UnpublishContentsResult result = this.contentService.unpublishContent(unpublishContentParams);
return result.getUnpublishedContents().stream().map(ContentId::toString).collect(Collectors.toList());
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class PageHandlerTest method getContentExistsButInsufficientRights.
@Test
public void getContentExistsButInsufficientRights() {
final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(User.ANONYMOUS).build();
final Context authenticatedContext = ContextBuilder.from(ContextAccessor.current()).authInfo(authenticationInfo).build();
final ContentPath path = ContentPath.from("/site/somepath/content");
when(this.contentService.getByPath(path)).thenThrow(new ContentNotFoundException(path, Branch.from("draft")));
when(this.contentService.contentExists(path)).thenReturn(true);
this.request.setContentPath(path);
final WebException e = assertThrows(WebException.class, () -> authenticatedContext.callWith(() -> this.handler.handle(this.request, PortalResponse.create().build(), null)));
assertEquals(HttpStatus.FORBIDDEN, e.getStatus());
assertEquals("You don't have permission to access [/site/somepath/content]", e.getMessage());
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class IdProviderRequestWrapperTest method isUserInRole.
@Test
void isUserInRole() {
final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.createDefault(), "userId")).login("usr").build();
final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN).build();
final Context context = ContextBuilder.create().build();
final Session session = new SessionMock();
context.getLocalScope().setSession(session);
session.setAttribute(authenticationInfo);
final Boolean isAdmin = context.callWith(() -> new IdProviderRequestWrapper(request).isUserInRole(RoleKeys.ADMIN.getId()));
assertTrue(isAdmin);
verifyNoInteractions(request);
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class IdProviderRequestWrapperTest method getUserPrincipal.
@Test
void getUserPrincipal() {
final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.createDefault(), "userId")).login("usr").build();
final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(user).build();
final Context context = ContextBuilder.create().build();
final Session session = new SessionMock();
context.getLocalScope().setSession(session);
session.setAttribute(authenticationInfo);
final Principal principal = context.callWith(() -> new IdProviderRequestWrapper(request).getUserPrincipal());
assertEquals(principal, user);
verifyNoInteractions(request);
}
Aggregations