1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.security.jaas.ext.jonas;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.MethodCache;
20  import com.liferay.portal.kernel.util.ReflectionUtil;
21  import com.liferay.portal.security.jaas.ext.BasicLoginModule;
22  
23  import java.lang.reflect.Method;
24  
25  import java.security.Principal;
26  
27  /**
28   * <a href="PortalLoginModule.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class PortalLoginModule extends BasicLoginModule {
33  
34      public boolean commit() {
35          boolean commitValue = super.commit();
36  
37          if (commitValue) {
38              getSubject().getPrincipals().add(getPrincipal());
39              getSubject().getPrivateCredentials().add(getPassword());
40  
41              Principal group = (Principal)ReflectionUtil.newInstance(
42                  _JGROUP, "Roles");
43              Object role = ReflectionUtil.newInstance(_JROLE, "users");
44  
45              try {
46                  Method method = MethodCache.get(
47                      _JGROUP, "addMember", new Class[] {role.getClass()});
48  
49                  method.invoke(group, new Object[] {role});
50              }
51              catch (Exception e) {
52                  _log.error(e, e);
53              }
54  
55              getSubject().getPrincipals().add(group);
56          }
57  
58          return commitValue;
59      }
60  
61      protected Principal getPortalPrincipal(String name) {
62          return (Principal)ReflectionUtil.newInstance(_JPRINCIPAL, name);
63      }
64  
65      private static final String _JGROUP =
66          "org.objectweb.jonas.security.auth.JGroup";
67  
68      private static final String _JPRINCIPAL =
69          "org.objectweb.jonas.security.auth.JPrincipal";
70  
71      private static final String _JROLE =
72          "org.objectweb.jonas.security.auth.JRole";
73  
74      private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
75  
76  }