GroupsAndRoles.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2017
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * GroupsAndRoles.java
  10. *
  11. * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  12. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. *
  14. * Description: This file contains methods for handling groups and roles
  15. *
  16. */
  17. import com.cognos.developer.schemas.bibus._3.BaseClass;
  18. import com.cognos.developer.schemas.bibus._3.BaseClassArrayProp;
  19. import com.cognos.developer.schemas.bibus._3.Group;
  20. import com.cognos.developer.schemas.bibus._3.Locale;
  21. import com.cognos.developer.schemas.bibus._3.MultilingualToken;
  22. import com.cognos.developer.schemas.bibus._3.MultilingualTokenProp;
  23. import com.cognos.developer.schemas.bibus._3.PropEnum;
  24. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  25. import com.cognos.developer.schemas.bibus._3.Role;
  26. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  27. import com.cognos.developer.schemas.bibus._3.Sort;
  28. import com.cognos.developer.schemas.bibus._3.StringProp;
  29. public class GroupsAndRoles
  30. {
  31. CSHandlers csHandler = new CSHandlers();
  32. /**
  33. * Create a new Cognos group.
  34. *
  35. * @param connection Connection to Server
  36. * @param groupName Name of new group
  37. * @param selectedNamespace Namespace in which new group is to be created
  38. * @return A string containing successful status information.
  39. *
  40. */
  41. public String createGroup(CRNConnect connection, String groupName, String selectedNamespace)
  42. throws java.rmi.RemoteException
  43. {
  44. // Create a new group.
  45. BaseClass group = new Group();
  46. String path = selectedNamespace;
  47. // Note that the defaultName of the new object will be set to
  48. // the first (in this case, only) name token.
  49. MultilingualToken[] names = new MultilingualToken[1];
  50. names[0] = new MultilingualToken();
  51. Locale[] locales = csHandler.getConfiguration(connection);
  52. names[0].setLocale(locales[0].getLocale());
  53. names[0].setValue(groupName);
  54. group.setName(new MultilingualTokenProp());
  55. group.getName().setValue(names);
  56. // Add the new group.
  57. csHandler.addObjectToCS(connection, group, path);
  58. return (
  59. "Successfully created group "
  60. + groupName
  61. + " in the Cognos namespace");
  62. }
  63. /**
  64. * Create a new Cognos role.
  65. *
  66. * @param connection Connection to Server
  67. * @return A string containing successful status information.
  68. *
  69. */
  70. public String createRole(CRNConnect connection, String roleName, String nameSpace)
  71. throws java.rmi.RemoteException
  72. {
  73. // Create a new role.
  74. BaseClass role = new Role();
  75. String path = nameSpace;
  76. // Note that the defaultName of the new object will be set to
  77. // the first (in this case, only) name token.
  78. MultilingualToken[] names = new MultilingualToken[1];
  79. names[0] = new MultilingualToken();
  80. Locale[] locales = csHandler.getConfiguration(connection);
  81. names[0].setLocale(locales[0].getLocale());
  82. names[0].setValue(roleName);
  83. role.setName(new MultilingualTokenProp());
  84. role.getName().setValue(names);
  85. // Add the new role.
  86. csHandler.addObjectToCS(connection, role, path);
  87. return (
  88. "Successfully created role "
  89. + roleName
  90. + " in the Cognos namespace");
  91. }
  92. /**
  93. * Add the specified user to the specified group or role.
  94. *
  95. * @param connection Connection to Server
  96. * @param pathOfUser Search path to user to be added
  97. * @param pathOfGroup Search path to the group
  98. *
  99. * @return A string containing successful status information.
  100. *
  101. */
  102. public String addUserToGroup(
  103. CRNConnect connection,
  104. String pathOfUser,
  105. String pathOfGroup)
  106. throws java.rmi.RemoteException
  107. {
  108. // get the source objects
  109. BaseClass user[] = csHandler.queryObjectInCS(connection, pathOfUser);
  110. try
  111. {
  112. addToGroup(connection, pathOfGroup, user, pathOfUser);
  113. }
  114. catch (java.rmi.RemoteException remoteEx)
  115. {
  116. remoteEx.printStackTrace();
  117. return "Exception caught: " + remoteEx.getMessage();
  118. }
  119. catch (java.lang.IllegalArgumentException illegalArg)
  120. {
  121. return pathOfUser + " was not added to " + pathOfGroup
  122. + " because it is already a member.";
  123. }
  124. return "Successfully added " + pathOfUser
  125. + " to " + pathOfGroup;
  126. }
  127. /**
  128. * Add the specified user to the specified group or role.
  129. *
  130. * @param connection Connection to Server
  131. * @param pathOfUser Search path to user to be added.
  132. * @param pathOfRole Search path to the role.
  133. *
  134. * @return A string containing successful status information.
  135. *
  136. */
  137. public String addUserToRole(
  138. CRNConnect connection,
  139. String pathOfUser,
  140. String pathOfRole)
  141. throws java.rmi.RemoteException
  142. {
  143. // get the source objects
  144. BaseClass user[] = csHandler.queryObjectInCS(connection, pathOfUser);
  145. try
  146. {
  147. addToRole(connection, pathOfRole, user, pathOfUser);
  148. }
  149. catch (java.rmi.RemoteException remoteEx)
  150. {
  151. remoteEx.printStackTrace();
  152. return "Exception caught: " + remoteEx.getMessage();
  153. }
  154. catch (java.lang.IllegalArgumentException illegalArg)
  155. {
  156. return pathOfUser + " was not added to " + pathOfRole
  157. + " because it is already a member.";
  158. }
  159. return (
  160. "Successfully added " + pathOfUser + " to " + pathOfRole);
  161. }
  162. /**
  163. * Add the specified member to the specified group.
  164. *
  165. * @param connection Connection to Server
  166. * @param pathOfGroup Search path to the group.
  167. * @param member User or group to be added.
  168. *
  169. */
  170. public void addToGroup(
  171. CRNConnect connection,
  172. String pathOfGroup,
  173. BaseClass[] member,
  174. String memberSearchPath)
  175. throws java.rmi.RemoteException, java.lang.IllegalArgumentException
  176. {
  177. // Get the current group membership.
  178. PropEnum[] props =
  179. { PropEnum.defaultName, PropEnum.searchPath, PropEnum.members };
  180. Group group =
  181. (Group)csHandler.queryObjectInCS(connection, pathOfGroup, props)[0];
  182. if (group.getMembers().getValue() == null)
  183. {
  184. group.setMembers(new BaseClassArrayProp());
  185. group.getMembers().setValue(member);
  186. csHandler.updateObjectInCS(connection, new BaseClass[] { group });
  187. }
  188. else
  189. {
  190. // Preserve all the existing members.
  191. BaseClass[] newMembers =
  192. new BaseClass[group.getMembers().getValue().length + 1];
  193. int index = 0;
  194. BaseClass obj = null;
  195. for (int i = 0; i < group.getMembers().getValue().length; i++)
  196. {
  197. obj = group.getMembers().getValue()[i];
  198. String objSearchPath = obj.getSearchPath().getValue();
  199. //Check to see this object is the same as the one to be added
  200. if (objSearchPath.equals(memberSearchPath))
  201. {
  202. throw new IllegalArgumentException("Requested addition is already a member of this group.");
  203. }
  204. //BaseClass[] memberProps =
  205. csHandler.queryObjectInCS(
  206. connection,
  207. obj.getSearchPath().getValue());
  208. newMembers[index] = obj;
  209. index++;
  210. }
  211. newMembers[index] = member[0];
  212. group.setMembers(new BaseClassArrayProp());
  213. group.getMembers().setValue(newMembers);
  214. // Update the membership.
  215. csHandler.updateObjectInCS(connection, new BaseClass[] { group });
  216. }
  217. }
  218. /**
  219. * Add the specified member to the specified role.
  220. *
  221. * @param connection Connection to Server
  222. * @param pathOfRole Search path to the role.
  223. * @param member User, group or role to be added.
  224. *
  225. */
  226. public void addToRole(
  227. CRNConnect connection,
  228. String pathOfRole,
  229. BaseClass[] member,
  230. String memberSearchPath)
  231. throws java.rmi.RemoteException, java.lang.IllegalArgumentException
  232. {
  233. // Get the current role membership.
  234. PropEnum[] props =
  235. { PropEnum.defaultName, PropEnum.searchPath, PropEnum.members };
  236. Role role = (Role)csHandler.queryObjectInCS(connection, pathOfRole, props)[0];
  237. if (role.getMembers().getValue() == null)
  238. {
  239. role.setMembers(new BaseClassArrayProp());
  240. role.getMembers().setValue(member);
  241. csHandler.updateObjectInCS(connection, new BaseClass[] { role });
  242. }
  243. else
  244. {
  245. // Preserve all the existing members.
  246. BaseClass[] newMembers =
  247. new BaseClass[role.getMembers().getValue().length + 1];
  248. int index = 0;
  249. BaseClass obj = null;
  250. for (int i = 0; i < role.getMembers().getValue().length; i++)
  251. {
  252. obj = role.getMembers().getValue()[i];
  253. String objSearchPath = obj.getSearchPath().getValue();
  254. //Check to see this object is the same as the one to be added
  255. if (objSearchPath.equals(memberSearchPath))
  256. {
  257. throw new IllegalArgumentException("Requested addition is already a member of this role.");
  258. }
  259. csHandler.queryObjectInCS(
  260. connection,
  261. obj.getSearchPath().getValue());
  262. newMembers[index] = obj;
  263. index++;
  264. }
  265. newMembers[index] = member[0];
  266. role.setMembers(new BaseClassArrayProp());
  267. role.getMembers().setValue(newMembers);
  268. // Update the membership.
  269. csHandler.updateObjectInCS(connection, new BaseClass[] { role });
  270. }
  271. }
  272. /**
  273. * Delete a user from a group.
  274. *
  275. * @param connection Connection to Server
  276. * @param groupSearchPath Search path to group.
  277. * @param userSearchPath Search path to the user.
  278. *
  279. * @return A string containing successful status information.
  280. *
  281. */
  282. public String deleteUserFromGroup(
  283. CRNConnect connection,
  284. String groupSearchPath,
  285. String userSearchPath)
  286. throws java.rmi.RemoteException
  287. {
  288. removeFromGroup(
  289. connection,
  290. groupSearchPath,
  291. (csHandler.queryObjectInCS(
  292. connection,
  293. userSearchPath))[0]);
  294. return (
  295. "Successfully deleted "
  296. + userSearchPath
  297. + " from "
  298. + groupSearchPath);
  299. }
  300. /**
  301. * Delete a user from a role.
  302. *
  303. * @param connection Connection to Server
  304. * @param roleSearchPath Search path to role.
  305. * @param memberSearchPath Search path to the user.
  306. *
  307. * @return A string containing successful status information.
  308. *
  309. */
  310. public String deleteUserFromRole(
  311. CRNConnect connection,
  312. String roleSearchPath,
  313. String memberSearchPath)
  314. throws java.rmi.RemoteException
  315. {
  316. removeFromRole(
  317. connection,
  318. roleSearchPath,
  319. (csHandler
  320. .queryObjectInCS(
  321. connection,
  322. memberSearchPath))[0]);
  323. return (
  324. "Successfully deleted "
  325. + memberSearchPath
  326. + " from "
  327. + roleSearchPath);
  328. }
  329. /**
  330. * Remove the specified member from the specified group.
  331. *
  332. * @param connection Connection to Server
  333. * @param pathOfGroup Search path to the group.
  334. * @param member An account or group.
  335. *
  336. */
  337. public void removeFromGroup(
  338. CRNConnect connection,
  339. String pathOfGroup,
  340. BaseClass member)
  341. throws java.rmi.RemoteException
  342. {
  343. // Get the current group membership.
  344. PropEnum[] props =
  345. { PropEnum.defaultName, PropEnum.searchPath, PropEnum.members };
  346. BaseClass[] objects =
  347. csHandler.queryObjectInCS(connection, pathOfGroup, props);
  348. Group group = (Group)objects[0];
  349. // Preserve all of the members except the specified member.
  350. BaseClass[] newMembers =
  351. new BaseClass[group.getMembers().getValue().length - 1];
  352. int index = 0;
  353. BaseClass obj = null;
  354. for (int i = 0; i < group.getMembers().getValue().length; i++)
  355. {
  356. obj = group.getMembers().getValue()[i];
  357. BaseClass[] memberProps =
  358. csHandler.queryObjectInCS(connection, obj.getSearchPath().getValue());
  359. if (memberProps[0]
  360. .getDefaultName()
  361. .getValue()
  362. .compareTo(member.getDefaultName().getValue())
  363. != 0
  364. && memberProps[0].getSearchPath().getValue().compareTo(
  365. member.getSearchPath().getValue())
  366. != 0)
  367. {
  368. newMembers[index] = obj;
  369. index++;
  370. }
  371. }
  372. group.setMembers(new BaseClassArrayProp());
  373. group.getMembers().setValue(newMembers);
  374. // Update the membership.
  375. csHandler.updateObjectInCS(connection, objects);
  376. }
  377. /**
  378. * Remove the specified member from the specified role.
  379. *
  380. * @param connection Connection to Server
  381. * @param pathOfRole Search path to the role.
  382. * @param member An account, group or role.
  383. *
  384. */
  385. public void removeFromRole(
  386. CRNConnect connection,
  387. String pathOfRole,
  388. BaseClass member)
  389. throws java.rmi.RemoteException
  390. {
  391. // Get the current role membership.
  392. PropEnum[] props =
  393. { PropEnum.defaultName, PropEnum.searchPath, PropEnum.members };
  394. BaseClass[] objects =
  395. csHandler.queryObjectInCS(connection, pathOfRole, props);
  396. Role role = (Role)objects[0];
  397. // Preserve all of the members except the specified member.
  398. BaseClass[] newMembers =
  399. new BaseClass[role.getMembers().getValue().length - 1];
  400. int index = 0;
  401. String csMember;
  402. String csMemberPath;
  403. BaseClass obj = null;
  404. for (int i = 0; i < role.getMembers().getValue().length; i++)
  405. {
  406. obj = role.getMembers().getValue()[i];
  407. BaseClass[] memberProps =
  408. csHandler.queryObjectInCS(connection, obj.getSearchPath().getValue());
  409. csMember = memberProps[0].getDefaultName().getValue();
  410. csMemberPath = memberProps[0].getSearchPath().getValue();
  411. if ((csMember.compareTo(member.getDefaultName().getValue()) != 0)
  412. && (csMemberPath.compareTo(member.getSearchPath().getValue())
  413. != 0))
  414. {
  415. newMembers[index] = obj;
  416. index++;
  417. }
  418. }
  419. role.setMembers(new BaseClassArrayProp());
  420. role.getMembers().setValue(newMembers);
  421. // Update the membership.
  422. csHandler.updateObjectInCS(connection, objects);
  423. }
  424. /**
  425. * Delete the specified group.
  426. *
  427. * @param connection Connection to Server
  428. * @param groupSearchPath The searchpath for the group to delete.
  429. * @return A string containing successful status information.
  430. *
  431. */
  432. public String deleteGroup(CRNConnect connection, String groupSearchPath)
  433. throws java.rmi.RemoteException
  434. {
  435. Group obj = new Group();
  436. obj.setSearchPath(new StringProp());
  437. obj.getSearchPath().setValue(groupSearchPath);
  438. csHandler.deleteObjectFromCS(connection, obj);
  439. return ("Successfully deleted " + groupSearchPath);
  440. }
  441. /**
  442. * Delete the specified role.
  443. *
  444. * @param connection Connection to Server
  445. * @param roleSearchPath The role to delete.
  446. * @return A string containing successful status information.
  447. *
  448. */
  449. public String deleteRole(CRNConnect connection, String roleSearchPath)
  450. throws java.rmi.RemoteException
  451. {
  452. Role obj = new Role();
  453. obj.setSearchPath(new StringProp());
  454. obj.getSearchPath().setValue(roleSearchPath);
  455. csHandler.deleteObjectFromCS(connection, obj);
  456. return ("Successfully deleted the " + roleSearchPath);
  457. }
  458. /**
  459. * Get information specific to a particular user.
  460. *
  461. * @param connection Connection to Server
  462. * @param pathOfUser Search path to user to query.
  463. * @return User Information.
  464. *
  465. */
  466. public BaseClass[] getMemberInfo(
  467. CRNConnect connection,
  468. String pathOfUser)
  469. {
  470. BaseClass groups[] = new BaseClass[] {};
  471. PropEnum props[] =
  472. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  473. try
  474. {
  475. groups =
  476. connection.getCMService().query(
  477. new SearchPathMultipleObject(pathOfUser),
  478. props,
  479. new Sort[] {},
  480. new QueryOptions());
  481. }
  482. catch (Exception e)
  483. {
  484. System.out.println(e);
  485. }
  486. return groups;
  487. }
  488. /**
  489. * This method queries the Content Store for all the available members
  490. * in a specific group.
  491. *
  492. * @param connection Connection to Server
  493. * @param group Search path to the group or role.
  494. *
  495. */
  496. public BaseClass[] getAvailableMembers(CRNConnect connection, String group)
  497. {
  498. PropEnum props[] =
  499. new PropEnum[] {
  500. PropEnum.searchPath,
  501. PropEnum.defaultName,
  502. PropEnum.members };
  503. BaseClass[] availableMembers;
  504. try
  505. {
  506. availableMembers = csHandler.queryObjectInCS(connection, group, props);
  507. return availableMembers;
  508. }
  509. catch (java.rmi.RemoteException remoteEx)
  510. {
  511. remoteEx.printStackTrace();
  512. return null;
  513. }
  514. }
  515. /**
  516. * This method queries the Content Store for all the available groups in
  517. * a specific namespace.
  518. *
  519. * @param connection Connection to Server
  520. * @param namespace Search path to the namespace.
  521. *
  522. */
  523. public BaseClass[] getAvailableGroups(
  524. CRNConnect connection,
  525. String namespace)
  526. {
  527. PropEnum props[] =
  528. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  529. BaseClass[] availableGroups;
  530. try
  531. {
  532. availableGroups =
  533. csHandler.queryObjectInCS(connection, namespace + "/group", props);
  534. return availableGroups;
  535. }
  536. catch (java.rmi.RemoteException remoteEx)
  537. {
  538. remoteEx.printStackTrace();
  539. return null;
  540. }
  541. }
  542. /**
  543. * This method queries the Content Store for all the available roles
  544. * in a specific namespace.
  545. *
  546. * @param connection Connection to Server
  547. * @param namespace Search path to the namespace.
  548. *
  549. */
  550. public BaseClass[] getAvailableRoles(
  551. CRNConnect connection,
  552. String namespace)
  553. {
  554. PropEnum props[] =
  555. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  556. BaseClass[] availableRoles;
  557. try
  558. {
  559. availableRoles =
  560. csHandler.queryObjectInCS(connection, namespace + "/role", props);
  561. return availableRoles;
  562. }
  563. catch (java.rmi.RemoteException remoteEx)
  564. {
  565. remoteEx.printStackTrace();
  566. return null;
  567. }
  568. }
  569. /**
  570. * This method queries the Content Store for all the available namespaces.
  571. *
  572. * @param connection Connection to Server
  573. *
  574. */
  575. public BaseClass[] getAvailableNamespaces(CRNConnect connection)
  576. {
  577. PropEnum props[] =
  578. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  579. BaseClass[] nameSpaces;
  580. try
  581. {
  582. nameSpaces =
  583. csHandler.queryObjectInCS(connection, "/directory/namespace", props);
  584. return nameSpaces;
  585. }
  586. catch (java.rmi.RemoteException remoteEx)
  587. {
  588. remoteEx.printStackTrace();
  589. return null;
  590. }
  591. }
  592. }