1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.InstanceFactory;
20  import com.liferay.portal.kernel.util.MethodCache;
21  import com.liferay.portal.security.jaas.ext.BasicLoginModule;
22  
23  import java.lang.reflect.Method;
24  
25  import java.security.Principal;
26  
27  import java.util.Set;
28  
29  import javax.security.auth.Subject;
30  import javax.security.auth.login.LoginException;
31  
32  /**
33   * <a href="PortalLoginModule.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class PortalLoginModule extends BasicLoginModule {
38  
39      public boolean commit() throws LoginException {
40          boolean commitValue = super.commit();
41  
42          if (commitValue) {
43              Subject subject = getSubject();
44  
45              Set<Principal> principals = subject.getPrincipals();
46  
47              principals.add(getPrincipal());
48  
49              Set<Object> privateCredentials = subject.getPrivateCredentials();
50  
51              privateCredentials.add(getPassword());
52  
53              try {
54                  Principal group = (Principal)InstanceFactory.newInstance(
55                      _JGROUP, String.class, "Roles");
56                  Object role = InstanceFactory.newInstance(
57                      _JROLE, String.class, "users");
58  
59                  Method method = MethodCache.get(
60                      _JGROUP, "addMember", new Class[] {role.getClass()});
61  
62                  method.invoke(group, new Object[] {role});
63  
64                  principals.add(group);
65              }
66              catch (Exception e) {
67                  _log.error(e, e);
68              }
69          }
70  
71          return commitValue;
72      }
73  
74      protected Principal getPortalPrincipal(String name) throws LoginException {
75          try {
76              return (Principal)InstanceFactory.newInstance(
77                  _JPRINCIPAL, String.class, name);
78          }
79          catch (Exception e) {
80              throw new LoginException(e.getMessage());
81          }
82      }
83  
84      private static final String _JGROUP =
85          "org.objectweb.jonas.security.auth.JGroup";
86  
87      private static final String _JPRINCIPAL =
88          "org.objectweb.jonas.security.auth.JPrincipal";
89  
90      private static final String _JROLE =
91          "org.objectweb.jonas.security.auth.JRole";
92  
93      private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
94  
95  }