File Source: CreateUser.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.ui.struts2.admin;
20
21 import java.util.Locale;
22 import java.util.TimeZone;
23 import org.apache.commons.lang.CharSetUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.weblogger.WebloggerException;
28 import org.apache.roller.weblogger.business.WebloggerFactory;
29 import org.apache.roller.weblogger.business.UserManager;
30 import org.apache.roller.weblogger.config.WebloggerConfig;
31 import org.apache.roller.weblogger.pojos.User;
32 import org.apache.roller.weblogger.ui.struts2.core.Register;
33 import org.apache.roller.weblogger.ui.struts2.util.UIAction;
34
35
36 /**
37 * Action for admins to manually create new user accounts.
38 */
39 public class CreateUser extends UIAction {
40
/*
P/P * Method: org.apache.roller.weblogger.ui.struts2.admin.CreateUser__static_init
*
* Postconditions:
* init'ed(log)
*/
41 private static Log log = LogFactory.getLog(CreateUser.class);
42
43 // a bean to store our form data
44 private CreateUserBean bean = new CreateUserBean();
45
46
/*
P/P * Method: void org.apache.roller.weblogger.ui.struts2.admin.CreateUser()
*
* Presumptions:
* init'ed(org/apache/roller/weblogger/ui/struts2/admin/CreateUserBean.java.lang.Boolean.TRUE)
*
* Postconditions:
* this.actionName == &"createUser"
* this.bean == &new CreateUserBean(CreateUser#1)
* this.desiredMenu == &"admin"
* this.pageTitle == &"userAdmin.title.createNewUser"
* new CreateUserBean(CreateUser#1) num objects == 1
* this.bean.activationCode == null
* this.bean.emailAddress == null
* this.bean.fullName == null
* this.bean.id == null
* this.bean.locale == null
* ...
*/
47 public CreateUser() {
48 this.actionName = "createUser";
49 this.desiredMenu = "admin";
50 this.pageTitle = "userAdmin.title.createNewUser";
51 }
52
53
54 // admin role required
55 public String requiredUserRole() {
/*
P/P * Method: String requiredUserRole()
*
* Postconditions:
* return_value == &"admin"
*/
56 return "admin";
57 }
58
59 // no weblog required
60 public boolean isWeblogRequired() {
/*
P/P * Method: bool isWeblogRequired()
*
* Postconditions:
* return_value == 0
*/
61 return false;
62 }
63
64
65 /**
66 * Show admin user creation form.
67 */
68 public String execute() {
69
70 // defaults for locale and timezone
/*
P/P * Method: String execute()
*
* Preconditions:
* this.bean != null
*
* Presumptions:
* java.util.Locale:getDefault(...)@71 != null
* java.util.TimeZone:getDefault(...)@72 != null
*
* Postconditions:
* java.util.Locale:toString(...)._tainted == 0
* return_value == &"input"
* this.bean.locale == &java.util.Locale:toString(...)
* init'ed(this.bean.timeZone)
*/
71 getBean().setLocale(Locale.getDefault().toString());
72 getBean().setTimeZone(TimeZone.getDefault().getID());
73
74 return INPUT;
75 }
76
77
78 /**
79 * Create a new user.
80 */
81 public String save() {
82
83 // run some validation
/*
P/P * Method: String save()
*
* Preconditions:
* (soft) log != null
* (soft) init'ed(org/apache/roller/weblogger/ui/struts2/core/Register.DEFAULT_ALLOWED_CHARS)
* (soft) this.bean != null
* (soft) init'ed(this.bean.activationCode)
* (soft) init'ed(this.bean.administrator)
* (soft) init'ed(this.bean.emailAddress)
* (soft) this.bean.enabled != null
* (soft) init'ed(this.bean.fullName)
* (soft) init'ed(this.bean.locale)
* (soft) init'ed(this.bean.password)
* ...
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@88 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@106 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@88 != null
*
* Postconditions:
* return_value == &"input"
*
* Test Vectors:
* this.bean.administrator: {0}, {1}
* org.apache.roller.weblogger.ui.struts2.admin.CreateUser:hasActionErrors(...)@86: {1}, {0}
*/
84 myValidate();
85
86 if (!hasActionErrors()) try {
87
88 UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
89
90 // copy form data into new user pojo
91 User newUser = new User();
92 getBean().copyTo(newUser, getLocale()); // doesn't copy password
93 newUser.setDateCreated(new java.util.Date());
94
95 // set username and password
96 newUser.setUserName(getBean().getUserName());
97 newUser.resetPassword(getBean().getPassword());
98
99 // are we granting the user admin rights?
100 if(((CreateUserBean)getBean()).isAdministrator()) {
101 newUser.grantRole("admin");
102 }
103
104 // save new user
105 mgr.addUser(newUser);
106 WebloggerFactory.getWeblogger().flush();
107
108 // TODO: i18n
109 addMessage("New user created");
110
111 return INPUT;
112
113 } catch (WebloggerException e) {
114 log.error("Error adding new user", e);
115 // TODO: i18n
116 addError("Error creating user");
117 }
118
119 return INPUT;
120 }
121
122
123 // TODO: replace with struts2 validation
124 private void myValidate() {
125
/*
P/P * Method: void myValidate()
*
* Preconditions:
* this.bean != null
* init'ed(this.bean.emailAddress)
* init'ed(this.bean.password)
* init'ed(this.bean.userName)
* (soft) init'ed(org/apache/roller/weblogger/ui/struts2/core/Register.DEFAULT_ALLOWED_CHARS)
*
* Presumptions:
* org.apache.commons.lang.CharSetUtils:keep(...)@130 != null
*
* Test Vectors:
* java.lang.String:equals(...)@134: {1}, {0}
* java.lang.String:length(...)@127: {1..232-1}, {0}
* org.apache.commons.lang.StringUtils:isEmpty(...)@132: {0}, {1}
* org.apache.commons.lang.StringUtils:isEmpty(...)@138: {0}, {1}
* org.apache.commons.lang.StringUtils:isEmpty(...)@142: {0}, {1}
* org.apache.roller.weblogger.config.WebloggerConfig:getProperty(...)@126: Addr_Set{null}, Inverse{null}
*/
126 String allowed = WebloggerConfig.getProperty("username.allowedChars");
127 if(allowed == null || allowed.trim().length() == 0) {
128 allowed = Register.DEFAULT_ALLOWED_CHARS;
129 }
130 String safe = CharSetUtils.keep(getBean().getUserName(), allowed);
131
132 if (StringUtils.isEmpty(getBean().getUserName())) {
133 addError("error.add.user.missingUserName");
134 } else if (!safe.equals(getBean().getUserName()) ) {
135 addError("error.add.user.badUserName");
136 }
137
138 if (StringUtils.isEmpty(getBean().getEmailAddress())) {
139 addError("error.add.user.missingEmailAddress");
140 }
141
142 if (StringUtils.isEmpty(getBean().getPassword()) &&
143 StringUtils.isEmpty(getBean().getPassword())) {
144 addError("error.add.user.missingPassword");
145 }
146 }
147
148
149 public CreateUserBean getBean() {
/*
P/P * Method: CreateUserBean getBean()
*
* Preconditions:
* init'ed(this.bean)
*
* Postconditions:
* return_value == this.bean
* init'ed(return_value)
*/
150 return bean;
151 }
152
153 public void setBean(CreateUserBean bean) {
/*
P/P * Method: void setBean(CreateUserBean)
*
* Postconditions:
* this.bean == bean
* init'ed(this.bean)
*/
154 this.bean = bean;
155 }
156
157 }
SofCheck Inspector Build Version : 2.18479
| CreateUser.java |
2009-Jan-02 14:25:38 |
| CreateUser.class |
2009-Sep-04 03:12:45 |