Coverage Summary for Class: NonRecursiveGrantResourcePermissionSysPersister (com.acciente.oacc.sql.internal.persister)

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