Skip to main content

Working with ACL Templates

Work with ACL Templates


Sample_Admin_ACL_Template_Operations.java

The following code sample illustrates how to create an ACL Template, update a template by adding and removing users, and delete a ACL Template.

import java.util.Vector;
import com.eruces.teadmin.ACLAdmin;
import com.eruces.teadmin.ACLTemplate;
import com.eruces.teadmin.ACLTemplateMember;
import com.eruces.teadmin.Administration;
import com.eruces.teadmin.PrincipalInfo;
import com.eruces.teagent.TEAgentConnection;
public class Sample_Admin_ACL_Template_Operations {
public static Vector<String> listAllAclTemplates(TEAgentConnection conn) {

Vector<String> out = new Vector<String>();
try {
ACLAdmin admin_acl = new ACLAdmin();
admin_acl.attachConnection(conn);

ACLTemplate[] templates = admin_acl.getACLTemplate();

for (int i = 0; i < templates.length; i++)
{
out.add(templates[i].getName());
}

System.out.println("Retrieved " + out.size() + " ACL templates.");

admin_acl.detachConnection();
}
catch (Exception e) {
e.printStackTrace();
}

return out;
}

public static void createAclTemplate(TEAgentConnection conn, String temp_name, String user_name) {

try {

Administration admin = new Administration();
admin.attachConnection(conn);

ACLAdmin admin_acl = new ACLAdmin();
admin_acl.attachConnection(conn);

PrincipalInfo user = admin.getUserByName(user_name);

ACLTemplate template = new ACLTemplate();
template.setName(temp_name);

// NOTE: You have to provide at least one member when creating an ACL template.
ACLTemplateMember member = new ACLTemplateMember();
member.setACLTemplateID(template.getACLTemplateID());
member.setSystemID(user.getSystemID());
member.setPrincipalID(user.getPrincipalID());
member.setRights(ACLTemplateMember.ACL_TEMPLATE_READ | ACLTemplateMember.ACL_TEMPLATE_WRITE);

template.addMember(member);
template = admin_acl.addACLTemplate(template);

System.out.println("Added ACL template \"" + temp_name + "\".");

admin.detachConnection();
admin_acl.detachConnection();
}
catch (Exception e) {
e.printStackTrace();
}
}

public static void removeAclTemplate(TEAgentConnection conn, String temp_name) {

try {
ACLAdmin admin_acl = new ACLAdmin();
admin_acl.attachConnection(conn);

ACLTemplate template = new ACLTemplate();
template.setName(temp_name);

template = admin_acl.getACLTemplate(template);

admin_acl.removeACLTemplate(template);

System.out.println("Removed ACL template \"" + temp_name + "\".");

admin_acl.detachConnection();
}
catch (Exception e) {
e.printStackTrace();
}
}

public static void AclTemplate_AddUserTo(TEAgentConnection conn, String temp_name, String user_name) {
try {
Administration admin = new Administration();
admin.attachConnection(conn);

ACLAdmin admin_acl = new ACLAdmin();
admin_acl.attachConnection(conn);

PrincipalInfo user = admin.getUserByName(user_name);

ACLTemplate template = new ACLTemplate();
template.setName(temp_name);

template = admin_acl.getACLTemplate(template);

ACLTemplateMember member = new ACLTemplateMember();
member.setACLTemplateID(template.getACLTemplateID());
member.setSystemID(user.getSystemID());
member.setPrincipalID(user.getPrincipalID());
member.setRights(ACLTemplateMember.ACL_TEMPLATE_READ | ACLTemplateMember.ACL_TEMPLATE_WRITE);

template.addMember(member);
admin_acl.updateACLTemplate(template);

System.out.println("Added user \"" + user_name + "\" to ACL template \"" + temp_name + "\".");

admin.detachConnection();
admin_acl.detachConnection();
}
catch (Exception e) {
e.printStackTrace();
}
}

public static void AclTemplate_RemoveUserFrom(TEAgentConnection conn, String temp_name, String user_name) {
try {
Administration admin = new Administration();
admin.attachConnection(conn);

ACLAdmin admin_acl = new ACLAdmin();
admin_acl.attachConnection(conn);

PrincipalInfo user = admin.getUserByName(user_name);

ACLTemplate template = new ACLTemplate();
template.setName(temp_name);

template = admin_acl.getACLTemplate(template);

ACLTemplateMember member = new ACLTemplateMember();
member.setSystemID(user.getSystemID());
member.setPrincipalID(user.getPrincipalID());

template.removeMember(member);

admin_acl.updateACLTemplate(template);

System.out.println("Removed user \"" + user_name + "\" from ACL template \"" + temp_name + "\".");

admin.detachConnection();
admin_acl.detachConnection();
}
catch (Exception e) {
e.printStackTrace();
}
}
}