1 26 package org.snipsnap.net.admin; 27 28 import org.snipsnap.config.Configuration; 29 import org.snipsnap.user.UserManager; 30 import org.snipsnap.user.User; 31 import org.snipsnap.user.Roles; 32 import org.snipsnap.container.Components; 33 import org.snipsnap.snip.HomePage; 34 import org.snipsnap.app.Application; 35 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import java.util.Map ; 39 import java.util.Set ; 40 import java.util.HashSet ; 41 42 public class ManageUsers implements SetupHandler { 43 public String getName() { 44 return "users"; 45 } 46 47 public Map setup(HttpServletRequest request, HttpServletResponse response, Configuration config, Map errors) { 48 UserManager um = (UserManager) Components.getComponent(UserManager.class); 49 if (request.getParameter("remove") != null) { 50 User user = um.load(request.getParameter("remove")); 51 if(user != null) { 52 um.remove(user); 53 } 54 return errors; 55 } else if (request.getParameter("edit") != null) { 56 User user = um.load(request.getParameter("login")); 57 request.setAttribute("editUser", user); 58 request.setAttribute("edit", "true"); 59 if(null == user) { 60 request.setAttribute("editUser", new User()); 61 request.setAttribute("create", "true"); 62 } 63 } else if (request.getParameter("save") != null || request.getParameter("create") != null) { 64 request.setAttribute("edit", "true"); 65 String login = request.getParameter("config.users.login"); 66 User user = um.load(login); 67 if(request.getParameter("create") != null) { 68 request.setAttribute("create", request.getParameter("create")); 69 70 if(user != null || null == login) { 71 errors.put("users.login", "users.login"); 72 } else { 73 User tmp = new User(login, "", ""); 74 if(update(request, errors, tmp)) { 75 user = um.create(login, tmp.getPasswd(), tmp.getEmail()); 76 update(request, errors, user); 77 if(errors.size() == 0) { 78 User currUser = Application.get().getUser(); 79 Application.get().setUser(user); 80 HomePage.create(user.getLogin()); 81 Application.get().setUser(currUser); 82 } 83 } 84 } 85 } else if(null == user) { 86 errors.put("users.login", "users.login"); 87 return errors; 88 } 89 90 if (update(request, errors, user)) { 91 um.store(user); 92 } 93 request.setAttribute("editUser", user); 94 if(errors.size() == 0) { 95 request.removeAttribute("edit"); 96 } 97 } 98 99 return errors; 100 } 101 102 105 private boolean update(HttpServletRequest request, Map errors, User user) { 106 107 String email = request.getParameter("config.users.email"); 108 String nPass = request.getParameter("config.users.password"); 109 String nPass2 = request.getParameter("config.users.password.vrfy"); 110 String status = request.getParameter("config.users.status"); 111 String roles[] = request.getParameterValues("config.users.roles"); 112 113 boolean modified = false; 114 if ((user.getEmail() == null && email != null) || 115 (user.getEmail() != null && !user.getEmail().equals(email))) { 116 modified = true; 117 user.setEmail(email); 118 } 119 120 if ((user.getStatus() == null && status != null) || 121 (user.getStatus() != null && !user.getStatus().equals(status))) { 122 modified = true; 123 user.setStatus(status); 124 } 125 126 Roles newRoles = new Roles(parseRoles(roles)); 127 if (!user.getRoles().equals(newRoles)) { 128 if (!newRoles.getRoleSet().isEmpty() && !Roles.allRoles().containsAll(newRoles.getRoleSet())) { 129 errors.put("users.roles", "users.roles"); 130 } else { 131 modified = true; 132 user.setRoles(newRoles); 133 } 134 } 135 136 137 if (nPass != null && nPass.length() > 0) { 138 if (nPass.equals(nPass2)) { 139 modified = true; 140 user.setPasswd(nPass); 141 } else { 142 errors.put("users.password", "users.password"); 143 } 144 } 145 146 return modified; 147 } 148 149 private Set parseRoles(String roles[]) { 150 Set list = new HashSet (); 151 for (int i = 0; roles != null && i < roles.length; i++) { 152 list.add(roles[i]); 153 } 154 return list; 155 } 156 } 157 | Popular Tags |