KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > dialogs > UserEditDialog


1 package info.magnolia.module.admininterface.dialogs;
2
3 import info.magnolia.cms.beans.config.ContentRepository;
4 import info.magnolia.cms.core.Content;
5 import info.magnolia.cms.core.ItemType;
6 import info.magnolia.cms.core.Path;
7 import info.magnolia.cms.gui.control.Save;
8 import info.magnolia.cms.gui.control.SelectOption;
9 import info.magnolia.cms.gui.dialog.DialogButton;
10 import info.magnolia.cms.gui.dialog.DialogDialog;
11 import info.magnolia.cms.gui.dialog.DialogEdit;
12 import info.magnolia.cms.gui.dialog.DialogFactory;
13 import info.magnolia.cms.gui.dialog.DialogInclude;
14 import info.magnolia.cms.gui.dialog.DialogPassword;
15 import info.magnolia.cms.gui.dialog.DialogSelect;
16 import info.magnolia.cms.gui.dialog.DialogStatic;
17 import info.magnolia.cms.gui.dialog.DialogTab;
18 import info.magnolia.cms.i18n.MessagesManager;
19 import info.magnolia.cms.security.Permission;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Locale JavaDoc;
28
29 import javax.jcr.RepositoryException;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.log4j.Logger;
35
36
37 /**
38  * @author Fabrizio Giustina
39  * @version $Id: $
40  */

41 public class UserEditDialog extends ConfiguredDialog {
42
43     /**
44      * Logger.
45      */

46     protected static Logger log = Logger.getLogger(UserEditDialog.class);
47
48     /**
49      * Stable serialVersionUID.
50      */

51     private static final long serialVersionUID = 222L;
52
53     private static final String JavaDoc NODE_ACLUSERS = "acl_users"; //$NON-NLS-1$
54

55     private static final String JavaDoc NODE_ACLROLES = "acl_userroles"; //$NON-NLS-1$
56

57     private static final String JavaDoc NODE_ROLES = "roles"; //$NON-NLS-1$
58

59     private static final String JavaDoc NODE_ACLCONFIG = "acl_config"; //$NON-NLS-1$
60

61     /*
62      * (non-Javadoc)
63      * @see info.magnolia.module.admininterface.DialogMVCHandler#getRepository()
64      */

65     public String JavaDoc getRepository() {
66         String JavaDoc repository = super.getRepository();
67         if (repository == null) {
68             repository = ContentRepository.USERS;
69         }
70         return repository;
71     }
72
73     /**
74      * @param name
75      * @param request
76      * @param response
77      * @param configNode
78      */

79     public UserEditDialog(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Content configNode) {
80         super(name, request, response, configNode);
81     }
82
83     /*
84      * (non-Javadoc)
85      * @see info.magnolia.module.admininterface.DialogMVCHandler#createDialog(info.magnolia.cms.core.Content,
86      * info.magnolia.cms.core.Content)
87      */

88     protected DialogDialog createDialog(Content configNode, Content storageNode) throws RepositoryException {
89         DialogDialog dialog = super.createDialog(configNode, storageNode);
90         dialog.setJavascriptSources("/admindocroot/js/dialogs/acl.js"); //$NON-NLS-1$
91

92         // opener.document.location.reload();window.close();
93

94         dialog.setConfig("width", DialogDialog.DIALOGSIZE_SLIM_WIDTH); //$NON-NLS-1$
95
dialog.setConfig("height", DialogDialog.DIALOGSIZE_SLIM_HEIGHT); //$NON-NLS-1$
96

97         dialog.setLabel(msgs.get("users.edit.edit")); //$NON-NLS-1$
98

99         DialogTab tab = dialog.addTab();
100
101         DialogStatic spacer = DialogFactory.getDialogStaticInstance(request, response, null, null);
102         spacer.setConfig("line", false); //$NON-NLS-1$
103

104         DialogStatic lineHalf = DialogFactory.getDialogStaticInstance(request, response, null, null);
105         lineHalf.setConfig("line", false); //$NON-NLS-1$
106

107         DialogStatic nameEdit = DialogFactory.getDialogStaticInstance(request, response, null, null);
108         nameEdit.setLabel("<strong>" + msgs.get("users.edit.username") + "</strong>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
109
nameEdit.setValue("<strong>" + storageNode.getName() + "</strong>"); //$NON-NLS-1$ //$NON-NLS-2$
110
tab.addSub(nameEdit);
111         tab.addSub(spacer);
112
113         DialogEdit title = DialogFactory.getDialogEditInstance(request, response, storageNode, null);
114         title.setName("title"); //$NON-NLS-1$
115
title.setLabel(msgs.get("users.edit.fullname")); //$NON-NLS-1$
116

117         tab.addSub(title);
118
119         DialogPassword pswd = DialogFactory.getDialogPasswordInstance(request, response, storageNode, null);
120         pswd.setName("pswd"); //$NON-NLS-1$
121
pswd.setLabel(msgs.get("users.edit.password")); //$NON-NLS-1$
122
tab.addSub(pswd);
123
124         tab.addSub(spacer);
125
126         // select language
127
DialogSelect langSelect = DialogFactory.getDialogSelectInstance(request, response, storageNode, null);
128         langSelect.setName("language"); //$NON-NLS-1$
129
langSelect.setLabel(msgs.get("users.edit.language")); //$NON-NLS-1$
130
List JavaDoc options = new ArrayList JavaDoc();
131
132         Collection JavaDoc col = MessagesManager.getAvailableLocales();
133
134         for (Iterator JavaDoc iter = col.iterator(); iter.hasNext();) {
135             Locale JavaDoc locale = (Locale JavaDoc) iter.next();
136             String JavaDoc code = locale.getLanguage();
137             if (StringUtils.isNotEmpty(locale.getCountry())) {
138                 code += "_" + locale.getCountry(); //$NON-NLS-1$
139
}
140             String JavaDoc name = locale.getDisplayName(MessagesManager.getCurrentLocale(request));
141             SelectOption option = new SelectOption(name, code);
142             options.add(option);
143         }
144
145         // sort them
146
Collections.sort(options, new Comparator JavaDoc() {
147
148             public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
149                 try {
150                     String JavaDoc name0 = ((SelectOption) arg0).getLabel();
151                     String JavaDoc name1 = ((SelectOption) arg1).getLabel();
152                     return name0.compareTo(name1);
153                 }
154                 catch (Exception JavaDoc e) {
155                     return 0;
156                 }
157             }
158         });
159
160         langSelect.setOptions(options);
161         tab.addSub(langSelect);
162         tab.addSub(spacer);
163
164         tab.addSub(spacer);
165
166         DialogInclude roles = DialogFactory.getDialogIncludeInstance(request, response, storageNode, null);
167         roles.setLabel(msgs.get("users.edit.roles")); //$NON-NLS-1$
168
roles.setName("aclRolesRepository"); //$NON-NLS-1$
169
roles.setConfig("file", "/.magnolia/dialogpages/usersEditRolesInclude.html"); //$NON-NLS-1$ //$NON-NLS-2$
170
tab.addSub(roles);
171
172         DialogButton add = DialogFactory.getDialogButtonInstance(request, response, null, null);
173         add.setConfig("buttonLabel", msgs.get("buttons.add")); //$NON-NLS-1$ //$NON-NLS-2$
174
add.setConfig("lineSemi", true); //$NON-NLS-1$
175
add.setConfig("onclick", "mgnlAclAdd(true,-1);"); //$NON-NLS-1$ //$NON-NLS-2$
176
tab.addSub(add);
177
178         dialog.setConfig("saveOnclick", "mgnlAclFormSubmit(true);"); //$NON-NLS-1$ //$NON-NLS-2$
179
return dialog;
180     }
181
182     protected Save onPreSave() {
183         Save control = super.onPreSave();
184         control.setPath(path);
185         return control;
186     }
187
188     protected void onPostSave(Save saveControl) {
189         Content user = this.getStorageNode();
190
191         // ######################
192
// # write users and roles acl
193
// ######################
194

195         // remove existing
196
for (int i = 0; i < ContentRepository.getAllRepositoryNames().length; i++) {
197             String JavaDoc repository = ContentRepository.getAllRepositoryNames()[i];
198             try {
199                 user.delete("acl_" + repository); //$NON-NLS-1$
200
}
201             catch (RepositoryException re) {
202                 // new user
203
}
204         }
205
206         // rewrite
207
try {
208
209             Content aclUsers;
210
211             aclUsers = user.createContent(NODE_ACLUSERS, ItemType.CONTENTNODE);
212
213             user.createContent(NODE_ACLROLES, ItemType.CONTENTNODE);
214             user.createContent(NODE_ACLCONFIG, ItemType.CONTENTNODE);
215
216             // give user permission to read and edit himself
217
Content u3 = aclUsers.createContent("0", ItemType.CONTENTNODE); //$NON-NLS-1$
218
u3.createNodeData("path").setValue(user.getHandle() + "/*"); //$NON-NLS-1$ //$NON-NLS-2$
219
u3.createNodeData("permissions").setValue(Permission.ALL); //$NON-NLS-1$
220

221             // ######################
222
// # roles acl
223
// ######################
224
// remove existing
225
try {
226                 user.delete(NODE_ROLES);
227             }
228             catch (RepositoryException re) {
229                 // roles node did not exist yet
230
}
231
232             // rewrite
233
Content roles = user.createContent(NODE_ROLES, ItemType.CONTENTNODE);
234
235             String JavaDoc[] rolesValue = form.getParameter("aclList").split(";"); //$NON-NLS-1$ //$NON-NLS-2$
236

237             for (int i = 0; i < rolesValue.length; i++) {
238                 try {
239                     String JavaDoc newLabel = Path.getUniqueLabel(hm, roles.getHandle(), "0"); //$NON-NLS-1$
240
Content r = roles.createContent(newLabel, ItemType.CONTENTNODE);
241                     r.createNodeData("path").setValue(rolesValue[i]); //$NON-NLS-1$
242
}
243                 catch (Exception JavaDoc e) {
244                     log.error(e.getMessage(), e);
245                 }
246             }
247
248             hm.save();
249         }
250         catch (RepositoryException re) {
251             log.error(re.getMessage(), re);
252         }
253     }
254
255 }
Popular Tags