Skip to content
Snippets Groups Projects
Commit 2e5d49aa authored by Martin Stähr's avatar Martin Stähr
Browse files

feat: implemented dummyservice and registration mechanism

parent 139da86c
No related branches found
No related tags found
No related merge requests found
...@@ -112,8 +112,10 @@ public class ManagementImpl implements Management { ...@@ -112,8 +112,10 @@ public class ManagementImpl implements Management {
String name = vereinfachtesResultSet.getString("name"); String name = vereinfachtesResultSet.getString("name");
String password = vereinfachtesResultSet.getString("password"); String password = vereinfachtesResultSet.getString("password");
String email = vereinfachtesResultSet.getString("email"); String email = vereinfachtesResultSet.getString("email");
String rocketChatId = vereinfachtesResultSet.getString("rocketchatid");
Boolean isStudent = vereinfachtesResultSet.getBoolean("isStudent"); Boolean isStudent = vereinfachtesResultSet.getBoolean("isStudent");
return new User(name, password, email, isStudent);
return new User(name, password, email, rocketChatId, isStudent);
} }
@Override @Override
......
...@@ -13,6 +13,8 @@ public class User { ...@@ -13,6 +13,8 @@ public class User {
private String password; private String password;
private String email; private String email;
private String token; private String token;
private String rocketChatAuthToken;
private String rocketChatId;
private Boolean isStudent; private Boolean isStudent;
public User() { public User() {
...@@ -25,6 +27,14 @@ public class User { ...@@ -25,6 +27,14 @@ public class User {
this.isStudent = isStudent; this.isStudent = isStudent;
} }
public User(String name, String password, String email, String rocketChatId, Boolean isStudent) {
this.name = name;
this.password = password;
this.email = email;
this.rocketChatId = rocketChatId;
this.isStudent = isStudent;
}
public String getName() { public String getName() {
return name; return name;
} }
...@@ -66,4 +76,20 @@ public class User { ...@@ -66,4 +76,20 @@ public class User {
public void setStudent(Boolean student) { public void setStudent(Boolean student) {
isStudent = student; isStudent = student;
} }
public String getRocketChatId() {
return rocketChatId;
}
public void setRocketChatId(String rocketChatId) {
this.rocketChatId = rocketChatId;
}
public String getRocketChatAuthToken() {
return rocketChatAuthToken;
}
public void setRocketChatAuthToken(String rocketChatAuthToken) {
this.rocketChatAuthToken = rocketChatAuthToken;
}
} }
package unipotsdam.gf.core.management.user; package unipotsdam.gf.core.management.user;
import unipotsdam.gf.core.management.ManagementImpl; import unipotsdam.gf.core.management.ManagementImpl;
import unipotsdam.gf.core.management.user.User; import unipotsdam.gf.interfaces.ICommunication;
import unipotsdam.gf.interfaces.IMunschkin; import unipotsdam.gf.modules.communication.service.CommunicationDummyService;
import unipotsdam.gf.modules.munchkin.controller.MunchkinImpl;
import unipotsdam.gf.modules.munchkin.model.Munschkin;
import javax.servlet.http.HttpServletRequest; import javax.ws.rs.FormParam;
import javax.servlet.http.HttpServletResponse; import javax.ws.rs.POST;
import javax.ws.rs.*; import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.net.URI; import java.net.URI;
...@@ -19,6 +18,7 @@ public class UserService { ...@@ -19,6 +18,7 @@ public class UserService {
/** /**
* creates a user with given credentials * creates a user with given credentials
*
* @param name * @param name
* @param password * @param password
* @param email * @param email
...@@ -33,14 +33,21 @@ public class UserService { ...@@ -33,14 +33,21 @@ public class UserService {
public Response createUser(@FormParam("name") String name, @FormParam("password") String password, public Response createUser(@FormParam("name") String name, @FormParam("password") String password,
@FormParam("email") String email, @FormParam("isStudent") String isStudent) @FormParam("email") String email, @FormParam("isStudent") String isStudent)
throws URISyntaxException { throws URISyntaxException {
ManagementImpl management = new ManagementImpl(); ManagementImpl management = new ManagementImpl();
User user = new User(name, password, email, isStudent == null); User user = new User(name, password, email, isStudent == null);
return login(true, user); ICommunication iCommunication = new CommunicationDummyService();
boolean chatUserCreated = iCommunication.registerUser(user);
if (chatUserCreated) {
return login(true, user);
} else {
return registrationError();
}
} }
/** /**
* checks if a user exists in order to log him in * checks if a user exists in order to log him in
*
* @param name * @param name
* @param password * @param password
* @param email * @param email
...@@ -56,12 +63,21 @@ public class UserService { ...@@ -56,12 +63,21 @@ public class UserService {
throws URISyntaxException { throws URISyntaxException {
ManagementImpl management = new ManagementImpl(); ManagementImpl management = new ManagementImpl();
User user = new User(name, password, email, null); User user = new User(name, password, email, null);
return login(false, user); ICommunication iCommunication = new CommunicationDummyService();
boolean isLoggedIn = iCommunication.loginUser(user);
if (isLoggedIn) {
return login(false, user);
} else {
return loginError();
}
} }
/** /**
* if create User is true, the user is created and logged in if he does not exist * if create User is true, the user is created and logged in if he does not exist
*
* @param createUser * @param createUser
* @param user * @param user
* @return * @return
...@@ -86,8 +102,19 @@ public class UserService { ...@@ -86,8 +102,19 @@ public class UserService {
} }
} }
private Response registrationError() throws URISyntaxException {
String existsUrl = "../register.jsp?registrationError=true";
return forwardToLocation(existsUrl);
}
private Response loginError() throws URISyntaxException {
String existsUrl = "../index.jsp?loginError=true";
return forwardToLocation(existsUrl);
}
/** /**
* helper function for redirecting to the right project page * helper function for redirecting to the right project page
*
* @param user * @param user
* @param management * @param management
* @return * @return
...@@ -105,7 +132,8 @@ public class UserService { ...@@ -105,7 +132,8 @@ public class UserService {
} }
/** /**
* * helper function for redirecting to a new page * * helper function for redirecting to a new page
*
* @param existsUrl * @param existsUrl
* @return * @return
* @throws URISyntaxException * @throws URISyntaxException
......
package unipotsdam.gf.modules.communication.service;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.interfaces.ICommunication;
import unipotsdam.gf.modules.communication.model.Message;
import unipotsdam.gf.modules.communication.model.chat.ChatMessage;
import unipotsdam.gf.modules.communication.model.chat.ChatRoom;
import java.util.List;
public class CommunicationDummyService implements ICommunication {
@Override
public List<ChatMessage> getChatHistory(String roomId) {
return null;
}
@Override
public boolean sendMessageToChat(Message message) {
return false;
}
@Override
public String createChatRoom(String name, List<User> studentIdentifierList) {
return null;
}
@Override
public boolean addUserToChatRoom(String roomId, User user) {
return false;
}
@Override
public boolean setChatRoomTopic(String roomId, String topic) {
return false;
}
@Override
public ChatRoom getChatRoomInfo(String roomId) {
return null;
}
@Override
public boolean loginUser(User user) {
user.setRocketChatId("1");
user.setRocketChatAuthToken("abc");
return true;
}
@Override
public boolean registerUser(User user) {
user.setRocketChatId("1");
return true;
}
}
...@@ -30,12 +30,23 @@ ...@@ -30,12 +30,23 @@
<button class="btn btn-primary btn-block" type="submit">login</button> <button class="btn btn-primary btn-block" type="submit">login</button>
<!-- scriptlets are terrible. Just tmp for porting the php --> <!-- scriptlets are terrible. Just tmp for porting the php -->
<% <%
String param = request.getParameter("userExists"); String message = "";
if (param != null) {
String userExists = request.getParameter("userExists");
if (userExists != null) {
message = "Nutzer oder Passwort inkorrekt";
}
String loginError = request.getParameter("loginError");
if (loginError != null) {
message = "Login bei RocketChat fehlgeschlagen! Bitte kontaktieren Sie den Administrator";
}
if (!message.isEmpty()) {
try { try {
PrintWriter p = response.getWriter(); PrintWriter p = response.getWriter();
p.println( p.println(
"<div class=\"alert alert-danger\" role=\"alert\"> Nutzer oder Passwort inkorrekt</div>"); "<div class=\"alert alert-danger\" role=\"alert\"> " + message + "</div>");
} finally { } finally {
} }
......
...@@ -43,8 +43,8 @@ ...@@ -43,8 +43,8 @@
<div class="form-group"> <div class="form-group">
<!-- scriptlets are terrible. Just tmp for porting the php --> <!-- scriptlets are terrible. Just tmp for porting the php -->
<%-- <% <%-- <%
String param = request.getParameter("userExists"); String userExists = request.getParameter("userExists");
if (param != null) { if (userExists != null) {
try (PrintWriter p = response.getWriter()) { try (PrintWriter p = response.getWriter()) {
p.println( p.println(
"<div class=\"alert alert-danger\" role=\"alert\"> Es existiert ein Nutzer mit dieser Email oder diesem Benutzernamen! </div>"); "<div class=\"alert alert-danger\" role=\"alert\"> Es existiert ein Nutzer mit dieser Email oder diesem Benutzernamen! </div>");
...@@ -53,12 +53,23 @@ ...@@ -53,12 +53,23 @@
%>--%> %>--%>
<% <%
String param = request.getParameter("userExists"); String message = "";
if (param != null) {
String userExists = request.getParameter("userExists");
if (userExists != null) {
message = "Es existiert ein Nutzer mit dieser Email oder diesem Benutzernamen!";
}
String registrationError = request.getParameter("registrationError");
if (registrationError != null) {
message = "Es ist ein Fehler beim Erstellen des Rocket Chat-Nutzers aufgetreten. Bitte kontaktieren Sie den Administrator";
}
if (!message.isEmpty()) {
try { try {
PrintWriter p = response.getWriter(); PrintWriter p = response.getWriter();
p.println( p.println(
"<div class=\"alert alert-danger\" role=\"alert\"> Es existiert ein Nutzer mit dieser Email oder diesem Benutzernamen! </div>"); "<div class=\"alert alert-danger\" role=\"alert\"> " + message + " </div>");
} finally { } finally {
} }
......
package unipotsdam.gf.modules.communication.service;
import org.junit.Before;
import org.junit.Test;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.interfaces.ICommunication;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class CommunicationDummyServiceTest {
private ICommunication iCommunication;
private User user;
@Before
public void setUp() {
iCommunication = new CommunicationDummyService();
user = new User("name", "password", "email", true);
}
@Test
public void loginUser() {
boolean isLoggedIn = iCommunication.loginUser(user);
assertNotNull(user.getRocketChatId());
assertNotNull(user.getRocketChatAuthToken());
assertTrue(isLoggedIn);
}
@Test
public void registerUser() {
boolean userCreated = iCommunication.registerUser(user);
assertNotNull(user.getRocketChatId());
assertNull(user.getRocketChatAuthToken());
assertTrue(userCreated);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment