Search in sources :

Example 1 with LocalScope

use of com.enonic.xp.context.LocalScope in project xp by enonic.

the class AuthResourceTest method testAuthenticated_authenticated.

@Test
public void testAuthenticated_authenticated() throws Exception {
    final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.system(), "user1")).displayName("User 1").modifiedTime(Instant.now(clock)).email("user1@enonic.com").login("user1").build();
    final LocalScope localScope = ContextAccessor.current().getLocalScope();
    final AuthenticationInfo authInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN_LOGIN).build();
    localScope.setAttribute(authInfo);
    localScope.setSession(new SessionMock());
    String jsonString = request().path("auth/authenticated").get().getAsString();
    assertJson("authenticated_success.json", jsonString);
}
Also used : User(com.enonic.xp.security.User) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) LocalScope(com.enonic.xp.context.LocalScope) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 2 with LocalScope

use of com.enonic.xp.context.LocalScope in project xp by enonic.

the class LoginHandler method createSession.

private void createSession(final AuthenticationInfo authInfo) {
    final LocalScope localScope = this.context.get().getLocalScope();
    final Session session = localScope.getSession();
    if (session != null) {
        final var attributes = session.getAttributes();
        session.invalidate();
        final Session newSession = localScope.getSession();
        if (newSession != null) {
            attributes.forEach(newSession::setAttribute);
            session.setAttribute(authInfo);
            if (this.sessionTimeout != null) {
                setSessionTimeout();
            }
        }
    }
}
Also used : LocalScope(com.enonic.xp.context.LocalScope) HttpSession(javax.servlet.http.HttpSession) Session(com.enonic.xp.session.Session)

Example 3 with LocalScope

use of com.enonic.xp.context.LocalScope in project xp by enonic.

the class ContextMapper method getAttributes.

private Map<String, Object> getAttributes() {
    LocalScope localScope = this.context.getLocalScope();
    Map<String, Object> attributes = this.context.getAttributes();
    Map<String, Object> localAttributes = localScope.getAttributes();
    Session session = localScope.getSession();
    Map<String, Object> sessionAttributes = session == null ? Map.of() : session.getAttributes();
    return Stream.of(attributes, localAttributes, sessionAttributes).flatMap(map -> map.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, HashMap::new));
}
Also used : PrincipalMapper(com.enonic.xp.lib.common.PrincipalMapper) User(com.enonic.xp.security.User) LocalScope(com.enonic.xp.context.LocalScope) HashMap(java.util.HashMap) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Collectors(java.util.stream.Collectors) Session(com.enonic.xp.session.Session) Stream(java.util.stream.Stream) PrincipalKey(com.enonic.xp.security.PrincipalKey) Map(java.util.Map) Context(com.enonic.xp.context.Context) MapGenerator(com.enonic.xp.script.serializer.MapGenerator) MapSerializable(com.enonic.xp.script.serializer.MapSerializable) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) HashMap(java.util.HashMap) Map(java.util.Map) LocalScope(com.enonic.xp.context.LocalScope) Session(com.enonic.xp.session.Session)

Example 4 with LocalScope

use of com.enonic.xp.context.LocalScope in project xp by enonic.

the class HtmlMacroProcessor method process.

public String process(final String text) {
    final LocalScope localScope = ContextAccessor.current().getLocalScope();
    return macroService.evaluateMacros(text, (macro) -> {
        Integer macroDocCounter = (Integer) localScope.getAttribute(MACRO_DOCUMENT_COUNTER);
        macroDocCounter = macroDocCounter == null ? 1 : macroDocCounter + 1;
        final String documentRef = MACRO_DOCUMENT_REF_PREFIX + macroDocCounter;
        localScope.setAttribute(documentRef, text);
        localScope.setAttribute(MACRO_DOCUMENT_COUNTER, macroDocCounter);
        macro = Macro.copyOf(macro).param(MacroInstruction.MACRO_DOCUMENT, documentRef).build();
        return macroService.postProcessInstructionSerialize(macro);
    });
}
Also used : LocalScope(com.enonic.xp.context.LocalScope)

Aggregations

LocalScope (com.enonic.xp.context.LocalScope)4 User (com.enonic.xp.security.User)2 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)2 Session (com.enonic.xp.session.Session)2 Context (com.enonic.xp.context.Context)1 PrincipalMapper (com.enonic.xp.lib.common.PrincipalMapper)1 MapGenerator (com.enonic.xp.script.serializer.MapGenerator)1 MapSerializable (com.enonic.xp.script.serializer.MapSerializable)1 PrincipalKey (com.enonic.xp.security.PrincipalKey)1 PrincipalKeys (com.enonic.xp.security.PrincipalKeys)1 SessionMock (com.enonic.xp.session.SessionMock)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 HttpSession (javax.servlet.http.HttpSession)1 Test (org.junit.jupiter.api.Test)1