Skip to content

Commit

Permalink
Add users without email support to update profile
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Dec 9, 2023
1 parent 946d864 commit bb9a768
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
15 changes: 10 additions & 5 deletions src/main/html/webapp/components/core/user/profile/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import templateNewTokenDialog from './dialogs/new.stache'
export default Control.extend({

"init": function(element, options) {

this.emailRequired = options.appState.attr('emailRequired');
$(element).hide();
User.findOne({
user: 'me'
}, function(user) {
$(element).html(template({
user: user
user: user,
emailRequired: options.appState.attr('emailRequired')
}));
options.user = user;
$(element).fadeIn();
Expand All @@ -40,8 +41,12 @@ export default Control.extend({

// mail
var mail = $(element).find("[name='mail']");
var mailError = user.checkMail(mail.val());
this.updateControl(mail, mailError);
if (!this.emailRequired && mail.val() != ""){
var mailError = user.checkMail(mail.val());
this.updateControl(mail, mailError);
} else {
this.updateControl(mail, undefined);
}

// password if password is not empty. else no password update on server side
var newPassword = $(element).find("[name='new-password']");
Expand Down Expand Up @@ -218,4 +223,4 @@ export default Control.extend({

}

});
});
24 changes: 20 additions & 4 deletions src/main/java/cloudgene/mapred/api/v2/users/UserProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,37 @@ public Representation post(Representation entity) {
return new JSONAnswer(error, false);
}

error = User.checkMail(mail);
if (error != null) {
return new JSONAnswer(error, false);
boolean mailProvided = (mail != null && !mail.isEmpty());

if (getSettings().isEmailRequired() || mailProvided) {
error = User.checkMail(mail);
if (error != null) {
return new JSONAnswer(error, false);
}
}

UserDao dao = new UserDao(getDatabase());
User newUser = dao.findByUsername(username);
newUser.setFullName(fullname);
newUser.setMail(mail);

if (!user.getMail().equals(newUser.getMail())) {
if (user.getMail() != null && !user.getMail().equals(newUser.getMail())) {
log.info(String.format("User: changed email address for user %s (ID %s)", newUser.getUsername(),
newUser.getId()));
}

if (!getSettings().isEmailRequired()) {
if ((newUser.getMail() == null || newUser.getMail().isEmpty()) && user.hasRole(RegisterUser.DEFAULT_ROLE)) {
newUser.replaceRole(RegisterUser.DEFAULT_ROLE, RegisterUser.DEFAULT_UNTRUSTED_ROLE);
log.info(String.format("User: changed role to %s for user %s (ID %s)", RegisterUser.DEFAULT_UNTRUSTED_ROLE, newUser.getUsername(),
newUser.getId()));
} else if ((newUser.getMail() != null && !newUser.getMail().isEmpty()) && user.hasRole(RegisterUser.DEFAULT_UNTRUSTED_ROLE)) {
newUser.replaceRole(RegisterUser.DEFAULT_UNTRUSTED_ROLE, RegisterUser.DEFAULT_ROLE);
log.info(String.format("User: changed role to %s for user %s (ID %s)", RegisterUser.DEFAULT_ROLE, newUser.getUsername(),
newUser.getId()));
}
}

// update password only when it's not empty
if (newPassword != null && !newPassword.isEmpty()) {

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/cloudgene/mapred/core/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,12 @@ public boolean equals(Object obj) {
return ((User) obj).getUsername().equals(username);
}

public void replaceRole(String oldRole, String newRole) {
for (int i = 0; i < roles.length; i++) {
if (roles[i].equalsIgnoreCase(oldRole)) {
roles[i] = newRole;
return;
}
}
}
}

0 comments on commit bb9a768

Please sign in to comment.