001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.api.security;
021    
022    import javax.annotation.Nullable;
023    import javax.servlet.http.HttpServletRequest;
024    
025    /**
026     * Note that prefix "do" for names of methods is reserved for future enhancements, thus should not be used in subclasses.
027     *
028     * @see SecurityRealm
029     * @since 2.14
030     */
031    public abstract class ExternalUsersProvider {
032    
033      /**
034       * This method is overridden by old versions of plugins such as LDAP 1.1. It should not be overridden anymore.
035       *
036       * @param username the username
037       * @return details for specified user, or null if such user doesn't exist
038       * @throws RuntimeException in case of unexpected error such as connection failure
039       * @deprecated replaced by {@link #doGetUserDetails(org.sonar.api.security.ExternalUsersProvider.Context)} since v. 3.1
040       */
041      @Deprecated
042      public UserDetails doGetUserDetails(@Nullable String username) {
043        return null;
044      }
045    
046      /**
047       * Override this method in order load user information.
048       *
049       * @return the user, or null if user doesn't exist
050       * @throws RuntimeException in case of unexpected error such as connection failure
051       * @since 3.1
052       */
053      public UserDetails doGetUserDetails(Context context) {
054        return doGetUserDetails(context.getUsername());
055      }
056    
057      public static final class Context {
058        private String username;
059        private HttpServletRequest request;
060    
061        public Context(@Nullable String username, HttpServletRequest request) {
062          this.username = username;
063          this.request = request;
064        }
065    
066        public String getUsername() {
067          return username;
068        }
069    
070        public HttpServletRequest getRequest() {
071          return request;
072        }
073      }
074    }