001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.ac.usecases;
019:
020: import java.util.Map;
021:
022: import org.apache.cocoon.components.ContextHelper;
023: import org.apache.cocoon.environment.ObjectModelHelper;
024: import org.apache.cocoon.environment.Request;
025: import org.apache.lenya.ac.Identity;
026: import org.apache.lenya.cms.publication.Publication;
027: import org.apache.lenya.cms.publication.PublicationUtil;
028: import org.apache.lenya.cms.usecase.UsecaseException;
029:
030: /**
031: * Usecase to login a user.
032: *
033: * @version $Id: Login.java 407305 2006-05-17 16:21:49Z andreas $
034: */
035: public class Login extends AccessControlUsecase {
036:
037: protected static final String HISTORY_SESSION_ATTRIBUTE = "org.apache.lenya.cms.cocoon.acting.History";
038: protected static final String PASSWORD = "password";
039: protected static final String USERNAME = "username";
040: protected static final String REFERRER_QUERY_STRING = "referrerQueryString";
041: protected static final String PUBLICATION = "publication";
042: protected static final String CURRENT_USER = "currentUser";
043:
044: /**
045: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
046: */
047: protected void initParameters() {
048: super .initParameters();
049:
050: Publication publication;
051:
052: try {
053: publication = PublicationUtil.getPublicationFromUrl(
054: this .manager, getDocumentFactory(), getSourceURL());
055: if (publication.exists()) {
056: setParameter(PUBLICATION, publication);
057: }
058: Identity identity = this .getSession().getIdentity();
059: if (identity != null && identity.getUser() != null) {
060: setParameter(CURRENT_USER, this .getSession()
061: .getIdentity().getUser());
062: }
063: } catch (Exception e) {
064: throw new RuntimeException(e);
065: }
066: }
067:
068: /**
069: * Ctor.
070: */
071: public Login() {
072: super ();
073: }
074:
075: /**
076: * Validates the request parameters.
077: * @throws UsecaseException if an error occurs.
078: */
079: void validate() throws UsecaseException {
080:
081: String userId = getParameterAsString(USERNAME);
082: String password = getParameterAsString(PASSWORD);
083:
084: if (userId.length() == 0) {
085: addErrorMessage("Please enter a user name.");
086: }
087: if (password.length() == 0) {
088: addErrorMessage("Please enter a password.");
089: }
090:
091: }
092:
093: /**
094: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
095: */
096: protected void doCheckExecutionConditions() throws Exception {
097:
098: validate();
099:
100: if (!hasErrors()) {
101: Map objectModel = ContextHelper
102: .getObjectModel(getContext());
103: Request request = ObjectModelHelper.getRequest(objectModel);
104: request.getSession(true);
105: if (getAccessController().authenticate(request)) {
106: request.getSession(false).removeAttribute(
107: HISTORY_SESSION_ATTRIBUTE);
108: setDefaultTargetURL(request.getPathInfo());
109: } else {
110: addErrorMessage("Authentication failed");
111: }
112: }
113: }
114:
115: /**
116: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getExitQueryString()
117: */
118: protected String getExitQueryString() {
119: String queryString = getParameterAsString(REFERRER_QUERY_STRING);
120: if (queryString != null && !queryString.equals("")) {
121: queryString = "?" + queryString;
122: }
123: return queryString;
124: }
125: }
|