org.ambraproject.registration.BaseRegistrationAction.java Source code

Java tutorial

Introduction

Here is the source code for org.ambraproject.registration.BaseRegistrationAction.java

Source

/*
 * Copyright (c) 2006-2014 by Public Library of Science
 *
 * http://plos.org
 * http://ambraproject.org
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ambraproject.registration;

import org.ambraproject.action.BaseActionSupport;
import org.ambraproject.models.UserProfile;
import org.ambraproject.models.UserRole;
import org.ambraproject.service.user.UserRegistrationService;
import org.ambraproject.service.user.RegistrationConstants;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Required;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Set;

import static org.ambraproject.Constants.AMBRA_USER_KEY;

/**
 * @author Alex Kudlick 9/28/12
 */
public abstract class BaseRegistrationAction extends BaseActionSupport {
    protected static final String BAD_TOKEN = "badToken";
    protected static final String CAS_TOKEN_AUTHENTICATE = "casTokenAuthenticate";
    protected static final String EMAIL_CHANGE_VERIFIED_URL = "ambra.services.registration.url.email-change-verified";
    protected static final String PASSWORD_CHANGE_VERIFIED_URL = "ambra.services.registration.url.password-change-verified";
    protected static final String IMPORTED_ACCOUNT_AUTHENTICATED_URL = "ambra.services.registration.url.imported-profile-verified";
    protected static final String ORCID_CONFIRMATION_URL = "ambra.services.registration.url.orcidConfirm";

    private static final String CAS_LOGIN_URL = "ambra.services.cas.url.login";

    protected UserRegistrationService userRegistrationService;

    public UserRole[] getUserRoles() {
        Map<String, Object> session = ServletActionContext.getContext().getSession();
        UserProfile user = (UserProfile) session.get(AMBRA_USER_KEY);

        if (user != null) {
            Set<UserRole> roles = user.getRoles();
            return roles.toArray(new UserRole[roles.size()]);
        } else {
            return new UserRole[] {};
        }
    }

    /**
     * Get the URL to use for CAS one time token authentication
     *
     * @param token the token to use
     * @param email the email address of the user
     * @param serviceURL the URL to send the user to on success
     *
     * @return the new URL
     *
     * @throws Exception
     */
    protected String getCasOTTURL(String token, String email, String serviceURL) throws Exception {
        //If token is not null and is valid,
        //send redirect to CAS for token based login, CAS should redirect back
        //to the confirmed page
        String casTokenURL = configuration.getString(CAS_LOGIN_URL);

        if (casTokenURL == null) {
            throw new Exception("CAS URL is null, check your configuration");
        }

        casTokenURL = casTokenURL + "?token=" + token + "&username=" + URLEncoder.encode(email, "UTF-8");
        casTokenURL = casTokenURL + "&service=" + URLEncoder.encode(serviceURL, "UTF-8");

        return casTokenURL;
    }

    public String getFromEmailAddress() {
        return configuration.getString(RegistrationConstants.FROM_EMAIL_KEY);
    }

    @Required
    public void setUserRegistrationService(UserRegistrationService userRegistrationService) {
        this.userRegistrationService = userRegistrationService;
    }
}