📄 userstore.java
字号:
/* * Copyright 2005,2006 WSO2, Inc. http://www.wso2.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.wso2.solutions.identity;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.wso2.solutions.identity.admin.ClaimsAdmin;import org.wso2.solutions.identity.i18n.Messages;import org.wso2.solutions.identity.persistence.IPPersistenceManager;import org.wso2.solutions.identity.persistence.dataobject.ClaimDO;import org.wso2.solutions.identity.persistence.dataobject.RealmConfigurationDO;import org.wso2.solutions.identity.persistence.dataobject.RealmConfigurationPropertyDO;import org.wso2.solutions.identity.persistence.dataobject.RealmDO;import org.wso2.solutions.identity.users.IdentityDefaultRealm;import org.wso2.solutions.identity.users.IdentityUserStoreReader;import org.wso2.usermanager.Authenticator;import org.wso2.usermanager.Realm;import org.wso2.usermanager.UserManagerException;import org.wso2.usermanager.UserStoreReader;import org.wso2.usermanager.readwrite.DefaultRealm;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;/** * Interface to the user store. * * This is a wrapper around the <a * href="http://www.wso2.org/projects/commons/usermanager">WSO2 Usermanager</a> * library. The UserStore will maintain a singleton instance of a * <code>org.wso2.usermanager.Realm</code> implementation. */public class UserStore { private static Log log = LogFactory.getLog(UserStore.class); private static Messages messages = Messages .getInstance(IdentityProviderConstants.RESOURCES); /** * The effective realm instance used to extract user information. */ private Realm realm; private static UserStore userStore; /** * Create a new instance of the <code>UserStore</code>. * * @throws IdentityProviderException */ private UserStore() throws IdentityProviderException { IPPersistenceManager db = IPPersistenceManager.getPersistanceManager(); this.realm = loadRealm(db.getEffectiveRealmConfiguration()); } public static UserStore getInstance() throws IdentityProviderException { if (userStore == null) { userStore = new UserStore(); } return userStore; } /** * Update the realm used to the given instance * * @param realmConfigName * @throws IdentityProviderException */ public void changeRalm(String realmConfigName) throws IdentityProviderException { IPPersistenceManager db = IPPersistenceManager.getPersistanceManager(); this.realm = this.loadRealm(db.getRealmConfiguration(realmConfigName)); } /** * Loads and sets the realm with the given realm configuration. * * @param realmConfig * The <code>RealmConfigurationDO</code> instance. * @throws IdentityProviderException */ private Realm loadRealm(RealmConfigurationDO realmConfig) throws IdentityProviderException { try { // Get the realm type RealmDO realmDO = realmConfig.getRealm(); // Instantiate the realm class Realm realmObj = (Realm) Class.forName(realmDO.getClassName()) .newInstance(); // Load the realm configuration String realmConfigClassName = realmDO.getConfigClassName(); if (realmConfigClassName != null) { Class realmConfigClass = Class.forName(realmConfigClassName); Object configObj = populateConfig(realmConfigClass, realmConfig .getConfigProperties()); // Init with the config object realmObj.init(configObj); } return realmObj; } catch (Exception e) { throw new IdentityProviderException("errorInitializingUserStore", e); } } /** * @param realmConfigClass * @param properties * @return */ private Object populateConfig(Class realmConfigClass, Set properties) throws IdentityProviderException { // Find the setters Method[] methods = realmConfigClass.getDeclaredMethods(); HashMap settersList = new HashMap(); for (int i = 0; i < methods.length; i++) { String name = methods[i].getName(); if (name.startsWith("set")) { settersList.put(name.substring(3, name.length()), methods[i]); } } // Instantiate the config class Object config; try { config = realmConfigClass.newInstance(); // Set the available properties Iterator propertyIterator = properties.iterator(); while (propertyIterator.hasNext()) { RealmConfigurationPropertyDO prop = (RealmConfigurationPropertyDO) propertyIterator .next(); // Get the property name String propName = prop.getName(); // Get the setter Method setter = (Method) settersList.get(propName); if (setter != null) { // Set the value setter.invoke(config, new Object[] { prop.getValue() }); } else { log.warn(messages .getMessage("noSetterForProperty", new String[] { propName, realmConfigClass.getName() })); } } } catch (Exception e) { throw new IdentityProviderException( "errorLoadingRealmConfiguration", new String[] { realmConfigClass.getName() }, e); } return config; } /** * Authenticate the given user credentials. * * @param username * @param password * @return whether the user with the given credentials are authenticated or * not. */ public boolean authenticate(String username, String password) { try { Authenticator authenticator = realm.getAuthenticator(); return authenticator.authenticate(username, password); } catch (UserManagerException e) { log.error("Exception in authenticating user : username = " + username + ",password = " + password); } return false; } /** * Return the list of claim values of the given user. * * @param username * Name of the user * @param propertyNames * Names of the claims required. * @return A map of claims with their values. * @throws IdentityProviderException */ public Map<String,String> getClaimValues(String username, List<String> propertyNames) throws IdentityProviderException { try { UserStoreReader usReader = realm.getUserStoreReader(); return usReader.getUserProperties(username); } catch (UserManagerException e) { throw new IdentityProviderException( "errorExtractingUserProperties", new String[] { username }, e); } } /** * Access the name of user properties in the store. * * @return A <code>java.util.List</code> of all user property names. * @throws IdentityProviderException */ public List<String> getPropertyNames() throws IdentityProviderException { try { UserStoreReader usReader = realm.getUserStoreReader(); List<String> propList = Arrays.asList((String[]) usReader .getUserPropertyNames()); // Default realm is a special case since attr ids are not available // until the first user is added // Therefore in that case we need the set of enabled claims if (realm instanceof DefaultRealm && propList.isEmpty()) { ClaimDO[] claims = new ClaimsAdmin().getAllEnabledClaims(); propList = new ArrayList<String>(); for (int i = 0; i < claims.length; i++) { propList.add(claims[i].getUri()); } } return propList; } catch (UserManagerException e) { throw new IdentityProviderException( "errorExtractingUserSecificProperties", new String[] { this.realm.getClass().getName() }); } } /** * Access the names of all users in the store. * * @return A <code>java.util.List</code> of all user names. * @throws IdentityProviderException */ public List<String> getAllUserNames() throws IdentityProviderException { try { UserStoreReader usReader = realm.getUserStoreReader(); return Arrays.asList((String[]) usReader.getAllUserNames()); } catch (UserManagerException e) { throw new IdentityProviderException("errorExtractingUserNames", new String[] { this.realm.getClass().getName() }, e); } } public Realm getRealm() { return realm; } /** * * @param username * @param profileName * @param propertyNames * @return * @throws IdentityProviderException */ public Map<String,String> getClaimValues(String username, String profileName, List<String> propertyNames) throws IdentityProviderException { try { IdentityUserStoreReader usReader = ((IdentityDefaultRealm)realm).getIdentityUserStoreReader(); return usReader.getUserProperties(username, profileName); } catch (UserManagerException e) { throw new IdentityProviderException( "errorExtractingUserProperties", new String[] { username }, e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -