From 59036a050007c0cc8942c211343f3ba381a7a939 Mon Sep 17 00:00:00 2001 From: Lukas Forer Date: Sat, 9 Dec 2023 11:53:10 +0100 Subject: [PATCH] Allow to activate user registration without email --- .../mapred/api/v2/server/Server.java | 1 + .../mapred/api/v2/users/RegisterUser.java | 33 +++++--- .../java/cloudgene/mapred/util/Settings.java | 9 +++ .../mapred/api/v2/users/RegisterUserTest.java | 79 +++++++++++++++++++ 4 files changed, 111 insertions(+), 11 deletions(-) diff --git a/src/main/java/cloudgene/mapred/api/v2/server/Server.java b/src/main/java/cloudgene/mapred/api/v2/server/Server.java index 0a20ddfd..dd288468 100644 --- a/src/main/java/cloudgene/mapred/api/v2/server/Server.java +++ b/src/main/java/cloudgene/mapred/api/v2/server/Server.java @@ -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()); diff --git a/src/main/java/cloudgene/mapred/api/v2/users/RegisterUser.java b/src/main/java/cloudgene/mapred/api/v2/users/RegisterUser.java index 667e685e..08bd614b 100644 --- a/src/main/java/cloudgene/mapred/api/v2/users/RegisterUser.java +++ b/src/main/java/cloudgene/mapred/api/v2/users/RegisterUser.java @@ -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) { @@ -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"); @@ -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) { @@ -66,7 +76,7 @@ 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 { @@ -74,7 +84,7 @@ public Representation post(Representation entity) { // 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); @@ -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); diff --git a/src/main/java/cloudgene/mapred/util/Settings.java b/src/main/java/cloudgene/mapred/util/Settings.java index 85cc5600..868a8f74 100644 --- a/src/main/java/cloudgene/mapred/util/Settings.java +++ b/src/main/java/cloudgene/mapred/util/Settings.java @@ -70,6 +70,8 @@ public class Settings { private int maxRunningJobsPerUser = 2; + private boolean emailRequired = true; + private boolean autoRetire = false; private boolean streaming = true; @@ -717,4 +719,11 @@ public String getServerUrl() { return serverUrl; } + public boolean isEmailRequired() { + return emailRequired; + } + + public void setEmailRequired(boolean emailRequired) { + this.emailRequired = emailRequired; + } } \ No newline at end of file diff --git a/src/test/java/cloudgene/mapred/api/v2/users/RegisterUserTest.java b/src/test/java/cloudgene/mapred/api/v2/users/RegisterUserTest.java index 6a4a67c9..073ba33d 100644 --- a/src/test/java/cloudgene/mapred/api/v2/users/RegisterUserTest.java +++ b/src/test/java/cloudgene/mapred/api/v2/users/RegisterUserTest.java @@ -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; @@ -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 { @@ -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", "test-blabla@test.com"); + 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 {