Search in sources :

Example 11 with UserSession

use of org.sonar.server.user.UserSession in project sonarqube by SonarSource.

the class UserSessionInitializer method setUserSession.

private void setUserSession(HttpServletRequest request, HttpServletResponse response) {
    Optional<UserDto> user = authenticate(request, response);
    if (user.isPresent()) {
        UserSession session = userSessionFactory.create(user.get());
        threadLocalSession.set(session);
        request.setAttribute(ACCESS_LOG_LOGIN, session.getLogin());
    } else {
        if (settings.getBoolean(CORE_FORCE_AUTHENTICATION_PROPERTY)) {
            throw AuthenticationException.newBuilder().setSource(Source.local(Method.BASIC)).setMessage("User must be authenticated").build();
        }
        threadLocalSession.set(userSessionFactory.createAnonymous());
        request.setAttribute(ACCESS_LOG_LOGIN, "-");
    }
}
Also used : UserDto(org.sonar.db.user.UserDto) ThreadLocalUserSession(org.sonar.server.user.ThreadLocalUserSession) UserSession(org.sonar.server.user.UserSession)

Aggregations

UserSession (org.sonar.server.user.UserSession)11 AnonymousMockUserSession (org.sonar.server.tester.AnonymousMockUserSession)5 MockUserSession (org.sonar.server.tester.MockUserSession)5 Before (org.junit.Before)4 DbClient (org.sonar.db.DbClient)4 ThreadLocalUserSession (org.sonar.server.user.ThreadLocalUserSession)4 WsTester (org.sonar.server.ws.WsTester)4 Test (org.junit.Test)3 System2 (org.sonar.api.utils.System2)1 UserDto (org.sonar.db.user.UserDto)1 TemplateGroupsAction (org.sonar.server.permission.ws.template.TemplateGroupsAction)1 TemplateUsersAction (org.sonar.server.permission.ws.template.TemplateUsersAction)1 TokenGenerator (org.sonar.server.usertoken.TokenGenerator)1