Skip to content
Snippets Groups Projects
Commit 508e415c authored by Martin Staehr's avatar Martin Staehr
Browse files

#63 feat: implement registration on rocketChat platform

parent 498667c3
No related branches found
No related tags found
No related merge requests found
Showing
with 192 additions and 31 deletions
......@@ -9,6 +9,6 @@ public class GFRocketChatConfig {
public static final String ADMIN_USERNAME = "";
public static final String ADMIN_TOKEN = "";
public static final User TEST_USER = new User("username", "password", "email",
"rocketChatUserName", false);
public static final User TEST_USER = new User("Test Nachname", "NRywSvBGZy6kp4448", "test@gmail.com",
"TestNachname", false);
}
......@@ -69,6 +69,14 @@ public class UserDAO {
return result;
}
public boolean existsByRocketChatUsername(String rocketChatUsername) {
connect.connect();
String mysqlRequest = "SELECT * FROM users where rocketChatUsername = ?";
VereinfachtesResultSet resultSet = connect.issueSelectStatement(mysqlRequest, rocketChatUsername);
connect.close();
return resultSet.next();
}
public List<User> getUsersByProjectId(String projectId) {
connect.connect();
String query =
......
......@@ -2,6 +2,7 @@ package unipotsdam.gf.core.states;
import unipotsdam.gf.core.database.mysql.MysqlConnect;
import unipotsdam.gf.core.management.project.Project;
import unipotsdam.gf.core.management.user.UserDAO;
import unipotsdam.gf.core.states.model.ConstraintsMessages;
import unipotsdam.gf.core.states.model.ProjectPhase;
import unipotsdam.gf.interfaces.Feedback;
......@@ -34,7 +35,9 @@ public class PhasesImpl implements IPhases {
private Feedback feedback = new DummyFeedback();
private ICommunication iCommunication = new CommunicationDummyService(new UnirestService());
private UserDAO userDAO = new UserDAO(new MysqlConnect());
private ICommunication iCommunication = new CommunicationDummyService(new UnirestService(), userDAO);
private IJournal iJournal = new IJournalImpl();
......
......@@ -78,8 +78,9 @@ public interface ICommunication {
boolean loginUser(User user);
/**
* api 1: https://rocket.chat/docs/developer-guides/rest-api/users/generatepersonalaccesstoken/
* api 2: https://rocket.chat/docs/developer-guides/rest-api/users/getpersonalaccesstokens/
* api 1: https://rocket.chat/docs/developer-guides/rest-api/users/register/
* api 2: https://rocket.chat/docs/developer-guides/rest-api/users/generatepersonalaccesstoken/
* api 3: https://rocket.chat/docs/developer-guides/rest-api/users/getpersonalaccesstokens/
*
* registers new user to rocket chat
*
......
package unipotsdam.gf.modules.communication;
import unipotsdam.gf.core.database.mysql.MysqlConnect;
import unipotsdam.gf.core.management.user.UserDAO;
import unipotsdam.gf.interfaces.ICommunication;
import unipotsdam.gf.modules.communication.service.CommunicationDummyService;
import unipotsdam.gf.modules.communication.service.UnirestService;
......@@ -22,7 +24,8 @@ public class ChatWindow extends SimpleTagSupport {
String groupToken = request.getParameter("groupToken");
String projectToken = request.getParameter("projectToken");
//get ProjetbyToken
ICommunication communicationService = new CommunicationDummyService(new UnirestService());
UserDAO userDAO = new UserDAO(new MysqlConnect());
ICommunication communicationService = new CommunicationDummyService(new UnirestService(), userDAO);
String chatRoomLink = communicationService.getChatRoomLink(token, projectToken, groupToken);
JspWriter out = getJspContext().getOut();
......
......@@ -2,7 +2,7 @@ package unipotsdam.gf.modules.communication.model.rocketChat;
import java.util.Map;
public class RocketChatResponse {
public class RocketChatLoginResponse {
private String status;
......@@ -47,7 +47,7 @@ public class RocketChatResponse {
@Override
public String toString() {
return "RocketChatResponse{" +
return "RocketChatLoginResponse{" +
"status='" + status + '\'' +
", data=" + data +
", error='" + error + '\'' +
......
package unipotsdam.gf.modules.communication.model.rocketChat;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.logging.log4j.util.Strings;
import java.util.Map;
public class RocketChatRegisterResponse {
private Map user;
private String error;
private boolean successful;
public Map getUser() {
return user;
}
public void setUser(Map user) {
this.user = user;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
@JsonProperty("success")
public boolean isSuccessful() {
return successful;
}
public void setSuccessful(boolean successful) {
this.successful = successful;
}
public String getUserId() {
if (!isSuccessful()) {
// TODO: throw error
return Strings.EMPTY;
}
return user.get("_id").toString();
}
public boolean isEmailExistError() {
boolean emailError = false;
if (!isSuccessful()) {
emailError = error.contains("Email already exists.");
}
return emailError;
}
}
......@@ -3,16 +3,17 @@ package unipotsdam.gf.modules.communication.service;
import io.github.openunirest.http.HttpResponse;
import unipotsdam.gf.assignments.Assignee;
import unipotsdam.gf.assignments.NotImplementedLogger;
import unipotsdam.gf.config.GFRocketChatConfig;
import unipotsdam.gf.core.management.project.Project;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.core.management.user.UserDAO;
import unipotsdam.gf.core.states.model.ConstraintsMessages;
import unipotsdam.gf.interfaces.ICommunication;
import unipotsdam.gf.modules.assessment.controller.model.StudentIdentifier;
import unipotsdam.gf.modules.communication.model.Message;
import unipotsdam.gf.modules.communication.model.chat.ChatMessage;
import unipotsdam.gf.modules.communication.model.chat.ChatRoom;
import unipotsdam.gf.modules.communication.model.rocketChat.RocketChatResponse;
import unipotsdam.gf.modules.communication.model.rocketChat.RocketChatLoginResponse;
import unipotsdam.gf.modules.communication.model.rocketChat.RocketChatRegisterResponse;
import javax.annotation.ManagedBean;
import javax.annotation.Resource;
......@@ -21,21 +22,26 @@ import javax.inject.Singleton;
import javax.ws.rs.core.Response;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static unipotsdam.gf.config.GFRocketChatConfig.ROCKET_CHAT_API_LINK;
@Resource
@ManagedBean
@Singleton
public class CommunicationDummyService implements ICommunication {
private UnirestService unirestService;
private UserDAO userDAO;
@Inject
public CommunicationDummyService(UnirestService unirestService) {
public CommunicationDummyService(UnirestService unirestService, UserDAO userDAO) {
this.unirestService = unirestService;
this.userDAO = userDAO;
}
@Override
......@@ -90,7 +96,7 @@ public class CommunicationDummyService implements ICommunication {
@Override
public boolean loginUser(User user) {
if (user.getEmail().isEmpty() || user.getPassword().isEmpty()) {
if (hasEmptyParameter(user.getEmail(), user.getPassword())) {
return false;
}
......@@ -98,30 +104,61 @@ public class CommunicationDummyService implements ICommunication {
rocketChatAuth.put("user", user.getEmail());
rocketChatAuth.put("password", user.getPassword());
HttpResponse<RocketChatResponse> response =
HttpResponse<RocketChatLoginResponse> response =
unirestService
.post(GFRocketChatConfig.ROCKET_CHAT_API_LINK + "login")
.post(ROCKET_CHAT_API_LINK + "login")
.body(rocketChatAuth)
.asObject(RocketChatResponse.class);
.asObject(RocketChatLoginResponse.class);
int status = response.getStatus();
if (status == Response.Status.UNAUTHORIZED.getStatusCode() || status == Response.Status.BAD_REQUEST.getStatusCode()) {
return false;
}
RocketChatResponse rocketChatResponse = response.getBody();
user.setRocketChatUserId(rocketChatResponse.getUserId());
user.setRocketChatAuthToken(rocketChatResponse.getAuthToken());
RocketChatLoginResponse rocketChatLoginResponse = response.getBody();
user.setRocketChatUserId(rocketChatLoginResponse.getUserId());
user.setRocketChatAuthToken(rocketChatLoginResponse.getAuthToken());
return true;
}
@Override
public boolean registerUser(User user) {
// register User
// login User and fill information
if (hasEmptyParameter(user.getEmail(), user.getName(), user.getPassword())) {
return false;
}
HashMap<String, String> rocketChatRegister = new HashMap<>();
String rocketChatUsername = createRocketChatUsername(user);
rocketChatRegister.put("username", rocketChatUsername);
rocketChatRegister.put("email", user.getEmail());
rocketChatRegister.put("pass", user.getPassword());
rocketChatRegister.put("name", user.getName());
// create custom access token with authToken
user.setRocketChatUserId("1");
return true;
HttpResponse<RocketChatRegisterResponse> response =
unirestService
.post(ROCKET_CHAT_API_LINK + "users.register")
.body(rocketChatRegister)
.asObject(RocketChatRegisterResponse.class);
if (response.getStatus() == Response.Status.BAD_REQUEST.getStatusCode()) {
return false;
}
RocketChatRegisterResponse registerResponse = response.getBody();
if (!registerResponse.isSuccessful()) {
return false;
}
user.setRocketChatUsername(rocketChatUsername);
user.setRocketChatUserId(registerResponse.getUserId());
if (!loginUser(user)) {
// TODO: eventually consider roleback because of error or something
return false;
}
return createCustomAccessToken(user);
}
@Override
......@@ -139,7 +176,7 @@ public class CommunicationDummyService implements ICommunication {
// TODO: Implement getProjectbyToken and getGroupByToken
//Project project = managementService.getProject(projectToken
String channelName = "general";
return GFRocketChatConfig.ROCKET_CHAT_API_LINK + "/channel/" + channelName + "?layout=embedded";
return ROCKET_CHAT_API_LINK + "/channel/" + channelName + "?layout=embedded";
}
@Override
......@@ -166,4 +203,49 @@ public class CommunicationDummyService implements ICommunication {
public User getUser() {
return new User("Martin Stähr", "test", "test@test.com", true);
}
private boolean hasEmptyParameter(String... parameters) {
return Arrays.stream(parameters).anyMatch(String::isEmpty);
}
private String createRocketChatUsername(User user) {
// TODO: eventually add username to normal registration
String possibleUsername = user.getName().replaceAll(" ", "");
int counter = 1;
while (userDAO.existsByRocketChatUsername(possibleUsername)) {
possibleUsername = user.getName().replaceAll(" ", "") + counter;
counter++;
}
return possibleUsername;
}
private boolean createCustomAccessToken(User user) {
if (hasEmptyParameter(user.getRocketChatAuthToken(), user.getRocketChatUserId())) {
return false;
}
HashMap<String, String> bodyMap = new HashMap<>();
bodyMap.put("tokenName", "fltrailToken");
HashMap<String, String> headerMap = new HashMap<>();
headerMap.put("X-Auth-Token", user.getRocketChatAuthToken());
headerMap.put("X-User-Id", user.getRocketChatUserId());
HttpResponse<Map> response = unirestService
.post(ROCKET_CHAT_API_LINK + "users.generatePersonalAccessToken")
.headers(headerMap)
.body(bodyMap)
.asObject(Map.class);
if (response.getStatus() == Response.Status.BAD_REQUEST.getStatusCode()) {
return false;
}
Map responseBody = response.getBody();
if (responseBody.containsKey("status")) {
return false;
}
user.setRocketChatPersonalAccessToken(responseBody.get("token").toString());
return true;
}
}
......@@ -14,9 +14,14 @@ import javax.inject.Singleton;
@Singleton
public class UnirestService {
private static boolean objectMapperSet = false;
public UnirestService() {
// has to be set for application
Unirest.setObjectMapper(new JacksonObjectMapper());
if (!objectMapperSet) {
Unirest.setObjectMapper(new JacksonObjectMapper());
objectMapperSet = true;
}
Unirest.setDefaultHeader("Content-Type", "application/json");
}
......
......@@ -2,12 +2,13 @@ package unipotsdam.gf.modules.communication.service;
import org.junit.Before;
import org.junit.Test;
import unipotsdam.gf.core.database.InMemoryMySqlConnect;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.core.management.user.UserDAO;
import unipotsdam.gf.interfaces.ICommunication;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static unipotsdam.gf.config.GFRocketChatConfig.TEST_USER;
......@@ -18,8 +19,9 @@ public class CommunicationDummyServiceTest {
@Before
public void setUp() {
iCommunication = new CommunicationDummyService(new UnirestService());
user = new User("name", "password", "email", true);
UserDAO userDAO = new UserDAO(new InMemoryMySqlConnect());
iCommunication = new CommunicationDummyService(new UnirestService(), userDAO);
user = new User("Vorname Nachname", "password", "email@uni.de", true);
}
@Test
......@@ -35,8 +37,10 @@ public class CommunicationDummyServiceTest {
@Test
public void registerUser() {
boolean userCreated = iCommunication.registerUser(user);
assertNotNull(user.getRocketChatUserId());
assertNull(user.getRocketChatAuthToken());
assertTrue(userCreated);
assertNotNull(user.getRocketChatUserId());
assertNotNull(user.getRocketChatAuthToken());
assertNotNull(user.getRocketChatPersonalAccessToken());
}
}
......@@ -22,11 +22,11 @@ public class DummyProjectCreationServiceTest {
@Test
public void testCreateExampleProject() {
ICommunication communication = new CommunicationDummyService(new UnirestService());
InMemoryMySqlConnect inMemoryMySqlConnect = new InMemoryMySqlConnect();
Management management = TestHelper.getManagementImpl(inMemoryMySqlConnect);
GroupDAO groupDAO = new GroupDAO(inMemoryMySqlConnect);
UserDAO userDAO = new UserDAO(inMemoryMySqlConnect);
ICommunication communication = new CommunicationDummyService(new UnirestService(), userDAO);
DummyProjectCreationService dummyProjectCreationService = new DummyProjectCreationService(communication, management, groupDAO, userDAO);
......
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