-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
O3-2761: Add ability to save user email #188
Open
jnsereko
wants to merge
6
commits into
openmrs:master
Choose a base branch
from
jnsereko:O3-2761
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df9ce07
O3-2761: Add ability to save user email
jnsereko 8626164
should not show email field for platform versions that do not support it
jnsereko c01867b
check platform version before setting email field
jnsereko 873d381
email field should noe be required
jnsereko ebbe6bb
email field should noe be required
jnsereko 418c867
remove unnecessary comments
jnsereko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.commons.validator.EmailValidator; | ||
import org.openmrs.Person; | ||
import org.openmrs.PersonName; | ||
import org.openmrs.Provider; | ||
|
@@ -43,6 +44,8 @@ | |
|
||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
@@ -112,6 +115,13 @@ public List<Role> getRoles(WebRequest request) { | |
public String showForm(@RequestParam(required = false, value = "userId") Integer userId, | ||
@RequestParam(required = false, value = "createNewPerson") String createNewPerson, | ||
@ModelAttribute("user") User user, ModelMap model) { | ||
|
||
try { | ||
Field emailField = getEmailField("email", user); | ||
model.addAttribute("email", emailField.get(user)); | ||
} catch (IllegalArgumentException | IllegalAccessException | NullPointerException e) { | ||
log.warn("Email field not available for setting", e); | ||
} | ||
|
||
// the formBackingObject method above sets up user, depending on userId and personId parameters | ||
|
||
|
@@ -157,6 +167,7 @@ public String handleSubmission(WebRequest request, HttpSession httpSession, Mode | |
@RequestParam(required = false, value = "roleStrings") String[] roles, | ||
@RequestParam(required = false, value = "createNewPerson") String createNewPerson, | ||
@RequestParam(required = false, value = "providerCheckBox") String addToProviderTableOption, | ||
@RequestParam(required = false, value = "email") String email, | ||
@ModelAttribute("user") User user, BindingResult errors, HttpServletResponse response) { | ||
|
||
UserService us = Context.getUserService(); | ||
|
@@ -227,6 +238,19 @@ public String handleSubmission(WebRequest request, HttpSession httpSession, Mode | |
} | ||
} | ||
|
||
//check validity of email then save it | ||
if (isEmailValid(email)) { | ||
Field emailField; | ||
try { | ||
emailField = getEmailField("email", user); | ||
emailField.set(user, email); | ||
} catch (IllegalArgumentException | IllegalAccessException | NullPointerException e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't these exceptions thrown and caught within getEmailField? |
||
log.error("Error while setting the email field", e); | ||
} | ||
} else { | ||
log.warn("Invalid or unspecified email: " + (email != null ? "'" + email + "'" : "not provided")); | ||
} | ||
|
||
Set<Role> newRoles = new HashSet<Role>(); | ||
if (roles != null) { | ||
for (String r : roles) { | ||
|
@@ -321,4 +345,27 @@ private Boolean isNewUser(User user) { | |
return user == null ? true : user.getUserId() == null; | ||
} | ||
|
||
/** | ||
* @return true if email is valid or false otherwise | ||
* @param email | ||
*/ | ||
private boolean isEmailValid(String email) { | ||
return StringUtils.hasLength(email) && EmailValidator.getInstance().isValid(email); | ||
} | ||
|
||
/** | ||
* @return an email field | ||
* @param emailFieldName | ||
* @param user | ||
*/ | ||
private Field getEmailField(String emailFieldName, User user) { | ||
jnsereko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
Field emailField = user.getClass().getDeclaredField(emailFieldName); | ||
emailField.setAccessible(true); | ||
return emailField; | ||
} catch (NoSuchFieldException | SecurityException e) { | ||
log.warn("Email field not available for setting", e); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see any value added by the above comment.