Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajout de l'anonymisation des données de demande #702

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
Expand All @@ -35,8 +34,8 @@ public class InformationRequest implements Serializable{
private static final long serialVersionUID = 5439786730972374577L;

@Id
@SequenceGenerator(name = "HibernateSequence", sequenceName = "hibernate_sequence", initialValue = 0, allocationSize = 1)
@GeneratedValue(generator = "HibernateSequence")
@SequenceGenerator(name = "RequestIdHibernateSequence", sequenceName = "hibernate_sequence", initialValue = 0, allocationSize = 1)
@GeneratedValue(generator = "RequestIdHibernateSequence")
private long requestId;

@ManyToOne(optional=false, fetch = FetchType.EAGER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class ObjectRequest implements Serializable {
private static final long serialVersionUID = 5439786730972374577L;

@Id
@SequenceGenerator(name = "HibernateSequence", sequenceName = "hibernate_sequence", initialValue = 0, allocationSize = 1)
@GeneratedValue(generator = "HibernateSequence")
@SequenceGenerator(name = "ObjectIdHibernateSequence", sequenceName = "hibernate_sequence", initialValue = 0, allocationSize = 1)
@GeneratedValue(generator = "ObjectIdHibernateSequence")
private long objectId;

@Column(name = "type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class UserRequest implements Serializable {
private static final long serialVersionUID = -9140660737315556020L;

@Id
@SequenceGenerator(name = "HibernateSequence", sequenceName = "hibernate_sequence", initialValue = 0, allocationSize = 1)
@GeneratedValue(generator = "HibernateSequence")
@SequenceGenerator(name = "UserIdHibernateSequence", sequenceName = "hibernate_sequence", initialValue = 0, allocationSize = 1)
@GeneratedValue(generator = "UserIdHibernateSequence")
@Column(name="userid")
private long userId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import org.georchestra.cadastrapp.model.request.InformationRequest;
import org.georchestra.cadastrapp.model.request.UserRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

Expand Down Expand Up @@ -56,5 +57,20 @@ public interface RequestRepository
@Query(value="select COALESCE(SUM(ir.objectNumber),0) from InformationRequest ir inner join ir.user u where u.cni= ?1 and u.type = ?2 and ir.requestDate >= ?3")
int sumObjectNumberByUserCniAndUserTypeAndRequestDateAfter(String cni, String type, Date date);

/**
* Return all informationRequest done before the specified date (exclusive)
*
* @param date The date to use
* @return The list of informationRequest done before the date
*/
List<InformationRequest> findAllByRequestDateBefore(Date date);

/**
* Count number of request between start and end dates (inclusive) by user
*
* @param start The start date to use
* @param end The end date to use
* @return Number of request between start and end dates (inclusive) by user
*/
int countByRequestDateBetweenAndUser(Date start, Date end, UserRequest user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.georchestra.cadastrapp.scheduler;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.List;

import javax.xml.bind.DatatypeConverter;

import org.georchestra.cadastrapp.model.request.InformationRequest;
import org.georchestra.cadastrapp.model.request.ObjectRequest;
import org.georchestra.cadastrapp.model.request.UserRequest;
import org.georchestra.cadastrapp.repository.ObjectRequestRepository;
import org.georchestra.cadastrapp.repository.RequestRepository;
import org.georchestra.cadastrapp.repository.UserRequestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("anonymizationSchedulerBean")
public class AnonymizationScheduler {
@Autowired
RequestRepository requestRepository;

@Autowired
ObjectRequestRepository objectRequestRepository;

@Autowired
UserRequestRepository userRequestRepository;

public void anonymize() throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("MD5");

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -30);
if(cal.after(Calendar.getInstance())){
cal.roll(Calendar.YEAR, -1);
}

List<InformationRequest> ls = requestRepository.findAllByRequestDateBefore(cal.getTime());

for (InformationRequest informationRequest : ls) {
UserRequest user = informationRequest.getUser();
if( requestRepository.countByRequestDateBetweenAndUser(cal.getTime(),Calendar.getInstance().getTime(),user) == 0 ) {
user.setAdress(null);
user.setCni(null);
user.setCodePostal(null);
user.setCommune(null);
user.setFirstName(null);
user.setLastName(null);
user.setMail(null);
user.setType(null);
userRequestRepository.save(user);
}

for (ObjectRequest objectRequest : informationRequest.getObjectsRequest()) {
String prop = objectRequest.getProprietaire();
if( !prop.startsWith("{HASH}") ) {
String hash = DatatypeConverter.printHexBinary(md.digest(prop.getBytes("UTF-8"))).toUpperCase();
objectRequest.setProprietaire("{HASH}"+hash);
objectRequestRepository.save(objectRequest);
}
}
}
}
}
3 changes: 3 additions & 0 deletions cadastrapp/src/main/resources/cadastrapp.properties
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,8 @@ purge.hours=24
# See http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html for example
purge.cronExpression=0 0 * * * ?

# Cron expression to launch information request anonymization
anonymization.cronExpression=0 0 0 L * *



12 changes: 11 additions & 1 deletion cadastrapp/src/main/webapp/WEB-INF/beans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd">

<context:component-scan base-package="org.georchestra.cadastrapp" />
<mvc:annotation-driven />
Expand Down Expand Up @@ -144,4 +146,12 @@
</property>
</bean>

<!-- ============================= -->
<!-- Request's database Anonymization -->
<!-- 0 0 0 L * * => last day of the month at midnight -->
<!-- ============================= -->
<task:scheduled-tasks scheduler="anonymizationScheduler">
<task:scheduled ref="anonymizationSchedulerBean" method="anonymize" cron="${anonymization.cronExpressions}" />
</task:scheduled-tasks>
<task:scheduler id="anonymizationScheduler"/>
</beans>