View Javadoc
1   /**
2    * @author mchyzer
3    * $Id$
4    */
5   package edu.internet2.middleware.grouper.grouperUi.beans.ui;
6   
7   import java.util.Set;
8   
9   import org.apache.commons.lang.StringUtils;
10  import org.apache.commons.logging.Log;
11  
12  import edu.internet2.middleware.grouper.Group;
13  import edu.internet2.middleware.grouper.GroupFinder;
14  import edu.internet2.middleware.grouper.GrouperSession;
15  import edu.internet2.middleware.grouper.attr.AttributeDef;
16  import edu.internet2.middleware.grouper.exception.GrouperSessionException;
17  import edu.internet2.middleware.grouper.grouperUi.beans.api.GuiRuleDefinition;
18  import edu.internet2.middleware.grouper.misc.GrouperSessionHandler;
19  import edu.internet2.middleware.grouper.privs.PrivilegeHelper;
20  import edu.internet2.middleware.grouper.rules.RuleUtils;
21  import edu.internet2.middleware.grouper.ui.GrouperUiFilter;
22  import edu.internet2.middleware.grouper.ui.util.GrouperUiConfig;
23  import edu.internet2.middleware.grouper.util.GrouperUtil;
24  import edu.internet2.middleware.subject.Subject;
25  
26  
27  /**
28   * container to show rules on screen
29   */
30  public class RulesContainer {
31  
32    /**
33     * 
34     */
35    public RulesContainer() {
36    }
37  
38    /**
39     * if can view privilege inheritance
40     * @return true if can
41     */
42    public boolean isCanReadPrivilegeInheritance() {
43  
44      boolean privilegeInheritanceReadRequireAdmin = GrouperUiConfig.retrieveConfig()
45          .propertyValueBoolean("uiV2.privilegeInheritanceReadRequireAdmin", false);
46  
47      final Subject loggedInSubject = GrouperUiFilter.retrieveSubjectLoggedIn();
48  
49      if (privilegeInheritanceReadRequireAdmin && !PrivilegeHelper.isWheelOrRoot(loggedInSubject)) {
50        return false;
51      }
52      
53      final String privilegeInheritanceReadRequireGroup = GrouperUiConfig.retrieveConfig()
54          .propertyValueString("uiV2.privilegeInheritanceReadRequireGroup");
55  
56      if (!StringUtils.isBlank(privilegeInheritanceReadRequireGroup)) {
57        
58        if (false == (Boolean)GrouperSession.callbackGrouperSession(GrouperSession.staticGrouperSession().internal_getRootSession(), new GrouperSessionHandler() {
59          
60          public Object callback(GrouperSession grouperSession) throws GrouperSessionException {
61            Group group = GroupFinder.findByName(grouperSession, privilegeInheritanceReadRequireGroup, true);
62            if (!group.hasMember(loggedInSubject)) {
63              return false;
64            }
65            return true;
66          }
67        })) {
68          return false;
69        }
70  
71      }
72      
73      boolean privilegeInheritanceDoesntRequireRulesPrivileges = GrouperUiConfig.retrieveConfig()
74          .propertyValueBoolean("uiV2.privilegeInheritanceDoesntRequireRulesPrivileges", true);
75      
76      if (privilegeInheritanceDoesntRequireRulesPrivileges) {
77        return true;
78      }
79      
80      return GrouperRequestContainer.retrieveFromRequestOrCreate().getRulesContainer().isCanReadRules();
81    }
82  
83    /**
84     * if can update privilege inheritance
85     * @return true if can
86     */
87    public boolean isCanUpdatePrivilegeInheritance() {
88  
89      boolean privilegeInheritanceUpdateRequireAdmin = GrouperUiConfig.retrieveConfig()
90          .propertyValueBoolean("uiV2.privilegeInheritanceUpdateRequireAdmin", false);
91      
92      final Subject loggedInSubject = GrouperUiFilter.retrieveSubjectLoggedIn();
93  
94      if (privilegeInheritanceUpdateRequireAdmin && !PrivilegeHelper.isWheelOrRoot(loggedInSubject)) {
95        return false;
96      }
97      
98      final String privilegeInheritanceUpdateRequireGroup = GrouperUiConfig.retrieveConfig()
99          .propertyValueString("uiV2.privilegeInheritanceUpdateRequireGroup");
100 
101     if (!StringUtils.isBlank(privilegeInheritanceUpdateRequireGroup)) {
102       
103       if (false == (Boolean)GrouperSession.callbackGrouperSession(GrouperSession.staticGrouperSession().internal_getRootSession(), new GrouperSessionHandler() {
104         
105         public Object callback(GrouperSession grouperSession) throws GrouperSessionException {
106           Group group = GroupFinder.findByName(grouperSession, privilegeInheritanceUpdateRequireGroup, true);
107           if (!group.hasMember(loggedInSubject)) {
108             return false;
109           }
110           return true;
111         }
112       })) {
113         return false;
114       }
115     }
116     boolean privilegeInheritanceDoesntRequireRulesPrivileges = GrouperUiConfig.retrieveConfig()
117         .propertyValueBoolean("uiV2.privilegeInheritanceDoesntRequireRulesPrivileges", true);
118     
119     if (privilegeInheritanceDoesntRequireRulesPrivileges) {
120       return true;
121     }
122     
123     return GrouperRequestContainer.retrieveFromRequestOrCreate().getRulesContainer().isCanUpdateRules();
124   }
125   
126 
127   /**
128    * rules to show on screen
129    */
130   private Set<GuiRuleDefinition> guiRuleDefinitions;
131   /**
132    * if the logged in user can read rules, lazy loaded
133    */
134   private Boolean canReadRules;
135   /**
136    * if the logged in user can update rules, lazy loaded
137    */
138   private Boolean canUpdateRules;
139   
140   /** logger */
141   private static final Log LOG = GrouperUtil.getLog(RulesContainer.class);
142 
143   
144   /**
145    * @return the guiRules
146    */
147   public Set<GuiRuleDefinition> getGuiRuleDefinitions() {
148     return this.guiRuleDefinitions;
149   }
150 
151   
152   /**
153    * @param guiRules1 the guiRules to set
154    */
155   public void setGuiRuleDefinitions(Set<GuiRuleDefinition> guiRules1) {
156     this.guiRuleDefinitions = guiRules1;
157   }
158 
159 
160   /**
161    * if the logged in user can read rules, lazy loaded
162    * @return if can read rules
163    */
164   public boolean isCanReadRules() {
165     if (this.canReadRules == null) {
166       try {
167         final Subject loggedInSubject = GrouperUiFilter.retrieveSubjectLoggedIn();
168         
169         this.canReadRules = (Boolean)GrouperSession.callbackGrouperSession(
170             GrouperSession.staticGrouperSession().internal_getRootSession(), new GrouperSessionHandler() {
171               
172               @Override
173               public Object callback(GrouperSession grouperSession) throws GrouperSessionException {
174 
175                 AttributeDef attributeDefType = RuleUtils.ruleTypeAttributeDef();
176                 boolean canReadType = attributeDefType.getPrivilegeDelegate().canAttrRead(loggedInSubject);
177                 AttributeDef attributeDefAttr = RuleUtils.ruleAttrAttributeDef();
178                 boolean canReadAttr = attributeDefAttr.getPrivilegeDelegate().canAttrRead(loggedInSubject);
179                 return canReadType && canReadAttr;
180               
181               }
182             });
183         
184   
185       } catch (Exception e) {
186         //ignore
187         if (LOG.isDebugEnabled()) {
188           LOG.debug("problem checking rule", e);
189         }
190         if (this.canReadRules == null) {
191           this.canReadRules = false;
192         }
193       }
194     }
195     
196     return this.canReadRules;
197   
198   }
199 
200 
201   /**
202    * if the logged in user can update rules, lazy loaded
203    * @return if can update rules
204    */
205   public boolean isCanUpdateRules() {
206     if (this.canUpdateRules == null) {
207       try {
208         final Subject loggedInSubject = GrouperUiFilter.retrieveSubjectLoggedIn();
209         
210         this.canUpdateRules = (Boolean)GrouperSession.callbackGrouperSession(
211             GrouperSession.staticGrouperSession().internal_getRootSession(), new GrouperSessionHandler() {
212               
213               @Override
214               public Object callback(GrouperSession grouperSession) throws GrouperSessionException {
215                 AttributeDef attributeDefType = RuleUtils.ruleTypeAttributeDef();
216                 boolean canUpdateType = attributeDefType.getPrivilegeDelegate().canAttrUpdate(loggedInSubject);
217                 AttributeDef attributeDefAttr = RuleUtils.ruleAttrAttributeDef();
218                 boolean canUpdateAttr = attributeDefAttr.getPrivilegeDelegate().canAttrUpdate(loggedInSubject);
219                 return canUpdateType && canUpdateAttr;
220               }
221             });
222         
223   
224       } catch (Exception e) {
225         //ignore
226         if (LOG.isDebugEnabled()) {
227           LOG.debug("problem checking rule", e);
228         }
229         if (this.canUpdateRules == null) {
230           this.canUpdateRules = false;
231         }
232       }
233     }
234     
235     return this.canUpdateRules;
236   
237   }
238   
239   
240   
241 }