Skip to content

[wip] promote entities deep #94

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
33 changes: 20 additions & 13 deletions cds-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ const { hasPersonalData } = require('./lib/utils')

const WRITE = ['CREATE', 'UPDATE', 'DELETE']

const _get_ancestry_of = (entity, service, ancestors = []) => {
for (const each of service.entities) {
for (const k in each.compositions) {
if (each.compositions[k].target === entity.name && k !== 'SiblingEntity') {
ancestors.push(each)
_get_ancestry_of(each, service, ancestors)
}
}
}
return ancestors
}

/*
* Add generic audit logging handlers
*/
Expand All @@ -15,24 +27,19 @@ cds.on('served', services => {
for (const service of services) {
if (!(service instanceof cds.ApplicationService)) continue

const relevantEntities = []
for (const entity of service.entities) if (hasPersonalData(entity)) relevantEntities.push(entity)
if (!relevantEntities.length) continue

// automatically promote entities that are associated with data subjects
for (const entity of relevantEntities) {
for (const entity of service.entities) {
if (entity['@PersonalData.EntitySemantics'] !== 'DataSubject') continue
for (const e of service.entities) {
for (const k in e.associations) {
if (e.associations[k].target === entity.name && k !== 'SiblingEntity') {
e['@PersonalData.EntitySemantics'] ??= 'Other'
e.associations[k]['@PersonalData.FieldSemantics'] ??= 'DataSubjectID'
if (!relevantEntities.includes(e)) relevantEntities.push(e)
}
}
const ancestors = _get_ancestry_of(entity, service)
for (const each of ancestors) {
each['@PersonalData.EntitySemantics'] ??= 'Other'
}
}

const relevantEntities = []
for (const entity of service.entities) if (hasPersonalData(entity)) relevantEntities.push(entity)
if (!relevantEntities.length) continue

for (const entity of relevantEntities) {
/*
* data access
Expand Down
10 changes: 10 additions & 0 deletions test/personal-data/deep.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const cds = require('@sap/cds')

cds.test('serve', 'srv/deep-service.cds').in(__dirname)

describe('personal data audit logging for deep operations', () => {
test('promotions', async () => {
const { DeepService } = cds.services

Check failure on line 7 in test/personal-data/deep.test.js

View workflow job for this annotation

GitHub Actions / test (20.x)

'DeepService' is assigned a value but never used

Check failure on line 7 in test/personal-data/deep.test.js

View workflow job for this annotation

GitHub Actions / test (18.x)

'DeepService' is assigned a value but never used
debugger

Check failure on line 8 in test/personal-data/deep.test.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Unexpected 'debugger' statement

Check failure on line 8 in test/personal-data/deep.test.js

View workflow job for this annotation

GitHub Actions / test (18.x)

Unexpected 'debugger' statement
})
})
41 changes: 41 additions & 0 deletions test/personal-data/srv/deep-service.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
service DeepService {

entity Foo {
key ID : UUID;
bars : Composition of many Bar
on bars.foo = $self;
moos : Composition of many {
key ID : UUID;
descr : String;
shus : Composition of many {
key ID : UUID;
descr : String;
};
};
};

entity Bar {
key ID : UUID;
foo : Association to Foo;
descr : String;
bazs : Composition of many Baz
on bazs.bar = $self;
};

entity Baz {
key ID : UUID;
bar : Association to Bar;
descr : String;
};

annotate Baz with @PersonalData: {EntitySemantics: 'DataSubject'} {
ID @PersonalData : {FieldSemantics: 'DataSubjectID', };
descr @PersonalData.IsPotentiallyPersonal;
};

annotate Foo.moos.shus with @PersonalData: {EntitySemantics: 'DataSubject'} {
ID @PersonalData : {FieldSemantics: 'DataSubjectID', };
descr @PersonalData.IsPotentiallyPersonal;
};

}
Loading