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
topicActionSubscribe
Subscribe to the topic.
SEND_MESSAGE_TO_TOPIC
topicActionSend
Send push messages to the topic.
* These symbols are defined in the KiiACLAction enumeration and can be specified like KiiACLAction.topicActionSubscribe.
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.
Swift:
Objective-C:
Here is a brief explanation of what is happening in the sample code.
Get the target topic GroupTopic from its URI.
Create an ACL handler by calling the topicAcl() method.
Call the KiiACLEntry(subject:andAction:) method to create ACL entries (entry1, entry2, and entry3).
Call the put(_:) method to locally save each ACL entry.
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 succeeded and failed to register from the succeeded and failed in the sample code.
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.
Swift:
Objective-C:
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 grant property to false.
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 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 an NSArray.
Swift:
Objective-C:
Create a KiiACL instance of the topic by executing the topicACL() method.
Call the listACLEntries(_:) method to get the topic's ACL as an NSArray of the KiiACLEntry instances.
Parse through each ACL entry in the NSArray.
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.