1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class GroupUniqueNameCaseInsensitiveHook extends GroupHooks {
51
52
53
54
55 public static void clearHook() {
56 registered = false;
57 }
58
59
60
61
62 private static boolean registered = false;
63
64
65
66
67 public static void registerHookIfNecessary() {
68
69 if (registered) {
70 return;
71 }
72
73 if (GrouperConfig.retrieveConfig().propertyValueBoolean("grouperHook.GroupUniqueNameCaseInsensitiveHook.autoRegister", true)) {
74
75 GrouperHooksUtils.addHookManual(GrouperHookType.GROUP.getPropertyFileKey(),
76 GroupUniqueNameCaseInsensitiveHook.class);
77 }
78
79 registered = true;
80
81 }
82
83
84
85
86 public static final String VETO_GROUP_UNIQUE_NAME_CASE_INSENSITIVE = "veto.group.unique.nameCaseInsensitive";
87
88
89
90
91 public static final String VETO_GROUP_UNIQUE_ID_CASE_INSENSITIVE = "veto.group.unique.idCaseInsensitive";
92
93
94
95
96
97 @Override
98 public void groupPreInsert(HooksContext hooksContext, HooksGroupBean preInsertBean) {
99 Group group = preInsertBean.getGroup();
100 verifyCaseInsensitiveName(group);
101 }
102
103
104
105
106
107 public static void verifyCaseInsensitiveName(Group group) {
108
109
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
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 }