Skip to content

Commit

Permalink
Merge pull request #256 from ec-jrc/#180/203Adding-a-user-to-the-plat…
Browse files Browse the repository at this point in the history
…form-let-them-accept-if-he/she-agrees-to-join

Adding a user to the platform let them accept if he/she agrees to join
  • Loading branch information
oruscalleda authored Feb 22, 2023
2 parents be3361d + 2d027dc commit 69c3314
Show file tree
Hide file tree
Showing 25 changed files with 1,011 additions and 26 deletions.
14 changes: 13 additions & 1 deletion dist/db-scripts/registry2_drop-and-create-and-init.sql.orig
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,14 @@ CREATE TABLE reg_user_reg_group_mapping
editdate TIMESTAMP WITHOUT TIME ZONE
);

CREATE TABLE reg_user_codes
(
uuid VARCHAR(50) NOT NULL,
reg_user VARCHAR(50) NOT NULL,
code VARCHAR(80),
action VARCHAR(50),
insertdate TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT now()
);

/* Create Primary Keys */

Expand Down Expand Up @@ -448,7 +456,9 @@ ALTER TABLE reg_user ADD CONSTRAINT PK_reg_user

ALTER TABLE reg_user_reg_group_mapping ADD CONSTRAINT PK_reg_user_reg_group_mapping
PRIMARY KEY (uuid);


ALTER TABLE reg_user_codes ADD CONSTRAINT PK_reg_user_codes
PRIMARY KEY (uuid);

/* Create Uniques */

Expand Down Expand Up @@ -722,6 +732,8 @@ ALTER TABLE reg_user_reg_group_mapping ADD CONSTRAINT FK_reg_user_reg_group_mapp
ALTER TABLE reg_user_reg_group_mapping ADD CONSTRAINT FK_reg_user_reg_group_mapping_reg_user
FOREIGN KEY (reg_user) REFERENCES reg_user (uuid) ON DELETE NO ACTION ON UPDATE CASCADE;

ALTER TABLE reg_user_codes ADD CONSTRAINT FK_reg_user_code_reg_user
FOREIGN KEY (reg_user) REFERENCES reg_user (uuid) ON DELETE NO ACTION ON UPDATE CASCADE;

/* Create functions and triggers */

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package eu.europa.ec.re3gistry2.web.controller;

import eu.europa.ec.re3gistry2.base.utility.BaseConstants;
import eu.europa.ec.re3gistry2.base.utility.Configuration;
import eu.europa.ec.re3gistry2.base.utility.PersistenceFactory;
import eu.europa.ec.re3gistry2.base.utility.WebConstants;
import eu.europa.ec.re3gistry2.crudimplementation.RegUserCodesManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegUserManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegUserRegGroupMappingManager;
import eu.europa.ec.re3gistry2.javaapi.handler.RegUserHandler;
import eu.europa.ec.re3gistry2.javaapi.handler.RegUserRegCodesHandler;
import eu.europa.ec.re3gistry2.model.RegUser;
import eu.europa.ec.re3gistry2.model.RegUserCodes;
import eu.europa.ec.re3gistry2.model.RegUserRegGroupMapping;
import java.io.IOException;
import java.util.List;
import javax.persistence.EntityManager;
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 org.apache.logging.log4j.Logger;

@WebServlet(WebConstants.PAGE_URINAME_ACTIVATE)
public class Activate extends HttpServlet {

private void processRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

//Init frontend servlet
//añadir booleano para no login?
Configuration.getInstance().initServlet(request, response, false, false);

// Setup the entity manager
EntityManager entityManager = PersistenceFactory.getEntityManagerFactory().createEntityManager();

// Init logger
Logger logger = Configuration.getInstance().getLogger();

// Instantiating managers
RegUserCodesManager regUserCodesManager = new RegUserCodesManager(entityManager);
RegUserRegCodesHandler regUserCodesHandler = new RegUserRegCodesHandler();
RegUserManager regUserManager = new RegUserManager(entityManager);
RegUserHandler regUserHandler = new RegUserHandler();

// Getting form parameter
String code = request.getParameter("code");

// Getting the user from the code
RegUserCodes regCode = regUserCodesManager.getByCode(code);
RegUser regUser = regUserManager.get(regCode.getRegUser());
List<RegUserCodes> regCodeAux = regUserCodesManager.getByRegUser(regCode.getRegUser());

if(regCode.getAction().equals(BaseConstants.KEY_USER_ACTION_ACTIVATE_USER)){
//Enabling the user
Boolean enabled = regUserHandler.toggleUserEnabled(regUser, Boolean.TRUE);

if(enabled){
//Delete the codes
for(RegUserCodes r:regCodeAux){
regUserCodesHandler.deleteCode(r);
}
request.getRequestDispatcher(WebConstants.PAGE_JSP_FOLDER + WebConstants.PAGE_PATH_ACTIVATE+ WebConstants.PAGE_URINAME_ACTIVATE + WebConstants.PAGE_JSP_EXTENSION).forward(request, response);
}else{
//Dispatch request
request.getRequestDispatcher(WebConstants.PAGE_JSP_FOLDER + WebConstants.PAGE_PATH_REGISTRYMANAGER_USERS_ADD + WebConstants.PAGE_URINAME_REGISTRYMANAGER_USERS_ADD + WebConstants.PAGE_JSP_EXTENSION).forward(request, response);
}
}else{
//Delete the codes
for(RegUserCodes r:regCodeAux){
regUserCodesHandler.deleteCode(r);
}

//Delete the user
Boolean removed = regUserHandler.removeUser(regUser);
if(removed){
//response.sendRedirect("/help");
request.getRequestDispatcher(WebConstants.PAGE_JSP_FOLDER + WebConstants.PAGE_PATH_DELETE_USER+ WebConstants.PAGE_URINAME_DELETE_USER + WebConstants.PAGE_JSP_EXTENSION).forward(request, response);
}else{
//Dispatch request
request.getRequestDispatcher(WebConstants.PAGE_JSP_FOLDER + WebConstants.PAGE_PATH_REGISTRYMANAGER_USERS_ADD + WebConstants.PAGE_URINAME_REGISTRYMANAGER_USERS_ADD + WebConstants.PAGE_JSP_EXTENSION).forward(request, response);
}
}
}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
processRequest(request, response);
} catch (Exception ex) {
Logger logger = Configuration.getInstance().getLogger();
logger.error(ex.getMessage(), ex);
}
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
processRequest(request, response);
} catch (Exception ex) {
Logger logger = Configuration.getInstance().getLogger();
logger.error(ex.getMessage(), ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@
import eu.europa.ec.re3gistry2.crudimplementation.RegGroupManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegItemRegGroupRegRoleMappingManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegLanguagecodeManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegUserCodesManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegUserManager;
import eu.europa.ec.re3gistry2.javaapi.handler.RegUserHandler;
import eu.europa.ec.re3gistry2.javaapi.handler.RegUserRegGrouprHandler;
import eu.europa.ec.re3gistry2.model.RegGroup;
import eu.europa.ec.re3gistry2.model.RegLanguagecode;
import eu.europa.ec.re3gistry2.model.RegUser;
import eu.europa.ec.re3gistry2.model.RegUserCodes;
import eu.europa.ec.re3gistry2.model.RegUserRegGroupMapping;
import eu.europa.ec.re3gistry2.model.uuidhandlers.RegUserRegGroupMappingUuidHelper;
import eu.europa.ec.re3gistry2.model.uuidhandlers.RegUserUuidHelper;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
Expand Down Expand Up @@ -172,7 +175,7 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
newUser.setSsoreference(email);
}
newUser.setInsertdate(new Date());
newUser.setEnabled(true);
newUser.setEnabled(false);

String loginType = properties.getProperty(BaseConstants.KEY_PROPERTY_LOGIN_TYPE, BaseConstants.KEY_PROPERTY_LOGIN_TYPE_SHIRO);
String key = "";
Expand All @@ -189,28 +192,46 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
/**
* save group reference
*/
RegGroupManager regGroupManager = new RegGroupManager(entityManager);
for (String selectedgroup : selectedgroups) {
RegUserRegGroupMapping regUserRegGroupMapping = new RegUserRegGroupMapping();
final RegGroup group = regGroupManager.get(selectedgroup);
String newRegUserRegGroupUUID = RegUserRegGroupMappingUuidHelper.getUuid(newUser, group);
regUserRegGroupMapping.setUuid(newRegUserRegGroupUUID);
regUserRegGroupMapping.setRegUser(newUser);
regUserRegGroupMapping.setRegGroup(group);
regUserRegGroupMapping.setIsGroupadmin(Boolean.TRUE);
regUserRegGroupMapping.setInsertdate(new Date());

try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
if(selectedgroups != null && selectedgroups.length > 0){
RegGroupManager regGroupManager = new RegGroupManager(entityManager);
for (String selectedgroup : selectedgroups) {
RegUserRegGroupMapping regUserRegGroupMapping = new RegUserRegGroupMapping();
final RegGroup group = regGroupManager.get(selectedgroup);
String newRegUserRegGroupUUID = RegUserRegGroupMappingUuidHelper.getUuid(newUser, group);
regUserRegGroupMapping.setUuid(newRegUserRegGroupUUID);
regUserRegGroupMapping.setRegUser(newUser);
regUserRegGroupMapping.setRegGroup(group);
regUserRegGroupMapping.setIsGroupadmin(Boolean.TRUE);
regUserRegGroupMapping.setInsertdate(new Date());

try {
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
entityManager.persist(regUserRegGroupMapping);
entityManager.getTransaction().commit();
} catch (Exception ec) {
logger.error("@ RegUserHandler.addUser: generic error.", e);
}
entityManager.persist(regUserRegGroupMapping);
entityManager.getTransaction().commit();
} catch (Exception ec) {
logger.error("@ RegUserHandler.addUser: generic error.", e);
}
}


//Generate activation and deletion codes
RegUserCodes codeActivation = new RegUserCodes(newUser.getUuid(),BaseConstants.KEY_USER_ACTION_ACTIVATE_USER,new Date());
RegUserCodes codeDeletion = new RegUserCodes(newUser.getUuid(),BaseConstants.KEY_USER_ACTION_DELETE_USER,new Date());

try{
if (!entityManager.getTransaction().isActive()) {
entityManager.getTransaction().begin();
}
//Persist both codes
entityManager.persist(codeActivation);
entityManager.persist(codeDeletion);
entityManager.getTransaction().commit();
} catch (Exception ec) {
logger.error("@ RegUserHandler.addUser: generic error.", ec);
}

// Prepare the email email to the user with the generated key
String recipientString = newUser.getEmail();
InternetAddress[] recipient = {
Expand All @@ -221,11 +242,17 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
String body;

if (loginType.equals(BaseConstants.KEY_PROPERTY_LOGIN_TYPE_SHIRO)) {
String host = request.getHeader(BaseConstants.KEY_PROPERTY_HOST);
URL activationUrl = new URL(host.concat(WebConstants.PAGE_PATH_ACTIVATE).concat("?").concat(BaseConstants.KEY_PROPERTY_CODE).concat("="+codeActivation.getCode()));
URL deletionUrl = new URL(host.concat(WebConstants.PAGE_PATH_ACTIVATE).concat("?").concat(BaseConstants.KEY_PROPERTY_CODE).concat("="+codeDeletion.getCode()));

body = systemLocalization.getString(BaseConstants.KEY_EMAIL_BODY_NEW_REGISTRATION);
body = (body != null)
? body.replace("{name}", name)
.replace("{email}", email)
.replace("{key}", key)
.replace("{acceptLink}",activationUrl.toString())
.replace("{deleteLink}",deletionUrl.toString())
: "";
} else {
body = systemLocalization.getString(BaseConstants.KEY_EMAIL_BODY_ECAS_NEW_REGISTRATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<class>eu.europa.ec.re3gistry2.model.RegUserRegGroupMapping</class>
<class>eu.europa.ec.re3gistry2.model.RegItemproposedRegGroupRegRoleMapping</class>
<class>eu.europa.ec.re3gistry2.model.RegItemhistoryRegGroupRegRoleMapping</class>
<class>eu.europa.ec.re3gistry2.model.RegUserCodes</class>

<exclude-unlisted-classes>false</exclude-unlisted-classes>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ mail.text.subject.error=Re3gistry - installation error
mail.text.body.error=An error has occurred during the Re3gistry installation, please review your settings.
mail.text.subject.newregistration=Re3gistry - Your account have been successfully added to the system
mail.text.error.newregistration=Dear {name}, <br/><br/> Your account has been successfully created and it is now enabled.<br/><br/>You can access the management interface using the following credentials:<br/> Username: {email} <br/>Key: {key}<br/><br/> Please change your key after the first access.<br/>
mail.text.option.newregistration=Dear {name}, <br/><br/> Your account has been successfully created.<br/><br/>You can access the management interface using the following credentials:<br/> Username: {email} <br/>Key: {key}<br/><br/> Please change your key after the first access.<br/> You can activate your account following this <a class="nav-link" href="{acceptLink}" role="tab">link</a>.</br>If you don\u2019t want to accept this invitation please follow this <a class="nav-link" href="{deleteLink}" role="tab">link</a>. to remove your user.<br/>

mail.text.subject.bulkimport.success=Re3gistry - bulk import {itemclass} success
mail.text.subject.bulkimport.error=Re3gistry - bulk import {itemclass} error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ index.text.solrindexer.temporary-text=This is a temporary panel to test the Solr

about.label.title=About

activate.label.title=User enabled
activate.body=Your user has been enabled!
activate.body.login=You can login pressing <a href=".{0}">here</a>.
userDeleted.body=Your account has been succesfully deleted.

help.label.title=Help

status.label.title=Status
Expand Down
2 changes: 2 additions & 0 deletions sources/Re3gistry2/src/main/resources/shiro.ini
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@ securityManager.realms = $jdbcRealm
/help/** = anon
/login = authc
/logout = logout
/activate = anon
/userDeleted = anon
/** = authc
73 changes: 73 additions & 0 deletions sources/Re3gistry2/src/main/webapp/jsp/activate.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<%--
/*
* Copyright 2007,2016 EUROPEAN UNION
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* https://ec.europa.eu/isa2/solutions/european-union-public-licence-eupl_en
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
* Date: 2020/05/11
* Authors:
* European Commission, Joint Research Centre - jrc-inspire-support@ec.europa.eu
*
* This work was supported by the Interoperability solutions for public
* administrations, businesses and citizens programme (http://ec.europa.eu/isa2)
* through Action 2016.10: European Location Interoperability Solutions for e-Government (ELISE)
*/
--%>
<%@page import="java.util.ResourceBundle"%>
<%@page import="java.text.MessageFormat"%>
<%@page import="eu.europa.ec.re3gistry2.base.utility.UserHelper"%>
<%@page import="eu.europa.ec.re3gistry2.crudimplementation.RegItemRegGroupRegRoleMappingManager"%>
<%@page import="eu.europa.ec.re3gistry2.model.RegGroup"%>
<%@page import="java.util.HashMap"%>
<%@page import="eu.europa.ec.re3gistry2.base.utility.BaseConstants"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="eu.europa.ec.re3gistry2.base.utility.PersistenceFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="includes/common.inc.jsp" %>
<jsp:useBean id="constants" class="eu.europa.ec.re3gistry2.base.utility.BaseConstants" scope="session"/>

<% // Checking if the current user has the rights to add a new itemclass
ResourceBundle localization = (ResourceBundle) request.getAttribute(BaseConstants.KEY_REQUEST_LOCALIZATION);
%>
<!DOCTYPE html>
<html lang="${localization.getString("property.localeid")}" role="document">
<%@include file="includes/head.inc.jsp" %>
<body>
<%@include file="includes/header.inc.jsp"%>

<div class="container">

<div class="row">
<div class="col">
<h1 class="page-heading">${localization.getString("activate.label.title")}</h1>
</div>
</div>

<div class="row">
<div class="col">

<p>${localization.getString("activate.body")}</p>
<p><%=MessageFormat.format(localization.getString("activate.body.login"), WebConstants.PAGE_URINAME_LOGIN)%></p>

</div>
</div>

<hr/>

</div>

<%@include file="includes/footer.inc.jsp" %>
<%@include file="includes/pageend.inc.jsp" %>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@
</tbody>
</table>
<div class="col-sm-3">

<button class="btn btn-success width100" id="mySaveButton"><i class="far fa-save"></i> ${localization.getString("label.savechanges")}</button>
</div>
<%
Expand Down
Loading

0 comments on commit 69c3314

Please sign in to comment.