001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
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 be overridden anymore.
035       *
036       * @return details for specified user, or null if such user doesn't exist
037       * @throws RuntimeException in case of unexpected error such as connection failure
038       * @deprecated replaced by {@link #doGetUserDetails(org.sonar.api.security.ExternalUsersProvider.Context)} since v. 3.1
039       */
040      @Deprecated
041      public UserDetails doGetUserDetails(@Nullable String username) {
042        return null;
043      }
044    
045      /**
046       * Override this method in order load user information.
047       *
048       * @return the user, or null if user doesn't exist
049       * @throws RuntimeException in case of unexpected error such as connection failure
050       * @since 3.1
051       */
052      public UserDetails doGetUserDetails(Context context) {
053        return doGetUserDetails(context.getUsername());
054      }
055    
056      public static final class Context {
057        private String username;
058        private HttpServletRequest request;
059    
060        public Context(@Nullable String username, HttpServletRequest request) {
061          this.username = username;
062          this.request = request;
063        }
064    
065        public String getUsername() {
066          return username;
067        }
068    
069        public HttpServletRequest getRequest() {
070          return request;
071        }
072      }
073    }