// The contents of this file are subject to the Mozilla Public License Version 1.1
// (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
// WARRANTY OF ANY KIND, either express or implied. See the License for the specific
// language governing rights and limitations under the License.
// 
// The Original Code is the Venice Web Communities System.
// 
// The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
// for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
// Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
// 
// Contributor(s): 

importClass(Packages.com.silverwrist.dynamo.Namespaces);
importClass(Packages.com.silverwrist.dynamo.UserInfoNamespace);
importPackage(Packages.com.silverwrist.dynamo.iface);
importPackage(Packages.com.silverwrist.dynamo.util);
importClass(Packages.com.silverwrist.venice.VeniceNamespaces);
importPackage(Packages.com.silverwrist.venice.frame);
importPackage(Packages.com.silverwrist.venice.session);

req = bsf.lookupBean("request");           // get request
rhelp = bsf.lookupBean("request_help");    // get request helper

// Retrieve the "target" string from the session.
session = rhelp.session;
target = session.getObject("venice-sso:","target") + "";
session.removeObject("venice-sso:","target");

// The authenticated User ID (user object) is in the request attributes.  It may be a proxy, so unwrap it.
auth_user = cast.queryDynamoUser(dynamo.unwrapObject(rhelp.getChainParameter("UserID")));

// There are some Dynamo-specific tests we need to make before we can be comfortable with logging in as
// this user.  Do those now.
errmsg = null;
if (auth_user==null)
{ // user account not found
  errmsg = "The user account you have specified does not exist.  Please try again.";
  audit.write(req,null,VeniceNamespaces.USER_EVENT_NAMESPACE,"login.fail","Bad username");

} // end if
else if (auth_user.isAnonymous())
{ // can't log in as Anonymous_Honyak!
  errmsg = "This account cannot be explicitly logged into.  Please try again.";
  audit.write(req,auth_user,VeniceNamespaces.USER_EVENT_NAMESPACE,"login.fail","Anonymous user");

} // end if
else if (auth_user.isLocked())
{ // locked account - can't log in
  errmsg = "This account has been locked out.  Please contact the system administrator for assistance.";
  audit.write(req,auth_user,VeniceNamespaces.USER_EVENT_NAMESPACE,"login.fail","Locked Account");

} // end else if

if (errmsg!=null)
{ // send the error message back to the login dialog
  session.setObject("venice-sso:","failure.message",errmsg);
  dynamo.scriptReturn(new Redirect("SERVLET","login.js.vs?tgt=" + stringutils.encodeURL(target)));
  
} // end if

logger.debug("User \"" + auth_user.getName() + "\" logged in successfully");
session.setObject(SessionInfoParams.NAMESPACE,SessionInfoParams.ATTR_USER,auth_user);
audit.write(req,auth_user,VeniceNamespaces.USER_EVENT_NAMESPACE,"login.ok");
auth_user.setLastAccessDate(auth_user,new java.util.Date());

// Now set up this user's default objects.
dynamo.exec("/util/setup_user.js");

// Has the user verified their E-mail address yet?  If not, bounce them there.
if (PropertyUtils.hasProperty(auth_user,VeniceNamespaces.USER_SETTINGS_NAMESPACE,"confirmation.number"))
  dynamo.scriptReturn(new Redirect("SERVLET","verify_email.js.vs?tgt=" + stringutils.encodeURL(target)));
else
  dynamo.scriptReturn(new Redirect("SERVLET",target));
