1 18 package org.apache.roller.ui.authoring.struts.actions; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.struts.action.ActionError; 30 import org.apache.struts.action.ActionErrors; 31 import org.apache.struts.action.ActionForm; 32 import org.apache.struts.action.ActionForward; 33 import org.apache.struts.action.ActionMapping; 34 import org.apache.struts.action.ActionMessage; 35 import org.apache.struts.action.ActionMessages; 36 import org.apache.struts.actions.DispatchAction; 37 import org.apache.roller.RollerException; 38 import org.apache.roller.model.Roller; 39 import org.apache.roller.model.RollerFactory; 40 import org.apache.roller.model.UserManager; 41 import org.apache.roller.pojos.PermissionsData; 42 import org.apache.roller.pojos.UserData; 43 import org.apache.roller.pojos.WebsiteData; 44 import org.apache.roller.ui.core.BasePageModel; 45 import org.apache.roller.ui.core.RollerRequest; 46 import org.apache.roller.ui.core.RollerSession; 47 import org.apache.roller.ui.authoring.struts.formbeans.MemberPermissionsForm; 48 49 55 public class MemberPermissionsAction extends DispatchAction { 56 private static Log mLogger = 57 LogFactory.getFactory().getInstance(MemberPermissionsAction.class); 58 59 60 public ActionForward unspecified( 61 ActionMapping mapping, 62 ActionForm actionForm, 63 HttpServletRequest request, 64 HttpServletResponse response) 65 throws Exception { 66 if (request.getMethod().equals("GET")) { 67 return edit(mapping, actionForm, request, response); 68 } 69 return save(mapping, actionForm, request, response); 70 } 71 72 73 public ActionForward send( 74 ActionMapping mapping, 75 ActionForm actionForm, 76 HttpServletRequest request, 77 HttpServletResponse response) 78 throws Exception { 79 return edit(mapping, actionForm, request, response); 80 } 81 82 public ActionForward cancel( 83 ActionMapping mapping, 84 ActionForm actionForm, 85 HttpServletRequest request, 86 HttpServletResponse response) 87 throws Exception { 88 return edit(mapping, actionForm, request, response); 89 } 90 91 public ActionForward edit( 92 ActionMapping mapping, 93 ActionForm actionForm, 94 HttpServletRequest request, 95 HttpServletResponse response) 96 throws Exception { 97 98 MemberPermissionsPageModel pageModel = 99 new MemberPermissionsPageModel(request, response, mapping); 100 request.setAttribute("model", pageModel); 101 RollerSession rses = RollerSession.getRollerSession(request); 102 103 if (pageModel.getWebsite() != null && rses.isUserAuthorizedToAdmin(pageModel.getWebsite())) { 105 MemberPermissionsForm form = (MemberPermissionsForm)actionForm; 106 form.setWebsiteId(pageModel.getWebsite().getId()); 107 ActionForward forward = mapping.findForward("memberPermissions.page"); 108 return forward; 109 } else { 110 return mapping.findForward("access-denied"); 111 } 112 } 113 114 public ActionForward save( 115 ActionMapping mapping, 116 ActionForm actionForm, 117 HttpServletRequest request, 118 HttpServletResponse response) 119 throws Exception { 120 ActionErrors errors = new ActionErrors(); 121 ActionMessages msgs = new ActionMessages(); 122 RollerSession rses = RollerSession.getRollerSession(request); 123 MemberPermissionsPageModel model = 124 new MemberPermissionsPageModel(request, response, mapping); 125 126 if (model.getWebsite() != null && rses.isUserAuthorizedToAdmin(model.getWebsite())) { 128 129 UserManager userMgr = RollerFactory.getRoller().getUserManager(); 130 131 Iterator iter = model.getPermissions().iterator(); 132 int removed = 0; 133 int changed = 0; 134 while (iter.hasNext()) { 135 PermissionsData perms = (PermissionsData)iter.next(); 136 String sval = request.getParameter("perm-" + perms.getId()); 137 if (sval != null) { 138 short val = Short.parseShort(sval); 139 UserData user = rses.getAuthenticatedUser(); 140 if (perms.getUser().getId().equals(user.getId()) 141 && val < perms.getPermissionMask()) { 142 errors.add(null,new ActionError( 143 "memberPermissions.noSelfDemotions")); 144 } else if (val != perms.getPermissionMask()) { 145 if (val == -1) { 146 userMgr.removePermissions(perms); 147 removed++; 148 } else { 149 perms.setPermissionMask(val); 150 userMgr.savePermissions(perms); 151 changed++; 152 } 153 } 154 } 155 } 156 if (removed > 0 || changed > 0) { 157 RollerFactory.getRoller().flush(); 158 } 159 if (removed > 0) { 160 msgs.add(null,new ActionMessage( 161 "memberPermissions.membersRemoved", new Integer (removed))); 162 } 163 if (changed > 0) { 164 msgs.add(null,new ActionMessage( 165 "memberPermissions.membersChanged", new Integer (changed))); 166 } 167 saveErrors(request, errors); 168 saveMessages(request, msgs); 169 MemberPermissionsPageModel updatedModel = 170 new MemberPermissionsPageModel(request, response, mapping); 171 request.setAttribute("model", updatedModel); 172 ActionForward forward = mapping.findForward("memberPermissions.page"); 173 return forward; 174 175 } else { 176 return mapping.findForward("access-denied"); 177 } 178 } 179 180 public static class MemberPermissionsPageModel extends BasePageModel { 181 private List permissions = new ArrayList (); 182 public MemberPermissionsPageModel(HttpServletRequest request, 183 HttpServletResponse response, ActionMapping mapping) throws RollerException { 184 super("memberPermissions.title", request, response, mapping); 185 Roller roller = RollerFactory.getRoller(); 186 RollerRequest rreq = RollerRequest.getRollerRequest(request); 187 RollerSession rollerSession = RollerSession.getRollerSession(request); 188 WebsiteData website = rreq.getWebsite(); 189 permissions = roller.getUserManager().getAllPermissions(website); 190 } 191 public List getPermissions() { 192 return permissions; 193 } 194 public void setWebsites(List permissions) { 195 this.permissions = permissions; 196 } 197 } 198 } 199 | Popular Tags |