DataSyncGrant

Fluent DataSync PAM v3 resource grant, passed to grantToken(...).authorizedUserId(...).grants(...).

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

It extends PNDataSyncResource rather than PNResource, so only the four DataSync-relevant permission flags (get/create/update/delete) are exposed — the PubSub-only bits (read/write/manage/join) are not part of the DataSync permission model.

Shared channels bucket: DataSync channels share the channels bucket with pub/sub and App Context v2, so channel(id).update() 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 matching pn-projections entry into the token meta automatically. Leave it unset 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 projection(...) on a membership grant throws.

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.java.v2.entities.DataSyncEntity, com.pubnub.api.java.v2.entities.DataSyncChannel, com.pubnub.api.java.v2.entities.DataSyncUser).


pubnub.grantToken(60)
    .authorizedUserId(new UserId("my-authorized-user"))
    .grants(Arrays.asList(
        // entities
        DataSyncGrant.entity("capy-001").get().update().projection("admin"),
        DataSyncGrant.entityPattern("capy-.*").get(),
        // relationships
        DataSyncGrant.relationship("user.A:channel.X").get().projection("admin"),
        DataSyncGrant.relationshipPattern("user\\.A:.*").get(),
        // memberships
        DataSyncGrant.membership("user-123:channel-X").get().delete(),
        DataSyncGrant.membershipPattern("user-123:.*").get(),
        // channels (plain `channels` bucket)
        DataSyncGrant.channel("chat-1").get().update().projection("admin"),
        DataSyncGrant.channelPattern("chat-.*").get(),
        // users (plain `users` bucket)
        DataSyncGrant.user("user-123").get().update(),
        DataSyncGrant.userPattern("user-.*").get().create(),
        // realtime subscribe (pub/sub `read` on the resolved ref-channel)
        DataSyncGrant.subscribe("capy-001"),                  // read on "capy-001"
        DataSyncGrant.subscribe("chat-1", "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-.*", "admin")    // read on "^__admin__(?:chat-.*)"
    ))
    .sync();

Properties

Link copied to clipboard
val DATASYNC_CHANNELS: String = "datasync:channels"
Link copied to clipboard
val DATASYNC_ENTITIES: String = "datasync:entities"
Link copied to clipboard
val DATASYNC_MEMBERSHIPS: String = "datasync:memberships"
Link copied to clipboard
val DATASYNC_RELATIONSHIPS: String = "datasync:relationships"
Link copied to clipboard
val DATASYNC_USERS: String = "datasync:users"
Link copied to clipboard
Link copied to clipboard
open val projection: String

Functions

Link copied to clipboard
open fun channel(name: String): DataSyncGrant
Grants DataSync REST CRUD on a channel record.
Link copied to clipboard
open fun channelPattern(pattern: String): DataSyncGrant
Pattern (regex) variant of channel.
Link copied to clipboard
open fun create(): DataSyncGrant
Link copied to clipboard
open fun delete(): DataSyncGrant
Link copied to clipboard
open fun entity(name: String): DataSyncGrant
Link copied to clipboard
open fun entityPattern(pattern: String): DataSyncGrant
Link copied to clipboard
open fun get(): DataSyncGrant
Link copied to clipboard
open fun getId(): String
Link copied to clipboard
open fun isCreate(): Boolean
Link copied to clipboard
Link copied to clipboard
Grants DataSync REST CRUD on a membership record.
Link copied to clipboard
Pattern (regex) variant of membership.
Link copied to clipboard
open fun projection(projection: String): DataSyncGrant
Sets the projection the token holder looks through for this resource.
Link copied to clipboard
open fun refChannel(id: String, projection: String): String
Resolves the ref-channel a DataSync resource publishes its realtime events on.
Link copied to clipboard
open fun refChannelPattern(pattern: String, projection: String): String
Builds the PAM channel regex matching the ref-channels of every DataSync resource whose id matches pattern, as seen through projection.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Grants a realtime subscribe on the default-projection ref-channel (id) of a DataSync entity, user or channel.
open fun subscribe(id: String, projection: String): ChannelGrant
Grants a realtime subscribe on the ref-channel of a DataSync entity, user or channel: a pub/sub read on refChannel (id for the default projection, __{projection}__{id} otherwise).
Link copied to clipboard
Pattern variant of subscribe for the default projection.
open fun subscribePattern(pattern: String, projection: String): ChannelGrant
Pattern variant of subscribe: a pub/sub read on every ref-channel whose id matches the regex pattern, as seen through projection.
Link copied to clipboard
open fun update(): DataSyncGrant
Link copied to clipboard
open fun user(name: String): DataSyncGrant
Grants DataSync REST CRUD on a user record.
Link copied to clipboard
open fun userPattern(pattern: String): DataSyncGrant
Pattern (regex) variant of user.