Customizing a Topic's ACL

You can customize the users who can subscribe and send messages to a topic by modifying the topic's ACL. This topic explains how you can do this.

Topic ACL entries

A topic ACL entry is composed of an action and a subject:

  • Action

    This item defines "what" the target user or group can execute.

    Action Representation in code * What the target user/group/thing can execute
    SUBSCRIBE_TO_TOPIC KiiACLSubscribeToTopic Subscribe to the topic.
    SEND_MESSAGE_TO_TOPIC KiiACLSendMessageToTopic Send push messages to the topic.

    * These symbols are defined in the KiiACLAction enumeration and can be specified like KiiACLAction.KiiACLSubscribeToTopic.

  • Subject

    This item defines "who" can execute.

    Subject Who can execute the designated action?
    KiiUser instance The specified user.
    KiiGroup instance The members of the specified group.
    KiiThing instance The specified thing.
    KiiAnyAuthenticatedUser Any authenticated users.
    KiiAnonymousUser Anonymous users.

    See Subject for the definition of the "Any authenticated users" and "Anonymous users".

Managing a topic's ACL

You can add and delete an ACL entry in a topic's ACL. You can also get a list of ACL entries.

Adding a topic ACL entry

See the following two samples which add ACL entries to the ACL of group-scope and user-scope topics.

Topic in a group scope

The first sample code will add the following three ACL entries to the ACL of the group-scope topic GroupTopic.

  • entry1: Permit the "subscribe to the topic" action to "all authenticated users"
  • entry2: Permit the "subscribe to the topic" action to the user _I_am_a_supervisor_.
  • entry3: Permit the "send push messages to the topic" action to the user _I_am_a_supervisor_.

Note that these changes correspond to the first and second scenarios presented in Changing a topic's ACL.

  • // Instantiate a group.
    var group = KiiGroup.groupWithURI(groupUri);
    
    // Refresh the group.
    group.refresh({
      success: function(theGroup) {
        // Find a user.
        KiiUser.findUserByUsername("_I_am_a_supervisor_",{
          success: function(foundUser) {
            // Instantiate a topic in the group scope.
            var topicName = "GroupTopic";
            var topic = group.topicWithName(topicName);
    
            // Get the ACL handler.
            var acl = topic.acl();
    
            // Allow the authenticated users to subscribe to the topic.
            var entry1 = KiiACLEntry.entryWithSubject(new KiiAnyAuthenticatedUser(), KiiACLAction.KiiACLSubscribeToTopic);
            acl.putACLEntry(entry1);
    
            // Allow the user to subscribe to the topic.
            var entry2 = KiiACLEntry.entryWithSubject(foundUser, KiiACLAction.KiiACLSubscribeToTopic);
            acl.putACLEntry(entry2);
    
            // Allow the user to send push messages to the topic.
            var entry3 = KiiACLEntry.entryWithSubject(foundUser, KiiACLAction.KiiACLSendMessageToTopic);
            acl.putACLEntry(entry3);
    
            // Save all the ACL entries.
            acl.save({
              success: function(theACL) {
                // Do something.
              },
              failure: function(theACL, errorString) {
                // Handle the error.
              }
            });
          },
          failure: function(errorString) {
            // Handle the error.
          }
        });
      },
      failure: function(theGroup, errorString) {
        // Handle the error.
      }
    });
  • // Instantiate a group.
    var group = KiiGroup.groupWithURI(groupUri);
    
    // Refresh the group.
    group.refresh().then(
      function(theGroup) {
        // Find a user.
        return KiiUser.findUserByUsername("_I_am_a_supervisor_");
      }
    ).then(
      function(foundUser) {
        // Instantiate a topic in the group scope.
        var topicName = "GroupTopic";
        var topic = group.topicWithName(topicName);
    
        // Get the ACL handler.
        var acl = topic.acl();
    
        // Allow the authenticated users to subscribe to the topic.
        var entry1 = KiiACLEntry.entryWithSubject(new KiiAnyAuthenticatedUser(), KiiACLAction.KiiACLSubscribeToTopic);
        acl.putACLEntry(entry1);
    
        // Allow the user to subscribe to the topic.
        var entry2 = KiiACLEntry.entryWithSubject(foundUser, KiiACLAction.KiiACLSubscribeToTopic);
        acl.putACLEntry(entry2);
    
        // Allow the user to send push messages to the topic.
        var entry3 = KiiACLEntry.entryWithSubject(foundUser, KiiACLAction.KiiACLSendMessageToTopic);
        acl.putACLEntry(entry3);
    
        // Save all the ACL entries.
        return acl.save();
      }
    ).then(
      function(theACL) {
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
    
        // Get the group for the failed refresh() method.
        var theGroup = error.target;
        // Get the ACL for the failed save() method.
        var theACL = error.target;
        // Get the error message.
        var errorString = error.message;
      }
    );

Here is a brief explanation of the sample code.

  1. Get the target topic groupTopic from its URI.
  2. Create an ACL handler by calling the acl() method.
  3. Call the KiiACLEntry() method to create ACL entries (entry1, entry2, and entry3).
  4. Call the putACLEntry() method to locally save each ACL entry.
  5. Call the save() method to save the ACL entries to Kii Cloud.

Topic in a user scope

The second sample code will add the following ACL entry to the ACL of the user-scope topic MyTODO.

  • Permit the "subscribe to the topic" action to the specified group members.

Note that this change corresponds to the third scenario presented in Changing a topic's ACL.

  • // Instantiate an existing topic in the user scope.
    var topicName = "MyTODO";
    var topic = KiiUser.getCurrentUser().topicWithName(topicName);
    
    // Get the ACL handler.
    var acl = topic.acl();
    
    // Instantiate a group.
    var group = KiiGroup.groupWithURI(groupUri);
    
    // Refresh the group.
    group.refresh({
      success: function(theGroup) {
        // Allow the group members to subscribe to the topic.
        var entry = KiiACLEntry.entryWithSubject(theGroup, KiiACLAction.KiiACLSubscribeToTopic);
        acl.putACLEntry(entry);
    
        // Save all the ACL entries.
        acl.save({
          success: function(theACL) {
            // Do something.
          },
          failure: function(theACL, errorString) {
            // Handle the error.
          }
        });
      },
      failure: function(theGroup, errorString) {
        // Handle the error.
      }
    });
  • // Instantiate an existing topic in the user scope.
    var topicName = "MyTODO";
    var topic = KiiUser.getCurrentUser().topicWithName(topicName);
    
    // Get the ACL handler.
    var acl = topic.acl();
    
    // Instantiate a group.
    var group = KiiGroup.groupWithURI(groupUri);
    
    // Refresh the group.
    group.refresh().then(
      function(theGroup) {
        // Allow the group members to subscribe to the topic.
        var entry = KiiACLEntry.entryWithSubject(theGroup, KiiACLAction.KiiACLSubscribeToTopic);
        acl.putACLEntry(entry);
    
        // Save all the ACL entries.
        return acl.save();
      }
    ).then(
      function(theACL) {
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
    
        // Get the group for the failed refresh() method.
        var theGroup = error.target;
        // Get the ACL for the failed save() method.
        var theACL = error.target;
        // Get the error message.
        var errorString = error.message;
      }
    );

The basic processing is the same as in the previous sample code. In this sample code, we specify a group as an ACL' entry's subject so as to permit the action to all group members.

Deleting a topic ACL entry

To delete an ACL entry, create a KiiACLEntry instance with its third parameter grant set to false and save it. The ACL entry will be deleted from the server.

For example, you can delete the ACL entries created in the previous sample code by executing entry.setGrant(false) after creating a KiiACLEntry instance.

Note that the removeACLEntry() method of the KiiACL just deletes an ACL entry from the local modification list. The method does not delete an ACL entry set on the server. In the above sample code, we first register a request to remove the KiiACLTopicActionSubscribe action from acl on the client and then execute the save() method to reflect the request on the server. If you execute the removeACLEntry() method, it would remove the request in the ACL modification list, but not from the ACL on the server.

Getting a topic's ACL

You can get the ACL set on a topic. Even if you have not set any ACL entries in the topic's ACL, you can get the default ACL of the topic.

Here is the sample code for getting a list of ACL entries as an array.

  • // Instantiate an existing topic in the user scope.
    var topicName = "MyTODO";
    var topic = KiiUser.getCurrentUser().topicWithName(topicName);
    
    // Get the ACL handler.
    var acl = topic.acl();
    
    // Get the ACL.
    acl.listACLEntries({
      success: function(theACL, theEntries) {
        for(var i = 0; i < theEntries.length; i++) {
          var action = theEntries[i].getAction();
          var subject = theEntries[i].getSubject();
          // Check the ACL entry.
        }
      },
      failure: function(theACL, errorString) {
        // Handle the error.
      }
    });
  • // Instantiate an existing topic in the user scope.
    var topicName = "MyTODO";
    var topic = KiiUser.getCurrentUser().topicWithName(topicName);
    
    // Get the ACL handler.
    var acl = topic.acl();
    
    // Get the ACL.
    acl.listACLEntries().then(
      function(params) {
        var theACL = params[0];
        var theEntries = params[1];
        for(var i = 0; i < theEntries.length; i++) {
          var action = theEntries[i].getAction();
          var subject = theEntries[i].getSubject();
          // Check the ACL entry.
        }
      }
    ).catch(
      function(error) {
        // Handle the error.
      }
    );
  1. Create a KiiACL instance of the topic by executing the acl() method.
  2. Call the listAclEntries() method to get the topic's ACL as an array of the KiiACLEntry.
  3. Parse through each ACL entry in the array.

action will have one of the KiiACLAction, and subject will have the subject of the action. See the lists in Topic ACL entries for more information.

Trying to set an existing ACL entry will give you an error. By checking existing ACL entries beforehand as shown in this sample code, you can find which ACL entries should be set.

Troubleshooting

  • I got an error when I run my application multiple times

    The ACL entry should be registered just once (e.g., in the initialization) and should not be registered again in the next run.

    If you try to save an existing ACL entry, you will get an error. If your application runs the same ACL entry registration flow every time, therefore, you will get an error because the application attempts to re-register an already existing ACL entry.

    When you are adding multiple ACL entries, the entry registration is handled one by one. If an error occurs before all entries are registered, some entries will remain unregistered. To recover from such a situation, you will first need to get a list of the existing ACL entries, remove the duplicating ACL entries from the ACL modification list, and then register remaining entries. See Getting a topic's ACL to learn how you can get the list of the existing ACL entries.

  • I cannot delete an ACL entry

    You cannot delete default ACL entries applied to scope owners and topic creators. See Cannot delete default ACL entries of scope owners and creators for more details.