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 likeKiiACL.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.
Here is a brief explanation of the sample code.
- Get the target topic
GroupTopic
from its URI. - Create an ACL handler by calling the
acl()
method. - Call the
KiiACLEntry()
method to create ACL entries (entry1
,entry2
, andentry3
). - Call the
putACLEntry()
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 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.
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.
- Create a
KiiACL
instance of the topic by executing theacl()
method. - Call the
listACLEntries()
method to get the topic's ACL as a Set ofKiiACLEntry
instances. - 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.