Coverage Summary for Class: CommonGrantGlobalResourcePermissionSysPersister (com.acciente.oacc.sql.internal.persister)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
CommonGrantGlobalResourcePermissionSysPersister | 100% (1/ 1) | 100% (9/ 9) | 80.9% (89/ 110) |
1 /*
2 * Copyright 2009-2018, Acciente LLC
3 *
4 * Acciente LLC licenses this file to you under the
5 * Apache License, Version 2.0 (the "License"); you
6 * may not use this file except in compliance with the
7 * License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
14 * OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License.
17 */
18 package com.acciente.oacc.sql.internal.persister;
19
20 import com.acciente.oacc.Resource;
21 import com.acciente.oacc.ResourcePermission;
22 import com.acciente.oacc.ResourcePermissions;
23 import com.acciente.oacc.sql.SQLProfile;
24 import com.acciente.oacc.sql.internal.persister.id.DomainId;
25 import com.acciente.oacc.sql.internal.persister.id.Id;
26 import com.acciente.oacc.sql.internal.persister.id.ResourceClassId;
27
28 import java.io.Serializable;
29 import java.sql.SQLException;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Map;
33 import java.util.Set;
34
35 public abstract class CommonGrantGlobalResourcePermissionSysPersister extends Persister implements GrantGlobalResourcePermissionSysPersister, Serializable {
36 private static final long serialVersionUID = 1L;
37
38 protected final SQLProfile sqlProfile;
39 protected final SQLStrings sqlStrings;
40
41 public CommonGrantGlobalResourcePermissionSysPersister(SQLProfile sqlProfile,
42 SQLStrings sqlStrings) {
43 this.sqlProfile = sqlProfile;
44 this.sqlStrings = sqlStrings;
45 }
46
47 @Override
48 public abstract Set<Resource> getResourcesByGlobalSysPermission(SQLConnection connection,
49 Resource accessorResource,
50 Id<ResourceClassId> resourceClassId,
51 ResourcePermission resourcePermission);
52
53 @Override
54 public abstract Set<Resource> getResourcesByGlobalSysPermission(SQLConnection connection,
55 Resource accessorResource,
56 Id<ResourceClassId> resourceClassId,
57 Id<DomainId> resourceDomainId,
58 ResourcePermission resourcePermission);
59
60 @Override
61 public abstract Set<ResourcePermission> getGlobalSysPermissionsIncludeInherited(SQLConnection connection,
62 Resource accessorResource,
63 Id<ResourceClassId> resourceClassId,
64 Id<DomainId> resourceDomainId);
65
66 @Override
67 public Set<ResourcePermission> getGlobalSysPermissions(SQLConnection connection,
68 Resource accessorResource,
69 Id<ResourceClassId> resourceClassId,
70 Id<DomainId> resourceDomainId) {
71 SQLStatement statement = null;
72 try {
73 // collect the system permissions that the accessor has to the accessed resource directly
74 SQLResult resultSet;
75 Set<ResourcePermission> resourcePermissions = new HashSet<>();
76
77 statement = connection.prepareStatement(sqlStrings.SQL_findInGrantGlobalResourcePermissionSys_withoutInheritance_SysPermissionID_IsWithGrant_BY_AccessorID_AccessedDomainID_ResourceClassID);
78 statement.setResourceId(1, accessorResource);
79 statement.setResourceDomainId(2, resourceDomainId);
80 statement.setResourceClassId(3, resourceClassId);
81 resultSet = statement.executeQuery();
82
83 while (resultSet.next()) {
84 resourcePermissions.add(getResourceSysPermission(resultSet));
85 }
86 resultSet.close();
87
88 return resourcePermissions;
89 }
90 catch (SQLException e) {
91 throw new RuntimeException(e);
92 }
93 finally {
94 closeStatement(statement);
95 }
96 }
97
98 protected static ResourcePermission getResourceSysPermission(SQLResult resultSet) throws SQLException {
99 final String sysPermissionName = resultSet.getResourceSysPermissionName("SysPermissionId");
100
101 if (resultSet.getBoolean("IsWithGrant")) {
102 return ResourcePermissions.getInstanceWithGrantOption(sysPermissionName);
103 }
104 else {
105 return ResourcePermissions.getInstance(sysPermissionName);
106 }
107 }
108
109 @Override
110 public abstract Map<String, Map<String, Set<ResourcePermission>>> getGlobalSysPermissionsIncludeInherited(SQLConnection connection,
111 Resource accessorResource);
112
113 @Override
114 public Map<String, Map<String, Set<ResourcePermission>>> getGlobalSysPermissions(SQLConnection connection,
115 Resource accessorResource) {
116 SQLStatement statement = null;
117 try {
118 // collect the system permissions that the accessor has
119 SQLResult resultSet;
120 Map<String, Map<String, Set<ResourcePermission>>> globalSysPermissionsMap = new HashMap<>();
121
122 statement = connection.prepareStatement(sqlStrings.SQL_findInGrantGlobalResourcePermissionSys_withoutInheritance_ResourceDomainName_ResourceClassName_SysPermissionID_IsWithGrant_BY_AccessorID);
123 statement.setResourceId(1, accessorResource);
124 resultSet = statement.executeQuery();
125
126 while (resultSet.next()) {
127 final String resourceDomainName;
128 final String resourceClassName;
129 Map<String, Set<ResourcePermission>> permissionsForResourceDomain;
130 Set<ResourcePermission> resourcePermissionsForResourceClass;
131
132 resourceDomainName = resultSet.getString("DomainName");
133 resourceClassName = resultSet.getString("ResourceClassName");
134
135 if ((permissionsForResourceDomain = globalSysPermissionsMap.get(resourceDomainName)) == null) {
136 globalSysPermissionsMap.put(resourceDomainName,
137 permissionsForResourceDomain = new HashMap<>());
138 }
139
140 if ((resourcePermissionsForResourceClass = permissionsForResourceDomain.get(resourceClassName)) == null) {
141 permissionsForResourceDomain.put(resourceClassName,
142 resourcePermissionsForResourceClass = new HashSet<>());
143 }
144
145 resourcePermissionsForResourceClass.add(getResourceSysPermission(resultSet));
146 }
147 resultSet.close();
148
149 return globalSysPermissionsMap;
150 }
151 catch (SQLException e) {
152 throw new RuntimeException(e);
153 }
154 finally {
155 closeStatement(statement);
156 }
157 }
158
159 @Override
160 public void addGlobalSysPermissions(SQLConnection connection,
161 Resource accessorResource,
162 Id<ResourceClassId> accessedResourceClassId,
163 Id<DomainId> accessedResourceDomainId,
164 Set<ResourcePermission> requestedResourcePermissions,
165 Resource grantorResource) {
166 SQLStatement statement = null;
167 try {
168 // add the new system permissions
169 statement = connection.prepareStatement(sqlStrings.SQL_createInGrantGlobalResourcePermissionSys_WITH_AccessorID_GrantorID_AccessedDomainID_IsWithGrant_ResourceClassID_SysPermissionID);
170 for (ResourcePermission resourcePermission : requestedResourcePermissions) {
171 if (resourcePermission.isSystemPermission()) {
172 statement.setResourceId(1, accessorResource);
173 statement.setResourceId(2, grantorResource);
174 statement.setResourceDomainId(3, accessedResourceDomainId);
175 statement.setBoolean(4, resourcePermission.isWithGrantOption());
176 statement.setResourceClassId(5, accessedResourceClassId);
177 statement.setResourceSystemPermissionId(6, resourcePermission.getSystemPermissionId());
178
179 assertOneRowInserted(statement.executeUpdate());
180 }
181 }
182 }
183 catch (SQLException e) {
184 throw new RuntimeException(e);
185 }
186 finally {
187 closeStatement(statement);
188 }
189 }
190
191 @Override
192 public void updateGlobalSysPermissions(SQLConnection connection,
193 Resource accessorResource,
194 Id<ResourceClassId> accessedResourceClassId,
195 Id<DomainId> accessedResourceDomainId,
196 Set<ResourcePermission> requestedResourcePermissions,
197 Resource grantorResource) {
198 SQLStatement statement = null;
199 try {
200 // add the new system permissions
201 statement = connection.prepareStatement(sqlStrings.SQL_updateInGrantGlobalResourcePermissionSys_SET_GrantorID_IsWithGrant_BY_AccessorID_AccessedDomainID_ResourceClassID_SysPermissionID);
202 for (ResourcePermission resourcePermission : requestedResourcePermissions) {
203 if (resourcePermission.isSystemPermission()) {
204 statement.setResourceId(1, grantorResource);
205 statement.setBoolean(2, resourcePermission.isWithGrantOption());
206 statement.setResourceId(3, accessorResource);
207 statement.setResourceDomainId(4, accessedResourceDomainId);
208 statement.setResourceClassId(5, accessedResourceClassId);
209 statement.setResourceSystemPermissionId(6, resourcePermission.getSystemPermissionId());
210
211 assertOneRowUpdated(statement.executeUpdate());
212 }
213 }
214 }
215 catch (SQLException e) {
216 throw new RuntimeException(e);
217 }
218 finally {
219 closeStatement(statement);
220 }
221 }
222
223 @Override
224 public void removeAllGlobalSysPermissions(SQLConnection connection,
225 Resource accessorResource) {
226
227 SQLStatement statement = null;
228 try {
229 // revoke any existing system permissions this accessor has to any domain + resource class
230 statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantGlobalResourcePermissionSys_BY_AccessorID);
231 statement.setResourceId(1, accessorResource);
232 statement.executeUpdate();
233 }
234 catch (SQLException e) {
235 throw new RuntimeException(e);
236 }
237 finally {
238 closeStatement(statement);
239 }
240 }
241
242 @Override
243 public abstract void removeAllGlobalSysPermissions(SQLConnection connection,
244 Id<DomainId> accessedDomainId);
245
246 @Override
247 public void removeGlobalSysPermissions(SQLConnection connection,
248 Resource accessorResource,
249 Id<ResourceClassId> accessedResourceClassId,
250 Id<DomainId> accessedResourceDomainId) {
251
252 SQLStatement statement = null;
253 try {
254 // revoke any existing system permissions this accessor has to this domain + resource class
255 statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantGlobalResourcePermissionSys_BY_AccessorID_AccessedDomainID_ResourceClassID);
256 statement.setResourceId(1, accessorResource);
257 statement.setResourceDomainId(2, accessedResourceDomainId);
258 statement.setResourceClassId(3, accessedResourceClassId);
259 statement.executeUpdate();
260 }
261 catch (SQLException e) {
262 throw new RuntimeException(e);
263 }
264 finally {
265 closeStatement(statement);
266 }
267 }
268
269 @Override
270 public void removeGlobalSysPermissions(SQLConnection connection,
271 Resource accessorResource,
272 Id<ResourceClassId> accessedResourceClassId,
273 Id<DomainId> accessedResourceDomainId,
274 Set<ResourcePermission> requestedResourcePermissions) {
275 SQLStatement statement = null;
276 try {
277 // remove the specified system permissions
278 statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantGlobalResourcePermissionSys_BY_AccessorID_AccessedDomainID_ResourceClassID_SysPermissionID);
279 for (ResourcePermission resourcePermission : requestedResourcePermissions) {
280 if (resourcePermission.isSystemPermission()) {
281 statement.setResourceId(1, accessorResource);
282 statement.setResourceDomainId(2, accessedResourceDomainId);
283 statement.setResourceClassId(3, accessedResourceClassId);
284 statement.setResourceSystemPermissionId(4, resourcePermission.getSystemPermissionId());
285
286 assertOneRowUpdated(statement.executeUpdate());
287 }
288 }
289 }
290 catch (SQLException e) {
291 throw new RuntimeException(e);
292 }
293 finally {
294 closeStatement(statement);
295 }
296 }
297 }