Get users by role


#1

I am attempting to use the dreamfactory roles system as a grouping system but have hit a bit of a snag in so far as the fact that I cannot seem to figure out the correct API route to retrieve users by role (get all users with role ‘x’)

I have played around the API docs (specifically the /system/user and /system/role) but neither are returning the information, though there seems to be an indication that it should from the response schema documentation but the actual response does not contain any of the information (ie, the /system/roles says it will returned relatedApps and relatedUsers but it does not). I expect this may have something to do with the ‘related’ parameter but also cannot find any information or examples of what should be passed in for that.

In any event, all I really need is the appropriate method to use and/or example code on how to do this


#2

This will work, although it may return more than what you expected. Each user can have a different role for each app, so you’ll see duplicate user ids in the results.

GET http://localhost:8888/api/v2/system/role?related=user_by_user_to_app_to_role


#3

Thanks Todd

Definitely prefer to have more than none, haha


#4

As it so happens, while the stated method does return the users per app per role (as well as the overabundance of data you mentioned), there seems to be a flaw in that each role returns a record, per user, per app, but with no way of identifying which app the record in the array applies too

For example, I have 4 apps in my test instance (admin, swagger, filemanager, and testapp) as well as 3 users and 2 roles. I queried for all roles but upon looking over the response, there are 4 results one for of the roles (id 1) for the user (id 2) but each and every one of the records in the array are EXACTLY the same:


adldap: null
confirm_code: "y"
created_by_id: 1
created_date: "2016-01-24 22:10:17"
default_app_id: null
email: "[REDACTED]"
first_name: "Testy"
id: 2
is_active: true
last_login_date: "2016-01-25 20:15:15"
last_modified_by_id: 1
last_modified_date: "2016-01-25 20:15:15"
last_name: "McTesterson"
name: "Testy McTesterson"
oauth_provider: null
phone: null
pivot:
{ role_id: 1
user_id: 2 }


That said, I am not really sure what good this will do me in searching for roles, and it seems to me that there MUST be some better/more straightforward way to look up this information. For the sake of discussion, the most straightforward use case I can think of - if you wanted to (via code) email everyone with the role ‘X’ in app ‘Y’, how would you go about do that?


#5

This could help.

GET /api/v2/system/role?related=user_to_app_to_role_by_role_id

It returns the user id and app id.

{
	"id": 1,
	"name": "testrole1",
	"description": null,
	"is_active": true,
	"created_date": "2016-01-06 18:57:04",
	"last_modified_date": "2016-01-07 18:55:24",
	"created_by_id": "1",
	"last_modified_by_id": "1",
	"user_to_app_to_role_by_role_id": [
		{
			"id": "1",
			"user_id": 2,
			"app_id": 1,
			"role_id": 1
		},
		{
			"id": "2",
			"user_id": 2,
			"app_id": 2,
			"role_id": 1
		},
		{
			"id": "3",
			"user_id": 2,
			"app_id": 3,
			"role_id": 1
		},
		{
			"id": "4",
			"user_id": 2,
			"app_id": 4,
			"role_id": 1
		},
		{
			"id": "29",
			"user_id": 2,
			"app_id": 15,
			"role_id": 1
		},
		{
			"id": "30",
			"user_id": 2,
			"app_id": 14,
			"role_id": 1
		},
		{
			"id": "31",
			"user_id": 2,
			"app_id": 13,
			"role_id": 1
		}
	]
}

If you know the app id for app ‘Y’ you can loop through the results and build a list of user ids to email. Then you can get the email address for those user ids and send the email. It’s multiple steps, but would that work for you?