123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663 |
- /**
- Licensed Materials - Property of IBM
- IBM Cognos Products: DOCS
- (C) Copyright IBM Corp. 2005, 2017
- US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
- IBM Corp.
- */
- /**
- * GroupsAndRoles.java
- *
- * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
- * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
- *
- * Description: This file contains methods for handling groups and roles
- *
- */
- import com.cognos.developer.schemas.bibus._3.BaseClass;
- import com.cognos.developer.schemas.bibus._3.BaseClassArrayProp;
- import com.cognos.developer.schemas.bibus._3.Group;
- import com.cognos.developer.schemas.bibus._3.Locale;
- import com.cognos.developer.schemas.bibus._3.MultilingualToken;
- import com.cognos.developer.schemas.bibus._3.MultilingualTokenProp;
- import com.cognos.developer.schemas.bibus._3.PropEnum;
- import com.cognos.developer.schemas.bibus._3.QueryOptions;
- import com.cognos.developer.schemas.bibus._3.Role;
- import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
- import com.cognos.developer.schemas.bibus._3.Sort;
- import com.cognos.developer.schemas.bibus._3.StringProp;
- public class GroupsAndRoles
- {
- CSHandlers csHandler = new CSHandlers();
- /**
- * Create a new Cognos group.
- *
- * @param connection Connection to Server
- * @param groupName Name of new group
- * @param selectedNamespace Namespace in which new group is to be created
- * @return A string containing successful status information.
- *
- */
- public String createGroup(CRNConnect connection, String groupName, String selectedNamespace)
- throws java.rmi.RemoteException
- {
- // Create a new group.
- BaseClass group = new Group();
- String path = selectedNamespace;
- // Note that the defaultName of the new object will be set to
- // the first (in this case, only) name token.
- MultilingualToken[] names = new MultilingualToken[1];
- names[0] = new MultilingualToken();
- Locale[] locales = csHandler.getConfiguration(connection);
- names[0].setLocale(locales[0].getLocale());
- names[0].setValue(groupName);
- group.setName(new MultilingualTokenProp());
- group.getName().setValue(names);
- // Add the new group.
- csHandler.addObjectToCS(connection, group, path);
- return (
- "Successfully created group "
- + groupName
- + " in the Cognos namespace");
- }
- /**
- * Create a new Cognos role.
- *
- * @param connection Connection to Server
- * @return A string containing successful status information.
- *
- */
- public String createRole(CRNConnect connection, String roleName, String nameSpace)
- throws java.rmi.RemoteException
- {
- // Create a new role.
- BaseClass role = new Role();
- String path = nameSpace;
- // Note that the defaultName of the new object will be set to
- // the first (in this case, only) name token.
- MultilingualToken[] names = new MultilingualToken[1];
- names[0] = new MultilingualToken();
- Locale[] locales = csHandler.getConfiguration(connection);
- names[0].setLocale(locales[0].getLocale());
- names[0].setValue(roleName);
- role.setName(new MultilingualTokenProp());
- role.getName().setValue(names);
- // Add the new role.
- csHandler.addObjectToCS(connection, role, path);
- return (
- "Successfully created role "
- + roleName
- + " in the Cognos namespace");
- }
- /**
- * Add the specified user to the specified group or role.
- *
- * @param connection Connection to Server
- * @param pathOfUser Search path to user to be added
- * @param pathOfGroup Search path to the group
- *
- * @return A string containing successful status information.
- *
- */
- public String addUserToGroup(
- CRNConnect connection,
- String pathOfUser,
- String pathOfGroup)
- throws java.rmi.RemoteException
- {
- // get the source objects
- BaseClass user[] = csHandler.queryObjectInCS(connection, pathOfUser);
-
- try
- {
- addToGroup(connection, pathOfGroup, user, pathOfUser);
- }
- catch (java.rmi.RemoteException remoteEx)
- {
- remoteEx.printStackTrace();
- return "Exception caught: " + remoteEx.getMessage();
- }
- catch (java.lang.IllegalArgumentException illegalArg)
- {
- return pathOfUser + " was not added to " + pathOfGroup
- + " because it is already a member.";
- }
-
- return "Successfully added " + pathOfUser
- + " to " + pathOfGroup;
- }
- /**
- * Add the specified user to the specified group or role.
- *
- * @param connection Connection to Server
- * @param pathOfUser Search path to user to be added.
- * @param pathOfRole Search path to the role.
- *
- * @return A string containing successful status information.
- *
- */
- public String addUserToRole(
- CRNConnect connection,
- String pathOfUser,
- String pathOfRole)
- throws java.rmi.RemoteException
- {
- // get the source objects
- BaseClass user[] = csHandler.queryObjectInCS(connection, pathOfUser);
- try
- {
- addToRole(connection, pathOfRole, user, pathOfUser);
- }
- catch (java.rmi.RemoteException remoteEx)
- {
- remoteEx.printStackTrace();
- return "Exception caught: " + remoteEx.getMessage();
- }
- catch (java.lang.IllegalArgumentException illegalArg)
- {
- return pathOfUser + " was not added to " + pathOfRole
- + " because it is already a member.";
- }
-
- return (
- "Successfully added " + pathOfUser + " to " + pathOfRole);
- }
- /**
- * Add the specified member to the specified group.
- *
- * @param connection Connection to Server
- * @param pathOfGroup Search path to the group.
- * @param member User or group to be added.
- *
- */
- public void addToGroup(
- CRNConnect connection,
- String pathOfGroup,
- BaseClass[] member,
- String memberSearchPath)
- throws java.rmi.RemoteException, java.lang.IllegalArgumentException
- {
- // Get the current group membership.
- PropEnum[] props =
- { PropEnum.defaultName, PropEnum.searchPath, PropEnum.members };
- Group group =
- (Group)csHandler.queryObjectInCS(connection, pathOfGroup, props)[0];
- if (group.getMembers().getValue() == null)
- {
- group.setMembers(new BaseClassArrayProp());
- group.getMembers().setValue(member);
- csHandler.updateObjectInCS(connection, new BaseClass[] { group });
- }
- else
- {
- // Preserve all the existing members.
- BaseClass[] newMembers =
- new BaseClass[group.getMembers().getValue().length + 1];
- int index = 0;
- BaseClass obj = null;
- for (int i = 0; i < group.getMembers().getValue().length; i++)
- {
- obj = group.getMembers().getValue()[i];
-
- String objSearchPath = obj.getSearchPath().getValue();
- //Check to see this object is the same as the one to be added
- if (objSearchPath.equals(memberSearchPath))
- {
- throw new IllegalArgumentException("Requested addition is already a member of this group.");
- }
- //BaseClass[] memberProps =
- csHandler.queryObjectInCS(
- connection,
- obj.getSearchPath().getValue());
- newMembers[index] = obj;
- index++;
- }
- newMembers[index] = member[0];
- group.setMembers(new BaseClassArrayProp());
- group.getMembers().setValue(newMembers);
- // Update the membership.
- csHandler.updateObjectInCS(connection, new BaseClass[] { group });
- }
- }
- /**
- * Add the specified member to the specified role.
- *
- * @param connection Connection to Server
- * @param pathOfRole Search path to the role.
- * @param member User, group or role to be added.
- *
- */
- public void addToRole(
- CRNConnect connection,
- String pathOfRole,
- BaseClass[] member,
- String memberSearchPath)
- throws java.rmi.RemoteException, java.lang.IllegalArgumentException
- {
- // Get the current role membership.
- PropEnum[] props =
- { PropEnum.defaultName, PropEnum.searchPath, PropEnum.members };
- Role role = (Role)csHandler.queryObjectInCS(connection, pathOfRole, props)[0];
- if (role.getMembers().getValue() == null)
- {
- role.setMembers(new BaseClassArrayProp());
- role.getMembers().setValue(member);
- csHandler.updateObjectInCS(connection, new BaseClass[] { role });
- }
- else
- {
- // Preserve all the existing members.
- BaseClass[] newMembers =
- new BaseClass[role.getMembers().getValue().length + 1];
- int index = 0;
- BaseClass obj = null;
- for (int i = 0; i < role.getMembers().getValue().length; i++)
- {
- obj = role.getMembers().getValue()[i];
- String objSearchPath = obj.getSearchPath().getValue();
- //Check to see this object is the same as the one to be added
- if (objSearchPath.equals(memberSearchPath))
- {
- throw new IllegalArgumentException("Requested addition is already a member of this role.");
- }
- csHandler.queryObjectInCS(
- connection,
- obj.getSearchPath().getValue());
- newMembers[index] = obj;
- index++;
- }
- newMembers[index] = member[0];
- role.setMembers(new BaseClassArrayProp());
- role.getMembers().setValue(newMembers);
- // Update the membership.
- csHandler.updateObjectInCS(connection, new BaseClass[] { role });
- }
- }
- /**
- * Delete a user from a group.
- *
- * @param connection Connection to Server
- * @param groupSearchPath Search path to group.
- * @param userSearchPath Search path to the user.
- *
- * @return A string containing successful status information.
- *
- */
- public String deleteUserFromGroup(
- CRNConnect connection,
- String groupSearchPath,
- String userSearchPath)
- throws java.rmi.RemoteException
- {
- removeFromGroup(
- connection,
- groupSearchPath,
- (csHandler.queryObjectInCS(
- connection,
- userSearchPath))[0]);
- return (
- "Successfully deleted "
- + userSearchPath
- + " from "
- + groupSearchPath);
- }
- /**
- * Delete a user from a role.
- *
- * @param connection Connection to Server
- * @param roleSearchPath Search path to role.
- * @param memberSearchPath Search path to the user.
- *
- * @return A string containing successful status information.
- *
- */
- public String deleteUserFromRole(
- CRNConnect connection,
- String roleSearchPath,
- String memberSearchPath)
- throws java.rmi.RemoteException
- {
- removeFromRole(
- connection,
- roleSearchPath,
- (csHandler
- .queryObjectInCS(
- connection,
- memberSearchPath))[0]);
- return (
- "Successfully deleted "
- + memberSearchPath
- + " from "
- + roleSearchPath);
- }
- /**
- * Remove the specified member from the specified group.
- *
- * @param connection Connection to Server
- * @param pathOfGroup Search path to the group.
- * @param member An account or group.
- *
- */
- public void removeFromGroup(
- CRNConnect connection,
- String pathOfGroup,
- BaseClass member)
- throws java.rmi.RemoteException
- {
- // Get the current group membership.
- PropEnum[] props =
- { PropEnum.defaultName, PropEnum.searchPath, PropEnum.members };
- BaseClass[] objects =
- csHandler.queryObjectInCS(connection, pathOfGroup, props);
- Group group = (Group)objects[0];
- // Preserve all of the members except the specified member.
- BaseClass[] newMembers =
- new BaseClass[group.getMembers().getValue().length - 1];
- int index = 0;
- BaseClass obj = null;
- for (int i = 0; i < group.getMembers().getValue().length; i++)
- {
- obj = group.getMembers().getValue()[i];
- BaseClass[] memberProps =
- csHandler.queryObjectInCS(connection, obj.getSearchPath().getValue());
- if (memberProps[0]
- .getDefaultName()
- .getValue()
- .compareTo(member.getDefaultName().getValue())
- != 0
- && memberProps[0].getSearchPath().getValue().compareTo(
- member.getSearchPath().getValue())
- != 0)
- {
- newMembers[index] = obj;
- index++;
- }
- }
- group.setMembers(new BaseClassArrayProp());
- group.getMembers().setValue(newMembers);
- // Update the membership.
- csHandler.updateObjectInCS(connection, objects);
- }
- /**
- * Remove the specified member from the specified role.
- *
- * @param connection Connection to Server
- * @param pathOfRole Search path to the role.
- * @param member An account, group or role.
- *
- */
- public void removeFromRole(
- CRNConnect connection,
- String pathOfRole,
- BaseClass member)
- throws java.rmi.RemoteException
- {
- // Get the current role membership.
- PropEnum[] props =
- { PropEnum.defaultName, PropEnum.searchPath, PropEnum.members };
- BaseClass[] objects =
- csHandler.queryObjectInCS(connection, pathOfRole, props);
- Role role = (Role)objects[0];
- // Preserve all of the members except the specified member.
- BaseClass[] newMembers =
- new BaseClass[role.getMembers().getValue().length - 1];
- int index = 0;
- String csMember;
- String csMemberPath;
- BaseClass obj = null;
- for (int i = 0; i < role.getMembers().getValue().length; i++)
- {
- obj = role.getMembers().getValue()[i];
- BaseClass[] memberProps =
- csHandler.queryObjectInCS(connection, obj.getSearchPath().getValue());
- csMember = memberProps[0].getDefaultName().getValue();
- csMemberPath = memberProps[0].getSearchPath().getValue();
- if ((csMember.compareTo(member.getDefaultName().getValue()) != 0)
- && (csMemberPath.compareTo(member.getSearchPath().getValue())
- != 0))
- {
- newMembers[index] = obj;
- index++;
- }
- }
- role.setMembers(new BaseClassArrayProp());
- role.getMembers().setValue(newMembers);
- // Update the membership.
- csHandler.updateObjectInCS(connection, objects);
- }
- /**
- * Delete the specified group.
- *
- * @param connection Connection to Server
- * @param groupSearchPath The searchpath for the group to delete.
- * @return A string containing successful status information.
- *
- */
- public String deleteGroup(CRNConnect connection, String groupSearchPath)
- throws java.rmi.RemoteException
- {
- Group obj = new Group();
- obj.setSearchPath(new StringProp());
- obj.getSearchPath().setValue(groupSearchPath);
- csHandler.deleteObjectFromCS(connection, obj);
- return ("Successfully deleted " + groupSearchPath);
-
- }
- /**
- * Delete the specified role.
- *
- * @param connection Connection to Server
- * @param roleSearchPath The role to delete.
- * @return A string containing successful status information.
- *
- */
- public String deleteRole(CRNConnect connection, String roleSearchPath)
- throws java.rmi.RemoteException
- {
- Role obj = new Role();
- obj.setSearchPath(new StringProp());
- obj.getSearchPath().setValue(roleSearchPath);
- csHandler.deleteObjectFromCS(connection, obj);
- return ("Successfully deleted the " + roleSearchPath);
- }
- /**
- * Get information specific to a particular user.
- *
- * @param connection Connection to Server
- * @param pathOfUser Search path to user to query.
- * @return User Information.
- *
- */
- public BaseClass[] getMemberInfo(
- CRNConnect connection,
- String pathOfUser)
- {
- BaseClass groups[] = new BaseClass[] {};
- PropEnum props[] =
- new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
- try
- {
- groups =
- connection.getCMService().query(
- new SearchPathMultipleObject(pathOfUser),
- props,
- new Sort[] {},
- new QueryOptions());
- }
- catch (Exception e)
- {
- System.out.println(e);
- }
- return groups;
- }
- /**
- * This method queries the Content Store for all the available members
- * in a specific group.
- *
- * @param connection Connection to Server
- * @param group Search path to the group or role.
- *
- */
- public BaseClass[] getAvailableMembers(CRNConnect connection, String group)
- {
- PropEnum props[] =
- new PropEnum[] {
- PropEnum.searchPath,
- PropEnum.defaultName,
- PropEnum.members };
- BaseClass[] availableMembers;
- try
- {
- availableMembers = csHandler.queryObjectInCS(connection, group, props);
- return availableMembers;
- }
- catch (java.rmi.RemoteException remoteEx)
- {
- remoteEx.printStackTrace();
- return null;
- }
- }
- /**
- * This method queries the Content Store for all the available groups in
- * a specific namespace.
- *
- * @param connection Connection to Server
- * @param namespace Search path to the namespace.
- *
- */
- public BaseClass[] getAvailableGroups(
- CRNConnect connection,
- String namespace)
- {
- PropEnum props[] =
- new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
- BaseClass[] availableGroups;
- try
- {
- availableGroups =
- csHandler.queryObjectInCS(connection, namespace + "/group", props);
- return availableGroups;
- }
- catch (java.rmi.RemoteException remoteEx)
- {
- remoteEx.printStackTrace();
- return null;
- }
- }
- /**
- * This method queries the Content Store for all the available roles
- * in a specific namespace.
- *
- * @param connection Connection to Server
- * @param namespace Search path to the namespace.
- *
- */
- public BaseClass[] getAvailableRoles(
- CRNConnect connection,
- String namespace)
- {
- PropEnum props[] =
- new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
- BaseClass[] availableRoles;
- try
- {
- availableRoles =
- csHandler.queryObjectInCS(connection, namespace + "/role", props);
- return availableRoles;
- }
- catch (java.rmi.RemoteException remoteEx)
- {
- remoteEx.printStackTrace();
- return null;
- }
- }
- /**
- * This method queries the Content Store for all the available namespaces.
- *
- * @param connection Connection to Server
- *
- */
- public BaseClass[] getAvailableNamespaces(CRNConnect connection)
- {
- PropEnum props[] =
- new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
- BaseClass[] nameSpaces;
- try
- {
- nameSpaces =
- csHandler.queryObjectInCS(connection, "/directory/namespace", props);
- return nameSpaces;
- }
- catch (java.rmi.RemoteException remoteEx)
- {
- remoteEx.printStackTrace();
- return null;
- }
- }
- }
|