001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.jaas.ext.jonas;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.MethodCache;
020    import com.liferay.portal.kernel.util.ReflectionUtil;
021    import com.liferay.portal.security.jaas.ext.BasicLoginModule;
022    
023    import java.lang.reflect.Method;
024    
025    import java.security.Principal;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PortalLoginModule extends BasicLoginModule {
031    
032            public boolean commit() {
033                    boolean commitValue = super.commit();
034    
035                    if (commitValue) {
036                            getSubject().getPrincipals().add(getPrincipal());
037                            getSubject().getPrivateCredentials().add(getPassword());
038    
039                            Principal group = (Principal)ReflectionUtil.newInstance(
040                                    _JGROUP, "Roles");
041                            Object role = ReflectionUtil.newInstance(_JROLE, "users");
042    
043                            try {
044                                    Method method = MethodCache.get(
045                                            _JGROUP, "addMember", new Class[] {role.getClass()});
046    
047                                    method.invoke(group, new Object[] {role});
048                            }
049                            catch (Exception e) {
050                                    _log.error(e, e);
051                            }
052    
053                            getSubject().getPrincipals().add(group);
054                    }
055    
056                    return commitValue;
057            }
058    
059            protected Principal getPortalPrincipal(String name) {
060                    return (Principal)ReflectionUtil.newInstance(_JPRINCIPAL, name);
061            }
062    
063            private static final String _JGROUP =
064                    "org.objectweb.jonas.security.auth.JGroup";
065    
066            private static final String _JPRINCIPAL =
067                    "org.objectweb.jonas.security.auth.JPrincipal";
068    
069            private static final String _JROLE =
070                    "org.objectweb.jonas.security.auth.JRole";
071    
072            private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
073    
074    }