Permissions
Bazaar permissions allow you to share content such as a shopping list and create social functionality like chatting with another user.
Create a Permission
Section titled “Create a Permission”Create, or grant, a new permission, i.e. share a resource.
import { GranteeType, PermissionType } from "@bzr/bazaar";const newPermission = { collectionName: "example-collection-name", granteeType: GranteeType.USER, granteeId: "example-user-id-to-grant-permission-to", types: [PermissionType.READ], filter: { id: "example-doc-id", },};
const { id } = await bzr.permissions.create(newPermission);The permission type can be PermissionType.READ, PermissionType.INSERT, PermissionType.UPDATE, and PermissionType.DELETE.
Grantees
Section titled “Grantees”granteeType sets who gets the permission:
granteeType | granteeId | Grants to |
|---|---|---|
GranteeType.USER | a user ID | that user |
GranteeType.TEAM | a team ID | every member of that team |
GranteeType.ORG | an org ID | every member of that org |
GranteeType.GROUP | a group ID | every member of that group |
GranteeType.ANY | (none) | any user of the app |
GranteeType.ADMINS | (none) | the admins of the database’s owner |
A user, team, or org grantee must exist, otherwise create fails with TeamDoesNotExist. Team and org grantees are mostly useful in org and team databases.
GranteeType.ANY grants the permission to any user of the app. This allows you to create documents that are accessible to every user or have the document itself dictate who has permission. For example:
await bzr.permissions.create({ collectionName: "messages", granteeType: GranteeType.ANY, types: [PermissionType.READ], filter: { audience: { $contains: "$user", }, },});This gives any user access to a document whose audience array holds their user ID.
Filters and Writes
Section titled “Filters and Writes”A filter covers both ends of a write. An update or replace needs the document to match before, and to still match after. An insert needs the new document to match. So with filter: { authorId: "$user" }, a user can’t insert a document with someone else’s authorId, and can’t change authorId on their own documents:
await notes.updateOne(myNoteId, { text: "edit" }); // fine: authorId staysawait notes.updateOne(myNoteId, { authorId: "someone-else" }); // NoPermissionawait notes.replaceOne(myNoteId, { text: "edit" }); // NoPermission: replaced doc has no authorIdupdateOne is checked on the merged document, so it can leave out the fields the filter names. To hand a document over to someone else, use the database owner, or a grant without a filter.
One exception: you can always take yourself out of a list. With filter: { members: { $contains: "$user" } }, a member can leave, even though they then no longer match:
await chats.updateOne(chatId, { members: everyoneElse }); // fine: the only change is you leavingawait chats.updateOne(chatId, { members: [...everyoneElse, "someone-new"] }); // NoPermissionawait chats.updateOne(chatId, { members: everyoneElse, title: "bye" }); // NoPermission: one thing at a timeThe list can’t end up empty, and it has to be a list: clearing authorId is refused. The same goes for permission groups, whose default grant has this filter.
Reserved Filter Values
Section titled “Reserved Filter Values”Permission filters can use these values. The server replaces them for the calling user:
"$user": the user’s ID"$teams": IDs of the user’s teams, e.g.{ teamId: { $in: "$teams" } }({ teamId: "$teams" }is refused: a list goes in$inor$containsAny)"$groups": IDs of the user’s permission groups"$purchases": IDs of the products the user has an active purchase of in this app, e.g.{ productId: { $in: "$purchases" } }. See Monetization.
Notify target user
Section titled “Notify target user”When a permission for a user, team, or org (GranteeType.USER, TEAM, or ORG) is created, the grantees may be notified something was shared with them. This is done via a sharing notification argument in the create function:
bzr.permissions.create(newPermission, notification);The notification has the following type:
type SharingNotification = { createNotification: boolean; sendMessage: SendNotification; /** * Defaults to "X shared something with you in app Y" */ message?: string; // max 250 characters};Without the argument, nobody is notified. The createNotification flag specifies if a notification on the target user’s Bazaar dashboard will be created.
The sendMessage field defines under what circumstances an email will be sent to the target user. The options are SendNotification.NEVER, SendNotification.ALWAYS, and SendNotification.INVITE_ONLY which only sends an email if the target user does not use the app the permission is for (it is an indirect invitation or nudge to use the app). Emails count toward a limit of 100 recipients an hour and 300 a day per user and app. Past it, the permission is still created, without emails.
List Permissions
Section titled “List Permissions”List the permissions in the database: the user’s own, or the owner’s in a context. This includes the default permissions every database starts with, on reserved collections such as _permissions.
// List all permissionsbzr.permissions.list();
// Specify options to filter the permissionsbzr.permissions.list({ collectionName: "example-collection-name", granteeType: GranteeType.USER, granteeId: "granted-user-id", type: PermissionType.READ,});Delete a Permission
Section titled “Delete a Permission”Delete a permission by its ID. This also deletes its granted permission:
bzr.permissions.delete("example-permission-id");Sharing Links
Section titled “Sharing Links”A sharing link allows a user to share a URL to give a specified permission. For example, you can create a link that gives the user that clicks on the link access to your private blog. A user who opens the link while logged in to Bazaar gets the permission as a GranteeType.USER permission and is sent to your app. Each user can redeem a link once.
A sharing link object looks as follows:
type Link = { id: string; permission: PermissionTemplate; limit: number; description: string; // max 30 characters users: string[]; // list of users that redeemed the link url: string;};Create a Link
Section titled “Create a Link”Create a link to share a resource:
const permissionTemplate = { collectionName: "example-collection-name", // Grant access to all permission types types: [ PermissionType.READ, PermissionType.INSERT, PermissionType.UPDATE, PermissionType.DELETE, ], // Specify an optional filter filter: { id: "example-doc-id", },};
const link = await bzr.permissions.links.create(permissionTemplate);const url = link.url; // Share this URL with the user you wantCreating link options
Section titled “Creating link options”When creating a link you can also specify a description and a limit:
const link = await bzr.permissions.links.create( permissionTemplate, description, limit,);The description is a max 30 character long string to help users remember the purpose of the created link. The limit specifies how many times the link may be redeemed. The default is 1. Setting it to 0 allows the link to be redeemed unlimited times.
List Links
Section titled “List Links”List all of a user’s links:
bzr.permissions.links.list({ collectionName: "example-collection-name", type: PermissionType.INSERT,});Subscribe to Share Link Events
Section titled “Subscribe to Share Link Events”To react to redeemed links, you can subscribe to link change events
const options = { collectionName: "example-collection-name", type: PermissionType.INSERT,};bzr.permissions.links.subscribe(options, { onChange: (oldLink, newLink) => { // compare oldLink.users and newLink.users to see who redeemed the link },});See more details about the listener object.
Delete a Link
Section titled “Delete a Link”Delete a sharing link:
bzr.permissions.links.delete("example-link-id");Permissions Granted to a User
Section titled “Permissions Granted to a User”When a permission for a user, team, or org is created, a granted permission object is created for the grantee:
type GrantedPermission = { id: string; /** * ID of the database's owner (a user, team, org, or app) */ ownerId: string; permission: Permission;};List Granted Permissions
Section titled “List Granted Permissions”List permissions that have been granted to the authenticated user in the current app. In a context, ctx.permissions.granted lists those granted to that team or org:
// List all permissions granted to a userbzr.permissions.granted.list();
// Specify options to filter permissions granted to a userbzr.permissions.granted.list({ collectionName: "example-collection-name", ownerId: "example-owner-id", type: PermissionType.READ,});Subscribe to Granted Permission Events
Section titled “Subscribe to Granted Permission Events”Subscribe to change event for permissions granted to a user:
const query = {};bzr.permissions.granted.subscribe(query, listener);See more details about the listener object.
Delete a Granted Permission
Section titled “Delete a Granted Permission”Delete a permission that was granted to the authenticated user:
bzr.permissions.granted.delete("example-granted-permission-id");Permission Groups
Section titled “Permission Groups”A group is a list of users your app manages itself, e.g. the members of a channel. It belongs to the database it’s created in, and users can be in it without being in a team. Unlike a team, it needs no plan.
const { id } = await bzr.permissions.groups.create({ label: "Channel members", // max 30 characters members: ["example-user-id"], // at least one});
await bzr.permissions.groups.addMember(id, "another-user-id");await bzr.permissions.groups.removeMember(id, "another-user-id");
const group = await bzr.permissions.groups.get(id);const groups = await bzr.permissions.groups.list();await bzr.permissions.groups.delete(id);Grant to a group with GranteeType.GROUP:
await bzr.permissions.create({ collectionName: "messages", granteeType: GranteeType.GROUP, granteeId: id, types: [PermissionType.READ, PermissionType.INSERT],});Or let each document say which groups may see it, with the "$groups" value:
await bzr.permissions.create({ collectionName: "messages", granteeType: GranteeType.ANY, types: [PermissionType.READ], filter: { groupId: { $in: "$groups" } },});Removing a user from a group takes their access away at once. In a context, ctx.permissions.groups manages that owner’s groups.