Coverage Summary for Class: CommonGrantDomainPermissionSysPersister (com.acciente.oacc.sql.internal.persister)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
CommonGrantDomainPermissionSysPersister | 100% (1/ 1) | 100% (10/ 10) | 86.1% (87/ 101) |
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.DomainPermission;
21 import com.acciente.oacc.DomainPermissions;
22 import com.acciente.oacc.Resource;
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 CommonGrantDomainPermissionSysPersister extends Persister implements GrantDomainPermissionSysPersister, Serializable {
36 private static final long serialVersionUID = 1L;
37
38 protected final SQLProfile sqlProfile;
39 protected final SQLStrings sqlStrings;
40
41 public static final DomainPermission DOMAIN_PERMISSION_SUPER_USER = DomainPermissions
42 .getInstance(DomainPermissions.SUPER_USER);
43
44 public CommonGrantDomainPermissionSysPersister(SQLProfile sqlProfile,
45 SQLStrings sqlStrings) {
46 this.sqlProfile = sqlProfile;
47 this.sqlStrings = sqlStrings;
48 }
49
50 @Override
51 public abstract Set<Resource> getResourcesByDomainSuperUserPermission(SQLConnection connection,
52 Resource accessorResource,
53 Id<ResourceClassId> resourceClassId);
54
55 @Override
56 public abstract Set<Resource> getResourcesByDomainSuperUserPermission(SQLConnection connection,
57 Resource accessorResource,
58 Id<ResourceClassId> resourceClassId,
59 Id<DomainId> resourceDomainId);
60
61 @Override
62 public abstract Set<DomainPermission> getDomainSysPermissionsIncludeInherited(SQLConnection connection,
63 Resource accessorResource,
64 Id<DomainId> resourceDomainId);
65
66 @Override
67 public Set<DomainPermission> getDomainSysPermissions(SQLConnection connection,
68 Resource accessorResource,
69 Id<DomainId> resourceDomainId) {
70 SQLStatement statement = null;
71
72 try {
73 statement = connection.prepareStatement(sqlStrings.SQL_findInGrantDomainPermissionSys_withoutInheritance_SysPermissionID_IsWithGrant_BY_AccessorID_DomainID);
74 statement.setResourceId(1, accessorResource);
75 statement.setResourceDomainId(2, resourceDomainId);
76 SQLResult resultSet = statement.executeQuery();
77
78 // collect the create permissions that this resource has to the domain directly
79 Set<DomainPermission> domainPermissions = new HashSet<>();
80 while (resultSet.next()) {
81 // on the domains only pre-defined system permissions are expected
82 domainPermissions.add(getDomainSysPermission(resultSet));
83 }
84 resultSet.close();
85
86 return domainPermissions;
87 }
88 catch (SQLException e) {
89 throw new RuntimeException(e);
90 }
91 finally {
92 closeStatement(statement);
93 }
94 }
95
96 @Override
97 public abstract Map<String, Set<DomainPermission>> getDomainSysPermissionsIncludeInherited(SQLConnection connection,
98 Resource accessorResource);
99
100 @Override
101 public Map<String, Set<DomainPermission>> getDomainSysPermissions(SQLConnection connection,
102 Resource accessorResource) {
103 SQLStatement statement = null;
104
105 try {
106 // collect the create permissions that this resource has to each domain
107 statement = connection.prepareStatement(sqlStrings.SQL_findInGrantDomainPermissionSys_withoutInheritance_ResourceDomainName_SysPermissionID_IsWithGrant_BY_AccessorID);
108 statement.setResourceId(1, accessorResource);
109 SQLResult resultSet = statement.executeQuery();
110
111 final Map<String, Set<DomainPermission>> domainPermissionsMap = new HashMap<>();
112
113 while (resultSet.next()) {
114 final String resourceDomainName = resultSet.getString("DomainName");
115
116 Set<DomainPermission> domainPermissions = domainPermissionsMap.get(resourceDomainName);
117
118 if (domainPermissions == null) {
119 domainPermissionsMap.put(resourceDomainName,
120 domainPermissions = new HashSet<>());
121 }
122
123 // on the domains only pre-defined system permissions are expected
124 domainPermissions.add(getDomainSysPermission(resultSet));
125 }
126 resultSet.close();
127
128 return domainPermissionsMap;
129 }
130 catch (SQLException e) {
131 throw new RuntimeException(e);
132 }
133 finally {
134 closeStatement(statement);
135 }
136 }
137
138 protected static DomainPermission getDomainSysPermission(SQLResult resultSet) throws SQLException {
139 final String sysPermissionName = resultSet.getDomainSysPermissionName("SysPermissionId");
140
141 if (resultSet.getBoolean("IsWithGrant")) {
142 return DomainPermissions.getInstanceWithGrantOption(sysPermissionName);
143 }
144 else {
145 return DomainPermissions.getInstance(sysPermissionName);
146 }
147 }
148
149 @Override
150 public void addDomainSysPermissions(SQLConnection connection,
151 Resource accessorResource,
152 Resource grantorResource,
153 Id<DomainId> resourceDomainId,
154 Set<DomainPermission> requestedDomainPermissions) {
155 SQLStatement statement = null;
156
157 try {
158 statement = connection.prepareStatement(sqlStrings.SQL_createInGrantDomainPermissionSys_WITH_AccessorID_GrantorID_AccessedDomainID_IsWithGrant_SysPermissionID);
159
160 for (DomainPermission domainPermission : requestedDomainPermissions) {
161 statement.setResourceId(1, accessorResource);
162 statement.setResourceId(2, grantorResource);
163 statement.setResourceDomainId(3, resourceDomainId);
164 statement.setBoolean(4, domainPermission.isWithGrantOption());
165 statement.setDomainSystemPermissionId(5, domainPermission.getSystemPermissionId());
166
167 assertOneRowInserted(statement.executeUpdate());
168 }
169 }
170 catch (SQLException e) {
171 throw new RuntimeException(e);
172 }
173 finally {
174 closeStatement(statement);
175 }
176 }
177
178 @Override
179 public void updateDomainSysPermissions(SQLConnection connection,
180 Resource accessorResource,
181 Resource grantorResource,
182 Id<DomainId> resourceDomainId,
183 Set<DomainPermission> requestedDomainPermissions) {
184 SQLStatement statement = null;
185
186 try {
187 statement = connection.prepareStatement(sqlStrings.SQL_updateInGrantDomainPermissionSys_SET_GrantorID_IsWithGrant_BY_AccessorID_AccessedDomainID_SysPermissionID);
188
189 for (DomainPermission domainPermission : requestedDomainPermissions) {
190 statement.setResourceId(1, grantorResource);
191 statement.setBoolean(2, domainPermission.isWithGrantOption());
192 statement.setResourceId(3, accessorResource);
193 statement.setResourceDomainId(4, resourceDomainId);
194 statement.setDomainSystemPermissionId(5, domainPermission.getSystemPermissionId());
195
196 assertOneRowUpdated(statement.executeUpdate());
197 }
198 }
199 catch (SQLException e) {
200 throw new RuntimeException(e);
201 }
202 finally {
203 closeStatement(statement);
204 }
205 }
206
207 @Override
208 public void removeAllDomainSysPermissions(SQLConnection connection,
209 Resource accessorResource) {
210 SQLStatement statement = null;
211
212 try {
213 statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantDomainPermissionSys_BY_AccessorID);
214 statement.setResourceId(1, accessorResource);
215 statement.executeUpdate();
216 }
217 catch (SQLException e) {
218 throw new RuntimeException(e);
219 }
220 finally {
221 closeStatement(statement);
222 }
223 }
224
225 @Override
226 public abstract void removeAllDomainSysPermissions(SQLConnection connection, Id<DomainId> domainId) ;
227
228 @Override
229 public void removeDomainSysPermissions(SQLConnection connection,
230 Resource accessorResource,
231 Id<DomainId> resourceDomainId) {
232 SQLStatement statement = null;
233
234 try {
235 statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantDomainPermissionSys_BY_AccessorID_AccessedDomainID);
236 statement.setResourceId(1, accessorResource);
237 statement.setResourceDomainId(2, resourceDomainId);
238 statement.executeUpdate();
239 }
240 catch (SQLException e) {
241 throw new RuntimeException(e);
242 }
243 finally {
244 closeStatement(statement);
245 }
246 }
247
248 @Override
249 public void removeDomainSysPermissions(SQLConnection connection,
250 Resource accessorResource,
251 Id<DomainId> resourceDomainId,
252 Set<DomainPermission> requestedDomainPermissions) {
253 SQLStatement statement = null;
254
255 try {
256 statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantDomainPermissionSys_BY_AccessorID_AccessedDomainID_SysPermissionID);
257 for (DomainPermission domainPermission : requestedDomainPermissions) {
258 statement.setResourceId(1, accessorResource);
259 statement.setResourceDomainId(2, resourceDomainId);
260 statement.setDomainSystemPermissionId(3, domainPermission.getSystemPermissionId());
261
262 assertOneRowUpdated(statement.executeUpdate());
263 }
264 }
265 catch (SQLException e) {
266 throw new RuntimeException(e);
267 }
268 finally {
269 closeStatement(statement);
270 }
271 }
272 }