/*
 * 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;


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 = "password1";
    private static final String PARAM_PASSWORD2 = "password2";

    @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;
        }
        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;
        }
        if (password1 == null || password1.length() < 6) {
            this.addError("Please enter a password with at least 6 characters.");
            return false;
        }
        if (!password1.equals(password2)) {
            this.addError("Passwords don't match.");
            return false;
        }
        return true;
    }

}

@@ -672,24 +672,25 @@
 g4j.Logger;%0A
+%0A
 import org.w
@@ -925,16 +925,75 @@
 ption;%0A%0A
+import java.util.regex.Pattern;%0A%0A/**%0A * Create new user%0A */
 %0Apublic 
@@ -2654,34 +2654,36 @@
 l %7C%7C id.length()
+ 
 ==
+ 
 0) %7B%0A           
@@ -2743,32 +2743,324 @@
   return false;%0A
+        %7D else %7B%0A            Pattern pattern = Pattern.compile(%22%5Ba-z0-9%5B-%5D%5B_%5D%5D*%22);%0A            if (!pattern.matcher(id).matches()) %7B%0A                this.addError(%22Please enter a user ID containing only characters: 'a-z' or '0-9' or '-' or '_'!%22);%0A                return false;%0A            %7D%0A
         %7D%0A      
@@ -3418,32 +3418,33 @@
 , e);%0A        %7D%0A
+%0A
         if (name
@@ -3460,34 +3460,36 @@
 %7C%7C name.length()
+ 
 ==
+ 
 0) %7B%0A           
@@ -3561,32 +3561,33 @@
 alse;%0A        %7D%0A
+%0A
         if (emai
@@ -3613,18 +3613,20 @@
 length()
+ 
 ==
+ 
 0) %7B%0A   
@@ -3710,32 +3710,217 @@
 false;%0A        %7D
+ else if(email.indexOf(%22@%22) %3C= 0) %7B // TODO: Implement some more strict validation%0A            this.addError(%22Please enter a valid email address!%22);%0A            return false;%0A        %7D%0A
 %0A        if (pas
@@ -3962,16 +3962,63 @@
 ) %3C 6) %7B
+ // TODO: Implement some more strict validation
 %0A       
@@ -4120,32 +4120,33 @@
 alse;%0A        %7D%0A
+%0A
         if (!pas
@@ -4254,32 +4254,33 @@
 alse;%0A        %7D%0A
+%0A
         return t
@@ -4290,11 +4290,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

check characters