Skip to content

Commit

Permalink
Allow to activate user registration without email
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Dec 9, 2023
1 parent 995950e commit 59036a0
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/main/java/cloudgene/mapred/api/v2/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Representation getServer() {
data.put("background", getSettings().getColors().get("background"));
data.put("foreground", getSettings().getColors().get("foreground"));
data.put("footer", getWebApp().getTemplate(Template.FOOTER));
data.put("emailRequired", getSettings().isEmailRequired());
if (user != null) {
JSONObject userJson = new JSONObject();
userJson.put("username", user.getUsername());
Expand Down
33 changes: 22 additions & 11 deletions src/main/java/cloudgene/mapred/api/v2/users/RegisterUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
import cloudgene.mapred.util.MailUtil;
import cloudgene.mapred.util.Template;

import java.util.Arrays;

public class RegisterUser extends BaseResource {
private static final Log log = LogFactory.getLog(RegisterUser.class);

public static final String DEFAULT_ROLE = "User";

public static final String DEFAULT_UNTRUSTED_ROLE = "User_Untrusted";

@Post
public Representation post(Representation entity) {

Expand All @@ -27,7 +31,7 @@ public Representation post(Representation entity) {
Form form = new Form(entity);
String username = form.getFirstValue("username");
String fullname = form.getFirstValue("full-name");
String mail = form.getFirstValue("mail").toString();
String mail = form.getFirstValue("mail");
String newPassword = form.getFirstValue("new-password");
String confirmNewPassword = form.getFirstValue("confirm-new-password");

Expand All @@ -41,15 +45,21 @@ public Representation post(Representation entity) {
return new JSONAnswer("Username already exists.", false);
}

// check email
error = User.checkMail(mail);
if (error != null) {
return new JSONAnswer(error, false);
}
if (dao.findByMail(mail) != null) {
return new JSONAnswer("E-Mail is already registered.", false);
boolean mailProvided = (mail != null && !mail.isEmpty());

if (getSettings().isEmailRequired() || mailProvided) {
// check email
error = User.checkMail(mail);
if (error != null) {
return new JSONAnswer(error, false);
}
if (dao.findByMail(mail) != null) {
return new JSONAnswer("E-Mail is already registered.", false);
}
}

String[] roles = new String[] { mailProvided ? DEFAULT_ROLE : DEFAULT_UNTRUSTED_ROLE};

// check password
error = User.checkPassword(newPassword, confirmNewPassword);
if (error != null) {
Expand All @@ -66,15 +76,15 @@ public Representation post(Representation entity) {
newUser.setUsername(username);
newUser.setFullName(fullname);
newUser.setMail(mail);
newUser.setRoles(new String[] { DEFAULT_ROLE });
newUser.setRoles(roles);
newUser.setPassword(HashUtil.hashPassword(newPassword));

try {

// if email server configured, send mails with activation link. Else
// activate user immediately.

if (getSettings().getMail() != null) {
if (getSettings().getMail() != null && mailProvided) {

String activationKey = HashUtil.getActivationHash(newUser);
newUser.setActive(false);
Expand All @@ -95,7 +105,8 @@ public Representation post(Representation entity) {

}

log.info(String.format("Registration: New user %s (ID %s - email %s)", newUser.getUsername(), newUser.getId(), newUser.getMail()));
log.info(String.format("Registration: New user %s (ID %s - email %s - roles %s)", newUser.getUsername(),
newUser.getId(), newUser.getMail(), Arrays.toString(newUser.getRoles())));
MailUtil.notifySlack(getSettings(), "Hi! say hello to " + username + " (" + mail + ") :hugging_face:");

dao.insert(newUser);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/cloudgene/mapred/util/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public class Settings {

private int maxRunningJobsPerUser = 2;

private boolean emailRequired = true;

private boolean autoRetire = false;

private boolean streaming = true;
Expand Down Expand Up @@ -717,4 +719,11 @@ public String getServerUrl() {
return serverUrl;
}

public boolean isEmailRequired() {
return emailRequired;
}

public void setEmailRequired(boolean emailRequired) {
this.emailRequired = emailRequired;
}
}
79 changes: 79 additions & 0 deletions src/test/java/cloudgene/mapred/api/v2/users/RegisterUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;

import cloudgene.mapred.core.User;
import cloudgene.mapred.database.UserDao;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.Form;
Expand Down Expand Up @@ -82,6 +84,12 @@ public void testWithCorrectData() throws JSONException, IOException {
assertEquals("E-Mail is already registered.", object.get("message"));
assertEquals(mailsBefore, mailServer.getReceivedEmailSize());
resource.release();

//check role
UserDao dao = new UserDao(TestServer.getInstance().getDatabase());
User user = dao.findByUsername("usernameunique");
assertEquals(1, user.getRoles().length);
assertEquals(RegisterUser.DEFAULT_ROLE, user.getRoles()[0]);
}

public void testWithEmptyUsername() throws JSONException, IOException {
Expand Down Expand Up @@ -228,6 +236,77 @@ public void testWithEmptyMail() throws JSONException, IOException {
assertTrue(object.get("message").toString().contains("E-Mail is required."));
assertEquals(mailsBefore, mailServer.getReceivedEmailSize());
resource.release();

}

public void testWithEmptyMailAndNoMailRequired() throws JSONException, IOException {

//set email required to false.

TestServer.getInstance().getSettings().setEmailRequired(false);

TestMailServer mailServer = TestMailServer.getInstance();
int mailsBefore = mailServer.getReceivedEmailSize();

Form form = new Form();
form.set("username", "abcdefgh");
form.set("full-name", "abcdefgh abcgd");
form.set("mail", "");
form.set("new-password", "Password27");
form.set("confirm-new-password", "Password27");

// register user
ClientResource resource = createClientResource("/api/v2/users/register");
resource.post(form);
assertEquals(200, resource.getStatus().getCode());
JSONObject object = new JSONObject(resource.getResponseEntity().getText());
assertEquals(object.get("success"), true);
assertEquals("User sucessfully created.", object.get("message"));
assertEquals(mailsBefore, mailServer.getReceivedEmailSize());
resource.release();

TestServer.getInstance().getSettings().setEmailRequired(true);

//check role
UserDao dao = new UserDao(TestServer.getInstance().getDatabase());
User user = dao.findByUsername("abcdefgh");
assertEquals(1, user.getRoles().length);
assertEquals(RegisterUser.DEFAULT_UNTRUSTED_ROLE, user.getRoles()[0]);
}

public void testWithMailAndNoMailRequired() throws JSONException, IOException {

//set email required to false.

TestServer.getInstance().getSettings().setEmailRequired(false);

TestMailServer mailServer = TestMailServer.getInstance();
int mailsBefore = mailServer.getReceivedEmailSize();

Form form = new Form();
form.set("username", "abcdefghi");
form.set("full-name", "abcdefgh abcgd");
form.set("mail", "[email protected]");
form.set("new-password", "Password27");
form.set("confirm-new-password", "Password27");

// register user
ClientResource resource = createClientResource("/api/v2/users/register");
resource.post(form);
assertEquals(200, resource.getStatus().getCode());
JSONObject object = new JSONObject(resource.getResponseEntity().getText());
assertEquals(object.get("success"), true);
assertEquals("User sucessfully created.", object.get("message"));
assertEquals(mailsBefore + 1, mailServer.getReceivedEmailSize());
resource.release();

TestServer.getInstance().getSettings().setEmailRequired(true);

//check role
UserDao dao = new UserDao(TestServer.getInstance().getDatabase());
User user = dao.findByUsername("abcdefghi");
assertEquals(1, user.getRoles().length);
assertEquals(RegisterUser.DEFAULT_ROLE, user.getRoles()[0]);
}

public void testWithWrongMail() throws JSONException, IOException {
Expand Down

0 comments on commit 59036a0

Please sign in to comment.