add( )
Sets a permissions handler for a specific channel or channel resource.
Description
The handler set by the add()
function is used for permission checks by the permissions
router for the channel or channel resource specified in the channel
property.
Adding a handler to a specific channel without a specified resource adds a handler for all permissions checks on the channel and all permissions checks on any of the channel's resources that do not have their own permissions handlers.
For example, suppose you have the following channel, resources, and permissions:
- channel: A, permissions: handler X is specified using the
add()
function - channel: A, resource: 1, permissions: handler Y is specified using the
add()
function - channel: A, resource: 2, permissions: no handler is specified
Permissions will be checked as follows:
- A: use handler X
- A, 1: use handler Y
- A, 2: use handler X
Syntax
function add(channel: Channel, handler: PermissionsHandler): Promise<void> | voidhandler: function PermissionsHandler(channel: Channel, subscriber: Subscriber): Promise<ChannelPermissions> | ChannelPermissions
add Parameters
NAME
TYPE
DESCRIPTION
Channel or channel resource to add a permissions handler for.
The name of the function or the function expression to use when checking permissions for the specified channel.
Returns
Return Type:
|
voidPermissionsHandler Parameters
NAME
TYPE
DESCRIPTION
The channel to grant permissions to.
The subscriber to grant permissions to.
Returns
Return Type:
|
ChannelPermissionsChannelPermissions
ChannelPermissions
NAME
TYPE
DESCRIPTION
Whether the subscriber has read permissions. Defaults to true
.
Was this helpful?
1// In realtime-permissions.js23import {permissionsRouter} from 'wix-realtime-backend';45// ...67const adminAnnouncements = {"name": "announcements"}89permissionsRouter.add( adminAnnouncements, (channel, subscriber) => {10 // add permissions check logic and return11 // permissions for announcements channel12 if(subscriber.type === "Admin") {13 return {"read": true};14 }15 else {16 return {"read": true};17 }18} );
1// In realtime-permissions.js23import {permissionsRouter} from 'wix-realtime-backend';45permissionsRouter.default( (channel, subscriber) => {6 return {"read": true};7} );89const membersOnlyChannel = {"name": "MembersOnly"};1011permissionsRouter.add(membersOnlyChannel, (channel, subscriber) => {12 if(subscriber.type === "Member") {13 return {"read": true};14 }15 else {16 return {"read": false};17 }18});1920export function realtime_check_permission(channel, subscriber) {21 return permissionsRouter.check(channel,subscriber);22}
1// In realtime-permissions.js23import { members } from `wix-members-backend`;4import { permissionsRouter } from 'wix-realtime-backend';56permissionsRouter.default((channel, subscriber) => {7 return {"read": true};8});910const membersOnlyChannel = {"name": "MembersOnly"};1112permissionsRouter.add( membersOnlyChannel, async (channel, subscriber) => {13 let member = await members.getMember(subscriber.id, { fieldsets: ['FULL'] });1415 if(channel.resourceId === "BobsOnly") {16 if(member.contactDetails.firstName === "Bob") {17 return { "read": true };18 }19 else {20 return { "read": false };21 }22 }23 else {24 return { "read": true };25 }26} );2728export function realtime_check_permission(channel, subscriber) {29 return permissionsRouter.check(channel, subscriber);30}