From 9596972b3df000368d146b693b6ba0e6674dd88b Mon Sep 17 00:00:00 2001 From: Julian Dehne <julian.dehne@uni-potsdam.de> Date: Tue, 5 Jun 2018 12:37:26 +0200 Subject: [PATCH] feat: implemented role for user (isStudent true/false) --- .../gf/core/management/ManagementImpl.java | 23 +++++---- .../gf/core/management/user/User.java | 28 ++++++++--- .../management/user/servlets/CreateUser.java | 17 ++----- .../management/user/servlets/UserExists.java | 19 ++----- .../user/servlets/UserHttpServlet.java | 31 ++++++++++++ .../unipotsdam/gf/interfaces/Feedback.java | 17 ++++--- .../peer2peerfeedback/Peer2PeerFeedback.java | 5 ++ .../webapp/core/assets/js/showProjects.js | 50 +++++++++++++++++++ .../src/main/webapp/register.jsp | 23 +++++---- .../src/scripts/dbschema/fltrail.sql | 3 ++ .../src/scripts/dbschema/fltrail_users.sql | 0 .../gf/interfaces/ManagementTest.java | 10 ++-- 12 files changed, 160 insertions(+), 66 deletions(-) create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/UserHttpServlet.java create mode 100644 gemeinsamforschen/src/main/webapp/core/assets/js/showProjects.js delete mode 100644 gemeinsamforschen/src/scripts/dbschema/fltrail_users.sql diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java index 8cb46156..43c88915 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java @@ -32,8 +32,9 @@ public class ManagementImpl implements Management { MysqlConnect connect = new MysqlConnect(); connect.connect(); - String mysqlRequest = "INSERT INTO users (`name`, `password`, `email`, `token`) values (?,?,?,?)"; - connect.issueInsertOrDeleteStatement(mysqlRequest, user.getName(), user.getPassword(), user.getEmail(), token); + String mysqlRequest = "INSERT INTO users (`name`, `password`, `email`, `token`,`isStudent`) values (?,?,?,?,?)"; + connect.issueInsertOrDeleteStatement(mysqlRequest, user.getName(), user.getPassword(), user.getEmail(), + token, user.getStudent()); connect.close(); // TODO implmement UserProfile @Mar @@ -95,11 +96,8 @@ public class ManagementImpl implements Management { VereinfachtesResultSet vereinfachtesResultSet = connect.issueSelectStatement(query, project.getId()); while (!vereinfachtesResultSet.isLast()) { vereinfachtesResultSet.next(); - String name = vereinfachtesResultSet.getString("name"); - String password = vereinfachtesResultSet.getString("password"); - String email = vereinfachtesResultSet.getString("email"); + User user = getUserFromResultSet(vereinfachtesResultSet); String token = vereinfachtesResultSet.getString("token"); - User user = new User(name, password, email); user.setToken(token); result.add(user); } @@ -107,6 +105,14 @@ public class ManagementImpl implements Management { return result; } + private User getUserFromResultSet(VereinfachtesResultSet vereinfachtesResultSet) { + String name = vereinfachtesResultSet.getString("name"); + String password = vereinfachtesResultSet.getString("password"); + String email = vereinfachtesResultSet.getString("email"); + Boolean isStudent = vereinfachtesResultSet.getBoolean("isStudent"); + return new User(name, password, email, isStudent); + } + @Override public String getUserToken(User user) { MysqlConnect connect = new MysqlConnect(); @@ -128,10 +134,7 @@ public class ManagementImpl implements Management { connect.issueSelectStatement(mysqlRequest, token); boolean next = vereinfachtesResultSet.next(); if (next) { - String name = vereinfachtesResultSet.getString("name"); - String password = vereinfachtesResultSet.getString("password"); - String email = vereinfachtesResultSet.getString("email"); - User user = new User(name, password, email); + User user = getUserFromResultSet(vereinfachtesResultSet); connect.close(); return user; } else { diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/User.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/User.java index d2992816..bf184705 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/User.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/User.java @@ -5,13 +5,24 @@ package unipotsdam.gf.core.management.user; */ public class User { + // the email is the id! + public String getId() { + return this.email; + } + private String name; + private String password; + private String email; + private String token; + private Boolean isStudent; + public User() { } - public User(String name, String password, String email) { + public User(String name, String password, String email, Boolean isStudent) { this.name = name; this.password = password; this.email = email; + this.isStudent = isStudent; } public String getName() { @@ -46,12 +57,13 @@ public class User { this.token = token; } - // the email is the id! - public String getId() { - return this.email; + + + public Boolean getStudent() { + return isStudent; + } + + public void setStudent(Boolean student) { + isStudent = student; } - private String name; - private String password; - private String email; - private String token; } diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/CreateUser.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/CreateUser.java index afa3c740..6345a3c1 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/CreateUser.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/CreateUser.java @@ -5,13 +5,12 @@ import unipotsdam.gf.core.management.user.User; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "CreateUser") -public class CreateUser extends HttpServlet +public class CreateUser extends UserHttpServlet { protected void doPost( HttpServletRequest request, HttpServletResponse response) @@ -26,24 +25,16 @@ public class CreateUser extends HttpServlet } private void handeRequest(HttpServletRequest request, HttpServletResponse response) { - - String password = request.getParameter("password"); - String email = request.getParameter("email"); - String name = request.getParameter("name"); - User user = new User(name, password, email); + User user = parseFullUser(request); ManagementImpl management = new ManagementImpl(); if (management.exists(user)) { String existsUrl = "../register.jsp?userExists=true"; - response.setHeader("Location", existsUrl); - response.setContentType("text/html"); - response.setStatus(HttpServletResponse.SC_FOUND); // SC_FOUND = 302 + forwardToLocation(response, existsUrl); } else { management.create(user, null); String successUrl = "../core/pages/projects.jsp?token="; successUrl+=management.getUserToken(user); - response.setHeader("Location", successUrl); - response.setContentType("text/html"); - response.setStatus(HttpServletResponse.SC_FOUND); // SC_FOUND = 302 + forwardToLocation(response, successUrl); } } diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/UserExists.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/UserExists.java index e0c3cb73..13e8f5e8 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/UserExists.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/UserExists.java @@ -3,17 +3,14 @@ package unipotsdam.gf.core.management.user.servlets; import unipotsdam.gf.core.management.ManagementImpl; import unipotsdam.gf.core.management.user.User; -import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.io.PrintWriter; @WebServlet(name = "UserExists") -public class UserExists extends HttpServlet { +public class UserExists extends UserHttpServlet { protected void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -32,10 +29,7 @@ public class UserExists extends HttpServlet { private void handeRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - - String password = request.getParameter("password"); - String email = request.getParameter("email"); - User user = new User(null, password, email); + User user = parseUser(request); String nextJSP = "../core/pages/projects.jsp?token="; String errorJSP = "../index.jsp?userExists=false"; @@ -48,17 +42,14 @@ public class UserExists extends HttpServlet { */ if (exists) { nextJSP+= management.getUserToken(user); - response.setHeader("Location", nextJSP); - response.setContentType("text/html"); - response.setStatus(HttpServletResponse.SC_FOUND); // SC_FOUND = 302 + forwardToLocation(response, nextJSP); } else { // if the user does not exist the page is forwarded to itself with a GET-Variable indicating an error - response.setHeader("Location", errorJSP); - response.setContentType("text/html"); - response.setStatus(HttpServletResponse.SC_FOUND); // SC_FOUND = 302 + forwardToLocation(response, errorJSP); } } + } diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/UserHttpServlet.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/UserHttpServlet.java new file mode 100644 index 00000000..0c93b13f --- /dev/null +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/user/servlets/UserHttpServlet.java @@ -0,0 +1,31 @@ +package unipotsdam.gf.core.management.user.servlets; + +import unipotsdam.gf.core.management.user.User; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class UserHttpServlet extends HttpServlet { + protected User parseUser(HttpServletRequest request) { + String password = request.getParameter("password"); + String email = request.getParameter("email"); + return new User(null, password, email, null); + } + + protected User parseFullUser(HttpServletRequest request) { + User part1 = parseUser(request); + String name = request.getParameter("name"); + Boolean isStudent = Boolean.parseBoolean(request.getParameter("isStudent")); + part1.setName(name); + part1.setStudent(isStudent); + return part1; + } + + protected void forwardToLocation(HttpServletResponse response, String nextJSP) { + response.setHeader("Location", nextJSP); + response.setContentType("text/html"); + response.setStatus(HttpServletResponse.SC_FOUND); // SC_FOUND = 302 + } + +} diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/Feedback.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/Feedback.java index e8574f44..6ba367ce 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/Feedback.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/Feedback.java @@ -1,6 +1,9 @@ package unipotsdam.gf.interfaces; -import unipotsdam.gf.modules.peer2peerfeedback.peer2peerfeedback; +import unipotsdam.gf.core.management.user.User; +import unipotsdam.gf.modules.peer2peerfeedback.Peer2PeerFeedback; import java.*; +import java.io.File; +import java.util.ArrayList; /** @@ -12,9 +15,9 @@ public interface Feedback { /** * create Peer2PeerFeedback Object * - * @param FeedbackUser: The student who is creating the feedback - * @param SelectedStudent: The student who receive the feedback - * @param Document: The selected document to give feedback about + * @param feedbackuser: The student who is creating the feedback + * @param selectedstudent: The student who receive the feedback + * @param document: The selected document to give feedback about * @return Returns the Peer2PeerFeedback Object */ @@ -23,8 +26,8 @@ public interface Feedback { /** * give Peer2PeerFeedback * - * @param Peer2PeerFeedback: The Peer2PeerFeedback as an Object - * @param Document: The selected document + * @param feedback: The Peer2PeerFeedback as an Object + * @param document: The selected document * @return Send feedback with doc and return true, if the feedback is successfully sended */ @@ -37,7 +40,7 @@ public interface Feedback { * @return List of Feedbacks with Docs */ - ArrayList <Peer2PeerFeedback> showFeedback(User student); + ArrayList<Peer2PeerFeedback> showFeedback(User student); /** * count Feedback diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/peer2peerfeedback/Peer2PeerFeedback.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/peer2peerfeedback/Peer2PeerFeedback.java index 7478e44a..4633872e 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/peer2peerfeedback/Peer2PeerFeedback.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/peer2peerfeedback/Peer2PeerFeedback.java @@ -1,4 +1,9 @@ package unipotsdam.gf.modules.peer2peerfeedback; + +import unipotsdam.gf.core.management.user.User; + +import java.io.File; + /** Peer2PeerFeedback Object */ diff --git a/gemeinsamforschen/src/main/webapp/core/assets/js/showProjects.js b/gemeinsamforschen/src/main/webapp/core/assets/js/showProjects.js new file mode 100644 index 00000000..ec4771b7 --- /dev/null +++ b/gemeinsamforschen/src/main/webapp/core/assets/js/showProjects.js @@ -0,0 +1,50 @@ +/** + * Created by fides-WHK on 09.01.2018. + */ +$(document).ready(function () { + $("#projectWrongPassword").hide(); + $("#projectIsMissing").hide(); + + $("#projectName").keypress(function (e) { + if (e.which == 13) { + document.getElementById("projectPassword").focus(); + } + }); + $("#projectPassword").keypress(function (e) { + if (e.which == 13) { + seeProject($('#projectName').val()); + } + }); + $("#seeProject").on('click', function () { + seeProject($('#projectName').val()); + }); +}); + +function seeProject(projectName) { + var url = "../database/getProjects.php?project=" + projectName + "&password=" + document.getElementById('projectPassword').value; + if (projectName === "") { + return false; + } else { + $.ajax({ + url: url, + projectName: projectName, + Accept: "text/plain; charset=utf-8", + contentType: "text/plain", + success: function (response) { + if (response === "project missing") { + $("#projectIsMissing").show(); + } else { + if (response !== "wrong password") { //if response !== project missing and not wrong password, its the projectToken + location.href="preferences.php?token="+getUserTokenFromUrl()+"&projectToken="+response; + } else { + $("#projectIsMissing").hide(); + $('#projectWrongPassword').show(); + } + } + }, + error: function (a, b, c) { + console.log(a); + } + }); + } +} \ No newline at end of file diff --git a/gemeinsamforschen/src/main/webapp/register.jsp b/gemeinsamforschen/src/main/webapp/register.jsp index 23ef08dc..1e2e75fc 100644 --- a/gemeinsamforschen/src/main/webapp/register.jsp +++ b/gemeinsamforschen/src/main/webapp/register.jsp @@ -31,20 +31,25 @@ <div class="form-group"><label>Bitte kein sensibeles Passwort verwenden!</label></div> <!--<div class="form-group"><input class="form-control" type="password" name="password" placeholder="Password wiederholen"></div>--> + <div class="form-group"> + <label> + <input id="is_Student" class="form-control" type="checkbox" name="isStudent">Dozierender + </label> + </div> <div class="form-group"> <button class="btn btn-primary btn-block" type="submit">registrieren</button> </div> <div class="form-group"> <!-- scriptlets are terrible. Just tmp for porting the php --> - <%-- <% - String param = request.getParameter("userExists"); - if (param != null) { - try (PrintWriter p = response.getWriter()) { - p.println( - "<div class=\"alert alert-danger\" role=\"alert\"> Es existiert ein Nutzer mit dieser Email oder diesem Benutzernamen! </div>"); - } - } - %>--%> + <%-- <% + String param = request.getParameter("userExists"); + if (param != null) { + try (PrintWriter p = response.getWriter()) { + p.println( + "<div class=\"alert alert-danger\" role=\"alert\"> Es existiert ein Nutzer mit dieser Email oder diesem Benutzernamen! </div>"); + } + } + %>--%> <% String param = request.getParameter("userExists"); diff --git a/gemeinsamforschen/src/scripts/dbschema/fltrail.sql b/gemeinsamforschen/src/scripts/dbschema/fltrail.sql index a70ccaed..d7978655 100644 --- a/gemeinsamforschen/src/scripts/dbschema/fltrail.sql +++ b/gemeinsamforschen/src/scripts/dbschema/fltrail.sql @@ -37,6 +37,9 @@ CREATE TABLE projectuser userId varchar(400) NOT NULL ); ENGINE=InnoDB DEFAULT CHARSET=utf8; +alter table users + add isStudent tinyint(1) default '1' null; + diff --git a/gemeinsamforschen/src/scripts/dbschema/fltrail_users.sql b/gemeinsamforschen/src/scripts/dbschema/fltrail_users.sql deleted file mode 100644 index e69de29b..00000000 diff --git a/gemeinsamforschen/src/test/java/unipotsdam/gf/interfaces/ManagementTest.java b/gemeinsamforschen/src/test/java/unipotsdam/gf/interfaces/ManagementTest.java index d5c4fd11..9463acbd 100644 --- a/gemeinsamforschen/src/test/java/unipotsdam/gf/interfaces/ManagementTest.java +++ b/gemeinsamforschen/src/test/java/unipotsdam/gf/interfaces/ManagementTest.java @@ -30,7 +30,7 @@ public class ManagementTest { @Test public void testExists() throws Exception { ManagementImpl management = new ManagementImpl(); - User user = new User("julian", "1234", "from1123123123@stuff.com"); + User user = new User("julian", "1234", "from1123123123@stuff.com", true); assert !management.exists(user); } @@ -41,7 +41,7 @@ public class ManagementTest { @Test public void testCreate() throws Exception { ManagementImpl management = new ManagementImpl(); - User user = new User("julian", "1234", "from@stuff.com"); + User user = new User("julian", "1234", "from@stuff.com", false); management.create(user, new UserProfile()); assert management.exists(user); } @@ -60,7 +60,7 @@ public class ManagementTest { @Test public void testRegister() throws Exception { ManagementImpl management = new ManagementImpl(); - User user = new User("julian", "1234", "from@stuff.com"); + User user = new User("julian", "1234", "from@stuff.com", true); management.create(user, new UserProfile()); assert management.exists(user); @@ -72,7 +72,7 @@ public class ManagementTest { @Test public void testGetUsers() throws Exception { ManagementImpl management = new ManagementImpl(); - User user = new User("julian", "1234", "from@stuff.com"); + User user = new User("julian", "1234", "from@stuff.com", false); management.create(user, new UserProfile()); assert management.exists(user); @@ -80,7 +80,7 @@ public class ManagementTest { management.create(project); management.register(user, project, null); - User user2 = new User("julian2", "12345", "from2@stuff.com"); + User user2 = new User("julian2", "12345", "from2@stuff.com", true); management.create(user2, new UserProfile()); assert management.exists(user2); -- GitLab