odatacdssap-cap

SAP CAP using cds.User attributes


I authenticate on the server by feeding req.user with an instance of cds.User, and I add some attributes:

User {
  id: '110226363079182595683',
  attr: { name: 'depth1', email: 'depth1@protonmail.com' },
  _roles: { 'identified-user': true, 'authenticated-user': true }
}

This allows me to call my CDS services with an authenticated user.
It works well.

Then, I have in my CDS schema an entity :

entity Comments {
    key ID      : Integer;
        project : Association to Projects;
        title  : String;
        text   : String;
        CreatedBy  : String  @cds.on.insert : $user;
        CreatedByName  : String  @cds.on.insert : $user.name;
}

My schema on an SQLite database. I start the server, I launch a UI5 application which allows to insert comments in OData V4 and here is what happens:

HTTP request:

--batch_id-1642708209182-45
Content-Type:application/http
Content-Transfer-Encoding:binary

POST Projects(1)/comments HTTP/1.1
Accept:application/json;odata.metadata=minimal;IEEE754Compatible=true
Accept-Language:fr-FR
Content-Type:application/json;charset=UTF-8;IEEE754Compatible=true

{"ID":0,"text":"test comment"}
--batch_id-1642708209182-45--
Group ID: $auto

Server Log:

[cds] - > CREATE Projects(1)/comments

HTTP Response:

--batch_id-1642708209182-45
content-type: application/http
content-transfer-encoding: binary

HTTP/1.1 201 Created
odata-version: 4.0
content-type: application/json;odata.metadata=minimal;IEEE754Compatible=true
location: Comments(101)

{"@odata.context":"../$metadata#Comments/$entity","ID":101,"project_ID":1,"title":null,"text":"test comment","CreatedBy":"110226363079182595683","CreatedByName":"depth1"}
--batch_id-1642708209182-45--

HTTP Request:

--batch_id-1642708209355-46
Content-Type:application/http
Content-Transfer-Encoding:binary

GET Projects(1)/comments(101)?$select=CreatedByName,ID,text HTTP/1.1
Accept:application/json;odata.metadata=minimal;IEEE754Compatible=true
Accept-Language:fr-FR
Content-Type:application/json;charset=UTF-8;IEEE754Compatible=true


--batch_id-1642708209355-46--
Group ID: $auto

Server log

[cds] - > READ Projects(1)/comments(101) { '$select': 'CreatedByName,ID,text' }

HTTP Response

--batch_id-1642708209355-46
content-type: application/http
content-transfer-encoding: binary

HTTP/1.1 200 OK
odata-version: 4.0
content-type: application/json;odata.metadata=minimal;IEEE754Compatible=true

{"@odata.context":"../$metadata#Comments(CreatedByName,ID,text)/$entity","CreatedByName":null,"ID":101,"text":"aaa"}
--batch_id-1642708209355-46--

In my Database, CreatedBy is filled but not CreatedByName. Also in the Create request i got the CreatedByName filled by the server and returned it's really strange.

How can i insert some cds.User attributes to the database ?! Thank you !


Solution

  • Oww I found a solution ! By adding a behavior in service.js

    const cds = require('@sap/cds')
    
    module.exports = cds.service.impl(function() {
        this.before('CREATE', 'Comments', fillData)
    })
    
    async function fillData(req) {
        req.data.CreatedByName = req.user.attr.name;
    }