001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package sample.contact;
016:
017: import org.acegisecurity.acls.Permission;
018: import org.acegisecurity.acls.domain.BasePermission;
019: import org.acegisecurity.acls.sid.PrincipalSid;
020:
021: import org.springframework.beans.factory.InitializingBean;
022:
023: import org.springframework.dao.DataAccessException;
024:
025: import org.springframework.util.Assert;
026:
027: import org.springframework.validation.BindException;
028:
029: import org.springframework.web.bind.RequestUtils;
030: import org.springframework.web.servlet.ModelAndView;
031: import org.springframework.web.servlet.mvc.SimpleFormController;
032: import org.springframework.web.servlet.view.RedirectView;
033:
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.LinkedHashMap;
037: import java.util.Map;
038:
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041:
042: /**
043: * Controller for adding an ACL permission.
044: *
045: * @author Ben Alex
046: * @version $Id: AddPermissionController.java 1754 2006-11-17 02:01:21Z benalex $
047: */
048: public class AddPermissionController extends SimpleFormController
049: implements InitializingBean {
050: //~ Instance fields ================================================================================================
051:
052: private ContactManager contactManager;
053:
054: //~ Methods ========================================================================================================
055:
056: public void afterPropertiesSet() throws Exception {
057: Assert.notNull(contactManager,
058: "A ContactManager implementation is required");
059: }
060:
061: protected ModelAndView disallowDuplicateFormSubmission(
062: HttpServletRequest request, HttpServletResponse response)
063: throws Exception {
064: BindException errors = new BindException(
065: formBackingObject(request), getCommandName());
066: errors.reject("err.duplicateFormSubmission",
067: "Duplicate form submission. *");
068:
069: return showForm(request, response, errors);
070: }
071:
072: protected Object formBackingObject(HttpServletRequest request)
073: throws Exception {
074: int contactId = RequestUtils.getRequiredIntParameter(request,
075: "contactId");
076:
077: Contact contact = contactManager.getById(new Long(contactId));
078:
079: AddPermission addPermission = new AddPermission();
080: addPermission.setContact(contact);
081:
082: return addPermission;
083: }
084:
085: protected ModelAndView handleInvalidSubmit(
086: HttpServletRequest request, HttpServletResponse response)
087: throws Exception {
088: return disallowDuplicateFormSubmission(request, response);
089: }
090:
091: private Map listPermissions(HttpServletRequest request) {
092: Map map = new LinkedHashMap();
093: map.put(new Integer(BasePermission.ADMINISTRATION.getMask()),
094: getApplicationContext().getMessage("select.administer",
095: null, "Administer", request.getLocale()));
096: map.put(new Integer(BasePermission.READ.getMask()),
097: getApplicationContext().getMessage("select.read", null,
098: "Read", request.getLocale()));
099: map.put(new Integer(BasePermission.DELETE.getMask()),
100: getApplicationContext().getMessage("select.delete",
101: null, "Delete", request.getLocale()));
102:
103: return map;
104: }
105:
106: private Map listRecipients(HttpServletRequest request) {
107: Map map = new LinkedHashMap();
108: map.put("", getApplicationContext().getMessage(
109: "select.pleaseSelect", null, "-- please select --",
110: request.getLocale()));
111:
112: Iterator recipientsIter = contactManager.getAllRecipients()
113: .iterator();
114:
115: while (recipientsIter.hasNext()) {
116: String recipient = (String) recipientsIter.next();
117: map.put(recipient, recipient);
118: }
119:
120: return map;
121: }
122:
123: protected ModelAndView onSubmit(HttpServletRequest request,
124: HttpServletResponse response, Object command,
125: BindException errors) throws Exception {
126: AddPermission addPermission = (AddPermission) command;
127:
128: PrincipalSid sid = new PrincipalSid(addPermission
129: .getRecipient());
130: Permission permission = BasePermission
131: .buildFromMask(addPermission.getPermission().intValue());
132:
133: try {
134: contactManager.addPermission(addPermission.getContact(),
135: sid, permission);
136: } catch (DataAccessException existingPermission) {
137: existingPermission.printStackTrace();
138: errors.rejectValue("recipient",
139: "err.recipientExistsForContact",
140: "Addition failure.");
141:
142: return showForm(request, response, errors);
143: }
144:
145: return new ModelAndView(new RedirectView(getSuccessView()));
146: }
147:
148: protected Map referenceData(HttpServletRequest request)
149: throws Exception {
150: Map model = new HashMap();
151: model.put("recipients", listRecipients(request));
152: model.put("permissions", listPermissions(request));
153:
154: return model;
155: }
156:
157: public void setContactManager(ContactManager contact) {
158: this.contactManager = contact;
159: }
160: }
|