fix idNot auth connexion (#70)

This commit is contained in:
Arnaud D. Natali 2023-09-20 18:49:55 +02:00 committed by GitHub
commit 721f5b6cb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 33 deletions

View File

@ -93,8 +93,6 @@ export default class DocumentsController extends ApiController {
try { try {
//init Document resource with request body values //init Document resource with request body values
const documentEntity = Document.hydrate<Document>(req.body); const documentEntity = Document.hydrate<Document>(req.body);
console.log(documentEntity);
//validate document //validate document
await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false }); await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false });

View File

@ -90,7 +90,6 @@ export default class FilesController extends ApiController {
//init File resource with request body values //init File resource with request body values
const fileEntity = File.hydrate<File>(JSON.parse(req.body["q"])); const fileEntity = File.hydrate<File>(JSON.parse(req.body["q"]));
console.log(fileEntity);
//validate File //validate File
// await validateOrReject(fileEntity, { groups: ["createFile"] }); // await validateOrReject(fileEntity, { groups: ["createFile"] });

View File

@ -7,7 +7,6 @@ import { JwtPayload } from "jsonwebtoken";
import IdNotService from "@Services/common/IdNotService/IdNotService"; import IdNotService from "@Services/common/IdNotService/IdNotService";
@Controller() @Controller()
@Service() @Service()
export default class UserController extends ApiController { export default class UserController extends ApiController {
@ -29,6 +28,10 @@ export default class UserController extends ApiController {
const idNotToken = await this.idNotService.getIdNotToken(code); const idNotToken = await this.idNotService.getIdNotToken(code);
const user = await this.idNotService.getOrCreateUser(idNotToken); const user = await this.idNotService.getOrCreateUser(idNotToken);
if(!user) {
this.httpUnauthorized(response);
return;
}
await this.idNotService.updateUser(user.uid); await this.idNotService.updateUser(user.uid);
await this.idNotService.updateOffice(user.office_uid); await this.idNotService.updateOffice(user.office_uid);
@ -77,7 +80,7 @@ export default class UserController extends ApiController {
let accessToken; let accessToken;
this.authService.verifyRefreshToken(token, (err, userPayload) => { this.authService.verifyRefreshToken(token, (err, userPayload) => {
if (err) { if (err) {
console.log(err) console.log(err);
this.httpUnauthorized(response); this.httpUnauthorized(response);
return; return;
} }

View File

@ -39,7 +39,7 @@ export default class CronService {
} }
} }
public async updateUsers() { public async updateUsers() {
const cronJob = new CronJob("*/15 * * * *", async () => { // Every 15 minutes const cronJob = new CronJob("0 0 * * *", async () => { // Once a day at midnight
try { try {
await this.idNotService.updateOffices(); await this.idNotService.updateOffices();
await this.idNotService.updateUsers(); await this.idNotService.updateUsers();

View File

@ -62,6 +62,9 @@ interface IOfficeData {
statutEntite: { statutEntite: {
name: string; name: string;
}; };
typeEntite: {
name: string;
}
} }
interface IOfficeLocation { interface IOfficeLocation {
@ -165,19 +168,43 @@ export default class IdNotService extends BaseService {
const searchParams = new URLSearchParams({ const searchParams = new URLSearchParams({
key: this.variables.IDNOT_API_KEY, key: this.variables.IDNOT_API_KEY,
}); });
const userRawData = await (await fetch( let userData = await (await fetch(
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${user.idNot}_${user.office_membership!.idNot}?` +
searchParams,
{
method: "GET",
},
)).json() as IRattachementData;
if (!userData.statutDuRattachement) {
const rattachements = await (await fetch(
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` + `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` +
searchParams, searchParams,
{ {
method: "GET", method: "GET",
}, },
)).json() as any; )).json() as any;
if (userRawData.totalResultCount === 0) { if (rattachements.totalResultCount === 0) {
await this.userService.updateCheckedAt(user.uid!); await this.userService.updateCheckedAt(user.uid!);
//await this.userService.delete(user.uid!); //await this.userService.delete(user.uid!);
return; return;
} }
const userData = userRawData.result[0] as IRattachementData; const rattachementsResults = rattachements.result as IRattachementData[];
rattachementsResults.forEach(async (rattachement) => {
if (rattachement.statutDuRattachement) {
const officeData = await (await fetch(
`${this.variables.IDNOT_API_BASE_URL + rattachement.entiteUrl}?` +
searchParams,
{
method: "GET",
},
)).json() as IOfficeData;
if(officeData.typeEntite.name === "office") {
userData = rattachement;
}
}
});
}
const roleFromIdNot = await this.getRole(userData.typeLien.name); const roleFromIdNot = await this.getRole(userData.typeLien.name);
let updates = 0; let updates = 0;
@ -186,24 +213,22 @@ export default class IdNotService extends BaseService {
user.role = roleFromIdNot; user.role = roleFromIdNot;
} }
if (user.office_membership!.idNot !== userData.entiteUrl.split("/")[5]!) { if (user.office_membership!.idNot !== userData.entite.ou) {
updates++; updates++;
let officeData = (await this.officeService.get({ where: { idNot: userData.entiteUrl.split("/")[5]! } }))[0]; let officeData = (await this.officeService.get({ where: { idNot:userData.entite.ou } }))[0];
if (!officeData) { if (!officeData) {
const officeIdNotData = (await ( const officeLocationData = (await (
await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entiteUrl}?` + searchParams, { method: "GET" }) await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" })
).json()) as IOfficeData; ).json()) as IOfficeLocation;
const office = { const office = {
idNot: userData.entiteUrl.split("/")[5]!, idNot: userData.entite.ou,
name: officeIdNotData.denominationSociale name: userData.entite.denominationSociale,
? officeIdNotData.denominationSociale crpcen: userData.entite.codeCrpcen,
: `office ${userData.entiteUrl.split("/")[5]!}`, office_status: this.getOfficeStatus(userData.entite.statutEntite.name),
crpcen: officeIdNotData.codeCrpcen,
office_status: this.getOfficeStatus(officeIdNotData.statutEntite.name),
address: { address: {
address: officeIdNotData.departementResidence[0]!.libelle, //officeLocationData.result[0]!.adrPostale4, address: officeLocationData.result[0]!.adrGeo4,
city: "city", //officeLocationData.result[0]!.adrPostaleVille, city: officeLocationData.result[0]!.adrGeoVille.split(" ")[0] ?? officeLocationData.result[0]!.adrGeoVille, //officeLocationData.result[0]!.adrPostaleVille,
zip_code: Number(officeIdNotData.departementResidence[0]!.code), zip_code: Number(officeLocationData.result[0]!.adrGeoCodePostal),
created_at: null, created_at: null,
updated_at: null, updated_at: null,
}, },
@ -272,10 +297,19 @@ export default class IdNotService extends BaseService {
}) })
).json()) as IRattachementData; ).json()) as IRattachementData;
if(!userData.statutDuRattachement || userData.entite.typeEntite.name !== "office") {
return null;
}
const officeLocationData = (await ( const officeLocationData = (await (
await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" }) await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" })
).json()) as IOfficeLocation; ).json()) as IOfficeLocation;
// if(officeLocationData.result[0]!.adrGeoCodePostal.slice(0,2) !== "35") {
// return null;
// }
const role = await this.getRole(userData.typeLien.name); const role = await this.getRole(userData.typeLien.name);
const userToAdd = { const userToAdd = {