Backendless Support
 
Solved

Current logged in user's information

I am trying to restrict some information but i cant seem to get it right,

lets say I log in as a consultant if i log in as the consultant i want to see only my appointments which are displayed on the list view not of other consultant.....please help!! kind regards Nomfundo

Leave a Comment

Comments (14)

photo
1

Hi Nomfundo,

Each object in the Data Service has a special "ownerId" field, which is automatically set to the objectId of a user saving it. You can then retrieve the user's object with a whereClause like "ownerId = <currentUserObjectId>" to get only the items saved by the current user.

photo
1

what should i use for my if statement should i also use the ownerId

photo
1

You don't need an if statement, you just retrieve only the data belonging to the current user, not all data.

photo
1

it throws an invalid whereclause error

photo
1

Please provide the code you used.

photo
photo
1

i have attached it below

photo
1

You should substitute <currentUsedObjectId> with a real objectId of the current user.

photo
1

String whereClause = "ownerId = E9D791EC-3F03-64A2-FF84-6CBC8F1E2200";by that u mean like this?

photo
1

Yes, just the objectId value should be in single quotes:

  1. ownerId = 'E9D791EC-3F03-64A2-FF84-6CBC8F1E2200'

photo
1

yes!! it works but then what happens wen a different user logs in does the ownerId change automatically or i have to hard code it again if yes how can i prevent that?

photo
1

The ownerId is just a field in each data row. Depending on the currently logged in user, Backendless.UserService.CurrentUser().getObjectId() will return different objectId values, which you should use in the whereClause to retrieve only the rows which the current user owns.

photo
1

i'm not sure if i understand

photo
1

Hi Nomfundo

for example:

you have 2 users userA(objectId = 'userAObjectId') and userB(objectId = 'userBObjectId')

and you have some items (in your case it's Appointments)

- appointment1 (ownerId = 'userAObjectId')

- appointment2 (ownerId = 'userAObjectId')

- appointment3 (ownerId = 'userAObjectId')

- appointment4 (ownerId = 'userAObjectId')

- appointment5 (ownerId = 'userBObjectId')

- appointment6 (ownerId = 'userBObjectId')

when you need to retrieve only items where userA is owner you should use this whereClause "ownerId = 'userAObjectId'"

and it returns only:

- appointment1

- appointment2

- appointment3

- appointment4

when you need to retrieve only items where userB is owner you should use this whereClause "ownerId = 'userBObjectId'"

result:

- appointment5

- appointment6

photo
1

thank you!!

photo