Skip to content

Commit

Permalink
Update user registration to handle optional email
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Dec 10, 2023
1 parent 79b2b4e commit 5ada7f6
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 41 deletions.
37 changes: 33 additions & 4 deletions src/main/html/webapp/components/core/user/profile/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,37 @@ 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,
anonymousAccount: (!options.appState.attr('emailRequired')),
emailProvided: (user.attr('mail') != "" && user.attr('mail') != undefined),
userEmailDescription: options.appState.attr('userEmailDescription'),
userWithoutEmailDescription: options.appState.attr('userWithoutEmailDescription')
}));
options.user = user;
$(element).fadeIn();
});
},

"#anonymous click" : function(){
if (!this.emailRequired){
var anonymousControl = $(this.element).find("[name='anonymous']");
var anonymous = !anonymousControl.is(':checked');
var mail = $(this.element).find("[name='mail']");
if (anonymous){
mail.attr('disabled','disabled');
} else {
mail.removeAttr('disabled');
}
mail.val("");
}
},

'submit': function(element, event) {
event.preventDefault();
var user = new User();
Expand All @@ -39,10 +58,20 @@ export default Control.extend({
var fullnameError = user.checkName(fullname.val());
this.updateControl(fullname, fullnameError);

var anonymous = false;
if (!this.emailRequired){
var anonymousControl = $(this.element).find("[name='anonymous']");
anonymous = !anonymousControl.is(':checked');
}

// mail
var mail = $(element).find("[name='mail']");
var mailError = user.checkMail(mail.val());
this.updateControl(mail, mailError);
if (!anonymous){
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 @@ -204,4 +233,4 @@ export default Control.extend({

}

});
});
30 changes: 27 additions & 3 deletions src/main/html/webapp/components/core/user/profile/profile.stache
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,34 @@ Please fill out the form below to change your account settings or your password.
<div class="invalid-feedback"></div>
</div>

{{#is(anonymousAccount, false)}}

<div class="form-group">
<label for="mail" class="control-label">E-Mail:</label>
<input id="mail" name="mail" type="text" value="{{user.mail}}" class="form-control col-sm-3">
<div class="invalid-feedback"></div>
</div>

{{else}}
<hr>
<div class="form-group">
<input type="checkbox" id="anonymous" name="anonymous" value="0" {{#emailProvided}}checked{{/emailProvided}}> <label for="anonymous" class="control-label">E-Mail Address</label>
<div class="form-group" style="margin-left: 30px;">
<p>
{{userEmailDescription}}
</p>
<label for="mail" class="control-label">E-Mail:</label>
<input id="mail" name="mail" type="text" class="form-control col-sm-3" autocomplete="off" {{#is(emailProvided, false)}}disabled{{/is}} value="{{user.mail}}">
<div class="invalid-feedback"></div>
<p><br>
{{userWithoutEmailDescription}}
</p>
</div>
</div>
<hr>

{{/is}}

<h4>Change password</h4>

<div class="form-group">
Expand All @@ -41,9 +63,9 @@ Please fill out the form below to change your account settings or your password.

</div>
</form>

<br>
<hr>

<br>
<h3>API Access</h3>

<p>This service provides a rich RestAPI to submit, monitor and download jobs.</p>
Expand All @@ -59,7 +81,7 @@ Please fill out the form below to change your account settings or your password.
<small class="{{#is(../user.apiTokenValid, false)}}text-danger{{#is}}">{{../user.apiTokenMessage}}</small>
{{else}}
<button class="btn btn-primary" id="create_token">Create API Token</button>
Expires in
Expires in
<select id="token_expiration">
<option value="30">30 days</option>
<option value="60">60 days</option>
Expand All @@ -69,7 +91,9 @@ Please fill out the form below to change your account settings or your password.

{{/user.hasApiToken}}

<br>
<hr>
<br>

<h3>Delete Account</h3>

Expand Down
45 changes: 42 additions & 3 deletions src/main/html/webapp/components/core/user/signup/signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,52 @@ import template from './signup.stache';
export default Control.extend({

"init": function(element, options) {
this.emailRequired = options.appState.attr('emailRequired');
$(element).hide();
$(element).html(template());
$(element).html(template({
emailRequired: options.appState.attr('emailRequired'),
userEmailDescription: options.appState.attr('userEmailDescription'),
userWithoutEmailDescription: options.appState.attr('userWithoutEmailDescription')
}));
$(element).fadeIn();
},


"#anonymous1 click" : function(){
this.updateEmailControl();
},

"#anonymous2 click" : function(){
this.updateEmailControl();
},

"updateEmailControl": function() {
if (!this.emailRequired){
var anonymousControl = $(this.element).find("[name='anonymous']:checked");
var anonymous = (anonymousControl.val() == "1");
var mail = $(this.element).find("[name='mail']");
if (anonymous){
mail.attr('disabled','disabled');
} else {
mail.removeAttr('disabled');
}
}
},

'submit': function(element, event) {
event.preventDefault();

var that = this;
var user = new User();

// anonymous radiobutton
var anonymous = false;

if (!this.emailRequired){
var anonymousControl = $(element).find("[name='anonymous']:checked");
anonymous = (anonymousControl.val() == "1");
}

// username
var username = $(element).find("[name='username']");
var usernameError = user.checkUsername(username.val());
Expand All @@ -32,8 +67,12 @@ export default Control.extend({

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

// password
var newPassword = $(element).find("[name='new-password']");
Expand Down
31 changes: 29 additions & 2 deletions src/main/html/webapp/components/core/user/signup/signup.stache
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
<br>

<div class="alert alert-success" id="success-message" style="display: none;">
<b>Well done!</b> An email including the activation code has been sent to your address.
<br>
{{#is(emailRequired, true)}}
<b>Well done!</b> An email including the activation code has been sent to your address.
{{else}}
<b>Well done!</b> Your account is now active. <a href="./">Login now</a>.
{{/is}}
<br>
</div>

<form id="signon-form" class="form-horizontal" autocomplete="off">
Expand All @@ -21,13 +25,36 @@
<div class="invalid-feedback"></div>
</div>

{{#is(emailRequired, true)}}

<div class="form-group">
<label for="mail" class="control-label">E-Mail:</label>
<input id="mail" name="mail" type="text" class="form-control col-sm-3" autocomplete="off">
<div class="invalid-feedback"></div>
</div>

{{else}}
<hr>
<div class="form-group">
<input type="radio" id="anonymous1" name="anonymous" value="0" checked> <label for="anonymous1" class="control-label">E-Mail Address</label>
<div class="form-group" style="margin-left: 30px;">
<p>
{{userEmailDescription}}
</p>
<label for="mail" class="control-label">E-Mail:</label>
<input id="mail" name="mail" type="text" class="form-control col-sm-3" autocomplete="off">
<div class="invalid-feedback"></div>
</div>
<input type="radio" id="anonymous2" name="anonymous" value="1"> <label for="anonymous2" class="control-label">I don't want to provide my email address</label>
<div class="form-group" style="margin-left: 30px;">
<p>
{{userWithoutEmailDescription}}
</p>
</div>
</div>
<hr>
{{/is}}

<div class="form-group">
<label for="new-password" class="control-label">Password:</label>
<input id="new-password" name="new-password" type="password" class="form-control col-sm-3" autocomplete="off">
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/cloudgene/mapred/core/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class Template {
public static final String FOOTER_SUBMIT_JOB = "FOOTER_SUBMIT_JOB";

public static final String TERMS = "TERMS";

public static final String USER_EMAIL_DESCRIPTION = "USER_EMAIL_DESCRIPTION";

public static final String USER_WITHOUT_EMAIL_DESCRIPTION = "USER_WITHOUT_EMAIL_DESCRIPTION";

public static final Template[] SNIPPETS = new Template[] {

Expand All @@ -42,7 +46,11 @@ public class Template {
new Template(FOOTER_SUBMIT_JOB, ""),

new Template(TERMS, "I will not attempt to re-identify or contact research participants.<br>" +
"I will report any inadvertent data release, security breach or other data management incident of which I become aware.")
"I will report any inadvertent data release, security breach or other data management incident of which I become aware."),

new Template(USER_EMAIL_DESCRIPTION, "Receive email notifications when jobs are completed."),

new Template(USER_WITHOUT_EMAIL_DESCRIPTION, "You can enter your email address at any time to upgrade your account.")

};

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/cloudgene/mapred/core/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ public boolean hasRole(String role) {
return false;
}

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

public boolean isAdmin() {
return hasRole(ROLE_ADMIN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public UserResponse get(Authentication authentication, String user2) {
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Secured(SecurityRule.IS_AUTHENTICATED)
public HttpResponse<MessageResponse> update(Authentication authentication, String user2, @Nullable String username,
@Parameter("full-name") String full_name, String mail, @Parameter("new-password") String new_password, @Parameter("confirm-new-password") String confirm_new_password) {
@Parameter("full-name") String full_name, @Nullable String mail,
@Nullable @Parameter("new-password") String new_password,
@Nullable @Parameter("confirm-new-password") String confirm_new_password) {

User user = authenticationService.getUserByAuthentication(authentication);

Expand Down Expand Up @@ -154,8 +156,8 @@ public HttpResponse<MessageResponse> resetPassword(@Nullable String username) {
@Post("/api/v2/users/register")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Secured(SecurityRule.IS_ANONYMOUS)
public HttpResponse<MessageResponse> register(String username, @Parameter("full-name") String full_name, String mail,
@Parameter("new-password") String new_password, @Parameter("confirm-new-password") String confirm_new_password) {
public HttpResponse<MessageResponse> register(String username, @Parameter("full-name") String full_name, @Nullable String mail,
@Nullable @Parameter("new-password") String new_password, @Nullable @Parameter("confirm-new-password") String confirm_new_password) {

MessageResponse response = userService.registerUser(username, mail, new_password, confirm_new_password,
full_name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public String getRoot(User user) {
data.put("background", application.getSettings().getColors().get("background"));
data.put("foreground", application.getSettings().getColors().get("foreground"));
data.put("footer", application.getTemplate(Template.FOOTER));
data.put("emailRequired", application.getSettings().isEmailRequired());
data.put("userEmailDescription", application.getTemplate(Template.USER_EMAIL_DESCRIPTION));
data.put("userWithoutEmailDescription", application.getTemplate(Template.USER_WITHOUT_EMAIL_DESCRIPTION));

List<String> authClients = new Vector<String>();
for (OauthClientConfigurationProperties client : clients) {
Expand Down
Loading

0 comments on commit 5ada7f6

Please sign in to comment.