Coverage Summary for Class: NonRecursiveGrantResourcePermissionPersister (com.acciente.oacc.sql.internal.persister)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
NonRecursiveGrantResourcePermissionPersister | 100% (1/ 1) | 100% (4/ 4) | 87.1% (54/ 62) |
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.sql.SQLProfile;
23 import com.acciente.oacc.sql.internal.persister.id.DomainId;
24 import com.acciente.oacc.sql.internal.persister.id.Id;
25 import com.acciente.oacc.sql.internal.persister.id.ResourceClassId;
26 import com.acciente.oacc.sql.internal.persister.id.ResourceId;
27 import com.acciente.oacc.sql.internal.persister.id.ResourcePermissionId;
28
29 import java.io.Serializable;
30 import java.sql.SQLException;
31 import java.util.HashSet;
32 import java.util.Set;
33
34 public class NonRecursiveGrantResourcePermissionPersister extends CommonGrantResourcePermissionPersister implements Serializable {
35 private static final long serialVersionUID = 1L;
36
37 public NonRecursiveGrantResourcePermissionPersister(SQLProfile sqlProfile,
38 SQLStrings sqlStrings) {
39 super(sqlProfile, sqlStrings);
40 }
41
42 @Override
43 public Set<Resource> getResourcesByResourcePermission(SQLConnection connection,
44 Resource accessorResource,
45 Id<ResourceClassId> resourceClassId,
46 ResourcePermission resourcePermission,
47 Id<ResourcePermissionId> resourcePermissionId) {
48 if (resourcePermission.isSystemPermission()) {
49 throw new IllegalArgumentException("Permission: " + resourcePermission + " is not a non-system permission");
50 }
51
52 SQLStatement statement = null;
53 try {
54 // first get all the resources from which the accessor inherits any permissions
55 final Set<Id<ResourceId>> accessorResourceIds
56 = NonRecursivePersisterHelper.getInheritedAccessorResourceIds(sqlStrings, connection, accessorResource);
57
58 // now accumulate the objects of the specified type that each (inherited) accessor has the specified permission to
59 SQLResult resultSet;
60 Set<Resource> resources = new HashSet<>();
61 statement = connection.prepareStatement(sqlStrings.SQL_findInGrantResourcePermission_withoutInheritance_ResourceID_ExternalID_BY_AccessorID_ResourceClassID_PermissionID_IsWithGrant);
62
63 for (Id<ResourceId> accessorResourceId : accessorResourceIds) {
64 statement.setResourceId(1, accessorResourceId);
65 statement.setResourceClassId(2, resourceClassId);
66 statement.setResourcePermissionId(3, resourcePermissionId);
67 statement.setBoolean(4, resourcePermission.isWithGrantOption());
68 resultSet = statement.executeQuery();
69
70 while (resultSet.next()) {
71 resources.add(resultSet.getResource("ResourceId", "ExternalId"));
72 }
73 resultSet.close();
74 }
75 return resources;
76 }
77 catch (SQLException e) {
78 throw new RuntimeException(e);
79 }
80 finally {
81 closeStatement(statement);
82 }
83 }
84
85 @Override
86 public Set<Resource> getResourcesByResourcePermission(SQLConnection connection,
87 Resource accessorResource,
88 Id<ResourceClassId> resourceClassId,
89 Id<DomainId> resourceDomainId,
90 ResourcePermission resourcePermission,
91 Id<ResourcePermissionId> resourcePermissionId) {
92 if (resourcePermission.isSystemPermission()) {
93 throw new IllegalArgumentException("Permission: " + resourcePermission + " is not a non-system permission");
94 }
95
96 SQLStatement statement = null;
97 try {
98 // first get all the resources from which the accessor inherits any permissions
99 final Set<Id<ResourceId>> accessorResourceIds
100 = NonRecursivePersisterHelper.getInheritedAccessorResourceIds(sqlStrings, connection, accessorResource);
101
102 // then get all the descendants of the specified domain
103 final Set<Id<DomainId>> descendantDomainIds
104 = NonRecursivePersisterHelper.getDescendantDomainIdsOrderedByAscendingLevel(sqlStrings,
105 connection,
106 resourceDomainId);
107
108 // now accumulate the objects of the specified type that each (inherited) accessor
109 // has the specified permission to in each of the descendant domains
110 SQLResult resultSet;
111 Set<Resource> resources = new HashSet<>();
112 statement = connection.prepareStatement(sqlStrings.SQL_findInGrantResourcePermission_withoutInheritance_ResourceID_ExternalID_BY_AccessorID_DomainID_ResourceClassID_PermissionID_IsWithGrant);
113
114 for (Id<ResourceId> accessorResourceId : accessorResourceIds) {
115 for (Id<DomainId> descendantDomainId : descendantDomainIds) {
116 statement.setResourceId(1, accessorResourceId);
117 statement.setResourceDomainId(2, descendantDomainId);
118 statement.setResourceClassId(3, resourceClassId);
119 statement.setResourcePermissionId(4, resourcePermissionId);
120 statement.setBoolean(5, resourcePermission.isWithGrantOption());
121 resultSet = statement.executeQuery();
122
123 while (resultSet.next()) {
124 resources.add(resultSet.getResource("ResourceId", "ExternalId"));
125 }
126 resultSet.close();
127 }
128 }
129
130 return resources;
131 }
132 catch (SQLException e) {
133 throw new RuntimeException(e);
134 }
135 finally {
136 closeStatement(statement);
137 }
138 }
139
140 @Override
141 public Set<ResourcePermission> getResourcePermissionsIncludeInherited(SQLConnection connection,
142 Resource accessorResource,
143 Resource accessedResource) {
144 SQLStatement statement = null;
145 try {
146 // first get all the resources from which the accessor inherits any permissions
147 final Set<Id<ResourceId>> accessorResourceIds
148 = NonRecursivePersisterHelper.getInheritedAccessorResourceIds(sqlStrings, connection, accessorResource);
149
150 // now accumulate the permissions on the accessed resource from each of the (inherited) accessors
151 SQLResult resultSet;
152 Set<ResourcePermission> resourcePermissions = new HashSet<>();
153 statement = connection.prepareStatement(sqlStrings.SQL_findInGrantResourcePermission_withoutInheritance_ResourceClassName_PermissionName_IsWithGrant_BY_AccessorID_AccessedID);
154
155 for (Id<ResourceId> accessorResourceId : accessorResourceIds) {
156 statement.setResourceId(1, accessorResourceId);
157 statement.setResourceId(2, accessedResource);
158 resultSet = statement.executeQuery();
159
160 while (resultSet.next()) {
161 resourcePermissions.add(getResourcePermission(resultSet));
162 }
163 resultSet.close();
164 }
165
166 return resourcePermissions;
167 }
168 catch (SQLException e) {
169 throw new RuntimeException(e);
170 }
171 finally {
172 closeStatement(statement);
173 }
174 }
175 }