PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/java/org/infoglue/cms/security/GenericCombinedAuthorizationModule.java

https://github.com/aboutyang/infoglue
Java | 673 lines | 419 code | 127 blank | 127 comment | 40 complexity | 4b2d9b6c66960e196898fa830d375503 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0
  1. /* ===============================================================================
  2. *
  3. * Part of the InfoGlue Content Management Platform (www.infoglue.org)
  4. *
  5. * ===============================================================================
  6. *
  7. * Copyright (C)
  8. *
  9. * This program is free software; you can redistribute it and/or modify it under
  10. * the terms of the GNU General Public License version 2, as published by the
  11. * Free Software Foundation. See the file LICENSE.html for more information.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
  19. * Place, Suite 330 / Boston, MA 02111-1307 / USA.
  20. *
  21. * ===============================================================================
  22. */
  23. package org.infoglue.cms.security;
  24. import java.io.Serializable;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.Iterator;
  28. import java.util.List;
  29. import java.util.Properties;
  30. import org.apache.log4j.Logger;
  31. import org.exolab.castor.jdo.Database;
  32. import org.infoglue.cms.entities.management.GroupVO;
  33. import org.infoglue.cms.entities.management.RoleVO;
  34. import org.infoglue.cms.entities.management.SystemUserVO;
  35. import org.infoglue.cms.exception.SystemException;
  36. import org.infoglue.cms.util.CmsPropertyHandler;
  37. import org.infoglue.cms.util.sorters.ReflectionComparator;
  38. /**
  39. * @author Mattias Bogeblad
  40. *
  41. * This authorization module is a generic multi-source authorization module. With this you can
  42. * define any number of underlying authorization modules which is combined by this module. The order of lookup
  43. * is the same as the order the underlying modules are defined in.
  44. *
  45. * To use this you state org.infoglue.cms.security.GenericCombinedAuthorizationModule as AuthorizationModule
  46. * and then you add the underlying modules in Extra security parameters as index based properties like this:
  47. *
  48. * 0.authorizerClassName=org.infoglue.cms.security.JNDIBasicAuthorizationModule
  49. * 1.authorizerClassName=org.infoglue.cms.security.InfoGlueBasicAuthorizationModule
  50. * 2.authorizerClassName=com.mycompany.cms.security.MyCustomAuthorizationModule
  51. *
  52. * Then all modules in turn are asked the queries and if not found the next module is asked.
  53. * If you for example want to have several JNDI-sources you are free to define the same module several times and
  54. * you can differentiate what properties it should use by setting the index in front of the properties.
  55. * For example if you have 2 JNDI sources and they differ in among other things
  56. * roleBase=cn=groups,dc=infoglue,dc=org you add the index in front of two lines of this:
  57. *
  58. * 0.roleBase=cn=groups,dc=infoglue,dc=org
  59. * 1.roleBase=cn=internal,cn=groups,dc=companyx,dc=com
  60. *
  61. * That way the module with index 0 will get all properties without an index and all properties with index 0 will
  62. * override the properties without index for just that module. That way you can also have some parameters in common
  63. * between two of the same modules.
  64. */
  65. public class GenericCombinedAuthorizationModule extends BasicAuthorizationModule implements AuthorizationModule, Serializable
  66. {
  67. private final static Logger logger = Logger.getLogger(GenericCombinedAuthorizationModule.class.getName());
  68. protected Properties extraProperties = null;
  69. private Database transactionObject = null;
  70. private List authorizationModules = new ArrayList();
  71. private AuthorizationModule getAuthorizationModule(String authorizationModuleClassName, int index) throws SystemException
  72. {
  73. AuthorizationModule authorizationModule = null;
  74. try
  75. {
  76. Properties localProperties = new Properties();
  77. Iterator propertiesIterator = this.extraProperties.keySet().iterator();
  78. while(propertiesIterator.hasNext())
  79. {
  80. String property = (String)propertiesIterator.next();
  81. String value = this.extraProperties.getProperty(property);
  82. if(property.startsWith("" + index + "."))
  83. property = property.substring(2);
  84. localProperties.setProperty(property, value);
  85. }
  86. localProperties.setProperty("authorizerIndex", "" + index);
  87. if(logger.isInfoEnabled())
  88. logger.info("InfoGlueAuthenticationFilter.authorizerClass:" + authorizationModuleClassName);
  89. authorizationModule = (AuthorizationModule)Class.forName(authorizationModuleClassName).newInstance();
  90. if(logger.isInfoEnabled())
  91. logger.info("authorizationModule:" + authorizationModule);
  92. authorizationModule.setExtraProperties(localProperties);
  93. authorizationModule.setTransactionObject(this.getTransactionObject());
  94. }
  95. catch(Exception e)
  96. {
  97. logger.error("There was an error initializing the authorizerClass:" + e.getMessage(), e);
  98. throw new SystemException("There was an error initializing the authorizerClass:" + e.getMessage(), e);
  99. }
  100. return authorizationModule;
  101. }
  102. /**
  103. * Gets an authorized InfoGluePrincipal. If the user has logged in with the root-account
  104. * we immediately return - otherwise we populate it.
  105. */
  106. public InfoGluePrincipal getAuthorizedInfoGluePrincipal(String userName) throws Exception
  107. {
  108. InfoGluePrincipal infogluePrincipal = null;
  109. int i=0;
  110. String authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  111. while(authorizerClassName != null && !authorizerClassName.equals("") && infogluePrincipal == null)
  112. {
  113. if(logger.isInfoEnabled())
  114. logger.info("getAuthorizedInfoGluePrincipal in " + authorizerClassName);
  115. try
  116. {
  117. infogluePrincipal = getAuthorizationModule(authorizerClassName, i).getAuthorizedInfoGluePrincipal(userName);
  118. }
  119. catch(Exception e)
  120. {
  121. e.printStackTrace();
  122. }
  123. i++;
  124. authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  125. }
  126. return infogluePrincipal;
  127. }
  128. /**
  129. * Gets an authorized InfoGlueRole.
  130. */
  131. public InfoGlueRole getAuthorizedInfoGlueRole(String roleName) throws Exception
  132. {
  133. InfoGlueRole role = null;
  134. int i=0;
  135. String authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  136. while(authorizerClassName != null && !authorizerClassName.equals("") && role == null)
  137. {
  138. try
  139. {
  140. role = getAuthorizationModule(authorizerClassName, i).getAuthorizedInfoGlueRole(roleName);
  141. }
  142. catch(Exception e)
  143. {
  144. e.printStackTrace();
  145. }
  146. i++;
  147. authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  148. }
  149. return role;
  150. }
  151. /**
  152. * Gets an authorized InfoGlueGroup.
  153. */
  154. public InfoGlueGroup getAuthorizedInfoGlueGroup(String groupName) throws Exception
  155. {
  156. InfoGlueGroup group = null;
  157. int i=0;
  158. String authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  159. while(authorizerClassName != null && !authorizerClassName.equals("") && group == null)
  160. {
  161. try
  162. {
  163. group = getAuthorizationModule(authorizerClassName, i).getAuthorizedInfoGlueGroup(groupName);
  164. }
  165. catch(Exception e)
  166. {
  167. e.printStackTrace();
  168. }
  169. i++;
  170. authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  171. }
  172. return group;
  173. }
  174. /**
  175. * This method gets a users roles
  176. */
  177. /*
  178. public List authorizeUser(String userName) throws Exception
  179. {
  180. List roles = new ArrayList();
  181. int i=0;
  182. String authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  183. while(authorizerClassName != null && !authorizerClassName.equals(""))
  184. {
  185. try
  186. {
  187. roles.addAll(getAuthorizationModule(authorizerClassName, i).authorizeUser(userName));
  188. }
  189. catch(Exception e)
  190. {
  191. e.printStackTrace();
  192. }
  193. i++;
  194. authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  195. }
  196. return roles;
  197. }
  198. */
  199. /**
  200. * This method gets a list of roles
  201. */
  202. public List getRoles() throws Exception
  203. {
  204. List roles = new ArrayList();
  205. int i=0;
  206. String authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  207. while(authorizerClassName != null && !authorizerClassName.equals(""))
  208. {
  209. try
  210. {
  211. List systemRoles = getAuthorizationModule(authorizerClassName, i).getRoles();
  212. if(logger.isInfoEnabled())
  213. logger.info("\nFound:" + systemRoles.size() + " roles in " + i);
  214. roles.addAll(systemRoles);
  215. }
  216. catch(Exception e)
  217. {
  218. e.printStackTrace();
  219. }
  220. i++;
  221. authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  222. }
  223. Collections.sort(roles, new ReflectionComparator("displayName"));
  224. return roles;
  225. }
  226. /**
  227. * This method gets a list of groups
  228. */
  229. public List getGroups() throws Exception
  230. {
  231. List groups = new ArrayList();
  232. int i=0;
  233. String authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  234. while(authorizerClassName != null && !authorizerClassName.equals(""))
  235. {
  236. if(logger.isInfoEnabled())
  237. logger.info("Looking for user in " + authorizerClassName);
  238. try
  239. {
  240. groups.addAll(getAuthorizationModule(authorizerClassName, i).getGroups());
  241. }
  242. catch(Exception e)
  243. {
  244. e.printStackTrace();
  245. }
  246. i++;
  247. authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  248. }
  249. Collections.sort(groups, new ReflectionComparator("displayName"));
  250. return groups;
  251. }
  252. /**
  253. * This method gets a list of users
  254. */
  255. public List getUsers() throws Exception
  256. {
  257. List users = new ArrayList();
  258. int i=0;
  259. String authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  260. while(authorizerClassName != null && !authorizerClassName.equals(""))
  261. {
  262. if(logger.isInfoEnabled())
  263. logger.info("Looking for users in " + authorizerClassName);
  264. try
  265. {
  266. users.addAll(getAuthorizationModule(authorizerClassName, i).getUsers());
  267. }
  268. catch(Exception e)
  269. {
  270. e.printStackTrace();
  271. }
  272. i++;
  273. authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  274. }
  275. Collections.sort(users, new ReflectionComparator("displayName"));
  276. return users;
  277. }
  278. public List getFilteredUsers(Integer offset, Integer limit, String sortProperty, String direction, String searchString, boolean populateRolesAndGroups) throws Exception
  279. {
  280. List users = new ArrayList();
  281. int i=0;
  282. String authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  283. while(authorizerClassName != null && !authorizerClassName.equals(""))
  284. {
  285. if(logger.isInfoEnabled())
  286. logger.info("Looking for users in " + authorizerClassName);
  287. try
  288. {
  289. users.addAll(getAuthorizationModule(authorizerClassName, i).getFilteredUsers(offset, limit, sortProperty, direction, searchString, populateRolesAndGroups));
  290. }
  291. catch(Exception e)
  292. {
  293. e.printStackTrace();
  294. }
  295. i++;
  296. authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  297. }
  298. Collections.sort(users, new ReflectionComparator("displayName"));
  299. return users;
  300. }
  301. /*
  302. public List getFilteredUsers(String firstName, String lastName, String userName, String email, String[] roleIds) throws Exception
  303. {
  304. throw new Exception("Unsupported operation");
  305. //return null;
  306. }
  307. */
  308. public List getUsers(String roleName) throws Exception
  309. {
  310. return getRoleUsers(roleName);
  311. }
  312. public List getRoleUsers(String roleName) throws Exception
  313. {
  314. List users = new ArrayList();
  315. InfoGlueRole role = getAuthorizedInfoGlueRole(roleName);
  316. users.addAll(role.getAutorizationModule().getRoleUsers(roleName));
  317. return users;
  318. }
  319. public List getGroupUsers(String groupName) throws Exception
  320. {
  321. List users = new ArrayList();
  322. InfoGlueGroup group = getAuthorizedInfoGlueGroup(groupName);
  323. users.addAll(group.getAutorizationModule().getGroupUsers(groupName));
  324. return users;
  325. }
  326. public void createInfoGluePrincipal(SystemUserVO systemUserVO) throws Exception
  327. {
  328. int i=0;
  329. String authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  330. while(authorizerClassName != null && !authorizerClassName.equals(""))
  331. {
  332. if(logger.isInfoEnabled())
  333. logger.info("Creating user in " + authorizerClassName);
  334. try
  335. {
  336. getAuthorizationModule(authorizerClassName, i).createInfoGluePrincipal(systemUserVO);
  337. }
  338. catch(Exception e)
  339. {
  340. e.printStackTrace();
  341. }
  342. i++;
  343. authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  344. }
  345. }
  346. public void updateInfoGluePrincipal(SystemUserVO systemUserVO, String[] roleNames, String[] groupNames) throws Exception
  347. {
  348. InfoGluePrincipal principal = getAuthorizedInfoGluePrincipal(systemUserVO.getUserName());
  349. principal.getAutorizationModule().updateInfoGluePrincipal(systemUserVO, roleNames, groupNames);
  350. }
  351. public void updateInfoGluePrincipal(SystemUserVO systemUserVO, String oldPassword, String[] roleNames, String[] groupNames) throws Exception
  352. {
  353. InfoGluePrincipal principal = getAuthorizedInfoGluePrincipal(systemUserVO.getUserName());
  354. principal.getAutorizationModule().updateInfoGluePrincipal(systemUserVO, oldPassword, roleNames, groupNames);
  355. }
  356. /**
  357. * This method is used to send out a newpassword to an existing users.
  358. */
  359. public void updateInfoGluePrincipalPassword(String userName) throws Exception
  360. {
  361. InfoGluePrincipal principal = getAuthorizedInfoGluePrincipal(userName);
  362. principal.getAutorizationModule().updateInfoGluePrincipalPassword(userName);
  363. }
  364. /**
  365. * This method is used to send out a newpassword to an existing users.
  366. */
  367. public void updateInfoGlueAnonymousPrincipalPassword() throws Exception
  368. {
  369. InfoGluePrincipal principal = getAuthorizedInfoGluePrincipal(CmsPropertyHandler.getAnonymousUser());
  370. principal.getAutorizationModule().updateInfoGlueAnonymousPrincipalPassword();
  371. }
  372. /**
  373. * This method is used to let a user update his password by giving his/her old one first.
  374. */
  375. public void updateInfoGluePrincipalPassword(String userName, String oldPassword, String newPassword) throws Exception
  376. {
  377. InfoGluePrincipal principal = getAuthorizedInfoGluePrincipal(userName);
  378. principal.getAutorizationModule().updateInfoGluePrincipalPassword(userName, oldPassword, newPassword);
  379. }
  380. public void changeInfoGluePrincipalUserName(String userName, String newUserName) throws Exception
  381. {
  382. throw new SystemException("This AuthorizationModule does not support changing user name of a principal");
  383. }
  384. public void deleteInfoGluePrincipal(String userName) throws Exception
  385. {
  386. InfoGluePrincipal principal = getAuthorizedInfoGluePrincipal(userName);
  387. principal.getAutorizationModule().deleteInfoGluePrincipal(userName);
  388. }
  389. public void createInfoGlueRole(RoleVO roleVO) throws Exception
  390. {
  391. int i=0;
  392. String authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  393. while(authorizerClassName != null && !authorizerClassName.equals(""))
  394. {
  395. if(logger.isInfoEnabled())
  396. logger.info("Creating role in " + authorizerClassName);
  397. try
  398. {
  399. getAuthorizationModule(authorizerClassName, i).createInfoGlueRole(roleVO);
  400. }
  401. catch(Exception e)
  402. {
  403. e.printStackTrace();
  404. }
  405. i++;
  406. authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  407. }
  408. }
  409. public void deleteInfoGlueRole(String roleName) throws Exception
  410. {
  411. InfoGlueRole role = getAuthorizedInfoGlueRole(roleName);
  412. role.getAutorizationModule().deleteInfoGlueRole(roleName);
  413. }
  414. public void updateInfoGlueRole(RoleVO roleVO, String[] userNames) throws Exception
  415. {
  416. InfoGlueRole role = getAuthorizedInfoGlueRole(roleVO.getRoleName());
  417. role.getAutorizationModule().updateInfoGlueRole(roleVO, userNames);
  418. }
  419. public void createInfoGlueGroup(GroupVO groupVO) throws Exception
  420. {
  421. int i=0;
  422. String authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  423. while(authorizerClassName != null && !authorizerClassName.equals(""))
  424. {
  425. if(logger.isInfoEnabled())
  426. logger.info("Creating Group in " + authorizerClassName);
  427. try
  428. {
  429. getAuthorizationModule(authorizerClassName, i).createInfoGlueGroup(groupVO);
  430. }
  431. catch(Exception e)
  432. {
  433. e.printStackTrace();
  434. }
  435. i++;
  436. authorizerClassName = this.extraProperties.getProperty("" + i + ".authorizerClassName");
  437. }
  438. }
  439. public void deleteInfoGlueGroup(String groupName) throws Exception
  440. {
  441. InfoGlueGroup group = getAuthorizedInfoGlueGroup(groupName);
  442. group.getAutorizationModule().deleteInfoGlueGroup(groupName);
  443. }
  444. public void updateInfoGlueGroup(GroupVO groupVO, String[] userNames) throws Exception
  445. {
  446. InfoGlueGroup group = getAuthorizedInfoGlueGroup(groupVO.getGroupName());
  447. group.getAutorizationModule().updateInfoGlueGroup(groupVO, userNames);
  448. }
  449. public void addUserToGroup(String groupName, String userName) throws Exception
  450. {
  451. InfoGlueGroup group = getAuthorizedInfoGlueGroup(groupName);
  452. group.getAutorizationModule().addUserToGroup(groupName, userName);
  453. }
  454. public void addUserToRole(String roleName, String userName) throws Exception
  455. {
  456. InfoGlueRole role = getAuthorizedInfoGlueRole(roleName);
  457. role.getAutorizationModule().addUserToRole(roleName, userName);
  458. }
  459. /**
  460. * This method is used to remove user from a role.
  461. */
  462. public void removeUserFromRole(String roleName, String userName) throws Exception
  463. {
  464. InfoGlueRole role = getAuthorizedInfoGlueRole(roleName);
  465. role.getAutorizationModule().removeUserFromRole(roleName, userName);
  466. }
  467. /**
  468. * This method is used to remove user from a group.
  469. */
  470. public void removeUserFromGroup(String groupName, String userName) throws Exception
  471. {
  472. InfoGlueGroup group = getAuthorizedInfoGlueGroup(groupName);
  473. group.getAutorizationModule().removeUserFromGroup(groupName, userName);
  474. }
  475. /**
  476. * This method is used find out if a user exists. Much quicker than getAuthorizedPrincipal
  477. */
  478. public boolean userExists(String userName) throws Exception
  479. {
  480. return (getAuthorizedInfoGluePrincipal(userName) == null ? false : true);
  481. }
  482. /**
  483. * This method is used find out if a role exists. Much quicker than getRole
  484. */
  485. public boolean roleExists(String roleName) throws Exception
  486. {
  487. return (getAuthorizedInfoGlueRole(roleName) == null ? false : true);
  488. }
  489. /**
  490. * This method is used find out if a group exists. Much quicker than getGroup
  491. */
  492. public boolean groupExists(String groupName) throws Exception
  493. {
  494. return (getAuthorizedInfoGlueGroup(groupName) == null ? false : true);
  495. }
  496. public boolean getSupportUpdate()
  497. {
  498. return true;
  499. }
  500. public boolean getSupportDelete()
  501. {
  502. return true;
  503. }
  504. public boolean getSupportCreate()
  505. {
  506. return true;
  507. }
  508. public List getFilteredUsers(String firstName, String lastName, String userName, String email, String[] roleIds) throws Exception
  509. {
  510. return null;
  511. }
  512. public Properties getExtraProperties()
  513. {
  514. return this.extraProperties;
  515. }
  516. public void setExtraProperties(Properties properties)
  517. {
  518. this.extraProperties = properties;
  519. }
  520. public void setTransactionObject(Object transactionObject)
  521. {
  522. this.transactionObject = (Database)transactionObject;
  523. }
  524. public Object getTransactionObject()
  525. {
  526. return this.transactionObject;
  527. }
  528. @Override
  529. public Integer getRoleCount(String searchString) throws Exception
  530. {
  531. return getRoles().size();
  532. }
  533. @Override
  534. public Integer getGroupCount(String searchString) throws Exception
  535. {
  536. return getGroups().size();
  537. }
  538. @Override
  539. public Integer getUserCount(String searchString) throws Exception
  540. {
  541. return getUsers().size();
  542. }
  543. }