Impersonate another user when constructing `Results`
See original GitHub issueGoals
Now that sync has implemented an API to impersonate another user it would be great to get this API exposed through Realm JS.
Note: This feature is probably only relevant for an admin user which has opened a reference Realm.
I believe this should be exposed on the Results class, but perhaps it would be possible to add it on the List too and we could put the method on the Collection instead? I’m imagining an API where a new method is added to the Results class to get the results filtered by the permissions of another user:
1st API suggested
interface Results<T> extends Collection<T> {
/**
* Query as a specific user, by applying fine-grained permissions to the elements of the result.
* @param {string} userId The ID of the user which we want to apply permissions for.
* @returns Results The results filtered by applying the fine-grained permissions.
*/
as(userId: string): Results<T>;
}
An example of usage could be
const realm = new Realm({ ... });
const wines = realm.objects<IWine>("Wine");
const oldMerlots = wines.filtered('variety == "Merlot" && vintage <= $0', 2010);
const johnsUserId = "0a8dfebc-40b8-11e9-b210-d663bd873d93";
const johnsOldMerlots = oldMerlots.as(johnsUserId);
console.log(`John has access to ${johnsOldMerlots.length} old merlots.`);
2nd API suggested (by @nirinchev 👇)
class Realm {
/**
* Create a user specific wrapper around the open Realm to perform queries as a specific user.
* @param {string} userId The ID of the user which we want to apply permissions for.
* @returns Realm A wrapper around the Realm instance which has modified behaviour to check fine-grained permissions when querying for and writing objects.
*/
as(userId: string): Realm;
}
An example of usage could be
const realm = new Realm({ ... });
const johnsUserId = "0a8dfebc-40b8-11e9-b210-d663bd873d93";
const realmAsJohn = realm.as(johnsUserId);
const johnsWines = realmAsJohn.objects<IWine>("Wine");
const johnsOldMerlots = johnsWines.filtered('variety == "Merlot" && vintage <= $0', 2010);
console.log(`John has access to ${johnsOldMerlots.length} old merlots.`);
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (8 by maintainers)
Top Results From Across the Web
Impersonate a user - Product Documentation | ServiceNow
Administrators can impersonate other authenticated users for testing purposes and view impersonation logs.
Read more >User Impersonation — a secure, easy way to troubleshoot ...
When impersonating another user, the administrator has access to exactly what that user can access in the system, including the same menus ......
Read more >The 3 Safe and Reliable Ways to Handle User Impersonation ...
One way to tackle the impersonation problem is by building an in-house solution, specifically crafted for your product. The tool should be able ......
Read more >Designing an Impersonate feature - Nolan Ramsey
A feature that is often requested is the ability for a privileged user to impersonate another user's account. If you aren't familiar with ......
Read more >How do you do Impersonation in .NET? - Stack Overflow
"Impersonation" in the .NET space generally means running code under a specific user account. It is a somewhat separate concept than getting access...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yes, we need another change in sync to facilitate impersonated write transactions. I was thinking more of wrapping the Realm into a thin js wrapper that replaces some methods with impersonated ones, so the Realm would be opened once as an admin user and then, you would call
const impersonate = realm.as('abc...')which will return an object with the same API asRealmbut will replace.objects,.write,.objectForPrimaryKeyand so on with user versions.I believe this can safely be closed now and perhaps reopened once we get more clarity on what replaces query-based sync and client-side permissions.