How to assign the current user id to post api in dreamfactory?


#1

(DSP 2.1.0, Mysql DB)
I have a table as tw_venue which contains the venue details fields and added user_id field as following snapshot

The thing wants to happen to me is,The user_id field will dynamically add to the table while i’l call the api
For instance
http://localhost:81/api/v2/treat_well/_table/tw_venue
{
“resource”: [
{
“name”: “Rest Poinjhtd”,
“type_id” : “1”,
“description”:“The comfort place to take rest with our care.”,
“added_date”:“2016-02-08”
}
]
}
here i want to append the “user_id”:{user.id} by DSP while calling this api. And this is not only for this table i have many tables to add the user_id field while calling the specific apis(via POST). Where is the place to do this context in DSP @benbusse @drewpearce ?

THANKS…


#2

Hi,

I suppose it can be done with event scripts pre-process http://wiki.dreamfactory.com/DreamFactory/Tutorials#Scripting

// POST /api/v2/treat_well/_table/tw_venue triggers script treat_well._table.tw_venue.post.pre_process
// This script runs BEFORE records are written to the db.
// records are in array event.request.payload.resource.

var lodash = require("lodash.min.js");

if (event.request.payload.resource) {
    lodash._.each(event.request.payload.resource, function( record ) {
        record.user_id: platform.session.user.id
    });
}

And maybe you can make things even simpler to use Data Mesh http://blog.dreamfactory.com/working-with-virtually-related-data-0


#3

@juniorconte
I have more than ten tables to add the user_id by back-end while calling the api . As you have said do i need to add this ** pre_process script** to all tables? it seems repeated work a lot… Do you have any solution to solve this context by reducing to write a script for each one?


#4

I developed a script that operate under all tables from treat_well._table.{table_name}.post.pre_process
I do not know how it will behave with requests that include related or data_mesh. To direct requests tables worked well.

// POST /api/v2/treat_well/_table triggers script treat_well._table.{table_name}.post.pre_process
// This script runs BEFORE records are written to the treat_well.
// records are in array event.request.payload.

var lodash = require("lodash.min.js");

function tableContainUserId(tableName) {
    var schema = platform.api.get('treat_well/_schema/' + tableName);
    
    return lodash._.chain(schema.content.field)
        .pluck('name')
        .indexOf('user_id')
        .value() > -1;
}

if (event.request.payload && tableContainUserId(event.resource)) {
    lodash._.each(event.request.payload, function(record) {
        record.user_id = platform.session.user.id;
    });
}

Expression NOW() not working DF 2.2
#5

I may be misunderstanding the request, but typically you would just include the user id lookup in the call.

{
“resource”: [
{
“name”: “Rest Poinjhtd”,
“type_id” : “1”,
“description”:“The comfort place to take rest with our care.”,
“added_date”:“2016-02-08”,
“user_id”:"{user.id}"
}
]
}


#6

:open_mouth: OMG, I did not know took to do it. Thank @drewpearce


#7

A note, if it is critical to identify the user, it would be better to do through pre_proccess script. For through the payload “{user.id}” You can enter an arbitrarily ID manually, without a guarantee of being the user id of the session.