📄 identitydefaultuserstoreadmin.java
字号:
package org.wso2.solutions.identity.users;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.sql.DataSource;
import org.wso2.solutions.identity.IdentityConstants;
import org.wso2.usermanager.UserManagerException;
import org.wso2.usermanager.readwrite.DefaultStrategy;
import org.wso2.usermanager.readwrite.DefaultUserStoreAdmin;
import org.wso2.usermanager.readwrite.util.UUIDGenerator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class IdentityDefaultUserStoreAdmin extends DefaultUserStoreAdmin
implements IdentityUserStoreAdmin {
private static Log log = LogFactory.getLog(DefaultUserStoreAdmin.class);
/**
* Class constructor specifying the data-source
* @param dataSource DataSource
*/
public IdentityDefaultUserStoreAdmin(DataSource dataSource) {
super(dataSource);
}
/**
* Class constructor specifying the data-source and the default strategy
* @param dataSource DataSource
* @param store DefaultStrategy
*/
public IdentityDefaultUserStoreAdmin(DataSource dataSource,
DefaultStrategy store) {
super(dataSource, store);
}
/**
* {@inheritDoc}
*/
public void setUserProperties(String userName, Map properties)
throws UserManagerException {
setUserProperties(userName, properties, null);
}
/**
* {@inheritDoc}
*/
public void setUserProperties(String userName,
Map<String, String> properties, String profileName)
throws UserManagerException {
String userid = data.getUserId(userName);
if (userid == null) {
throw new UserManagerException("nullUser");
}
Connection dbConnection = null;
boolean isDefaultProfile = true;
PreparedStatement sqlStatement = null;
try {
dbConnection = dataSource.getConnection();
if (dbConnection == null) {
throw new UserManagerException("null_connection");
}
dbConnection.setAutoCommit(false);
sqlStatement = dbConnection
.prepareStatement("insert into user_profile_values (attr_name, attr_value, profile_id, id) values (?, ?, ?, ?)");
String profileId = UUIDGenerator.getUUID();
if (properties != null) {
Iterator ite = properties.entrySet().iterator();
while (ite.hasNext()) {
Entry entry = (Entry) ite.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
if (value != null) {
if (!key
.equalsIgnoreCase(IdentityConstants.PROFILE_NAME)) {
String idUserAttribute = UUIDGenerator.getUUID();
sqlStatement.setString(1, key);
sqlStatement.setString(2, value);
sqlStatement.setString(3, profileId);
sqlStatement.setString(4, idUserAttribute);
sqlStatement.executeUpdate();
} else {
profileName = value;
}
}
}
}
sqlStatement.close();
sqlStatement = dbConnection
.prepareStatement("select * from user_profile where user_id=? AND is_default=?");
sqlStatement.setString(1, userid);
sqlStatement.setBoolean(2, true);
ResultSet results = sqlStatement.executeQuery();
if (results != null) {
if (results.next()) {
isDefaultProfile = false;
}
results.close();
}
if (profileName == null && isDefaultProfile)
profileName = "Default Profile";
dbConnection.commit();
addUserProfile(userid, profileId, isDefaultProfile, profileName);
} catch (SQLException e) {
try {
dbConnection.rollback();
} catch (SQLException e1) {
log.debug(e);
throw new UserManagerException("errorModifyingUserStore", e);
}
log.debug(e);
throw new UserManagerException("errorModifyingUserStore", e);
} finally {
try {
if (sqlStatement != null) {
sqlStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
} catch (SQLException e) {
throw new UserManagerException("errorClosingConnection", e);
}
}
}
/**
* Add a new profile for a given user.
* @param userId User Id
* @param profileId Profile Id
* @param isDefault Indicates whether the given profile is default or not
* @param profileName Name of the profile
* @throws UserManagerException
*/
protected void addUserProfile(String userId, String profileId,
boolean isDefault, String profileName) throws UserManagerException {
Connection dbConnection = null;
PreparedStatement sqlStatement = null;
try {
dbConnection = dataSource.getConnection();
dbConnection.setAutoCommit(false);
sqlStatement = dbConnection
.prepareStatement("insert into user_profile (profile_id, user_id, profile_name, is_default,id) values (?, ?, ?, ?,?)");
sqlStatement.setString(1, profileId);
sqlStatement.setString(2, userId);
sqlStatement.setString(3, profileName);
sqlStatement.setBoolean(4, isDefault);
sqlStatement.setString(5, UUIDGenerator.getUUID());
sqlStatement.executeUpdate();
dbConnection.commit();
} catch (SQLException e) {
try {
dbConnection.rollback();
} catch (SQLException e1) {
log.debug(e);
throw new UserManagerException("errorModifyingUserStore", e1);
}
log.debug(e);
throw new UserManagerException("errorModifyingUserStore", e);
} finally {
try {
if (sqlStatement != null) {
sqlStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
} catch (SQLException e) {
log.debug(e);
throw new UserManagerException("errorModifyingUserStore", e);
}
}
}
/**
* {@inheritDoc}
*/
public void updateUserProperties(String userName,
Map<String, String> properties, String profileName)
throws UserManagerException {
String userId = data.getUserId(userName);
if (userId == null) {
throw new UserManagerException("nullUser");
}
Connection dbConnection = null;
String profileId = null;
PreparedStatement sqlStatement = null;
try {
dbConnection = dataSource.getConnection();
if (dbConnection == null) {
throw new UserManagerException("null_connection");
}
dbConnection.setAutoCommit(false);
sqlStatement = dbConnection
.prepareStatement("select profile_id from user_profile where user_id=? and profile_name=?");
sqlStatement.setString(1, userId);
sqlStatement.setString(2, profileName);
ResultSet results = sqlStatement.executeQuery();
if (results != null) {
if (results.next()) {
profileId = results.getString(1);
}
results.close();
}
if (profileId == null)
throw new UserManagerException("errorModifyingUserStore");
sqlStatement.close();
sqlStatement = dbConnection
.prepareStatement("delete from user_profile_values where profile_id=?");
sqlStatement.setString(1, profileId);
sqlStatement.executeUpdate();
sqlStatement.close();
sqlStatement = dbConnection
.prepareStatement("insert into user_profile_values (attr_name, attr_value, profile_id, id) values (?, ?, ?, ?)");
if (properties != null) {
Iterator ite = properties.entrySet().iterator();
while (ite.hasNext()) {
Entry entry = (Entry) ite.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
if (value != null) {
String idUserAttribute = UUIDGenerator.getUUID();
sqlStatement.setString(1, key);
sqlStatement.setString(2, value);
sqlStatement.setString(3, profileId);
sqlStatement.setString(4, idUserAttribute);
sqlStatement.executeUpdate();
}
}
}
dbConnection.commit();
} catch (SQLException e) {
try {
dbConnection.rollback();
} catch (SQLException e1) {
log.debug(e);
throw new UserManagerException("errorModifyingUserStore", e);
}
log.debug(e);
throw new UserManagerException("errorModifyingUserStore", e);
} finally {
try {
if (sqlStatement != null) {
sqlStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
} catch (SQLException e) {
throw new UserManagerException("errorClosingConnection", e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -