All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.server.user.ThreadLocalUserSession Maven / Gradle / Ivy

There is a newer version: 7.2.1
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.user;

import com.google.common.base.Objects;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.annotation.CheckForNull;

/**
 * Part of the current HTTP session
 */
public class ThreadLocalUserSession implements UserSession {

  private static final ThreadLocal THREAD_LOCAL = new ThreadLocal<>();

  public UserSession get() {
    return Objects.firstNonNull(THREAD_LOCAL.get(), AnonymousUserSession.INSTANCE);
  }

  public void set(UserSession session) {
    THREAD_LOCAL.set(session);
  }

  public void remove() {
    THREAD_LOCAL.remove();
  }

  public boolean hasSession() {
    return THREAD_LOCAL.get() != null;
  }

  @Override
  @CheckForNull
  public String getLogin() {
    return get().getLogin();
  }

  @Override
  @CheckForNull
  public String getName() {
    return get().getName();
  }

  @Override
  @CheckForNull
  public Integer getUserId() {
    return get().getUserId();
  }

  @Override
  public Set getUserGroups() {
    return get().getUserGroups();
  }

  @Override
  public boolean isLoggedIn() {
    return get().isLoggedIn();
  }

  @Override
  public Locale locale() {
    return get().locale();
  }

  @Override
  public UserSession checkLoggedIn() {
    return get().checkLoggedIn();
  }

  @Override
  public UserSession checkPermission(String globalPermission) {
    return get().checkPermission(globalPermission);
  }

  @Override
  public UserSession checkGlobalPermission(String globalPermission) {
    return get().checkGlobalPermission(globalPermission);
  }

  @Override
  public UserSession checkAnyPermissions(Collection globalPermissions) {
    return get().checkAnyPermissions(globalPermissions);
  }

  @Override
  public boolean hasPermission(String globalPermission) {
    return get().hasPermission(globalPermission);
  }

  @Override
  public boolean hasGlobalPermission(String globalPermission) {
    return get().hasGlobalPermission(globalPermission);
  }

  @Override
  public List globalPermissions() {
    return get().globalPermissions();
  }

  @Override
  public UserSession checkComponentPermission(String projectPermission, String componentKey) {
    return get().checkComponentPermission(projectPermission, componentKey);
  }

  @Override
  public UserSession checkComponentUuidPermission(String permission, String componentUuid) {
    return get().checkComponentUuidPermission(permission, componentUuid);
  }

  @Override
  public boolean hasComponentPermission(String permission, String componentKey) {
    return get().hasComponentPermission(permission, componentKey);
  }

  @Override
  public boolean hasComponentUuidPermission(String permission, String componentUuid) {
    return get().hasComponentUuidPermission(permission, componentUuid);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy