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 SUBSCRIBE_TO_TOPIC Subscribe to the topic.
    SEND_MESSAGE_TO_TOPIC SEND_MESSAGE_TO_TOPIC Send push messages to the topic.

    * These symbols are defined in the TopicAction enumeration and can be specified like KiiACL.TopicAction.SUBSCRIBE_TO_TOPIC.

  • 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.

  • try {
      // Instantiate a group.
      KiiGroup group = KiiGroup.createByUri(groupUri);
    
      // Refresh the group.
      group.refresh();
    
      // Instantiate a topic in the group scope.
      String topicName = "GroupTopic";
      KiiTopic topic = group.topic(topicName);
    
      // Get the ACL handler.
      KiiACL acl = topic.acl();
    
      // Allow the authenticated users to subscribe to the topic.
      KiiACLEntry entry1 = new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
          KiiACL.TopicAction.SUBSCRIBE_TO_TOPIC, true);
      acl.putACLEntry(entry1);
    
      // Find a user.
      KiiUser user = KiiUser.findUserByUserName("_I_am_a_supervisor_");
    
      // Allow the user to subscribe to the topic.
      KiiACLEntry entry2 = new KiiACLEntry(user,
          KiiACL.TopicAction.SUBSCRIBE_TO_TOPIC, true);
      acl.putACLEntry(entry2);
    
      // Allow the user to send push messages to the topic.
      KiiACLEntry entry3 = new KiiACLEntry(user,
          KiiACL.TopicAction.SEND_MESSAGE_TO_TOPIC, true);
      acl.putACLEntry(entry3);
    
      // Save all the ACL entries.
      acl.save();
    
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    } catch (GroupOperationException e) {
      // Handle the error.
    } catch (ACLOperationException e) {
      // Handle the error.
    }
  • // Instantiate a group.
    KiiGroup group = KiiGroup.createByUri(groupUri);
    
    // Refresh the group.
    group.refresh(new KiiGroupCallBack() {
      @Override
      public void onRefreshCompleted(int token, KiiGroup group, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
        // Instantiate a topic in the group scope.
        String topicName = "GroupTopic";
        KiiTopic topic = group.topic(topicName);
    
        // Get the ACL handler.
        final KiiACL acl = topic.acl();
    
        // Allow the authenticated users to subscribe to the topic.
        KiiACLEntry entry1 = new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
                KiiACL.TopicAction.SUBSCRIBE_TO_TOPIC, true);
        acl.putACLEntry(entry1);
    
        // Find a user.
        KiiUser.findUserByUserName("_I_am_a_supervisor_", new KiiUserCallBack() {
          @Override
          public void onFindCompleted(int token, KiiUser caller, KiiUser found, Exception exception) {
            if (exception != null) {
              // Handle the error.
              return;
            }
            KiiUser user = found;
    
            // Allow the user to subscribe to the topic.
            KiiACLEntry entry2 = new KiiACLEntry(user,
                    KiiACL.TopicAction.SUBSCRIBE_TO_TOPIC, true);
            acl.putACLEntry(entry2);
    
            // Allow the user to send push messages to the topic.
            KiiACLEntry entry3 = new KiiACLEntry(user,
                    KiiACL.TopicAction.SEND_MESSAGE_TO_TOPIC, true);
            acl.putACLEntry(entry3);
    
            // Save all the ACL entries.
            acl.save(new KiiACLCallBack() {
              @Override
              public void onSaveCompleted(int token, KiiACL acl, Exception exception) {
                if (exception != null) {
                  // Handle the error.
                  return;
                }
              }
            });
          }
        });
      }
    });

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.

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. In such a situation, you can get a list of ACL entries failed to register by executing the getFailedACLEntries() method of the ACLOperationException class.

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.

  • try {
      // Instantiate an existing topic in the user scope.
      String topicName = "MyTODO";
      KiiTopic topic = KiiUser.topic(topicName);
    
      // Get the ACL handler.
      KiiACL acl = topic.acl();
    
      // Instantiate a group.
      KiiGroup group = KiiGroup.createByUri(groupUri);
    
      // Refresh the group.
      group.refresh();
    
      // Allow the group members to subscribe to the topic.
      KiiACLEntry entry = new KiiACLEntry(group,
          KiiACL.TopicAction.SUBSCRIBE_TO_TOPIC, true);
      acl.putACLEntry(entry);
    
      // Save all the ACL entries.
      acl.save();
    
    } catch (GroupOperationException ge) {
      // Handle the error.
    } catch (ACLOperationException e) {
      // Handle the error.
    }
  • // Instantiate an existing topic in the user scope.
    String topicName = "MyTODO";
    KiiTopic topic = KiiUser.topic(topicName);
    
    // Get the ACL handler.
    final KiiACL acl = topic.acl();
    
    // Instantiate a group.
    KiiGroup group = KiiGroup.createByUri(groupUri);
    
    // Refresh the group.
    group.refresh(new KiiGroupCallBack() {
      @Override
      public void onRefreshCompleted(int token, KiiGroup group, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
        // Allow the group members to subscribe to the topic.
        KiiACLEntry entry = new KiiACLEntry(group,
                KiiACL.TopicAction.SUBSCRIBE_TO_TOPIC, true);
        acl.putACLEntry(entry);
    
        // Save all the ACL entries.
        acl.save(new KiiACLCallBack() {
          @Override
          public void onSaveCompleted(int token, KiiACL acl, Exception exception) {
            if (exception != null) {
              // Handle the error.
              return;
            }
          }
        });
      }
    });

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 setting the third parameter grant of the new KiiACLEntry to false.

Note that the removeACLEntry() method of the KiiACL class 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 SUBSCRIBE_TO_TOPIC 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 from 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 a Set.

  • // Instantiate an existing topic in the user scope.
    String topicName = "MyTODO";
    KiiTopic topic = KiiUser.topic(topicName);
    
    // Get the ACL handler.
    KiiACL topicAcl = topic.acl();
    
    try {
      // Get the ACL.
      Set<KiiACLEntry> list = topicAcl.listACLEntries();
      for (KiiACLEntry entry : list) {
        KiiACL.Action action = entry.getAction();
        KiiSubject subject = entry.getSubject();
        // Check the ACL entry.
      }
    } catch (ACLOperationException e) {
      // Handle the error.
    }
  • // Instantiate an existing topic in the user scope.
    String topicName = "MyTODO";
    KiiTopic topic = KiiUser.topic(topicName);
    
    // Get the ACL handler.
    KiiACL topicAcl = topic.acl();
    
    // Get the ACL.
    topicAcl.listACLEntries(new KiiACLCallBack() {
      public void onListACLEntriesCompleted(int token, Set<KiiACLEntry> list, KiiACL acl, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
        for (KiiACLEntry entry : list) {
          KiiACL.Action action = entry.getAction();
          KiiSubject subject = entry.getSubject();
          // Check the ACL entry.
        }
      }
    });
  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 a Set of KiiACLEntry instances.
  3. Parse through each ACL entry in the Set.

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.