diff --git a/src/main/java/org/hisp/dhis/security/context/Dhis2Context.java b/src/main/java/org/hisp/dhis/security/context/Dhis2Context.java index 0e965edd..a26d346e 100644 --- a/src/main/java/org/hisp/dhis/security/context/Dhis2Context.java +++ b/src/main/java/org/hisp/dhis/security/context/Dhis2Context.java @@ -27,9 +27,10 @@ */ package org.hisp.dhis.security.context; +import static org.apache.commons.lang3.StringUtils.isNotBlank; + import java.util.Objects; import lombok.Getter; -import org.hisp.dhis.model.Me; /** Represents the security context for a DHIS2 session. */ @Getter @@ -37,28 +38,39 @@ public class Dhis2Context { /** The DHIS2 session identifier. */ private final String sessionId; - /** The currently authenticated user as {@link Me}. */ - private final Me user; + /** The username of the currently authenticated user. */ + private final String username; + + /** + * Constructor. + * + * @param sessionId the session identifier. + */ + public Dhis2Context(String sessionId) { + this.sessionId = sessionId; + this.username = null; + Objects.requireNonNull(sessionId); + } /** * Constructor. * * @param sessionId the session identifier. - * @param user the currently authenticated user as {@link Me}. + * @param username the username of the currently authenticated user. */ - public Dhis2Context(String sessionId, Me user) { + public Dhis2Context(String sessionId, String username) { this.sessionId = sessionId; - this.user = user; + this.username = username; Objects.requireNonNull(sessionId); } - /** Indicates whether a valid session identifier is present. */ + /** Indicates whether a session identifier is present. */ public boolean hasSessionId() { - return sessionId != null && sessionId.trim().length() > 16; + return isNotBlank(sessionId); } - /** Indicates whether an authenticated user is present. */ - public boolean hasUser() { - return user != null; + /** Indicates whether a username is present. */ + public boolean hasUsername() { + return isNotBlank(username); } } diff --git a/src/test/java/org/hisp/dhis/security/context/Dhis2ContextHolderTest.java b/src/test/java/org/hisp/dhis/security/context/Dhis2ContextHolderTest.java new file mode 100644 index 00000000..429c5dd4 --- /dev/null +++ b/src/test/java/org/hisp/dhis/security/context/Dhis2ContextHolderTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2004-2025, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.security.context; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +class Dhis2ContextHolderTest { + @Test + void testSetContext() { + String sessionId = "64E50FE921A40ECDC45ECA695B68CD1F"; + String username = "system"; + + Dhis2Context context = new Dhis2Context(sessionId, username); + + Dhis2ContextHolder.setContext(context); + + assertNotNull(Dhis2ContextHolder.getContext()); + assertEquals(sessionId, Dhis2ContextHolder.getContext().getSessionId()); + assertEquals(username, Dhis2ContextHolder.getContext().getUsername()); + + Dhis2ContextHolder.clearContext(); + + assertNull(Dhis2ContextHolder.getContext()); + } +}