Class JdbcUserDirectory
- java.lang.Object
-
- org.ametys.runtime.plugin.component.AbstractLogEnabled
-
- org.ametys.plugins.core.impl.user.directory.AbstractCachingUserDirectory
-
- org.ametys.plugins.core.impl.user.directory.JdbcUserDirectory
-
- All Implemented Interfaces:
ModifiableUserDirectory
,UserDirectory
,Cacheable
,Model
,ModelItemAccessor
,ModelItemContainer
,LogEnabled
,Disposable
,Component
,Contextualizable
,Serviceable
public class JdbcUserDirectory extends AbstractCachingUserDirectory implements ModifiableUserDirectory, Component, Contextualizable
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
Fields Modifier and Type Field Description protected Context
_context
The avalon contextprotected String
_dataSourceId
The identifier of data sourceprotected ServiceManager
_manager
The avalon service managerprotected Map<String,ElementDefinition>
_model
Modelprotected SourceResolver
_sourceResolver
The cocoon source resolverprotected String
_userTableName
The name of users' SQL tableprotected static String
BASE_PLUGIN_NAME
The base plugin (for i18n key)
-
Constructor Summary
Constructors Constructor Description JdbcUserDirectory()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 informationsprotected 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, boolean isLogin)
Populate the user list with the result setprotected 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 databaseprotected String
getCacheTypeLabel()
Returns a String identifying the type of this directory, used in the cache screen in _admin, such as 'SQL' or 'LDAP'.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 IdCollection<? extends ModelItem>
getModelItems()
Retrieves all the model items of this accessorprotected ObservationManager
getObservationManager()
Lazy lookup theObservationManager
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 (search should be case insensitive).Collection<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 Modelvoid
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
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.plugins.core.impl.user.directory.AbstractCachingUserDirectory
dispose, getCacheByLogin, getCacheByMail, getCacheManager, getId, getLabel, getManagedCaches, getParameterValues, getPopulationId, getUserDirectoryModelId, setPopulationId
-
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, hasComputableSize, isCachingEnabled, removeCaches
-
Methods inherited from interface org.ametys.runtime.model.ModelItemAccessor
getChild, getModelItem, hasModelItem
-
Methods inherited from interface org.ametys.core.user.directory.UserDirectory
getId, getLabel, getParameterValues, getPopulationId, getUserDirectoryModelId, isCaseSensitive, setPopulationId
-
-
-
-
Field Detail
-
BASE_PLUGIN_NAME
protected static final String BASE_PLUGIN_NAME
The base plugin (for i18n key)- See Also:
- Constant Field Values
-
_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
-
_manager
protected ServiceManager _manager
The avalon service manager
-
_sourceResolver
protected SourceResolver _sourceResolver
The cocoon source resolver
-
-
Constructor Detail
-
JdbcUserDirectory
public JdbcUserDirectory()
-
-
Method Detail
-
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
- Overrides:
service
in classAbstractCachingUserDirectory
- Throws:
ServiceException
-
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)
-
getCacheTypeLabel
protected String getCacheTypeLabel()
Description copied from class:AbstractCachingUserDirectory
Returns a String identifying the type of this directory, used in the cache screen in _admin, such as 'SQL' or 'LDAP'.- Specified by:
getCacheTypeLabel
in classAbstractCachingUserDirectory
- Returns:
- this directory's type
-
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
- Overrides:
init
in classAbstractCachingUserDirectory
- Parameters:
id
- The non-null and non-empty unique identifierudModelId
- The id of user directory extension pointparamValues
- The parameters' valueslabel
- The optional label
-
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
-
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 (search should be case insensitive).- 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:ModelItemAccessor
Retrieves all the model items of this accessor- Specified by:
getModelItems
in interfaceModelItemAccessor
- 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, boolean isLogin) throws SQLException
Populate the user list with the result set- Parameters:
rs
- The result setisLogin
- true for login, false for email- 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 reflecting 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
-
-