<commit_before>/*
 * Copyright 2007-2009 Wyona
 *
 *  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.wyona.org/licenses/APACHE-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.wyona.yanel.impl.resources;

import org.apache.log4j.Logger;

import org.wyona.security.core.api.AccessManagementException;
import org.wyona.security.core.api.UserManager;
import org.wyona.yanel.impl.resources.usecase.ExecutableUsecaseResource;
import org.wyona.yanel.impl.resources.usecase.UsecaseException;

import java.util.regex.Pattern;

/**
 * Create new user
 */
public class CreateUserResource extends ExecutableUsecaseResource {

    private static final Logger log = Logger.getLogger(CreateUserResource.class);

    private static final String PARAM_USER_ID = "userID";
    private static final String PARAM_NAME = "name";
    private static final String PARAM_EMAIL = "email";
    private static final String PARAM_PASSWORD1 = "<PASSWORD>";
    private static final String PARAM_PASSWORD2 = "<PASSWORD>";

    @Override
    public void execute() throws UsecaseException {
        UserManager userManager = getRealm().getIdentityManager().getUserManager();
        String id = getParameterAsString(PARAM_USER_ID);
        String name = getParameterAsString(PARAM_NAME);
        String email = getParameterAsString(PARAM_EMAIL);
        String password = getParameterAsString(PARAM_PASSWORD1);
        try {
            if (log.isDebugEnabled()) {
                log.debug("creating user: " + id + " " + name + " " + email);
            }
            userManager.createUser(id, name, email, password);
            addInfoMessage("User " + id + " (" + name + ") created successfully.");
        } catch (AccessManagementException e) {
            throw new UsecaseException(e.getMessage(), e);
        }
    }

    @Override
    public boolean checkPreconditions() throws UsecaseException {
        String id = getParameterAsString(PARAM_USER_ID);
        String name = getParameterAsString(PARAM_NAME);
        String email = getParameterAsString(PARAM_EMAIL);
        String password1 = getParameterAsString(PARAM_PASSWORD1);
        String password2 = getParameterAsString(PARAM_PASSWORD2);
        
        if (id == null || id.length() == 0) {
            this.addError("Please enter a user ID.");
            return false;
        } else {
            Pattern pattern = Pattern.compile("[a-z0-9[-][_]]*");
            if (!pattern.matcher(id).matches()) {
                this.addError("Please enter a user ID containing only characters: 'a-z' or '0-9' or '-' or '_'!");
                return false;
            }
        }
        UserManager userManager = getRealm().getIdentityManager().getUserManager();
        try {
            if (userManager.existsUser(id)) {
                this.addError("User with this ID exists already.");
                return false;
            }
        } catch (AccessManagementException e) {
            throw new UsecaseException(e.getMessage(), e);
        }

        if (name == null || name.length() == 0) {
            this.addError("Please enter a user name.");
            return false;
        }

        if (email == null || email.length() == 0) {
            this.addError("Please enter an email address.");
            return false;
        } else if(email.indexOf("@") <= 0) { // TODO: Implement some more strict validation
            this.addError("Please enter a valid email address!");
            return false;
        }

        if (password1 == null || password1.length() < 6) { // TODO: Implement some more strict validation
            this.addError("Please enter a password with at least 6 characters.");
            return false;<commit_msg>also allow at sign such that email addresses can be used as user IDs<commit_after>/*
 * Copyright 2007-2009 Wyona
 *
 *  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.wyona.org/licenses/APACHE-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.wyona.yanel.impl.resources;

import org.apache.log4j.Logger;

import org.wyona.security.core.api.AccessManagementException;
import org.wyona.security.core.api.UserManager;
import org.wyona.yanel.impl.resources.usecase.ExecutableUsecaseResource;
import org.wyona.yanel.impl.resources.usecase.UsecaseException;

import java.util.regex.Pattern;

/**
 * Create new user
 */
public class CreateUserResource extends ExecutableUsecaseResource {

    private static final Logger log = Logger.getLogger(CreateUserResource.class);

    private static final String PARAM_USER_ID = "userID";
    private static final String PARAM_NAME = "name";
    private static final String PARAM_EMAIL = "email";
    private static final String PARAM_PASSWORD1 = "<PASSWORD>";
    private static final String PARAM_PASSWORD2 = "<PASSWORD>";

    @Override
    public void execute() throws UsecaseException {
        UserManager userManager = getRealm().getIdentityManager().getUserManager();
        String id = getParameterAsString(PARAM_USER_ID);
        String name = getParameterAsString(PARAM_NAME);
        String email = getParameterAsString(PARAM_EMAIL);
        String password = getParameterAsString(PARAM_PASSWORD1);
        try {
            if (log.isDebugEnabled()) {
                log.debug("creating user: " + id + " " + name + " " + email);
            }
            userManager.createUser(id, name, email, password);
            addInfoMessage("User " + id + " (" + name + ") created successfully.");
        } catch (AccessManagementException e) {
            throw new UsecaseException(e.getMessage(), e);
        }
    }

    @Override
    public boolean checkPreconditions() throws UsecaseException {
        String id = getParameterAsString(PARAM_USER_ID);
        String name = getParameterAsString(PARAM_NAME);
        String email = getParameterAsString(PARAM_EMAIL);
        String password1 = getParameterAsString(PARAM_PASSWORD1);
        String password2 = getParameterAsString(PARAM_PASSWORD2);
        
        if (id == null || id.length() == 0) {
            this.addError("Please enter a user ID.");
            return false;
        } else {
            Pattern pattern = Pattern.compile("[a-z0-9[-][_][@]]*");
            if (!pattern.matcher(id).matches()) {
                this.addError("Please enter a user ID containing only characters: 'a-z' or '0-9' or '-' or '_'!");
                return false;
            }
        }
        UserManager userManager = getRealm().getIdentityManager().getUserManager();
        try {
            if (userManager.existsUser(id)) {
                this.addError("User with this ID exists already.");
                return false;
            }
        } catch (AccessManagementException e) {
            throw new UsecaseException(e.getMessage(), e);
        }

        if (name == null || name.length() == 0) {
            this.addError("Please enter a user name.");
            return false;
        }

        if (email == null || email.length() == 0) {
            this.addError("Please enter an email address.");
            return false;
        } else if(email.indexOf("@") <= 0) { // TODO: Implement some more strict validation
            this.addError("Please enter a valid email address!");
            return false;
        }

        if (password1 == null || password1.length() < 6) { // TODO: Implement some more strict validation
            this.addError("Please enter a password with at least 6 characters.");
            return false;
{'fraction_non_alphanumeric': 0.10365853658536585, 'fraction_numerical': 0.007651841224294596, 'mean_word_length': 2.621212121212121, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4336986', 'n_tokens_mistral': 2215, 'n_tokens_neox': 2195, 'n_words': 658}