Class BasicUserInfo
- All Implemented Interfaces:
IBasicUserInfoDetailNames
- Author:
- Christopher Mindus
Field Summary
Modifier and TypeFieldDescriptionfinal long
The time in milliseconds since the epoch when this instance was created and refreshed.Fields inherited from class com.iizix.user.UserInfoBase
created, email, id, langCodes, MAXIMUM_USER_NAME_LENGTH, userName, userNameLower
Fields inherited from interface com.iizix.user.IBasicUserInfoDetailNames
DETAIL_AdditionalEmail, DETAIL_Address, DETAIL_Address_2, DETAIL_Address_3, DETAIL_City, DETAIL_CompanyDivision, DETAIL_CompanyName, DETAIL_Country, DETAIL_CroppedUploadedProfileImage, DETAIL_DBAdministrationUserName, DETAIL_DeveloperGroupID, DETAIL_DeveloperID, DETAIL_Email, DETAIL_EmailVerify, DETAIL_FirstName, DETAIL_FixedPassword, DETAIL_FullName, DETAIL_HomeFax, DETAIL_HomeMobile, DETAIL_HomePhone, DETAIL_JobTitle, DETAIL_LargeProfileImage, DETAIL_LastName, DETAIL_LocalizedFirstName, DETAIL_LocalizedFullName, DETAIL_LocalizedLastName, DETAIL_LocalizedMiddleName, DETAIL_LocalizedTitle, DETAIL_MiddleName, DETAIL_Phone, DETAIL_Phone_2, DETAIL_PinCode, DETAIL_RecoveryAnswer, DETAIL_RecoveryQuestion, DETAIL_StateProvince, DETAIL_ThumbnailProfileImage, DETAIL_Title, DETAIL_UploadedProfileImage, DETAIL_UploadedProfileImageCrop, DETAIL_WorkFax, DETAIL_WorkMobile, DETAIL_WorkPhone, DETAIL_ZipCode, detailsSet, profilePictureDetailsList
Method Summary
Modifier and TypeMethodDescriptionint
addGroups
(Connection conn, GroupInfo... groups) Adds the groups to the user.Checks if the user is allowed to change his password.changePassword
(String oldPassword, String password) Changes the password for the user without checking if the password entropy is met.static BasicUserInfo
createNewUser
(String userName, String hashPW, boolean changePW, String email, String langCode, boolean hasLoggedIn, Map<String, String> details, String... groupNames) Creates a new user and writes the information to the database.static BasicUserInfo
createNewUser
(Connection conn, String userName, String hashPW, boolean changePW, String email, String langCode, boolean hasLoggedIn, Map<String, String> details, GroupInfo... groups) Creates a new user and writes the information to the database.Gets the cached and current user details.getDetails
(boolean reload) Gets the (cached and current) user details.getDetails
(Connection conn, boolean reload) Gets the user details.Gets the 'disabled' reason, null for user not disabled.int
Gets the 'fail_count' value, zero for never.getGroups
(boolean reload) Gets the groups of this user.long
Gets the 'last_fail' time in milliseconds since the epoch, 0 for never.long
Gets the 'last_login' time in milliseconds since the epoch, 0 for never.Gets all the mails that needs verification.Gets the 'hash_pw', never null (max 120 chars).static long[]
getUserIDs
(Connection conn) Gets all the defined user ID's.getUUID()
Returns the UUID for this user.boolean
Returns if the password must be changed upon next login.boolean
Checks if a new password needs to be provided to the database during current successful login.static BasicUserInfo
newBasicUserInfo
(Connection conn, long id) Constructs the UserInfo by loading the information from the database.static BasicUserInfo
newBasicUserInfo
(Connection conn, String userName, boolean lockForUpdate) Constructs the UserInfo by loading the information from the database.static BasicUserInfo
newBasicUserInfoFromEmail
(Connection conn, String emailAddress) Constructs the UserInfo by loading the information from the database.int
removeGroups
(Connection conn, GroupInfo... groups) Removes the groups from the user.resetPassword
(StringBuilder randomPassword) Resets the password for the user, returning a random password set for the user, and that the user must change password upon next sign in.int
sendMailsToUnverifiedMailAddresses
(List<String> verifyMails, MailSender mailSender, String subject, String fromEmail, String bccEmails, String message, String externalWebServerBaseURL) Helper method to send a mail to all unverified mails for this user.int
setEmailVerified
(String email) Marks an email to be verified.int
setEmailVerified
(Connection conn, String email) Marks an email to be verified.setUserDetail
(Connection conn, String detail, String value) Sets a detail and stores it in the database.toString()
String representation of the class.int
transferEmailDetails
(Map<String, Object> otherDetails) Transfers all email settings such as primary email, optional additional email(s), verified emails, etc, from the details inside this BasicUserInfo instance and another details map.void
updateFailedLogin
(Connection conn, String disabledReason) Updates the information to the database for failed login.void
updateSuccessfulLogin
(Connection conn, String newHashedPassword) Updates the information to the database for a successful login.void
updateSuccessfulLogin
(Connection conn, String newHashedPassword, boolean resetChangePassword) Updates the information to the database for a successful login.void
updateUserDetails
(Map<String, String> detailsMap) Updates user details and stores it in the database.Methods inherited from class com.iizix.user.UserInfoBase
equals
Field Details
refreshTime
public final long refreshTimeThe time in milliseconds since the epoch when this instance was created and refreshed.
Method Details
getUserIDs
Gets all the defined user ID's. With the user ID's, you can then retrieve the users using:long id=...; UserAuthentication userAuth=UserAuthentication.getInstance(); AuthenticatedUser user=userAuth.getUser(id); BasicUserInfo info=user.getBasicUserInfo();
- Parameters:
conn
- The JDBC connection.- Returns:
- The array of User ID's.
- Throws:
SQLException
- For SQL errors.
createNewUser
public static BasicUserInfo createNewUser(String userName, String hashPW, boolean changePW, String email, String langCode, boolean hasLoggedIn, Map<String, String> details, String... groupNames) throws SQLException, NotFoundExceptionCreates a new user and writes the information to the database.Note: The length of a details value should be maximum 3000 characters. If the it exceeds, it will be truncated.
- Parameters:
userName
- The user name: unique, not empty, maximum length 254.hashPW
- The hashed password. It is based on PBKDF2 with Keyed-Hash Message Authentication Code (HMAC) with a SHA-256 hash. It can start with "EXT:
" if the user has signed in using an external Sign In Provider, e.g. LinkedIn or Facebook, the remaining part of the string identifies the provider and the users identifier for that provider.changePW
- Require user to change password upon next login (set tofalse
forEXTERNAL
password hash).email
- Email address, null for none: maximum length 254.langCode
- The selected language code, null for none.hasLoggedIn
- Flag for user is or has logged in (will update last_login time).details
- Details map: keys maximum length 40, values maximum length 300. Setdetails
to null for none.groupNames
- The groups the user should belong to.- Throws:
SQLException
- For SQL errors.NotFoundException
- If a group name is not found.NullPointerException
- IfhashPW
isnull
.IllegalArgumentException
- IfhashPW
is an empty string.
createNewUser
public static BasicUserInfo createNewUser(Connection conn, String userName, String hashPW, boolean changePW, String email, String langCode, boolean hasLoggedIn, Map<String, String> details, GroupInfo... groups) throws SQLException, NotFoundExceptionCreates a new user and writes the information to the database.Note: The length of a details value should be maximum 3000 characters. If the it exceeds, it will be truncated.
- Parameters:
userName
- The user name: unique, not empty, maximum length 254.hashPW
- The hashed password. It is based on PBKDF2 with Keyed-Hash Message Authentication Code (HMAC) with a SHA-256 hash. It can start with "EXTERNAL:
" if the user has signed in using an external Sign In Provider, e.g. LinkedIn or Facebook, the remaining part of the string identifies the provider and the users identifier for that provider.changePW
- Require user to change password upon next login (set tofalse
forEXTERNAL
password hash).email
- Email address, null for none: maximum length 254.langCode
- The selected language code, null for none.hasLoggedIn
- Flag for user is or has logged in (will update last_login time).details
- Details map: keys maximum length 40, values maximum length 300. Setdetails
to null for none.groups
- The groups the user should belong to.- Throws:
SQLException
- For SQL errors.NotFoundException
- If a group name is not found.NullPointerException
- IfhashPW
isnull
.IllegalArgumentException
- IfhashPW
is an empty string.
newBasicUserInfo
public static BasicUserInfo newBasicUserInfo(Connection conn, String userName, boolean lockForUpdate) throws SQLException, NotFoundException Constructs the UserInfo by loading the information from the database.- Parameters:
conn
- The connection.userName
- The user name.lockForUpdate
- Flag indicating the user should be locked for update.- Throws:
SQLException
- For SQL errors.NotFoundException
- If user is not found.
newBasicUserInfo
public static BasicUserInfo newBasicUserInfo(Connection conn, long id) throws SQLException, NotFoundException Constructs the UserInfo by loading the information from the database.- Parameters:
conn
- The connection.id
- The unique ID.- Throws:
SQLException
- For SQL errors.NotFoundException
- If user is not found.
newBasicUserInfoFromEmail
public static BasicUserInfo newBasicUserInfoFromEmail(Connection conn, String emailAddress) throws SQLException, NotFoundException Constructs the UserInfo by loading the information from the database.- Parameters:
conn
- The connection.emailAddress
- The email address to look-up.- Throws:
SQLException
- For SQL errors.NotFoundException
- If user is not found.
toString
String representation of the class.getPasswordHash
Gets the 'hash_pw', never null (max 120 chars). It is based on PBKDF2 with Keyed-Hash Message Authentication Code (HMAC) with a SHA-256 hash. It can start with "EXT:
" if the user has signed in using an external Sign In Provider, e.g. LinkedIn or Facebook, the remaining part of the string identifies the provider and the users identifier for that provider.- Returns:
- A string with a hashed password, or "
EXTERNAL
" if the user has signed in using an external Sign In Provider, e.g. LinkedIn or Facebook.
mustChangePassword
public boolean mustChangePassword()Returns if the password must be changed upon next login.- Specified by:
mustChangePassword
in classUserInfoBase
- Returns:
- Flag 'change_pw' to change the password.
canChangePassword
Checks if the user is allowed to change his password. Note that this does not check if the user id disabled: please do so usinggetDisabledReason()
.- Specified by:
canChangePassword
in classUserInfoBase
- Returns:
null
if allowed and user can change his password, otherwise aUserLocaleString
instance for the reason of the password being fixed and that it can never be changed.
getLastLogin
public long getLastLogin()Gets the 'last_login' time in milliseconds since the epoch, 0 for never.- Specified by:
getLastLogin
in classUserInfoBase
getLastFail
public long getLastFail()Gets the 'last_fail' time in milliseconds since the epoch, 0 for never.- Specified by:
getLastFail
in classUserInfoBase
getFailCount
public int getFailCount()Gets the 'fail_count' value, zero for never.- Specified by:
getFailCount
in classUserInfoBase
getDisabledReason
Gets the 'disabled' reason, null for user not disabled.- Specified by:
getDisabledReason
in classUserInfoBase
- Returns:
- The user 'disabled' reason, or
null
for user not disabled.
getUUID
Returns the UUID for this user.- Returns:
- The UUID of this user based of the ID, user name and email.
needsNewHashedPassword
public boolean needsNewHashedPassword()Checks if a new password needs to be provided to the database during current successful login. As the password is available, it makes it possible to save a new hash with perhaps new algorithms. This makes it very hard to crack.- Returns:
- true if 'failCount' is larger than 3 or 'disabledReason' is present. true is also returned if 'lastLogin' is longer than a week from current time. false is otherwise returned.
updateSuccessfulLogin
Updates the information to the database for a successful login.The connection must be in auto-commit "off" and have the user selected for update.
If it is required, the password hash is updated. This is done every week or after a failure. This method will also reset the disabled status.
- Parameters:
conn
- The connection.newHashedPassword
- A new hashed password to update the entry with, null for no change.- Throws:
SQLException
- For SQL errors.
updateSuccessfulLogin
public void updateSuccessfulLogin(Connection conn, String newHashedPassword, boolean resetChangePassword) throws SQLException Updates the information to the database for a successful login.The connection must be in auto-commit "off" and have the user selected for update.
If it is required, the password hash is updated. This is done every week or after a failure. This method will also reset the disabled status.
- Parameters:
conn
- The connection.newHashedPassword
- A new hashed password to update the entry with, null for no change.resetChangePassword
- Flag to also reset the changePW (only used when newHashedPassword is specified).- Throws:
SQLException
- For SQL errors.
updateFailedLogin
public void updateFailedLogin(Connection conn, String disabledReason) throws SQLException, NotFoundException Updates the information to the database for failed login.The connection must be in auto-commit "off" and have the user selected for update.
- Parameters:
conn
- The connection.disabledReason
- The reason to disable the user, null for no change.- Throws:
SQLException
- For SQL errors.NotFoundException
- If the user has been deleted since login.
changePassword
public UserLocaleString changePassword(String oldPassword, String password) throws SQLException, NotFoundException Changes the password for the user without checking if the password entropy is met.- Parameters:
oldPassword
- The old password.password
- The new password.- Returns:
null
for success, otherwise the error enum value.- Throws:
NullPointerException
- If the one of the passwords is null.SQLException
- For SQL errors.NotFoundException
- If the user has been deleted since login.
resetPassword
public UserLocaleString resetPassword(StringBuilder randomPassword) throws SQLException, NotFoundException Resets the password for the user, returning a random password set for the user, and that the user must change password upon next sign in.- Parameters:
randomPassword
- The StringBuilder instance that is filled in with the generated random password meeting the highest password entropy configurable.- Returns:
null
for success, otherwise the error reason.- Throws:
SQLException
- For SQL errors.NotFoundException
- If the user has been deleted since login.
getGroups
Gets the groups of this user.- Parameters:
reload
- Forces reload of the groups.- Returns:
- The array of group informations.
- Throws:
NotFoundException
- If a group is not found.SQLException
- For SQL errors.
addGroups
Adds the groups to the user.- Parameters:
conn
- The connection.groups
- The groups to add.- Returns:
- Count of added groups (if already added before).
- Throws:
SQLException
- For SQL errors.
removeGroups
Removes the groups from the user.- Parameters:
conn
- The connection.groups
- The groups to add.- Returns:
- Count of removed groups (if not present before).
- Throws:
SQLException
- For SQL errors.
getDetails
Gets the cached and current user details. Details are e.g. fax number, profile picture.- Specified by:
getDetails
in classUserInfoBase
- Returns:
- A new read-only copy of the details with name as key and the value for it.
getDetails
Gets the (cached and current) user details. Details are e.g. fax number, profile picture.Note: If you have a JDBC connection, use
getDetails(Connection, boolean)
instead for efficiency.- Specified by:
getDetails
in classUserInfoBase
- Parameters:
reload
- Reload flag from database.- Returns:
- If reload is false, a cached read-only map with the details name as key and the value for it, otherwise a fresh copy of the details from the database.
- Throws:
SQLException
- For SQL errors.
getDetails
Gets the user details. Details are e.g. fax number, profile picture. If not previously loaded, the "reload" flag is ignored and the details are loaded.- Specified by:
getDetails
in classUserInfoBase
- Parameters:
conn
- The connection, null for cached values only.reload
- Reload flag, i.e. instructs a full reload of all details from the database.- Returns:
- A cached read-only map with the details name as key and the value for it.
- Throws:
SQLException
- For SQL errors.
updateUserDetails
Updates user details and stores it in the database. A detail value can be added and removed. It is added if the entry is not present previously, updated if the entry did exist and the value is non-null, removed if the value is null.The cached details map are maintained for the user in a map, but outside updates to the database will not reflect changes to this.
The exceptions thrown in this method may occur during the processing of changes. The database will be rolled back, however, the cached copy might be partially updated.
- Parameters:
detailsMap
- The map with details to change, keys are the detail names and the values are the new respective detail value. If the detail value is null, the detail is deleted.The detail following names are ignored:
IBasicUserInfoDetailNames.DETAIL_UploadedProfileImage
,IBasicUserInfoDetailNames.DETAIL_LargeProfileImage
,IBasicUserInfoDetailNames.DETAIL_ThumbnailProfileImage
, andIBasicUserInfoDetailNames.DETAIL_DBAdministrationUserName
.
These names are used in conjunction with the License System.
- Throws:
SQLException
- For SQL errors.NullPointerException
- Ifdetail
is null.
setUserDetail
Sets a detail and stores it in the database. A detail value can be added and removed.Details are maintained for the user in a map, but outside updates to the database will not reflect changes to this.
Note: The length of the detail value should be maximum 3000 characters. If the it exceeds, it will be truncated.
- Parameters:
conn
- The connection.detail
- The detail name.value
- The value, null to delete the detail.- Returns:
- The previous value (if all details are previously loaded), null otherwise. If the detail name should not be stored in the details (see
detail
) parameter, null is returned and nothing is performed. - Throws:
SQLException
- For SQL errors.NullPointerException
- Ifdetail
is null.
setEmailVerified
Marks an email to be verified. The details like DETAIL_VerifyEmail[-nn] are checked and the appropriate one is removed.- Parameters:
email
- The mail verified.- Returns:
- 1 for successful removal of verification, 0 if already verified, -1 if email is not registered with the user, or -2 for database SQL errors (already logged).
- Throws:
NullPointerException
- Ifemail
is null.
setEmailVerified
Marks an email to be verified. The details like DETAIL_VerifyEmail[-nn] are checked and the appropriate one is removed.- Parameters:
conn
- The connection.email
- The mail verified.- Returns:
- 1 for successful removal of verification, 0 if already verified, or -1 if email is not registered with the user.
- Throws:
SQLException
- For SQL errors.NullPointerException
- Ifemail
is null.
getMailsToVerify
Gets all the mails that needs verification. The current details are used, so if you need a fresh copy, update them using @{linkgetDetails(boolean)
or preferablegetDetails(Connection, boolean)
if you have a database connection.- Returns:
- The list of alphabetically sorted mails to verify.
sendMailsToUnverifiedMailAddresses
public int sendMailsToUnverifiedMailAddresses(List<String> verifyMails, MailSender mailSender, String subject, String fromEmail, String bccEmails, String message, String externalWebServerBaseURL) throws NotFoundException, IOException, IllegalStateException, MessagingException Helper method to send a mail to all unverified mails for this user. The mail will be sent as plain text UTF-8 encoded mail.The
message
parameter is a formatted message that should contain two tags:- ${url} required tag that will contain the URL that the user must open in order to verify the mail.
- ${email} optional tag that will be replaced with the email that needs verification.
- Parameters:
verifyMails
- The mails to verify, retrieved fromgetMailsToVerify()
.mailSender
- The mail sender, null to get the default mail sender.subject
- Subject that can contain the tag${mail}
.fromEmail
- From email address.bccEmails
- BCC email(s) [comma separated for multiple],null
for none.message
- The mail message to send.externalWebServerBaseURL
- The external base URL for the web server, e.g. "https://lic.mindus.co", usenull
for default. The default address is retrieved usingServerShell.getExternalWebServerBaseURL()
.- Returns:
- Count of mails queued for sending.
- Throws:
NotFoundException
- IfmailSender
cannot not be found (and the parametermailSender
, or if theexternalWebServerBaseURL
isnull
and the methodServerShell.getExternalWebServerBaseURL()
returnsnull
.IOException
- If the mail queue directory does not exist.MessagingException
- For messaging errors.IllegalStateException
- For illegal state when creating a mail to queue, or if the server is not started.
transferEmailDetails
Transfers all email settings such as primary email, optional additional email(s), verified emails, etc, from the details inside this BasicUserInfo instance and another details map.- Parameters:
otherDetails
- Other details map to update.- Returns:
- The count of changed values (new value or removed value).