001: /**********************************************************************************
002: * $URL: $
003: * $Id: $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006,2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.poll.tool.producers;
021:
022: import org.sakaiproject.tool.api.ToolManager;
023: import org.sakaiproject.poll.logic.PollListManager;
024: import org.sakaiproject.user.api.UserDirectoryService;
025: import org.sakaiproject.authz.cover.AuthzGroupService;
026: import org.sakaiproject.authz.api.AuthzGroup;
027: import org.sakaiproject.authz.api.Role;
028: import org.sakaiproject.authz.cover.FunctionManager;
029: import uk.org.ponder.rsf.components.UIBoundBoolean;
030:
031: import uk.org.ponder.messageutil.MessageLocator;
032: import uk.org.ponder.rsf.components.UICommand;
033: import uk.org.ponder.rsf.components.UIContainer;
034: import uk.org.ponder.rsf.components.UIBranchContainer;
035: import uk.org.ponder.rsf.components.UIELBinding;
036: import uk.org.ponder.rsf.components.UIOutput;
037: import uk.org.ponder.rsf.components.UIForm;
038: import uk.org.ponder.rsf.view.ComponentChecker;
039: import uk.org.ponder.rsf.view.ViewComponentProducer;
040: import uk.org.ponder.rsf.viewstate.ViewParameters;
041: import uk.org.ponder.rsf.flow.jsfnav.NavigationCase;
042: import uk.org.ponder.localeutil.LocaleGetter;
043: import uk.org.ponder.rsf.viewstate.SimpleViewParameters;
044: import uk.org.ponder.rsf.flow.jsfnav.NavigationCase;
045: import uk.org.ponder.rsf.flow.jsfnav.NavigationCaseReporter;
046: import org.sakaiproject.site.cover.SiteService;
047: import org.sakaiproject.site.api.Site;
048:
049: import java.util.List;
050: import java.util.Map;
051: import java.util.Set;
052: import java.util.ArrayList;
053: import java.util.Iterator;
054:
055: import org.apache.commons.logging.Log;
056: import org.apache.commons.logging.LogFactory;
057:
058: public class PermissionsProducer implements ViewComponentProducer,
059: NavigationCaseReporter {
060:
061: public static final String VIEW_ID = "votePermissions";
062:
063: private UserDirectoryService userDirectoryService;
064: private PollListManager pollListManager;
065: private ToolManager toolManager;
066: private MessageLocator messageLocator;
067: private LocaleGetter localegetter;
068:
069: private static final String PERMISSION_PREFIX = "poll";
070: private static Log m_log = LogFactory
071: .getLog(PermissionsProducer.class);
072:
073: public String getViewID() {
074: // TODO Auto-generated method stub
075: return VIEW_ID;
076: }
077:
078: public void setMessageLocator(MessageLocator messageLocator) {
079:
080: this .messageLocator = messageLocator;
081: }
082:
083: public void setUserDirectoryService(
084: UserDirectoryService userDirectoryService) {
085: this .userDirectoryService = userDirectoryService;
086: }
087:
088: public void setPollListManager(PollListManager pollListManager) {
089: this .pollListManager = pollListManager;
090: }
091:
092: public void setToolManager(ToolManager toolManager) {
093: this .toolManager = toolManager;
094: }
095:
096: public void setLocaleGetter(LocaleGetter localegetter) {
097: this .localegetter = localegetter;
098: }
099:
100: Map perms = null;
101:
102: public void setRoleperms(Map perms) {
103: this .perms = perms;
104: }
105:
106: public void fillComponents(UIContainer tofill, ViewParameters arg1,
107: ComponentChecker arg2) {
108:
109: try {
110:
111: //populate the site name ect
112: UIOutput.make(tofill, "permissions-title", messageLocator
113: .getMessage("permissions_title"));
114: UIOutput.make(tofill, "permissions-instruction",
115: messageLocator
116: .getMessage("permissions_instruction"));
117:
118: Site site = SiteService.getSite(toolManager
119: .getCurrentPlacement().getContext());
120: UIOutput.make(tofill, "site-name", site.getTitle());
121:
122: //we need a list of permissions
123:
124: String[] perms = new String[] {
125: PollListManager.PERMISSION_VOTE,
126: PollListManager.PERMISSION_ADD,
127: PollListManager.PERMISSION_DELETE_OWN,
128: PollListManager.PERMISSION_DELETE_ANY,
129: PollListManager.PERMISSION_EDIT_OWN,
130: PollListManager.PERMISSION_EDIT_ANY };
131: for (int i = 0; i < perms.length; i++) {
132: String this Perm = (String) perms[i];
133: this Perm = this Perm
134: .substring(this Perm.indexOf('.') + 1);
135: UIBranchContainer b = UIBranchContainer.make(tofill,
136: "head-row:");
137: UIOutput.make(b, "perm-name", this Perm);
138: }
139:
140: AuthzGroup group = AuthzGroupService.getAuthzGroup("/site/"
141: + toolManager.getCurrentPlacement().getContext());
142: Set roles = group.getRoles();
143: Iterator i = roles.iterator();
144: UIForm form = UIForm.make(tofill, "perm-form");
145: UIOutput.make(form, "permissions-role", messageLocator
146: .getMessage("permissions_role"));
147: while (i.hasNext()) {
148: Role role = (Role) i.next();
149: m_log.debug("got role " + role.getId());
150: UIBranchContainer row = UIBranchContainer.make(form,
151: "permission-row:", role.getId());
152: UIOutput.make(row, "role", role.getId());
153: //now iterate through the permissions
154: String prefix = "#{roleperms." + role.getId();
155: for (int ip = 0; ip < perms.length; ip++) {
156: String this Perm = (String) perms[ip];
157: this Perm = this Perm
158: .substring(this Perm.indexOf('.') + 1);
159: UIBranchContainer col = UIBranchContainer.make(row,
160: "box-row:", this Perm);
161: m_log.debug("drawing box for " + this Perm
162: + " for role " + role.getId());
163: //new Boolean(role.isAllowed((String)perms[ip]))
164: UIBoundBoolean.make(col, "perm-box", prefix + "."
165: + this Perm + "}", new Boolean(role
166: .isAllowed((String) perms[ip])));
167:
168: }
169: }
170: UICommand sub = UICommand.make(form, "submit",
171: messageLocator.getMessage("new_poll_submit"),
172: "#{permissionAction.setPermissions}");
173: sub.parameters.add(new UIELBinding(
174: "#{permissionAction.submissionStatus}", "submit"));
175: UICommand cancel = UICommand.make(form, "cancel",
176: messageLocator.getMessage("vote_cancel"),
177: "#{permissionAction.cancel}");
178: cancel.parameters.add(new UIELBinding(
179: "#{permissionAction.submissionStatus}", "cancel"));
180: } catch (Exception e) {
181: e.printStackTrace();
182: }
183: }
184:
185: public List reportNavigationCases() {
186: List togo = new ArrayList(); // Always navigate back to this view.
187: togo.add(new NavigationCase(null, new SimpleViewParameters(
188: VIEW_ID)));
189: togo.add(new NavigationCase("Success",
190: new SimpleViewParameters(PollToolProducer.VIEW_ID)));
191: togo.add(new NavigationCase("cancel", new SimpleViewParameters(
192: PollToolProducer.VIEW_ID)));
193: return togo;
194: }
195:
196: }
|