1 package org.apache.slide.projector.processor.security; 2 3 import java.util.Map ; 4 5 import org.apache.slide.projector.Context; 6 import org.apache.slide.projector.Information; 7 import org.apache.slide.projector.Processor; 8 import org.apache.slide.projector.Projector; 9 import org.apache.slide.projector.Result; 10 import org.apache.slide.projector.descriptor.ParameterDescriptor; 11 import org.apache.slide.projector.descriptor.ResultDescriptor; 12 import org.apache.slide.projector.descriptor.ResultEntryDescriptor; 13 import org.apache.slide.projector.descriptor.StateDescriptor; 14 import org.apache.slide.projector.descriptor.StringValueDescriptor; 15 import org.apache.slide.projector.i18n.DefaultMessage; 16 import org.apache.slide.projector.i18n.ErrorMessage; 17 import org.apache.slide.projector.i18n.ParameterMessage; 18 import org.apache.slide.projector.repository.UserExistsException; 19 import org.apache.slide.projector.value.NullValue; 20 import org.apache.slide.projector.value.URIValue; 21 import org.apache.slide.projector.value.Value; 22 23 26 27 public class CreateUser implements Processor { 28 private final static String USERNAME = "username"; 29 private final static String PASSWORD = "password"; 30 private final static String RETYPE_PASSWORD = "retypePassword"; 31 32 private final static String USER = "user"; 33 private final static String OK = "ok"; 34 private final static String FAILED = "failed"; 35 36 private final static ParameterDescriptor [] parameterDescriptor = new ParameterDescriptor[] { 37 new ParameterDescriptor(USERNAME, new ParameterMessage("create-user/username"), new StringValueDescriptor(1,24)), 38 new ParameterDescriptor(PASSWORD, new ParameterMessage("create-user/password"), new StringValueDescriptor(1,24)), 39 new ParameterDescriptor(RETYPE_PASSWORD, new ParameterMessage("create-user/retyped-password"), new StringValueDescriptor(1,24)), 40 }; 41 42 private final static ResultDescriptor resultDescriptor = new ResultDescriptor( 43 new StateDescriptor[] { 44 new StateDescriptor(OK, new DefaultMessage("create-user/state/ok")), 45 new StateDescriptor(FAILED, new DefaultMessage("create-user/state/failed")) 46 }, new ResultEntryDescriptor[] { 47 new ResultEntryDescriptor(USER, new DefaultMessage("createUser/result/user"), URIValue.CONTENT_TYPE, false) 48 }); 49 50 public Result process(Map parameter, Context context) throws Exception { 51 String username = parameter.get(USERNAME).toString(); 52 String retypedPassword = parameter.get(RETYPE_PASSWORD).toString(); 53 String password = parameter.get(PASSWORD).toString(); 54 String state = OK; 55 Value user = NullValue.NULL; 57 if ( !password.equals(retypedPassword) ) { 58 context.addInformation(new Information(Information.ERROR, new ErrorMessage("register/passwordsNotIdentical"), new String [] { PASSWORD, RETYPE_PASSWORD })); 59 state = FAILED; 60 } else { 61 try { 62 user = Projector.getRepository().createUser(username, password, context.getCredentials()); 63 } catch ( UserExistsException exception ) { 64 context.addInformation(new Information(Information.ERROR, exception.getErrorMessage(), new String [] { USERNAME })); 65 state = FAILED; 66 } 67 } 68 if ( user == null ) { 69 context.addInformation(new Information(Information.ERROR, new ErrorMessage("register/failed"), new String [0])); 70 state = FAILED; 71 } 72 return new Result(state, USER, user); 73 } 74 75 public ParameterDescriptor[] getParameterDescriptors() { 76 return parameterDescriptor; 77 } 78 79 public ResultDescriptor getResultDescriptor() { 80 return resultDescriptor; 81 } 82 } | Popular Tags |