/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.test.jbossnet.security.server;

import org.jboss.security.SimpleGroup;
import org.jboss.security.SimplePrincipal;
import org.jboss.security.auth.spi.UsernamePasswordLoginModule;

import javax.security.auth.login.LoginException;
import java.security.acl.Group;

/**
 * Alternate login module for replacing the other domain
 * @author jung
 * @since 12.11.03
 */
public class TestLoginModule extends UsernamePasswordLoginModule
{

   /* (non-Javadoc)
    * @see org.jboss.security.auth.spi.UsernamePasswordLoginModule#getUsersPassword()
    */
   protected String getUsersPassword() throws LoginException
   {
      log.debug("Using Pricipal: " + getUsername());

      if ("jduke".equals(getUsername()))
      {
         return "theduke";
      }
      else if ("jdukeman".equals(getUsername()))
      {
         return "anotherduke";
      }
      return null;
   }

   /* (non-Javadoc)
    * @see org.jboss.security.auth.spi.AbstractServerLoginModule#getRoleSets()
    */
   protected Group[] getRoleSets() throws LoginException
   {
      SimpleGroup roles = new SimpleGroup("Roles");

      if ("jduke".equals(getUsername()))
      {
         roles.addMember(new SimplePrincipal("orb.jboss.net.Administrator"));
         roles.addMember(new SimplePrincipal("Role1"));
         roles.addMember(new SimplePrincipal("Role2"));
      }
      else if ("jdukeman".equals(getUsername()))
      {
         roles.addMember(new SimplePrincipal("Role2"));
         roles.addMember(new SimplePrincipal("Role3"));
      }

      log.debug("User Roles: " + roles);
      return new Group[]{roles};
   }

}
