/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2009 L. Meckert - laurent.meckert@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS 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 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS 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 XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.dgfip.xemelios.web.connexion;

import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.auth.impl.std.db.DataHelper;
import fr.gouv.finances.dgfip.xemelios.auth.impl.std.db.StdDbAuthEngineImpl;
import jespa.http.HttpSecurityServletRequest;
import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import java.lang.reflect.Method;

/**
 * @author LM
 */
public class NtlmAuthEngineImpl extends fr.gouv.finances.dgfip.xemelios.auth.impl.std.db.StdDbAuthEngineImpl {

   private static final Logger logger = Logger.getLogger(NtlmAuthEngineImpl.class);

   public NtlmAuthEngineImpl() throws SQLException {
      super(false);
   }

   public boolean isLoginRequired() {
      return false;
   }

   public boolean isPasswordRequired() {
      return false;
   }

   public XemeliosUser createUser(String unusedlogin, String unusedpassword) {
      ThreadLocal tl = fr.gouv.finances.dgfip.xemelios.web.connexion.ConnexionAction.getThreadLocalRequestHolder();
      HttpSecurityServletRequest request;
      String userLogin;
      Connection connection = null;
      try {
         connection = PoolManager.getInstance().getConnection();
         request = (HttpSecurityServletRequest) (tl.get());
         userLogin = request.getRemoteUser();
         logger.info("userLogin=" + userLogin);
         if (userLogin != null) {
            // Pour rire
//                if( request.isUserInRole("AXYUS\\commercial") ) {
//                     logger.info("connect as adamin");
//                    return doCreateUser(connection, "admin");
//                } else {
//                     logger.info("connect as " + userLogin);
            XemeliosUser dejala = doCreateUser(connection, userLogin);
            if (dejala != null) {
               return dejala;
            } else {
               DataHelper.StdUser stdUser = new DataHelper.StdUser(userLogin, userLogin, userLogin, false, false, false, -1);
               DataHelper.saveUser(stdUser, "*");
               return doCreateUser(connection, userLogin);
            }

            //  }
            } else {
            return null;
         }
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      } finally {
         PoolManager.getInstance().releaseConnection(connection);
      }

   }

   protected void insertAdminMenu() {
      boolean doNotStop = false;
      Class clazz = null;
      Object instance = null;
      try {
         clazz = Class.forName("fr.gouv.finances.cp.xemelios.ui.MainWindow");
         Method meth = clazz.getMethod("getInstance");
         instance = meth.invoke(null);
         doNotStop = (instance != null);
      } catch (Throwable t) {
      }

   }
}
