From 178c3a27b0e779e0d0fa2b27843f3e6bae1c8987 Mon Sep 17 00:00:00 2001 From: lutfialquraan Date: Thu, 9 May 2019 06:09:59 +0300 Subject: [PATCH] add license --- PATCHES/create-license-table | 23 +++++ .../admin/license/AddLicenseAction.java | 53 ++++++++++++ .../admin/license/ShowHomeLicenseAction.java | 28 ++++++ .../admin/license/ShowLicensesAction.java | 37 ++++++++ .../user/{ => access}/LogInAction.java | 2 +- .../user/{ => access}/LogoutAction.java | 2 +- .../user/{ => access}/SignUpAction.java | 2 +- .../user/{ => preview}/GetArticleAction.java | 6 +- .../{ => preview}/ShowArticlesAction.java | 2 +- .../user/{ => preview}/ShowHomeAction.java | 2 +- src/main/java/model/database/LicenseDAO.java | 85 +++++++++++++++++-- .../java/model/database/SubmittedFileDAO.java | 2 +- src/main/java/model/license/License.java | 40 +++++++++ src/main/java/utilities/AccessControl.java | 25 ++++++ src/main/java/utilities/ControlSession.java | 2 + web/actions/actions.xml | 18 ++-- web/jsp/admin/license/add-license.jsp | 33 +++++++ .../admin/license/admin-license-nav-bar.jsp | 8 +- web/jsp/admin/license/show-licenses.jsp | 37 ++++++++ 19 files changed, 382 insertions(+), 25 deletions(-) create mode 100644 src/main/java/controller/actions/admin/license/AddLicenseAction.java create mode 100644 src/main/java/controller/actions/admin/license/ShowHomeLicenseAction.java create mode 100644 src/main/java/controller/actions/admin/license/ShowLicensesAction.java rename src/main/java/controller/actions/user/{ => access}/LogInAction.java (97%) rename src/main/java/controller/actions/user/{ => access}/LogoutAction.java (93%) rename src/main/java/controller/actions/user/{ => access}/SignUpAction.java (97%) rename src/main/java/controller/actions/user/{ => preview}/GetArticleAction.java (92%) rename src/main/java/controller/actions/user/{ => preview}/ShowArticlesAction.java (96%) rename src/main/java/controller/actions/user/{ => preview}/ShowHomeAction.java (95%) create mode 100644 web/jsp/admin/license/add-license.jsp create mode 100644 web/jsp/admin/license/show-licenses.jsp diff --git a/PATCHES/create-license-table b/PATCHES/create-license-table index e69de29..501f289 100644 --- a/PATCHES/create-license-table +++ b/PATCHES/create-license-table @@ -0,0 +1,23 @@ +create table license +( + id int not null, + email varchar not null, + license varchar +); + +create unique index license_id_uindex + on license (id); + +alter table license_table + add constraint license_pk + primary key (id); + +create sequence license_id_seq; + +alter table license_table alter column id set default nextval('public.license_id_seq'); + +alter sequence license_id_seq owned by license.id; + +alter table license_table alter column license type date using license::date; + + diff --git a/src/main/java/controller/actions/admin/license/AddLicenseAction.java b/src/main/java/controller/actions/admin/license/AddLicenseAction.java new file mode 100644 index 0000000..15dd1aa --- /dev/null +++ b/src/main/java/controller/actions/admin/license/AddLicenseAction.java @@ -0,0 +1,53 @@ +package controller.actions.admin.users; + +import at.favre.lib.crypto.bcrypt.BCrypt; +import controller.actions.IAction; +import model.database.DAO; +import model.database.LicenseDAO; +import model.database.UsersDAO; +import model.enums.Role; +import model.license.License; +import model.users.AbstractBaseUser; +import model.users.Admin; +import model.users.BasicUser; +import model.users.SuperAdmin; +import utilities.AccessControl; + +import javax.servlet.RequestDispatcher; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.time.LocalDate; + +public class AddLicenseAction implements IAction { + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception { + + Role role = (Role) request.getSession().getAttribute("role"); + if (AccessControl.isLoggedIn(request) && (AccessControl.isAdmin(role) || AccessControl.isSuperAdmin(role))) { + String email = request.getParameter("email"); + LocalDate date = LocalDate.now(); + date = date.plusDays(Integer.parseInt(request.getParameter("date"))); + + License license = new License(email,date); + + DAO licenseDAO = new LicenseDAO(); + licenseDAO.insert(license); + response.sendRedirect("/admin/showLicense"); + } else { + response.sendRedirect("/admin"); + } + } + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception { + + Role role = (Role) request.getSession().getAttribute("role"); + if (AccessControl.isLoggedIn(request) && (AccessControl.isAdmin(role) || AccessControl.isSuperAdmin(role))) { + RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/admin/license/add-license.jsp"); + requestDispatcher.forward(request, response); + } else { + response.sendRedirect("/admin"); + } + + } +} \ No newline at end of file diff --git a/src/main/java/controller/actions/admin/license/ShowHomeLicenseAction.java b/src/main/java/controller/actions/admin/license/ShowHomeLicenseAction.java new file mode 100644 index 0000000..75d7428 --- /dev/null +++ b/src/main/java/controller/actions/admin/license/ShowHomeLicenseAction.java @@ -0,0 +1,28 @@ +package controller.actions.admin.license; + +import controller.actions.IAction; +import model.enums.Role; +import utilities.AccessControl; + +import javax.servlet.RequestDispatcher; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class ShowHomeLicenseAction implements IAction { + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception { + doGet(request,response); + } + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception { + Role role = (Role) request.getSession().getAttribute("role"); + if (AccessControl.isLoggedIn(request) && (AccessControl.isAdmin(role) || AccessControl.isSuperAdmin(role))) { + RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/admin/license/admin-license-home.jsp"); + requestDispatcher.forward(request, response); + } else { + response.sendRedirect("/admin"); + } + + } +} diff --git a/src/main/java/controller/actions/admin/license/ShowLicensesAction.java b/src/main/java/controller/actions/admin/license/ShowLicensesAction.java new file mode 100644 index 0000000..e44a797 --- /dev/null +++ b/src/main/java/controller/actions/admin/license/ShowLicensesAction.java @@ -0,0 +1,37 @@ +package controller.actions.admin.license; + +import controller.actions.IAction; +import model.contents.ContentMeta; +import model.database.ContentMetaDAO; +import model.database.DAO; +import model.database.LicenseDAO; +import model.enums.Role; +import model.license.License; +import utilities.AccessControl; + +import javax.servlet.RequestDispatcher; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.List; + +public class ShowLicensesAction implements IAction { + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception { + doGet(request,response); + } + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception { + Role role = (Role) request.getSession().getAttribute("role"); + if (AccessControl.isLoggedIn(request) && (AccessControl.isAdmin(role) || AccessControl.isSuperAdmin(role))) { + DAO licenseDAO = new LicenseDAO(); + List license = (List) (List) licenseDAO.selectAll(); + request.setAttribute("license", license); + RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/admin/license/show-licenses.jsp"); + requestDispatcher.forward(request, response); + } else { + response.sendRedirect("/admin"); + } + + } +} diff --git a/src/main/java/controller/actions/user/LogInAction.java b/src/main/java/controller/actions/user/access/LogInAction.java similarity index 97% rename from src/main/java/controller/actions/user/LogInAction.java rename to src/main/java/controller/actions/user/access/LogInAction.java index 4420e1c..7c3ea04 100644 --- a/src/main/java/controller/actions/user/LogInAction.java +++ b/src/main/java/controller/actions/user/access/LogInAction.java @@ -1,4 +1,4 @@ -package controller.actions.user; +package controller.actions.user.access; import controller.actions.IAction; import model.database.DAO; diff --git a/src/main/java/controller/actions/user/LogoutAction.java b/src/main/java/controller/actions/user/access/LogoutAction.java similarity index 93% rename from src/main/java/controller/actions/user/LogoutAction.java rename to src/main/java/controller/actions/user/access/LogoutAction.java index d1c8311..85d8ad8 100644 --- a/src/main/java/controller/actions/user/LogoutAction.java +++ b/src/main/java/controller/actions/user/access/LogoutAction.java @@ -1,4 +1,4 @@ -package controller.actions.user; +package controller.actions.user.access; import controller.actions.IAction; import utilities.ControlSession; diff --git a/src/main/java/controller/actions/user/SignUpAction.java b/src/main/java/controller/actions/user/access/SignUpAction.java similarity index 97% rename from src/main/java/controller/actions/user/SignUpAction.java rename to src/main/java/controller/actions/user/access/SignUpAction.java index 4132263..44588e4 100644 --- a/src/main/java/controller/actions/user/SignUpAction.java +++ b/src/main/java/controller/actions/user/access/SignUpAction.java @@ -1,4 +1,4 @@ -package controller.actions.user; +package controller.actions.user.access; import at.favre.lib.crypto.bcrypt.BCrypt; import controller.actions.IAction; diff --git a/src/main/java/controller/actions/user/GetArticleAction.java b/src/main/java/controller/actions/user/preview/GetArticleAction.java similarity index 92% rename from src/main/java/controller/actions/user/GetArticleAction.java rename to src/main/java/controller/actions/user/preview/GetArticleAction.java index cda8fff..7a904ce 100644 --- a/src/main/java/controller/actions/user/GetArticleAction.java +++ b/src/main/java/controller/actions/user/preview/GetArticleAction.java @@ -1,4 +1,4 @@ -package controller.actions.user; +package controller.actions.user.preview; import controller.actions.IAction; import utilities.AccessControl; @@ -15,7 +15,7 @@ public class GetArticleAction implements IAction { @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception { - if (AccessControl.isLoggedIn(request)) { + if (AccessControl.isLoggedIn(request) && AccessControl.hasLicense(request) ) { String doi = request.getParameter("doi"); String htmlPath = DirectoryPaths.CONTENTS_FILE_PATH + doi + ".html"; String pdf = DirectoryPaths.CONTENTS_FILE_PATH + doi + ".pdf"; @@ -44,4 +44,4 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception { doPost(request, response); } -} +} \ No newline at end of file diff --git a/src/main/java/controller/actions/user/ShowArticlesAction.java b/src/main/java/controller/actions/user/preview/ShowArticlesAction.java similarity index 96% rename from src/main/java/controller/actions/user/ShowArticlesAction.java rename to src/main/java/controller/actions/user/preview/ShowArticlesAction.java index ec94073..fc68896 100644 --- a/src/main/java/controller/actions/user/ShowArticlesAction.java +++ b/src/main/java/controller/actions/user/preview/ShowArticlesAction.java @@ -1,4 +1,4 @@ -package controller.actions.user; +package controller.actions.user.preview; import controller.actions.IAction; import model.contents.ContentMeta; diff --git a/src/main/java/controller/actions/user/ShowHomeAction.java b/src/main/java/controller/actions/user/preview/ShowHomeAction.java similarity index 95% rename from src/main/java/controller/actions/user/ShowHomeAction.java rename to src/main/java/controller/actions/user/preview/ShowHomeAction.java index 73bda0b..d172e21 100644 --- a/src/main/java/controller/actions/user/ShowHomeAction.java +++ b/src/main/java/controller/actions/user/preview/ShowHomeAction.java @@ -1,4 +1,4 @@ -package controller.actions.user; +package controller.actions.user.preview; import controller.actions.IAction; diff --git a/src/main/java/model/database/LicenseDAO.java b/src/main/java/model/database/LicenseDAO.java index 0f8ca3a..d9de88b 100644 --- a/src/main/java/model/database/LicenseDAO.java +++ b/src/main/java/model/database/LicenseDAO.java @@ -1,30 +1,105 @@ package model.database; +import model.license.License; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Statement; +import java.time.LocalDate; +import java.util.ArrayList; import java.util.List; public class LicenseDAO extends DAO { @Override public void insert(Object object) { - + try { + License license = (License) object; + String sql = "insert into license_table (email,license) values (?,?)"; + PreparedStatement preparedStatement = connectionPool.getConnection().prepareStatement(sql); + preparedStatement.setString(1, license.getEmail()); + preparedStatement.setObject(2, license.getDate()); + preparedStatement.execute(); + } catch (Exception e) { + e.printStackTrace(); + } } @Override public void delete(Object object) { - + try { + String email = (String) object; + String sql = "delete from license_table where email = ? "; + PreparedStatement preparedStatement = connectionPool.getConnection().prepareStatement(sql); + preparedStatement.setString(1, email); + preparedStatement.execute(); + } catch (Exception e) { + e.printStackTrace(); + } } @Override public void update(Object object) { - + try { + License license = (License) object; + String sql = "update license_table set license =? where id = ? ;"; + PreparedStatement preparedStatement = connectionPool.getConnection().prepareStatement(sql); + preparedStatement.setObject(1,license.getDate()); + preparedStatement.setInt(2, license.getId()); + preparedStatement.execute(); + } catch (Exception e) { + e.printStackTrace(); + } } @Override public List selectAll() { - return null; + List licenses = new ArrayList<>(); + Statement myStat; + ResultSet myRes; + try { + String sql = "select * from license_table order by id"; + myStat = connectionPool.getConnection().createStatement(); + myRes = myStat.executeQuery(sql); + licenses = getLicense(myRes); + } catch (Exception e) { + e.printStackTrace(); + } + return licenses; } @Override public Object select(Object object) { - return null; + License license = null; + PreparedStatement myStat; + String email = (String) object; + ResultSet myRes = null; + try { + String sql = "select * from license_table where email = ?"; + myStat = connectionPool.getConnection().prepareStatement(sql); + myStat.setString(1, email); + myRes = myStat.executeQuery(); + license = (License) getLicense(myRes).get(0); + } catch (Exception e) { + e.printStackTrace(); + } + return license; + } + + private List getLicense(ResultSet resultSet) { + List licenses = new ArrayList<>(); + License theLicense = null; + try { + while (resultSet.next()) { + + String email = resultSet.getString("email"); + LocalDate license = resultSet.getObject("license",LocalDate.class); + int id = resultSet.getInt("id"); + theLicense = new License(id,email,license); + licenses.add(theLicense); + } + } catch (Exception e) { + e.printStackTrace(); + } + return licenses; } } diff --git a/src/main/java/model/database/SubmittedFileDAO.java b/src/main/java/model/database/SubmittedFileDAO.java index 357d147..f414db6 100644 --- a/src/main/java/model/database/SubmittedFileDAO.java +++ b/src/main/java/model/database/SubmittedFileDAO.java @@ -76,7 +76,7 @@ public Object select(Object object) { int file_id = (int) object; ResultSet myRes = null; try { - String sql = "select * from user_table where file_id = ?"; + String sql = "select * from unprocessed_file where file_id = ?"; myStat = connectionPool.getConnection().prepareStatement(sql); myStat.setInt(1, file_id); myRes = myStat.executeQuery(); diff --git a/src/main/java/model/license/License.java b/src/main/java/model/license/License.java index 3cf07ea..b823423 100644 --- a/src/main/java/model/license/License.java +++ b/src/main/java/model/license/License.java @@ -1,4 +1,44 @@ package model.license; +import java.time.LocalDate; + public class License { + private int id; + private String email; + private LocalDate date; + + public License(int id, String email, LocalDate date) { + this.id = id; + this.email = email; + this.date = date; + } + + public License(String email, LocalDate date) { + this.email = email; + this.date = date; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public LocalDate getDate() { + return date; + } + + public void setDate(LocalDate date) { + this.date = date; + } } diff --git a/src/main/java/utilities/AccessControl.java b/src/main/java/utilities/AccessControl.java index 93ca1a9..53cd9c6 100644 --- a/src/main/java/utilities/AccessControl.java +++ b/src/main/java/utilities/AccessControl.java @@ -1,10 +1,14 @@ package utilities; import at.favre.lib.crypto.bcrypt.BCrypt; +import model.database.DAO; +import model.database.LicenseDAO; import model.enums.Role; +import model.license.License; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; +import java.time.LocalDate; public final class AccessControl { @@ -49,4 +53,25 @@ public static boolean isLoggedIn(HttpServletRequest request) } return allowed; } + + public static boolean hasLicense (HttpServletRequest request) + { + boolean allowed = false; + HttpSession session = request.getSession(); + String email = (String) session.getAttribute("email"); + if (email!=null) + { + DAO licenseDAO = new LicenseDAO(); + License license = (License) licenseDAO.select(email); + LocalDate today = LocalDate.now(); + LocalDate expiry = license.getDate(); + + if (expiry.compareTo(today)>0) + { + allowed = true; + } + } + + return allowed; + } } \ No newline at end of file diff --git a/src/main/java/utilities/ControlSession.java b/src/main/java/utilities/ControlSession.java index 47add41..1ea0fa4 100644 --- a/src/main/java/utilities/ControlSession.java +++ b/src/main/java/utilities/ControlSession.java @@ -14,12 +14,14 @@ public static void createSession(HttpServletRequest request, AbstractBaseUser ba HttpSession session = request.getSession(); session.setAttribute("role",baseUser.getRole()); session.setAttribute("name", baseUser.getFirstName()); + session.setAttribute("email",baseUser.getEmail()); } public static void deleteSession(HttpServletRequest request) { HttpSession session = request.getSession(false); session.removeAttribute("name"); session.removeAttribute("role"); + session.removeAttribute("email"); session.invalidate(); } } diff --git a/web/actions/actions.xml b/web/actions/actions.xml index 476a6e5..d92c7db 100644 --- a/web/actions/actions.xml +++ b/web/actions/actions.xml @@ -1,15 +1,15 @@ - - + + - + - - - - + + + + @@ -31,4 +31,8 @@ + + + + \ No newline at end of file diff --git a/web/jsp/admin/license/add-license.jsp b/web/jsp/admin/license/add-license.jsp new file mode 100644 index 0000000..347f65b --- /dev/null +++ b/web/jsp/admin/license/add-license.jsp @@ -0,0 +1,33 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + + + + + Add-User + + + + + +
+ +
+
+ + +
+
+ + +
+ +
+
+ + diff --git a/web/jsp/admin/license/admin-license-nav-bar.jsp b/web/jsp/admin/license/admin-license-nav-bar.jsp index 251df87..203c6ef 100644 --- a/web/jsp/admin/license/admin-license-nav-bar.jsp +++ b/web/jsp/admin/license/admin-license-nav-bar.jsp @@ -13,16 +13,16 @@ Show Licensees(current)