View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.lifecycle.model;
17  
18  import org.kuali.rice.kim.api.identity.Person;
19  import org.kuali.rice.krad.uif.component.Component;
20  import org.kuali.rice.krad.uif.component.DataBinding;
21  import org.kuali.rice.krad.uif.container.Group;
22  import org.kuali.rice.krad.uif.element.Action;
23  import org.kuali.rice.krad.uif.field.ActionField;
24  import org.kuali.rice.krad.uif.field.DataField;
25  import org.kuali.rice.krad.uif.field.Field;
26  import org.kuali.rice.krad.uif.lifecycle.AbstractViewLifecycleTask;
27  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
28  import org.kuali.rice.krad.uif.lifecycle.ViewLifecyclePhase;
29  import org.kuali.rice.krad.uif.view.View;
30  import org.kuali.rice.krad.uif.view.ViewAuthorizer;
31  import org.kuali.rice.krad.uif.view.ViewModel;
32  import org.kuali.rice.krad.uif.view.ViewPresentationController;
33  import org.kuali.rice.krad.uif.widget.Widget;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  
36  /**
37   * Apply authorization and presentation logic for the component.
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class ApplyAuthAndPresentationLogicTask extends AbstractViewLifecycleTask {
42  
43      /**
44       * Constructor.
45       * 
46       * @param phase The apply model phase for the component.
47       */
48      public ApplyAuthAndPresentationLogicTask(ViewLifecyclePhase phase) {
49          super(phase);
50      }
51  
52      /**
53       * Invokes the view's configured {@link ViewAuthorizer} and {@link ViewPresentationController}
54       * to set state of the component
55       * 
56       * <p>
57       * The following authorization is done here: Fields: edit, view, required, mask, and partial
58       * mask Groups: edit and view Actions: take action
59       * </p>
60       * 
61       * <p>
62       * Note additional checks are also done for fields that are part of a collection group. This
63       * authorization is found in {@link org.kuali.rice.krad.uif.container.CollectionGroupBuilder}
64       * </p>
65       * 
66       * @see org.kuali.rice.krad.uif.lifecycle.AbstractViewLifecycleTask#performLifecycleTask()
67       */
68      @Override
69      protected void performLifecycleTask() {
70          ViewModel model = (ViewModel) getPhase().getModel();
71          Component component = getPhase().getComponent();
72          View view = ViewLifecycle.getView();
73          ViewPresentationController presentationController = view.getPresentationController();
74          ViewAuthorizer authorizer = view.getAuthorizer();
75  
76          // if user session is not established cannot perform authorization
77          if (GlobalVariables.getUserSession() == null) {
78              return;
79          }
80  
81          Person user = GlobalVariables.getUserSession().getPerson();
82  
83          // if component not flagged for render no need to check auth and controller logic
84          if (!component.isRender()) {
85              return;
86          }
87  
88          // check top level view edit authorization
89          if (component instanceof View) {
90              if (!view.isReadOnly()) {
91                  boolean canEditView = authorizer.canEditView(view, model, user);
92                  if (canEditView) {
93                      canEditView = presentationController.canEditView(view, model);
94                  }
95                  view.setReadOnly(!canEditView);
96              }
97          }
98  
99          // perform group authorization and presentation logic
100         else if (component instanceof Group) {
101             Group group = (Group) component;
102 
103             // if group is not hidden, do authorization for viewing the group
104             if (!group.isHidden()) {
105                 boolean canViewGroup = authorizer.canViewGroup(view, model, group, group.getId(), user);
106                 if (canViewGroup) {
107                     canViewGroup = presentationController.canViewGroup(view, model, group, group.getId());
108                 }
109                 group.setHidden(!canViewGroup);
110                 group.setRender(canViewGroup);
111             }
112 
113             // if group is editable, do authorization for editing the group
114             if (!group.isReadOnly()) {
115                 boolean canEditGroup = authorizer.canEditGroup(view, model, group, group.getId(), user);
116                 if (canEditGroup) {
117                     canEditGroup = presentationController.canEditGroup(view, model, group, group.getId());
118                 }
119                 group.setReadOnly(!canEditGroup);
120             }
121         }
122 
123         // perform field authorization and presentation logic
124         else if (component instanceof Field && !(component instanceof ActionField)) {
125             Field field = (Field) component;
126 
127             String propertyName = null;
128             if (field instanceof DataBinding) {
129                 propertyName = ((DataBinding) field).getPropertyName();
130             }
131 
132             // if field is not hidden, do authorization for viewing the field
133             if (!field.isHidden()) {
134                 boolean canViewField = authorizer.canViewField(view, model, field, propertyName, user);
135                 if (canViewField) {
136                     canViewField = presentationController.canViewField(view, model, field, propertyName);
137                 }
138                 field.setHidden(!canViewField);
139                 field.setRender(canViewField);
140             }
141 
142             // if field is not readOnly, check edit authorization
143             if (!field.isReadOnly()) {
144                 // check field edit authorization
145                 boolean canEditField = authorizer.canEditField(view, model, field, propertyName, user);
146                 if (canEditField) {
147                     canEditField = presentationController.canEditField(view, model, field, propertyName);
148                 }
149                 field.setReadOnly(!canEditField);
150             }
151 
152             // if field is not already required, invoke presentation logic to determine if it should be
153             if ((field.getRequired() == null) || !field.getRequired().booleanValue()) {
154                 // boolean fieldIsRequired = 
155                 presentationController.fieldIsRequired(view, model, field, propertyName);
156             }
157 
158             if (field instanceof DataField) {
159                 DataField dataField = (DataField) field;
160 
161                 // check mask authorization
162                 boolean canUnmaskValue = authorizer.canUnmaskField(view, model, dataField, dataField.getPropertyName(),
163                         user);
164                 if (!canUnmaskValue) {
165                     dataField.setApplyMask(true);
166                     dataField.setMaskFormatter(dataField.getDataFieldSecurity().getAttributeSecurity().
167                             getMaskFormatter());
168                 } else {
169                     // check partial mask authorization
170                     boolean canPartiallyUnmaskValue = authorizer.canPartialUnmaskField(view, model, dataField,
171                             dataField.getPropertyName(), user);
172                     if (!canPartiallyUnmaskValue) {
173                         dataField.setApplyMask(true);
174                         dataField.setMaskFormatter(
175                                 dataField.getDataFieldSecurity().getAttributeSecurity().getPartialMaskFormatter());
176                     }
177                 }
178             }
179         }
180 
181         // perform action authorization and presentation logic
182         else if (component instanceof ActionField || component instanceof Action) {
183             Action action = null;
184             if (component instanceof ActionField) {
185                 action = ((ActionField) component).getAction();
186             } else {
187                 action = (Action) component;
188             }
189 
190             boolean canTakeAction = authorizer.canPerformAction(view, model, action, action.getActionEvent(),
191                     action.getId(), user);
192             if (canTakeAction) {
193                 canTakeAction = presentationController.canPerformAction(view, model, action, action.getActionEvent(),
194                         action.getId());
195             }
196             action.setRender(canTakeAction);
197         }
198 
199         // perform widget authorization and presentation logic
200         else if (component instanceof Widget) {
201             Widget widget = (Widget) component;
202 
203             // if widget is not hidden, do authorization for viewing the widget
204             if (!widget.isHidden()) {
205                 boolean canViewWidget = authorizer.canViewWidget(view, model, widget, widget.getId(), user);
206                 if (canViewWidget) {
207                     canViewWidget = presentationController.canViewWidget(view, model, widget, widget.getId());
208                 }
209                 widget.setHidden(!canViewWidget);
210                 widget.setRender(canViewWidget);
211             }
212 
213             // if widget is not readOnly, check edit authorization
214             if (!widget.isReadOnly()) {
215                 boolean canEditWidget = authorizer.canEditWidget(view, model, widget, widget.getId(), user);
216                 if (canEditWidget) {
217                     canEditWidget = presentationController.canEditWidget(view, model, widget, widget.getId());
218                 }
219                 widget.setReadOnly(!canEditWidget);
220             }
221         }
222     }
223 
224 }