Class JdbcUserDirectory
- java.lang.Object
-
- org.ametys.runtime.plugin.component.AbstractLogEnabled
-
- org.ametys.plugins.core.impl.user.directory.JdbcUserDirectory
-
- All Implemented Interfaces:
ModifiableUserDirectory
,UserDirectory
,Cacheable
,Model
,ModelItemContainer
,LogEnabled
,PluginAware
,Disposable
,Component
,Contextualizable
,Serviceable
public class JdbcUserDirectory extends AbstractLogEnabled implements ModifiableUserDirectory, Component, Serviceable, Contextualizable, PluginAware, Cacheable, Disposable
Use a jdbc driver for getting the list of users, modifying them and also authenticate them.
Passwords need to be encrypted with MD5 and encoded in base64.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
JdbcUserDirectory.AbstractJdbcQueryExecutor<T>
An internal query executor.class
JdbcUserDirectory.JdbcPredicate
Class representing a SQL predicate (to use in a WHERE or HAVING clause), with optional string parameters.protected class
JdbcUserDirectory.SelectUserJdbcQueryExecutor<T>
Query executor in order to select an userprotected class
JdbcUserDirectory.SelectUsersJdbcQueryExecutor<T>
Query executor in order to select users-
Nested classes/interfaces inherited from interface org.ametys.core.util.Cacheable
Cacheable.SingleCacheConfiguration
-
-
Field Summary
-
Constructor Summary
Constructors Constructor Description JdbcUserDirectory()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private I18nizableText
_buildI18n(String i18Key)
protected User
_createUserFromResultSet(ResultSet rs)
Create the user implementation from the result set of the requestprotected void
_fillModifyStatement(PreparedStatement stmt, Map<String,String> userInformation)
Fill the statement with the user informationsprivate Cache<String,User>
_getCacheByLogin()
private Cache<String,User>
_getCacheByMail()
protected JdbcUserDirectory.JdbcPredicate
_getMandatoryPredicate(String pattern)
Get the mandatory predicate to use when querying users by pattern.protected String
_getPatternToMatch(String pattern)
Get the pattern to match user loginprotected User
_getUserProcessResultSet(ResultSet rs, String login, Cache<String,User> cache)
Retrieve an user from a result setprotected List<User>
_getUsersProcessResultSet(ResultSet rs, Cache<String,User> cache)
Populate the user list with the result setprivate void
_initModelParameter(String name, String parameterType, String labelKey, String descriptionKey, Validator validator)
private void
_initModelParameters()
protected boolean
_isMD5Encrypted(String password)
Determines if the password is encrypted with MD5 algorithmprotected void
_updateToSSHAPassword(String login, String password)
Generate a salt key and encrypt the password with the sha2 algorithmvoid
add(Map<String,String> userInformation)
Add a new user to the list.boolean
checkCredentials(String login, String password)
Authenticate a user with its credentialsvoid
contextualize(Context context)
protected PreparedStatement
createAddStatement(Connection con, Map<String,String> userInformation)
Create Add statementprotected PreparedStatement
createModifyStatement(Connection con, Map<String,String> userInformation)
Create statement to update databasevoid
dispose()
AbstractCacheManager
getCacheManager()
Returns the instance of the implementation ofAbstractCacheManager
to use.protected CurrentUserProvider
getCurrentUserProvider()
Lazy lookup theCurrentUserProvider
String
getFamilyId()
Retrieves the Family Id (for example the extension point) Each model Id have to be unique in it's family IdString
getId()
A unique identifierString
getLabel()
Get the label of the CredentialProviderCollection<Cacheable.SingleCacheConfiguration>
getManagedCaches()
Gets the managed caches.Collection<? extends ModelItem>
getModelItems()
Retrieves all the model items of this containerprotected ObservationManager
getObservationManager()
Lazy lookup theObservationManager
Map<String,Object>
getParameterValues()
Get the values of parameters (from user directory model)String
getPopulationId()
Get the id of the population this user directory belongs to.protected Connection
getSQLConnection()
Get the connection to the databaseUser
getUser(String login)
Get a particular user by his login.User
getUserByEmail(String email)
Get a particular user by his email.String
getUserDirectoryModelId()
Get the id of theUserDirectoryModel
extension pointCollection<User>
getUsers()
Get the list of all users of one directory.List<User>
getUsers(int count, int offset, Map<String,Object> parameters)
Get a list of users from a directory given the parametersView
getView()
Retrieve the view of the Modelboolean
hasComputableSize()
Determines if the cache has a computable size.void
init(String id, String udModelId, Map<String,Object> paramValues, String label)
Initialize the user's directory with given parameters' values.void
remove(String login)
Remove an user from the list.void
service(ServiceManager manager)
void
setPluginInfo(String pluginName, String featureName, String id)
Sets the plugin info relative to the current component.
Note : The feature name may be null if the targeted component in declared at plugin level.void
setPopulationId(String populationId)
Set the value of the id of the population this user directory belong to.void
update(Map<String,String> userInformation)
Modify informations about an user of the list.Map<String,Errors>
validate(Map<String,String> userInformation)
Validate user information.-
Methods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.ametys.core.util.Cacheable
createCaches, getCache, isCachingEnabled, removeCaches
-
Methods inherited from interface org.ametys.runtime.model.ModelItemContainer
getChild, getModelItem, hasModelItem
-
-
-
-
Field Detail
-
BASE_PLUGIN_NAME
protected static final String BASE_PLUGIN_NAME
The base plugin (for i18n key)- See Also:
- Constant Field Values
-
__ORDERBY_COLUMNS
static final String[] __ORDERBY_COLUMNS
-
__DATASOURCE_PARAM_NAME
private static final String __DATASOURCE_PARAM_NAME
Name of the parameter holding the datasource id- See Also:
- Constant Field Values
-
__USERS_TABLE_PARAM_NAME
private static final String __USERS_TABLE_PARAM_NAME
Name of the parameter holding the table users' name- See Also:
- Constant Field Values
-
__COLUMN_LOGIN
private static final String __COLUMN_LOGIN
- See Also:
- Constant Field Values
-
__COLUMN_PASSWORD
private static final String __COLUMN_PASSWORD
- See Also:
- Constant Field Values
-
__COLUMN_FIRSTNAME
private static final String __COLUMN_FIRSTNAME
- See Also:
- Constant Field Values
-
__COLUMN_LASTNAME
private static final String __COLUMN_LASTNAME
- See Also:
- Constant Field Values
-
__COLUMN_EMAIL
private static final String __COLUMN_EMAIL
- See Also:
- Constant Field Values
-
__COLUMN_SALT
private static final String __COLUMN_SALT
- See Also:
- Constant Field Values
-
__JDBC_USERDIRECTORY_USER_BY_LOGIN_CACHE_NAME_PREFIX
private static final String __JDBC_USERDIRECTORY_USER_BY_LOGIN_CACHE_NAME_PREFIX
-
__JDBC_USERDIRECTORY_USER_BY_MAIL_CACHE_NAME_PREFIX
private static final String __JDBC_USERDIRECTORY_USER_BY_MAIL_CACHE_NAME_PREFIX
-
_dataSourceId
protected String _dataSourceId
The identifier of data source
-
_userTableName
protected String _userTableName
The name of users' SQL table
-
_model
protected Map<String,ElementDefinition> _model
Model
-
_pluginName
protected String _pluginName
Plugin name
-
_manager
protected ServiceManager _manager
The avalon service manager
-
_sourceResolver
protected SourceResolver _sourceResolver
The cocoon source resolver
-
_observationManager
private ObservationManager _observationManager
-
_currentUserProvider
private CurrentUserProvider _currentUserProvider
-
_udModelId
private String _udModelId
-
_paramValues
private Map<String,Object> _paramValues
-
_populationId
private String _populationId
-
_uniqueCacheSuffix
private final String _uniqueCacheSuffix
-
_lazyInitialized
private boolean _lazyInitialized
-
_jdbcParameterTypeExtensionPoint
private JdbcParameterTypeExtensionPoint _jdbcParameterTypeExtensionPoint
-
_cacheManager
private AbstractCacheManager _cacheManager
-
-
Constructor Detail
-
JdbcUserDirectory
public JdbcUserDirectory()
-
-
Method Detail
-
setPluginInfo
public void setPluginInfo(String pluginName, String featureName, String id)
Description copied from interface:PluginAware
Sets the plugin info relative to the current component.
Note : The feature name may be null if the targeted component in declared at plugin level.- Specified by:
setPluginInfo
in interfacePluginAware
- Parameters:
pluginName
- Unique identifier for the plugin hosting the extensionfeatureName
- Unique feature identifier (unique for a given pluginName)id
- Unique identifier of this component
-
contextualize
public void contextualize(Context context) throws ContextException
- Specified by:
contextualize
in interfaceContextualizable
- Throws:
ContextException
-
service
public void service(ServiceManager manager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Throws:
ServiceException
-
getId
public String getId()
Description copied from interface:UserDirectory
A unique identifier- Specified by:
getId
in interfaceModel
- Specified by:
getId
in interfaceUserDirectory
- Returns:
- The non-null and non-empty identifier
-
getFamilyId
public String getFamilyId()
Description copied from interface:Model
Retrieves the Family Id (for example the extension point) Each model Id have to be unique in it's family Id- Specified by:
getFamilyId
in interfaceModel
- Returns:
- content point (for example "org.ametys.cms.contenttype.ContentTypeExtensionPoint" for a content)
-
getLabel
public String getLabel()
Description copied from interface:UserDirectory
Get the label of the CredentialProvider- Specified by:
getLabel
in interfaceUserDirectory
- Returns:
- The optionnal label
-
dispose
public void dispose()
- Specified by:
dispose
in interfaceDisposable
-
getManagedCaches
public Collection<Cacheable.SingleCacheConfiguration> getManagedCaches()
Description copied from interface:Cacheable
Gets the managed caches.
This is meant to be implemented in order to describe the managed caches and automatically create and remove the corresponding caches inCacheable.createCaches()
andCacheable.removeCaches()
default methods.
This is not meant to be called manually.- Specified by:
getManagedCaches
in interfaceCacheable
- Returns:
- A collection of
Cacheable.SingleCacheConfiguration
s to manage
-
hasComputableSize
public boolean hasComputableSize()
Description copied from interface:Cacheable
Determines if the cache has a computable size. This operation can be very slow.- Specified by:
hasComputableSize
in interfaceCacheable
- Returns:
true
if we can compute the size on the cache.
-
_buildI18n
private I18nizableText _buildI18n(String i18Key)
-
_getCacheByLogin
private Cache<String,User> _getCacheByLogin()
-
_getCacheByMail
private Cache<String,User> _getCacheByMail()
-
getCacheManager
public AbstractCacheManager getCacheManager()
Description copied from interface:Cacheable
Returns the instance of the implementation ofAbstractCacheManager
to use.
This is not meant to be called manually.- Specified by:
getCacheManager
in interfaceCacheable
- Returns:
- The
AbstractCacheManager
to bind
-
init
public void init(String id, String udModelId, Map<String,Object> paramValues, String label)
Description copied from interface:UserDirectory
Initialize the user's directory with given parameters' values.- Specified by:
init
in interfaceUserDirectory
- Parameters:
id
- The non-null and non-empty unique identifierudModelId
- The id of user directory extension pointparamValues
- The parameters' valueslabel
- The optional label
-
_initModelParameters
private void _initModelParameters()
-
_initModelParameter
private void _initModelParameter(String name, String parameterType, String labelKey, String descriptionKey, Validator validator)
-
getObservationManager
protected ObservationManager getObservationManager()
Lazy lookup theObservationManager
- Returns:
- the observation manager
-
getCurrentUserProvider
protected CurrentUserProvider getCurrentUserProvider()
Lazy lookup theCurrentUserProvider
- Returns:
- the current user provider
-
getSQLConnection
protected Connection getSQLConnection()
Get the connection to the database- Returns:
- the SQL connection
-
setPopulationId
public void setPopulationId(String populationId)
Description copied from interface:UserDirectory
Set the value of the id of the population this user directory belong to.- Specified by:
setPopulationId
in interfaceUserDirectory
- Parameters:
populationId
- The id of the population the user directory belongs to.
-
getPopulationId
public String getPopulationId()
Description copied from interface:UserDirectory
Get the id of the population this user directory belongs to.- Specified by:
getPopulationId
in interfaceUserDirectory
- Returns:
- The id of the population
-
getParameterValues
public Map<String,Object> getParameterValues()
Description copied from interface:UserDirectory
Get the values of parameters (from user directory model)- Specified by:
getParameterValues
in interfaceUserDirectory
- Returns:
- the parameters' values
-
getUserDirectoryModelId
public String getUserDirectoryModelId()
Description copied from interface:UserDirectory
Get the id of theUserDirectoryModel
extension point- Specified by:
getUserDirectoryModelId
in interfaceUserDirectory
- Returns:
- the id of extension point
-
getUsers
public Collection<User> getUsers()
Description copied from interface:UserDirectory
Get the list of all users of one directory.- Specified by:
getUsers
in interfaceUserDirectory
- Returns:
- list of users as Collection of
User
s, empty if a problem occurs.
-
getUsers
public List<User> getUsers(int count, int offset, Map<String,Object> parameters)
Description copied from interface:UserDirectory
Get a list of users from a directory given the parameters- Specified by:
getUsers
in interfaceUserDirectory
- Parameters:
count
- The limit of users to retrieveoffset
- The number of result to ignore before starting to collect users.parameters
- A map of additional parameters, see implementation.- Returns:
- The list of retrieved
User
-
getUser
public User getUser(String login)
Description copied from interface:UserDirectory
Get a particular user by his login.- Specified by:
getUser
in interfaceUserDirectory
- Parameters:
login
- Login of the user to get. Cannot be null.- Returns:
- User's information as a
User
instance or null if the user login does not exist.
-
getUserByEmail
public User getUserByEmail(String email) throws NotUniqueUserException
Description copied from interface:UserDirectory
Get a particular user by his email.- Specified by:
getUserByEmail
in interfaceUserDirectory
- Parameters:
email
- Email of the user to get. Cannot be null.- Returns:
- User's information as a
User
instance or null if the user email does not exist. - Throws:
NotUniqueUserException
- If many users match this email
-
checkCredentials
public boolean checkCredentials(String login, String password)
Description copied from interface:UserDirectory
Authenticate a user with its credentials- Specified by:
checkCredentials
in interfaceUserDirectory
- Parameters:
login
- The login to check. Cannot be null.password
- The password to check.- Returns:
- true if the user is authenticated, false otherwise.
-
add
public void add(Map<String,String> userInformation) throws InvalidModificationException
Description copied from interface:ModifiableUserDirectory
Add a new user to the list.- Specified by:
add
in interfaceModifiableUserDirectory
- Parameters:
userInformation
- Informations about the user, see implementation. Cannot be null.- Throws:
InvalidModificationException
- if the login exists yet or if at least one of the parameter is invalid.
-
validate
public Map<String,Errors> validate(Map<String,String> userInformation)
Description copied from interface:ModifiableUserDirectory
Validate user information.- Specified by:
validate
in interfaceModifiableUserDirectory
- Parameters:
userInformation
- Informations about the user, see implementation. Cannot be null.- Returns:
- validation errors.
-
update
public void update(Map<String,String> userInformation) throws InvalidModificationException
Description copied from interface:ModifiableUserDirectory
Modify informations about an user of the list.- Specified by:
update
in interfaceModifiableUserDirectory
- Parameters:
userInformation
- New informations about the user, see implementation. Cannot be null.- Throws:
InvalidModificationException
- if the login does not match in the list or if at least one of the parameter is invalid.
-
remove
public void remove(String login) throws InvalidModificationException
Description copied from interface:ModifiableUserDirectory
Remove an user from the list.- Specified by:
remove
in interfaceModifiableUserDirectory
- Parameters:
login
- The user's login. Cannot be null.- Throws:
InvalidModificationException
- if the user cannot be removed
-
getModelItems
public Collection<? extends ModelItem> getModelItems()
Description copied from interface:ModelItemContainer
Retrieves all the model items of this container- Specified by:
getModelItems
in interfaceModelItemContainer
- Returns:
- the model items
-
_getMandatoryPredicate
protected JdbcUserDirectory.JdbcPredicate _getMandatoryPredicate(String pattern)
Get the mandatory predicate to use when querying users by pattern.- Parameters:
pattern
- The pattern to match, can be null.- Returns:
- a
JdbcUserDirectory.JdbcPredicate
, can be null.
-
_getPatternToMatch
protected String _getPatternToMatch(String pattern)
Get the pattern to match user login- Parameters:
pattern
- the pattern- Returns:
- the pattern to match user login
-
_isMD5Encrypted
protected boolean _isMD5Encrypted(String password)
Determines if the password is encrypted with MD5 algorithm- Parameters:
password
- The encrypted password- Returns:
- true if the password is encrypted with MD5 algorithm
-
_updateToSSHAPassword
protected void _updateToSSHAPassword(String login, String password)
Generate a salt key and encrypt the password with the sha2 algorithm- Parameters:
login
- The user loginpassword
- The user pasword
-
createAddStatement
protected PreparedStatement createAddStatement(Connection con, Map<String,String> userInformation) throws SQLException
Create Add statement- Parameters:
con
- The sql connectionuserInformation
- the user informations- Returns:
- The statement
- Throws:
SQLException
- if an error occurred
-
createModifyStatement
protected PreparedStatement createModifyStatement(Connection con, Map<String,String> userInformation) throws SQLException
Create statement to update database- Parameters:
con
- The sql connectionuserInformation
- The user information- Returns:
- The statement
- Throws:
SQLException
- if an error occurred
-
_fillModifyStatement
protected void _fillModifyStatement(PreparedStatement stmt, Map<String,String> userInformation) throws SQLException
Fill the statement with the user informations- Parameters:
stmt
- The statement of the sql requestuserInformation
- the user informations- Throws:
SQLException
- if an error occurred
-
_getUsersProcessResultSet
protected List<User> _getUsersProcessResultSet(ResultSet rs, Cache<String,User> cache) throws SQLException
Populate the user list with the result set- Parameters:
rs
- The result setcache
- the cache to use. Is null if caching is not enabled- Returns:
- The user list
- Throws:
SQLException
- If an SQL exception occurs
-
_createUserFromResultSet
protected User _createUserFromResultSet(ResultSet rs) throws SQLException
Create the user implementation from the result set of the request- Parameters:
rs
- The result set where you can use get methods- Returns:
- The user refleting the current cursor position in the result set
- Throws:
SQLException
- if an error occurred
-
_getUserProcessResultSet
protected User _getUserProcessResultSet(ResultSet rs, String login, Cache<String,User> cache) throws SQLException
Retrieve an user from a result set- Parameters:
rs
- The result setlogin
- The user logincache
- the cache to use. Is null if caching is not enabled- Returns:
- The retrieved user or null if not found
- Throws:
SQLException
- If an SQL Exception occurs
-
getView
public View getView()
Description copied from interface:ModifiableUserDirectory
Retrieve the view of the Model- Specified by:
getView
in interfaceModifiableUserDirectory
- Returns:
- The View
-
-