Package org.ametys.plugins.core.user
Class ImportUsers
- java.lang.Object
-
- org.ametys.runtime.plugin.component.AbstractLogEnabled
-
- org.ametys.plugins.core.user.ImportUsers
-
- All Implemented Interfaces:
LogEnabled
,Component
,Serviceable
public class ImportUsers extends AbstractLogEnabled implements Component, Serviceable
Import users from a CSV or text file.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ImportUsers.ImportUserActionException
Internal exception to throw with a i18n message
-
Field Summary
Fields Modifier and Type Field Description private static String[]
_ALLOWED_EXTENSIONS
private static String
_COLUMN_EMAIL
private static String
_COLUMN_FIRSTNAME
private static String
_COLUMN_LASTNAME
private static String
_COLUMN_LOGIN
private static String
_COLUMN_PASSWORD
private static String
_COLUMN_SALT
private static String[]
_COLUMNS
protected CurrentUserProvider
_currentUserProvider
Current User Providerprivate static String
_DEFAULT_HASHED_PASSWORD
private static String
_DEFAULT_PASSWORD_SALT
protected I18nUtils
_i18nUtils
I18n utilsprotected ObservationManager
_observationManager
Observation Managerprotected UserManager
_userManager
User Manager.protected UserDAO
_usersDao
The subscribers DAO.static String
ROLE
Avalon Role
-
Constructor Summary
Constructors Constructor Description ImportUsers()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
_checkCsvHeader(String[] columns, boolean hashedPassword)
Check a line (the header) and throw an exception if not correctprivate String
_parseErrors(Map<String,Errors> errors)
protected List<Map<String,String>>
clearUserDirectory(String populationId, String userDirectoryId, List<UserIdentity> usersToSave)
Remove all users in a userDirectory, avoiding those in usersToSave listMap<String,Object>
importUsers(Part filePart, String populationId, String userDirectoryId, boolean cleanDirectory, boolean hashedPasswords)
Read a file from disk and add users inside in the requested population/directoryprotected Map<String,List<UserIdentity>>
parseFile(BOMInputStream bomIS, String populationId, String userDirectoryId, boolean hashedPassword)
Parse the file to add new users in the directoryvoid
service(ServiceManager smanager)
-
Methods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
-
-
-
Field Detail
-
_ALLOWED_EXTENSIONS
private static final String[] _ALLOWED_EXTENSIONS
-
_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
-
_DEFAULT_HASHED_PASSWORD
private static final String _DEFAULT_HASHED_PASSWORD
- See Also:
- Constant Field Values
-
_DEFAULT_PASSWORD_SALT
private static final String _DEFAULT_PASSWORD_SALT
- See Also:
- Constant Field Values
-
_userManager
protected UserManager _userManager
User Manager.
-
_i18nUtils
protected I18nUtils _i18nUtils
I18n utils
-
_observationManager
protected ObservationManager _observationManager
Observation Manager
-
_currentUserProvider
protected CurrentUserProvider _currentUserProvider
Current User Provider
-
-
Constructor Detail
-
ImportUsers
public ImportUsers()
-
-
Method Detail
-
service
public void service(ServiceManager smanager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Throws:
ServiceException
-
importUsers
public Map<String,Object> importUsers(Part filePart, String populationId, String userDirectoryId, boolean cleanDirectory, boolean hashedPasswords) throws FileNotFoundException, InvalidModificationException
Read a file from disk and add users inside in the requested population/directory- Parameters:
filePart
- filepopulationId
- population IduserDirectoryId
- user directorycleanDirectory
- true to clear every user that was not in the import filehashedPasswords
- true if the passwords are already hashed in the file (salt must be in the file too, and password hashed with "saltPassword" with nothing in-between)- Returns:
- a map containing success, addedCount, existingCount, errorCount, deletedCount, message success : true if success addedCount, existingCount, errorCount : number of imported user added, modified, an error occured (see server log) message : error message if something wrong happened (usually malformed file)
- Throws:
FileNotFoundException
- error reading the fileInvalidModificationException
- error modifying the directory
-
clearUserDirectory
protected List<Map<String,String>> clearUserDirectory(String populationId, String userDirectoryId, List<UserIdentity> usersToSave) throws InvalidModificationException
Remove all users in a userDirectory, avoiding those in usersToSave list- Parameters:
populationId
- population IduserDirectoryId
- directory IdusersToSave
- list of users that need to be saved- Returns:
- list of deleted users (containing login and populationId)
- Throws:
InvalidModificationException
- If modification is not possible
-
parseFile
protected Map<String,List<UserIdentity>> parseFile(BOMInputStream bomIS, String populationId, String userDirectoryId, boolean hashedPassword) throws IOException, ImportUsers.ImportUserActionException
Parse the file to add new users in the directory- Parameters:
bomIS
- input streampopulationId
- population IduserDirectoryId
- directory IdhashedPassword
- true if the password is hashed- Returns:
- a map, containing added/edited/error, each containing the logins in each list
- Throws:
IOException
- if an error occursImportUsers.ImportUserActionException
- file parsing failed
-
_parseErrors
private String _parseErrors(Map<String,Errors> errors)
-
_checkCsvHeader
private void _checkCsvHeader(String[] columns, boolean hashedPassword) throws ImportUsers.ImportUserActionException
Check a line (the header) and throw an exception if not correct- Parameters:
columns
- list of fieldshashedPassword
- is the password in clear text ? (special case for 'salt' column)- Throws:
ImportUsers.ImportUserActionException
- thrown if error
-
-