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 ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.Comparator ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Locale ; 28 29 import javax.jcr.RepositoryException; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 33 import org.apache.commons.lang.StringUtils; 34 import org.apache.log4j.Logger; 35 36 37 41 public class UserEditDialog extends ConfiguredDialog { 42 43 46 protected static Logger log = Logger.getLogger(UserEditDialog.class); 47 48 51 private static final long serialVersionUID = 222L; 52 53 private static final String NODE_ACLUSERS = "acl_users"; 55 private static final String NODE_ACLROLES = "acl_userroles"; 57 private static final String NODE_ROLES = "roles"; 59 private static final String NODE_ACLCONFIG = "acl_config"; 61 65 public String getRepository() { 66 String repository = super.getRepository(); 67 if (repository == null) { 68 repository = ContentRepository.USERS; 69 } 70 return repository; 71 } 72 73 79 public UserEditDialog(String name, HttpServletRequest request, HttpServletResponse response, Content configNode) { 80 super(name, request, response, configNode); 81 } 82 83 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"); 92 94 dialog.setConfig("width", DialogDialog.DIALOGSIZE_SLIM_WIDTH); dialog.setConfig("height", DialogDialog.DIALOGSIZE_SLIM_HEIGHT); 97 dialog.setLabel(msgs.get("users.edit.edit")); 99 DialogTab tab = dialog.addTab(); 100 101 DialogStatic spacer = DialogFactory.getDialogStaticInstance(request, response, null, null); 102 spacer.setConfig("line", false); 104 DialogStatic lineHalf = DialogFactory.getDialogStaticInstance(request, response, null, null); 105 lineHalf.setConfig("line", false); 107 DialogStatic nameEdit = DialogFactory.getDialogStaticInstance(request, response, null, null); 108 nameEdit.setLabel("<strong>" + msgs.get("users.edit.username") + "</strong>"); nameEdit.setValue("<strong>" + storageNode.getName() + "</strong>"); tab.addSub(nameEdit); 111 tab.addSub(spacer); 112 113 DialogEdit title = DialogFactory.getDialogEditInstance(request, response, storageNode, null); 114 title.setName("title"); title.setLabel(msgs.get("users.edit.fullname")); 117 tab.addSub(title); 118 119 DialogPassword pswd = DialogFactory.getDialogPasswordInstance(request, response, storageNode, null); 120 pswd.setName("pswd"); pswd.setLabel(msgs.get("users.edit.password")); tab.addSub(pswd); 123 124 tab.addSub(spacer); 125 126 DialogSelect langSelect = DialogFactory.getDialogSelectInstance(request, response, storageNode, null); 128 langSelect.setName("language"); langSelect.setLabel(msgs.get("users.edit.language")); List options = new ArrayList (); 131 132 Collection col = MessagesManager.getAvailableLocales(); 133 134 for (Iterator iter = col.iterator(); iter.hasNext();) { 135 Locale locale = (Locale ) iter.next(); 136 String code = locale.getLanguage(); 137 if (StringUtils.isNotEmpty(locale.getCountry())) { 138 code += "_" + locale.getCountry(); } 140 String name = locale.getDisplayName(MessagesManager.getCurrentLocale(request)); 141 SelectOption option = new SelectOption(name, code); 142 options.add(option); 143 } 144 145 Collections.sort(options, new Comparator () { 147 148 public int compare(Object arg0, Object arg1) { 149 try { 150 String name0 = ((SelectOption) arg0).getLabel(); 151 String name1 = ((SelectOption) arg1).getLabel(); 152 return name0.compareTo(name1); 153 } 154 catch (Exception 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")); roles.setName("aclRolesRepository"); roles.setConfig("file", "/.magnolia/dialogpages/usersEditRolesInclude.html"); tab.addSub(roles); 171 172 DialogButton add = DialogFactory.getDialogButtonInstance(request, response, null, null); 173 add.setConfig("buttonLabel", msgs.get("buttons.add")); add.setConfig("lineSemi", true); add.setConfig("onclick", "mgnlAclAdd(true,-1);"); tab.addSub(add); 177 178 dialog.setConfig("saveOnclick", "mgnlAclFormSubmit(true);"); 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 195 for (int i = 0; i < ContentRepository.getAllRepositoryNames().length; i++) { 197 String repository = ContentRepository.getAllRepositoryNames()[i]; 198 try { 199 user.delete("acl_" + repository); } 201 catch (RepositoryException re) { 202 } 204 } 205 206 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 Content u3 = aclUsers.createContent("0", ItemType.CONTENTNODE); u3.createNodeData("path").setValue(user.getHandle() + "/*"); u3.createNodeData("permissions").setValue(Permission.ALL); 221 try { 226 user.delete(NODE_ROLES); 227 } 228 catch (RepositoryException re) { 229 } 231 232 Content roles = user.createContent(NODE_ROLES, ItemType.CONTENTNODE); 234 235 String [] rolesValue = form.getParameter("aclList").split(";"); 237 for (int i = 0; i < rolesValue.length; i++) { 238 try { 239 String newLabel = Path.getUniqueLabel(hm, roles.getHandle(), "0"); Content r = roles.createContent(newLabel, ItemType.CONTENTNODE); 241 r.createNodeData("path").setValue(rolesValue[i]); } 243 catch (Exception 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 |