DataSyncGrant

Factory for DataSync (App Context v4) PAM v3 resource grants, passed in the grants list of com.pubnub.api.PubNub.grantToken.

Every DataSync resource is granted here, on an exact resource id or on a regex pattern:

Only the four DataSync-relevant permission flags are exposed: get, create, update and delete.

Shared channels bucket: DataSync channels share the channels bucket with pub/sub and App Context v2, so channel(id, update = true) also authorizes App Context v2 setChannelMetadata for the same id (and get / delete likewise). Grants on the same id are OR-merged, so combining a ChannelGrant and a channel grant on one id is safe: the token carries the union of both.

entity, relationship, channel and user grants (and their pattern variants) can also carry an optional projection: when this client uses the token to access this resource, they see it through this projection. A projection is a named, filtered view of a resource's fields, defined in the class schema under projections. When set, the SDK emits the corresponding pn-projections entry into the token meta automatically. Omit it (or pass null) to use the implicit __default__ projection. Don't write pn-projections into the token meta yourself: grantToken rejects a caller meta that contains it.

Which projections a resource has depends on its class:

  • entities and relationships: the projections declared by their custom class;

  • channels and users: only __default__ for the built-in Channel / User classes. A named projection only has an effect for a custom Channel / User subclass that declares it;

  • memberships: always __default__ (the built-in Membership class has no named projections), so membership takes no projection parameter.

These grants authorize DataSync REST CRUD (get/create/update/delete) on the resource record only. They do not authorize subscribing to realtime events: a realtime subscribe is a plain PubSub read of the resource's ref-channel. Use subscribe / subscribePattern for that. They resolve the ref-channel names for you. See the subscription(...) methods on the DataSync handles (com.pubnub.api.v2.entities.DataSyncEntity, com.pubnub.api.v2.entities.DataSyncChannel, com.pubnub.api.v2.entities.DataSyncUser).

pubnub.grantToken(
ttl = 60,
authorizedUserId = UserId("pam-debug-admin"),
grants = listOf(
// entities
DataSyncGrant.entity("capy-001", get = true, update = true, projection = "admin"),
DataSyncGrant.entityPattern("capy-.*", get = true),
// relationships
DataSyncGrant.relationship("user.A:channel.X", get = true, projection = "admin"),
DataSyncGrant.relationshipPattern("user\\.A:.*", get = true),
// memberships
DataSyncGrant.membership("user-123:channel-X", get = true, delete = true),
DataSyncGrant.membershipPattern("user-123:.*", get = true),
// channels (plain `channels` bucket)
DataSyncGrant.channel("chat-1", get = true, update = true, projection = "admin"),
DataSyncGrant.channelPattern("chat-.*", get = true),
// users (plain `users` bucket)
DataSyncGrant.user("user-123", get = true, update = true),
DataSyncGrant.userPattern("user-.*", get = true, create = true),
// realtime subscribe (pub/sub `read` on the resolved ref-channel)
DataSyncGrant.subscribe("capy-001"), // read on "capy-001"
DataSyncGrant.subscribe("chat-1", projection = "admin"), // read on "__admin__chat-1"
// default projection publishes on the bare id, so no prefix; `^` keeps it off the `__admin__capy-…` mirrors
DataSyncGrant.subscribePattern("capy-.*"), // read on "^(?:capy-.*)"
DataSyncGrant.subscribePattern("chat-.*", projection = "admin"), // read on "^__admin__(?:chat-.*)"
),
).sync().token

Functions

Link copied to clipboard
fun channel(name: String, get: Boolean = false, create: Boolean = false, update: Boolean = false, delete: Boolean = false, projection: String? = null): DataSyncGrantType

Grants DataSync REST CRUD on a channel record. The permission bits land in the plain channels bucket, shared with pub/sub and App Context v2: update = true also authorizes App Context v2 setChannelMetadata for the same id.

Link copied to clipboard
fun channelPattern(pattern: String, get: Boolean = false, create: Boolean = false, update: Boolean = false, delete: Boolean = false, projection: String? = null): DataSyncGrantType

Pattern (regex) variant of channel.

Link copied to clipboard
fun entity(name: String, get: Boolean = false, create: Boolean = false, update: Boolean = false, delete: Boolean = false, projection: String? = null): DataSyncGrantType
Link copied to clipboard
fun entityPattern(pattern: String, get: Boolean = false, create: Boolean = false, update: Boolean = false, delete: Boolean = false, projection: String? = null): DataSyncGrantType
Link copied to clipboard
fun membership(name: String, get: Boolean = false, create: Boolean = false, update: Boolean = false, delete: Boolean = false): DataSyncGrantType

Grants DataSync REST CRUD on a membership record. There is no projection parameter: memberships always use the built-in Membership class, which declares no named projections, so they are always read through __default__. For a custom membership-like class with projections, use a relationship class and relationship.

Link copied to clipboard
fun membershipPattern(pattern: String, get: Boolean = false, create: Boolean = false, update: Boolean = false, delete: Boolean = false): DataSyncGrantType

Pattern (regex) variant of membership.

Link copied to clipboard
fun relationship(name: String, get: Boolean = false, create: Boolean = false, update: Boolean = false, delete: Boolean = false, projection: String? = null): DataSyncGrantType
Link copied to clipboard
fun relationshipPattern(pattern: String, get: Boolean = false, create: Boolean = false, update: Boolean = false, delete: Boolean = false, projection: String? = null): DataSyncGrantType
Link copied to clipboard
fun subscribe(id: String, projection: String? = null): ChannelGrant

Grants a realtime subscribe on the ref-channel of a DataSync entity, user or channel: a pub/sub read on DataSyncNamespace.refChannel (id for the default projection, __{projection}__{id} otherwise). Relationships and memberships have no ref-channel of their own: their events are published to both endpoint refs.

Link copied to clipboard
fun subscribePattern(pattern: String, projection: String? = null): ChannelGrant

Pattern variant of subscribe: a pub/sub read on every ref-channel whose id matches the regex pattern, as seen through projection. The channel regex is built by DataSyncNamespace.refChannelPattern: it is anchored at the start and wraps pattern in a non-capturing group, e.g. subscribePattern("capy-.*") → ^(?:capy-.*) and subscribePattern("capy-.*", "admin") → ^__admin__(?:capy-.*).

Link copied to clipboard
fun user(name: String, get: Boolean = false, create: Boolean = false, update: Boolean = false, delete: Boolean = false, projection: String? = null): DataSyncGrantType

Grants DataSync REST CRUD on a user record. The permission bits land in the plain users bucket.

Link copied to clipboard
fun userPattern(pattern: String, get: Boolean = false, create: Boolean = false, update: Boolean = false, delete: Boolean = false, projection: String? = null): DataSyncGrantType

Pattern (regex) variant of user.