org.cherchgk.actions.security.SignUpAction.java Source code

Java tutorial

Introduction

Here is the source code for org.cherchgk.actions.security.SignUpAction.java

Source

/*
 * Copyright 2012-2016 Andrey Grigorov, Anton Grigorov
 *
 * 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.cherchgk.actions.security;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.validator.routines.EmailValidator;
import org.cherchgk.services.SecurityService;
import org.cherchgk.utils.ActionContextHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;

/**
 * ? ?  ?.
 *
 * @author Andrey Grigorov (peneksglazami@gmail.com)
 */
public class SignUpAction extends ActionSupport {

    private SecurityService securityService;

    @Autowired
    public void setSecurityService(SecurityService securityService) {
        this.securityService = securityService;
    }

    @Override
    public String execute() throws Exception {
        String login = ActionContextHelper.getRequestParameterValue("login");
        String email = ActionContextHelper.getRequestParameterValue("email");
        String password = ActionContextHelper.getRequestParameterValue("password");
        String password2 = ActionContextHelper.getRequestParameterValue("password2");

        boolean validationResult = true;

        if ((login == null) || "".equals(login)) {
            addActionError("    ?");
            validationResult = false;
        } else {
            if (securityService.getUserByName(login) != null) {
                addActionError(
                        " ?    ??");
                validationResult = false;
            }
        }

        if (!EmailValidator.getInstance().isValid(email)) {
            addActionError("  ? ? ");
            validationResult = false;
        } else {
            if (securityService.getUserByEmail(email) != null) {
                addActionError(
                        " ?  ? ?   ?");
                validationResult = false;
            }
        }

        if ((password == null) || "".equals(password)) {
            addActionError("    ?");
            validationResult = false;
        }

        if ((password != null) && !password.equals(password2)) {
            addActionError(
                    "      ?");
            validationResult = false;
        }

        if (!validationResult) {
            return Action.ERROR;
        }

        try {
            securityService.registerNewUser(login, password, email);
            addActionMessage(
                    "? ?  ?   ? ? ? .");
        } catch (Exception ex) {
            addActionError("??  ?.");
            return Action.ERROR;
        }

        return Action.SUCCESS;
    }

    @Override
    public Collection<String> getActionErrors() {
        return super.getActionErrors();
    }

    @Override
    public Collection<String> getActionMessages() {
        return super.getActionMessages();
    }
}