View Javadoc
1   /*******************************************************************************
2    * Copyright 2015 Internet2
3    * 
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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  /*
17   * @author mchyzer
18   * $Id: GroupAttributeNameValidationHook.java,v 1.6 2009-03-24 17:12:08 mchyzer Exp $
19   */
20  package edu.internet2.middleware.grouper.hooks.examples;
21  
22  import edu.internet2.middleware.grouper.Group;
23  import edu.internet2.middleware.grouper.cfg.GrouperConfig;
24  import edu.internet2.middleware.grouper.cfg.text.GrouperTextContainer;
25  import edu.internet2.middleware.grouper.hibernate.HibernateSession;
26  import edu.internet2.middleware.grouper.hooks.GroupHooks;
27  import edu.internet2.middleware.grouper.hooks.beans.HooksContext;
28  import edu.internet2.middleware.grouper.hooks.beans.HooksGroupBean;
29  import edu.internet2.middleware.grouper.hooks.logic.GrouperHookType;
30  import edu.internet2.middleware.grouper.hooks.logic.GrouperHooksUtils;
31  import edu.internet2.middleware.grouper.hooks.logic.HookVeto;
32  import edu.internet2.middleware.grouper.util.GrouperUtil;
33  
34  
35  /**
36   * <pre>
37   * built in hook to grouper, which is turned on when it is configured in the grouper.properties.
38   * 
39   * group names will case insensitive be unique
40   * 
41   * set that with grouper.properties:
42   * 
43   * hooks.group.class = edu.internet2.middleware.grouper.hooks.examples.GroupUniqueNameCaseInsensitiveHook
44   * 
45   * or
46   * 
47   * grouperHook.GroupUniqueNameCaseInsensitiveHook.autoRegister = true (default)
48   * </pre>
49   */
50  public class GroupUniqueNameCaseInsensitiveHook extends GroupHooks {
51    
52    /**
53     * 
54     */
55    public static void clearHook() {
56      registered = false;
57    }
58  
59    /**
60     * only register once
61     */
62    private static boolean registered = false;
63    
64    /**
65     * see if this is configured in the grouper.properties, if so, register this hook
66     */
67    public static void registerHookIfNecessary() {
68      
69      if (registered) {
70        return;
71      }
72      
73      if (GrouperConfig.retrieveConfig().propertyValueBoolean("grouperHook.GroupUniqueNameCaseInsensitiveHook.autoRegister", true)) {
74        //register this hook
75        GrouperHooksUtils.addHookManual(GrouperHookType.GROUP.getPropertyFileKey(), 
76            GroupUniqueNameCaseInsensitiveHook.class);
77      }
78      
79      registered = true;
80  
81    }
82  
83    /**
84     * veto key
85     */
86    public static final String VETO_GROUP_UNIQUE_NAME_CASE_INSENSITIVE = "veto.group.unique.nameCaseInsensitive";
87  
88    /**
89     * veto key
90     */
91    public static final String VETO_GROUP_UNIQUE_ID_CASE_INSENSITIVE = "veto.group.unique.idCaseInsensitive";
92  
93    /**
94     * 
95     * @see edu.internet2.middleware.grouper.hooks.GroupHooks#groupPreInsert(edu.internet2.middleware.grouper.hooks.beans.HooksContext, edu.internet2.middleware.grouper.hooks.beans.HooksGroupBean)
96     */
97    @Override
98    public void groupPreInsert(HooksContext hooksContext, HooksGroupBean preInsertBean) {
99      Group group = preInsertBean.getGroup();
100     verifyCaseInsensitiveName(group);
101   }
102 
103   /**
104    * 
105    * @param group
106    */
107   public static void verifyCaseInsensitiveName(Group group) {
108     
109     //see if there is another group with the same name case insensitive
110     long count = HibernateSession.byHqlStatic().createQuery("select count(theGroup) "
111         + "from Group as theGroup where "
112         + " (lower(theGroup.nameDb) in (:theName, :theName2) or lower(theGroup.alternateNameDb) in (:theName, :theName2) or lower(theGroup.displayNameDb) = :theName3) "
113         + "and theGroup.uuid != :theUuid ")
114         .setString("theName", group.getName().toLowerCase())
115         .setString("theName2", GrouperUtil.defaultIfEmpty(group.getAlternateName(), group.getName()).toLowerCase())
116         .setString("theName3", group.getDisplayName().toLowerCase())
117         .setString("theUuid", group.getId()).uniqueResult(long.class);
118 
119     if (count > 0) {
120       count = HibernateSession.byHqlStatic().createQuery("select count(theGroup) "
121           + "from Group as theGroup where "
122           + " (lower(theGroup.nameDb) in (:theName, :theName2) or lower(theGroup.alternateNameDb) in (:theName, :theName2)) "
123           + "and theGroup.uuid != :theUuid ")
124           .setString("theName", group.getName().toLowerCase())
125           .setString("theName2", GrouperUtil.defaultIfEmpty(group.getAlternateName(), group.getName()).toLowerCase())
126           .setString("theUuid", group.getId()).uniqueResult(long.class);
127       if (count > 0) {
128         throw new HookVeto(VETO_GROUP_UNIQUE_ID_CASE_INSENSITIVE, GrouperTextContainer.textOrNull("veto.group.unique.idCaseInsensitive.default"));
129       } else {
130         throw new HookVeto(VETO_GROUP_UNIQUE_NAME_CASE_INSENSITIVE, GrouperTextContainer.textOrNull("veto.group.unique.nameCaseInsensitive.default"));
131       }
132     }
133 
134     
135   }
136   
137   /**
138    * @see edu.internet2.middleware.grouper.hooks.GroupHooks#groupPreUpdate(edu.internet2.middleware.grouper.hooks.beans.HooksContext, edu.internet2.middleware.grouper.hooks.beans.HooksGroupBean)
139    */
140   @Override
141   public void groupPreUpdate(HooksContext hooksContext, HooksGroupBean preUpdateBean) {
142     Group group = preUpdateBean.getGroup();
143     if (group.dbVersionDifferentFields().contains(Group.FIELD_EXTENSION) || group.dbVersionDifferentFields().contains(Group.FIELD_NAME) 
144         || group.dbVersionDifferentFields().contains(Group.FIELD_DISPLAY_EXTENSION) || group.dbVersionDifferentFields().contains(Group.FIELD_DISPLAY_NAME) 
145         || group.dbVersionDifferentFields().contains(Group.FIELD_ALTERNATE_NAME_DB)) {
146       verifyCaseInsensitiveName(group);
147     }
148   }
149   
150 }