Access()
Enabling Sync Gateway data access
Related Topics: access() | channel() | expiry() | requireAccess() | requireAdmin() | requireRole() | requireUser() | role() | throw()
- Function
-
access(username, channelname)
Arguments
Argument | Description |
---|---|
username |
Must be a string identifying a user, or an array of strings identifying multiple users; the function is applied to each user in the array. If the value resolves to null the function result is a no-op. |
|
Must be a string identifying a channel name, or an array of strings to specify multiple channel names (for example: If the value resolves to null the function result is a no-op. |
As a convenience, the resolved value of either argument may be null or undefined , in which case nothing happens.
|
Context
You can invoke this function multiple times from within your Sync Function. But note that, invoking it multiple times to grant the same user access to the same channel, will result in negative performance implications.
Prefix the username argument value with role: to apply this function to a role rather than a user.
This grants access to the specified channel(s) for all users assigned that role.
|
The effects of all access calls by all active documents are effectively combined in a union, so if any document grants a user access to a channel, that user has access to the channel.
You can use the all channels wildcard ('*') to grant the user access to all documents in all channels.
Use
This example shows some valid ways to call access()
:
access ("jchris", "mtv"); (1)
access ("jchris", ["mtv", "mtv2", "vh1"]); (2)
access (["snej", "jchris", "role:admin"], "vh1"); (3)
access (["snej", "jchris"], ["mtv", "mtv2", "vh1"]); (4)
access (null, "hbo"); (5)
access ("snej", null);
1 | Allow access of single channel to single user |
2 | Allow access of multiple channels to single user |
3 | Allow access of single channel to multiple users |
4 | Allow access of multiple channels to multiple users |
5 | The null arguments mean these are treated as no-ops |