# How to create users while registering with different roles via API?

**URL:** <https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190>\
**Category:** Users & Roles\
**Created:** [February 14, 2016, 10:21am UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190 "2016-02-14T10:21:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramnew2006](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/ramnew2006/32/580_2.png) [@ramnew2006](https://community.dreamfactory.com/u/ramnew2006)\
**Post date:** [February 14, 2016, 10:21am UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/1 "2016-02-14T10:21:05Z")

</div>

Hi everyone,

I am trying to call the API and create users. I have created two roles Role 1 and Role 2. For some users I want to assign Role 1 and for some users Role 2. The use case is something like my application will have vendors and customers. From the vendor app the default role for user registration should be role 1 and for customer app it should be role 2. I tried different methods mentioned in the forum but nothing works.

I tried the script on Post process, tried sending a post request to /user/register?role\_id=2 and other stuff too but nothing seem to be working. I tried this post as well [https://blog.dreamfactory.com/adding-users-to-your-dsp](https://blog.dreamfactory.com/adding-users-to-your-dsp)

Can someone give me a link or tell me the procedure on creating users with different roles using API?

Thanks

---

<div class="post-metadata">

**Author:** ![juniorconte](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/juniorconte/32/1007_2.png) [@juniorconte](https://community.dreamfactory.com/u/juniorconte)\
**Post date:** [February 14, 2016, 4:05pm UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/2 "2016-02-14T16:05:06Z")

</div>

Hello, @ramnew2006  
Users will be registered by an Admin? Or will register themselves from another app?

best regards,

---

<div class="post-metadata">

**Author:** ![ramnew2006](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/ramnew2006/32/580_2.png) [@ramnew2006](https://community.dreamfactory.com/u/ramnew2006)\
**Post date:** [February 14, 2016, 4:19pm UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/3 "2016-02-14T16:19:14Z")

</div>

Hi @juniorconte users have to register by themselves not by an admin.

Let’s say there are two mobile apps for my company where the customers register themselves and raise certain requests. There is another mobile app which the vendors use to accept the requests raised by customers. So when the user gets registered from customer app they should be assigned customer role where as it will be vendor role for the other mobile app. Hope this makes sense.

---

<div class="post-metadata">

**Author:** ![juniorconte](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/juniorconte/32/1007_2.png) [@juniorconte](https://community.dreamfactory.com/u/juniorconte)\
**Post date:** [February 14, 2016, 8:17pm UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/4 "2016-02-14T20:17:49Z")

</div>

Hi, @ramnew2006

I understand your scenario.  
I follow a path that should have worked, I think may be a bug, @formerstaff could confirm this.

1 - I created an event script user.register.post.post\_process

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

var role1Id = 2;
var role2Id = 3;

var appVendorId = 2;
var appCustomerId = 3;

if (!!event.request.payload.type && !!platform.session.user) {
  var userObject = platform.api.get('system/user/'+platform.session.user.id+'?related=user_to_app_to_role_by_user_id');

  lodash._.each(userObject.user_to_app_to_role_by_user_id, function(userApp) {
    switch (event.request.payload.type) {
      case 'customer':
        if (userApp.app_id === appCustomerId) {
          userApp.role_id = role1Id;
        }
        break;
      case 'vendor':
        if (userApp.app_id === appVendorId) {
          userApp.role_id = role2Id;
        }
        break;
     }
   });

  platform.api.patch('system/user/'+platform.session.user.id+'?related=user_to_app_to_role_by_user_id', userObject);
}

```

2 - In addition to the standard body of the user registration request, I added the ‘type’ attribute to JSON

```auto
{
  "email": "string",
  "first_name": "string",
  "last_name": "string",
  "display_name": "string",
  "new_password": "string",
  "type": "string"
}

```

3 - To be available section during script execution, the query string passed login = true

```auto
http://localhost/api/v2/user/register?login=true

```

4 - I configured a role to access via SCRIPT to the system / user with GET and PATCH. **And it was here that something went wrong**.

Follows the settings made:

* * *

 ![](https://canada1.discourse-cdn.com/flex035/uploads/dreamfactory/original/1X/34a21bdd9f5f10885e5a6e9aed287e4e6f4a3743.jpg)

* * *

 ![](https://canada1.discourse-cdn.com/flex035/uploads/dreamfactory/original/1X/3d88327d53aced1c6d01e5e234ce576167fdaff3.jpg)

* * *

 ![](https://canada1.discourse-cdn.com/flex035/uploads/dreamfactory/original/1X/a49e70ed813cc03d2e572fdc58fe1f16f6be6416.jpg)

* * *

 ![](https://canada1.discourse-cdn.com/flex035/uploads/dreamfactory/original/1X/097a10c34dd6bcb131f9b27fae109241f46877ba.jpg)

* * *

If it is not a bug, I have no other idea how to do 😔

---

<div class="post-metadata">

**Author:** ![ramnew2006](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/ramnew2006/32/580_2.png) [@ramnew2006](https://community.dreamfactory.com/u/ramnew2006)\
**Post date:** [February 15, 2016, 7:43am UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/5 "2016-02-15T07:43:01Z")

</div>

I am not sure whether platform.session.user will be available for the first time registration? Correct me if i am wrong because I tried to get the user id using platform.api.get(“user/session”).user\_id but it says “There is no valid session token”

One curious thing I have noticed is a POST request made directly to “system/user” with “user\_to\_app\_to\_role\_by\_user\_id” : { “app\_id”: 1, “role\_id”: 1} in the user details payload is working perfectly fine and the user is being created with exact assignment of roles.

This whole system/user and user/register is quite confusing to me and I am not sure which one to use.

---

<div class="post-metadata">

**Author:** ![juniorconte](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/juniorconte/32/1007_2.png) [@juniorconte](https://community.dreamfactory.com/u/juniorconte)\
**Post date:** [February 15, 2016, 10:04am UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/6 "2016-02-15T10:04:03Z")

</div>

> [@ramnew2006](#):
>
> I am not sure whether platform.session.user will be available for the first time registration?

I think there is a session, as in the request, the user/73 “as the error log that have attached” was created in the registration request.

> [@ramnew2006](#):
>
> This whole system/user and user/register is quite confusing to me and I am not sure which one to use.

I think the problem is to allow the user to request this system / user via the API. He could to set permissions that should not have access. The user / register prevents it.

---

<div class="post-metadata">

**Author:** ![ramnew2006](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/ramnew2006/32/580_2.png) [@ramnew2006](https://community.dreamfactory.com/u/ramnew2006)\
**Post date:** [February 15, 2016, 10:25am UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/7 "2016-02-15T10:25:59Z")

</div>

I have setup a role as you have mentioned and assigned it to Open registration default role. Also created an app with this default role. Now, I have made the following API call.

```
$.ajax({
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    url: "http://api.doorstepsolutions.co.in"+ '/api/v2/user/register?login=true',
                    data: JSON.stringify({
                        "email": "storygag111@gmail.com",
                        "new_password": "password",
                        "type":"customer"
                    }),
                    cache:false,
                    method:'POST',
headers:{
"X-DreamFactory-API-Key":"API_KEY"
},
                    success:function (response) {
                        console.log(response);
                    },
                    error:function (response) {
                        console.log(response);
                        return false;
                    }
                });

```

I have also tried printing the event.response and also the Patch event result. The response says platform.session.user is null. I am not sure why but in my case platform.session.user is returning null instead of the id of the user.

---

<div class="post-metadata">

**Author:** ![juniorconte](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/juniorconte/32/1007_2.png) [@juniorconte](https://community.dreamfactory.com/u/juniorconte)\
**Post date:** [February 15, 2016, 4:14pm UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/8 "2016-02-15T16:14:22Z")

</div>

There is something strange about the user.register.post.post\_process. Even putting something simple like: event.response.content.foo = ‘bar’; foo does not return to the request.

Making a var\_dump (event.response.content); foo value appears on the console “tail -f …/apps/dreamfacotry/htdocs/storage/logs/dreamfacotry.log” . The same thing happens if I do a var\_dump (platform.session.user)

I will investigate further, and any news notice here. If I get to conclusion which is really a bug, I will open an issue in Git Hub.

My DreamFactory Version: 2.1.0

---

<div class="post-metadata">

**Author:** ![ramnew2006](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/ramnew2006/32/580_2.png) [@ramnew2006](https://community.dreamfactory.com/u/ramnew2006)\
**Post date:** [February 15, 2016, 6:56pm UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/9 "2016-02-15T18:56:57Z")

</div>

Sure thanks.

BTW My dreamfactory version is 2.0.4

---

<div class="post-metadata">

**Author:** ![juniorconte](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/juniorconte/32/1007_2.png) [@juniorconte](https://community.dreamfactory.com/u/juniorconte)\
**Post date:** [February 16, 2016, 12:10am UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/10 "2016-02-16T00:10:00Z")

</div>

Apparently it’s a bug and is already registered:

> <https://github.com/dreamfactorysoftware/dreamfactory/issues/32>
>
> Hi,
> 
> In the version 2.1.2 in the post\_process custom script the "event.response.…content" is not reflecting the data pushed to it.
> 
> This is how the issue can be replicated:
> \- create a table "test" on a mysql database
> \- set a script for the post process "db.\_table.test.post.post\_process", with this content:
> 
> \`\`\`
> event.response.status\_code = 400;
> event.response.content = {"test": "value"};
> \`\`\`
> 
> Push a record on the table. The expected result should be a 400 code response and {"test": "value"} as an output. 
> Instead, the return code is modified correctly, but not the response.content, that will return always the original response.
> 
> Please advise.
> Best regards
> 
> Gabriel

> [@Changes in response doesn't stick](https://community.dreamfactory.com/t/changes-in-response-doesnt-stick/2178):
>
> I’m having some difficulties trying to change the response from a GET request. In the get.post\_proccess for a given table I’m modifying the event.response object but the changes doesn’t stick! Here is a simple example to illustrate what is happening. If I use this script in the get.post\_process of a table, it has no effect: event.response = ""; OR event.response = {} (By “no effect” I mean I get all the records as if the response were not changed) This script has the same result (no effect t…

---

<div class="post-metadata">

**Author:** ![ramnew2006](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/ramnew2006/32/580_2.png) [@ramnew2006](https://community.dreamfactory.com/u/ramnew2006)\
**Post date:** [February 16, 2016, 5:36am UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/11 "2016-02-16T05:36:28Z")

</div>

I am not sure about changes to event.response not sticking. I was able to set event.response.resp\_check = true; statement and when I return the event.response resp\_check was there intact in the response printed in console.

The problem I have is about the platform.session.user and the patch event. For me platform.session.user returns null and not working because I have setup a check inside the if loop if(!!platform.session.user) which is not going through whereas the above resp\_check is set outside the loop and this is being set successfully.

So, event there is a bug with platform.session.user being null while the time of registering in the POST process script.

---

<div class="post-metadata">

**Author:** ![dreamfactorydev](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dreamfactorydev](https://community.dreamfactory.com/u/dreamfactorydev)\
**Post date:** [February 18, 2016, 7:17am UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/12 "2016-02-18T07:17:01Z")

</div>

@juniorconte  
Did you find any solution for this bug? I’also have been implemented it in my script tab. Actually i think that trouble arises while we calling this `platform.api.get('system/user/'+platform.session.user.id+'?related=user_to_app_to_role_by_user_id');`  
api it’s really not get the **userObject** in case of failure it returns **undefined** value. I have tested these api calls via post man it’s working.

---

<div class="post-metadata">

**Author:** ![dreamfactorydev](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dreamfactorydev](https://community.dreamfactory.com/u/dreamfactorydev)\
**Post date:** [February 18, 2016, 7:24am UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/13 "2016-02-18T07:24:44Z")

</div>

Did you find the solution for this context?

---

<div class="post-metadata">

**Author:** ![ramnew2006](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/ramnew2006/32/580_2.png) [@ramnew2006](https://community.dreamfactory.com/u/ramnew2006)\
**Post date:** [February 18, 2016, 7:26am UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/14 "2016-02-18T07:26:23Z")

</div>

I guess @formerstaff @benbusse needs to step in and give us an update on this 😛

---

<div class="post-metadata">

**Author:** ![dreamfactorydev](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dreamfactorydev](https://community.dreamfactory.com/u/dreamfactorydev)\
**Post date:** [February 18, 2016, 9:18am UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/15 "2016-02-18T09:18:20Z")

</div>

@formerstaff Can you say the trouble(error) of @juniorconte solution of this post.  
If you say the suggestions(any other solutions for role assignment via api) then it’l be useful us as go through DSP faster in our development.

Thanks…

---

<div class="post-metadata">

**Author:** ![leehicks](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/leehicks/32/55_2.png) [@leehicks](https://community.dreamfactory.com/u/leehicks)\
**Post date:** [February 18, 2016, 3:00pm UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/16 "2016-02-18T15:00:23Z")

</div>

@juniorconte When changing the content of the response you will need to set event.response.content\_changed = true before it will get copied back to the main process stream. This was to prevent copying content unnecessarily when handling events. See if that helps this scenario. We have someone looking into the big picture question right now.

---

<div class="post-metadata">

**Author:** ![juniorconte](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/juniorconte/32/1007_2.png) [@juniorconte](https://community.dreamfactory.com/u/juniorconte)\
**Post date:** [February 18, 2016, 4:34pm UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/17 "2016-02-18T16:34:29Z")

</div>

Hi @leehicks, I did some testing

setting **event.response.content\_changed = true** on post\_proccess script, the change was included in the request’s response.

```auto
event.response.content.foo = "bar";
event.response.content_changed = true;

```

**foo is exposed to client/req.**

* * *

I wonder if the idea of spending login = true for user / register, should create a valid session during post\_proccess the user request / register. That still does not fuincionou correctly, apparently the role is not applied correctly.

best regards,

---

<div class="post-metadata">

**Author:** ![juniorconte](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/juniorconte/32/1007_2.png) [@juniorconte](https://community.dreamfactory.com/u/juniorconte)\
**Post date:** [February 18, 2016, 5:13pm UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/18 "2016-02-18T17:13:55Z")

</div>

Hello everyone,  
I’m sorry, there were some errors in my previous script.

the correct **user.register.post.post\_process** is:

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

var role1Id = 5;
var role2Id = 6;

var appVendorId = 5;
var appCustomerId = 6;

if (!!event.request.payload.type && !!platform.session.user) {
  
  var userObject = platform.api.get('system/user/'+platform.session.user.id+'?related=user_to_app_to_role_by_user_id');
  
  lodash._.each(userObject.content.user_to_app_to_role_by_user_id, function(userApp) {
    switch (event.request.payload.type) {
      case 'customer':
        if (userApp.app_id === appCustomerId) {
          userApp.role_id = role1Id;
        }
        break;
      case 'vendor':
        if (userApp.app_id === appVendorId) {
          userApp.role_id = role2Id;
        }
        break;
     }
  });

  var req = platform.api.patch('system/user/'+platform.session.user.id+'?related=user_to_app_to_role_by_user_id', userObject.content);
  
}

```

And I create role named “public”, and assigned role on service User \> open registration role, And assigned public role on default role App.

On public role, Assign access to:

service: system  
component: user/\*  
access: GET, PATCH  
requestor: SCRIPT

Thats, it. Works fine. 😉

---

<div class="post-metadata">

**Author:** ![ramnew2006](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/ramnew2006/32/580_2.png) [@ramnew2006](https://community.dreamfactory.com/u/ramnew2006)\
**Post date:** [February 18, 2016, 5:32pm UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/19 "2016-02-18T17:32:07Z")

</div>

Still doesn’t work for me ☹

Both event.response.content\_changed and the patch event are not working still! 😭

---

<div class="post-metadata">

**Author:** ![juniorconte](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/juniorconte/32/1007_2.png) [@juniorconte](https://community.dreamfactory.com/u/juniorconte)\
**Post date:** [February 18, 2016, 5:40pm UTC](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190/20 "2016-02-18T17:40:24Z")

</div>

Ok, we have to check all the settings, but we know that is not a platform BUG.  
Here a few hours, I will prepare a material comprising all config points in detail.

[Next page](https://community.dreamfactory.com/t/how-to-create-users-while-registering-with-different-roles-via-api/2190.md?page=2)
