Skip to content

Commit

Permalink
Merge pull request #103 from Luis-Cruz/feature/CgdWebServiceIntegration
Browse files Browse the repository at this point in the history
Added integration with CGD for production of ULisboa IdCards.
  • Loading branch information
pedrosan7os committed Sep 4, 2015
2 parents fec30af + 4f34643 commit 809b283
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 6 deletions.
5 changes: 5 additions & 0 deletions fenixedu-ist-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@
<artifactId>bennu-oauth</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>pt.ist</groupId>
<artifactId>fenixedu-ulisboa-cgdIntegration</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class cgd.CgdCardCounter {

class cgd.CgdCard {
int serialNumber;
boolean allowSendDetails;
DateTime successfulSentData;
}

class student.importation.DgesBaseProcess extends .org.fenixedu.academic.domain.QueueJobWithFile {
Expand Down Expand Up @@ -266,4 +268,5 @@ relation RemoteSystemRemoteHost {
relation RemoteSystemDomainRoot {
RemoteSystem playsRole remoteSystem;
.pt.ist.fenixframework.DomainRoot playsRole domainRoot;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@
*/
package pt.ist.fenixedu.integration.domain.cgd;

import java.time.Year;

import org.fenixedu.academic.domain.Person;
import org.fenixedu.academic.domain.student.Registration;
import org.fenixedu.academic.domain.student.Student;
import org.fenixedu.bennu.core.domain.User;
import org.fenixedu.bennu.core.security.Authenticate;

import com.qubit.solution.fenixedu.integration.cgd.services.form43.CgdForm43Sender;

import pt.ist.fenixframework.Atomic;
import pt.ist.fenixframework.FenixFramework;

public class CgdCard extends CgdCard_Base {

Expand All @@ -42,4 +53,77 @@ protected static String fillLeftString(final String string, final char c, final
return stringBuilder.toString();
}

static int yearFor(final String cardSerialNumber) {
return Integer.parseInt(cardSerialNumber.substring(0, 2));
}

static int serialNumberFor(final String memberId) {
return Integer.parseInt(memberId.substring(2));
}

@Atomic
public static CgdCard setGrantAccess(final boolean allowAccess) {
final User user = Authenticate.getUser();
if (user != null) {
final int year = Year.now().getValue();
final CgdCard card = findCardFor(user, year, allowAccess);
if (card != null) {
card.setAllowSendDetails(allowAccess);
if (allowAccess) {
return card;
}
}
}
return null;
}

private static CgdCard findCardFor(final User user, final int year, final boolean createIfNotExists) {
final CgdCard card = user.getCgdCardSet().stream().filter(c -> c.getCgdCardCounter().getYear() == year).findAny().orElse(null);
return card == null && createIfNotExists ? CgdCardCounter.findCounterForYear(year).createNewSerialNumber(user) : card;
}

public static boolean hasCGDAccessResponse() {
final User user = Authenticate.getUser();
final int year = Year.now().getValue();
return user != null && user.getCgdCardSet().stream().anyMatch(c -> c.getCgdCardCounter().getYear() == year);
}

private static class Sender extends Thread {

private final String cardId;
public Sender(final String externalId) {
this.cardId = externalId;
}

@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
doit();
}

@Atomic
private void doit() {
final CgdCard card = FenixFramework.getDomainObject(cardId);
final Person person = card.getUser().getPerson();
if (person != null) {
final Student student = person.getStudent();
if (student != null) {
for (final Registration registration : student.getRegistrationsSet()) {
if (registration.isActive()) {
new CgdForm43Sender().sendForm43For(registration);
}
}
}
}
}

}

public void send() {
new Sender(this.getExternalId()).start();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,29 @@ public static String getNextSerialNumber(final User user) {
}

private String nextSerialNumber(final User user) {
return user.getCgdCardSet().stream().filter(c -> c.getCgdCardCounter() == this).findAny()
.orElse(createNewSerialNumber(user)).getSerialNumberForCard();
return user.getCgdCardSet().stream().filter(c -> c.getCgdCardCounter() == this)
.findAny()
.orElseGet(() -> createNewSerialNumber(user)).getSerialNumberForCard();
}

private CgdCard createNewSerialNumber(User user) {
CgdCard createNewSerialNumber(User user) {
final int count = getCount() + 1;
setCount(count);
return new CgdCard(this, user, count);
}

private static CgdCardCounter getCounterForYear(final int year) {
static CgdCardCounter getCounterForYear(final int year) {
CgdCardCounter counter = findCounterForYear(year);
return counter == null ? new CgdCardCounter(year) : counter;
}

static CgdCardCounter findCounterForYear(final int year) {
for (final CgdCardCounter counter : Bennu.getInstance().getCgdCardCounterSet()) {
if (counter.getYear() == year) {
return counter;
}
}
return new CgdCardCounter(year);
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pt.ist.fenixedu.integration.domain.cgd;

import java.time.Year;

import org.fenixedu.academic.domain.Person;
import org.fenixedu.bennu.core.domain.User;

import com.qubit.solution.fenixedu.integration.cgd.webservices.resolver.memberid.IMemberIDAdapter;

public class NumberObfuscationAdaptor implements IMemberIDAdapter {

@Override
public String retrieveMemberID(final Person person) {
final User user = person.getUser();
return CgdCardCounter.getNextSerialNumber(user);
}

@Override
public Person readPerson(final String memberId) {
final CgdCardCounter counter = CgdCardCounter.findCounterForYear(CgdCard.yearFor(memberId));
final int serialNumber = CgdCard.serialNumberFor(memberId);
return counter == null ? null : counter.getCgdCardSet().stream().filter(c -> c.getSerialNumber() == serialNumber)
.map(c -> c.getUser().getPerson()).findAny().orElse(null);
}

@Override
public boolean isAllowedAccessToMember(final Person person) {
final User user = person.getUser();
if (user != null) {
final int year = Year.now().getValue();
for (final CgdCard card : user.getCgdCardSet()) {
if (card.getAllowSendDetails() && card.getCgdCardCounter().getYear() == year) {
return true;
}
}
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright © 2013 Instituto Superior Técnico
*
* This file is part of FenixEdu IST Integration.
*
* FenixEdu IST Integration is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FenixEdu IST Integration is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FenixEdu IST Integration. If not, see <http://www.gnu.org/licenses/>.
*/
package pt.ist.fenixedu.integration.ui.spring.controller;

import org.fenixedu.bennu.spring.portal.SpringApplication;
import org.fenixedu.bennu.spring.portal.SpringFunctionality;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import pt.ist.fenixedu.integration.domain.cgd.CgdCard;

@SpringApplication(group = "#logged", path = "authorize-personal-data-access", title = "authorize.personal.data.access.title")
@SpringFunctionality(app = AuthorizePersonalDataAccessController.class, title = "authorize.personal.data.access.title")
@Controller
@RequestMapping("/authorize-personal-data-access")
public class AuthorizePersonalDataAccessController {

@RequestMapping(method = RequestMethod.POST)
public String setCandiadteCGDAccess(@RequestParam(required = false) Boolean allowAccess, @RequestParam(required = false) String qs, Model model) {
final boolean authorize = allowAccess != null && allowAccess.booleanValue();
final CgdCard card = CgdCard.setGrantAccess(authorize);
if (card != null) {
card.send();
}
return "redirect:" + qs;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ oauthapps.label.terms.checksum = Control code:{0}
oauthapps.label.terms.generated.date = This page was generated on {0}
oauthapps.text.manage.applications.agree.terms = I have read and agree to the FenixAPI Service Agreement describe above.
oauthapps.text.manage.applications.register = To register external application and have access to the API it is necessary to accept the following service agreement:
authorize.personal.data.access.title = Manage Data Access
print = Print
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ oauthapps.label.terms.checksum = Control code:{0}
oauthapps.label.terms.generated.date = This page was generated on {0}
oauthapps.text.manage.applications.agree.terms = I have read and agree to the FenixAPI Service Agreement describe above.
oauthapps.text.manage.applications.register = To register external application and have access to the API it is necessary to accept the following service agreement:
authorize.personal.data.access.title = Manage Data Access
print = Print
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ oauthapps.label.terms.checksum = Código de controlo:{0}
oauthapps.label.terms.generated.date = Esta página foi gerada em {0}
oauthapps.text.manage.applications.agree.terms = Eu li e aceito o contrato de serviço descrito acima.
oauthapps.text.manage.applications.register = Para registar aplicações externas e ter acesso à API é necessário aceitar os seguintes termos de utilização:
authorize.personal.data.access.title = Gerir Cedência de Dados
print = Imprimir

0 comments on commit 809b283

Please sign in to comment.