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

Class Class, % Method, % Line, %
RecursiveGrantResourcePermissionPersister 100% (1/ 1) 100% (4/ 4) 84% (42/ 50)


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.ResourcePermissionId; 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 RecursiveGrantResourcePermissionPersister extends CommonGrantResourcePermissionPersister implements Serializable { 34  private static final long serialVersionUID = 1L; 35  36  public RecursiveGrantResourcePermissionPersister(SQLProfile sqlProfile, 37  SQLStrings sqlStrings) { 38  super(sqlProfile, sqlStrings); 39  } 40  41  @Override 42  public Set<Resource> getResourcesByResourcePermission(SQLConnection connection, 43  Resource accessorResource, 44  Id<ResourceClassId> resourceClassId, 45  ResourcePermission resourcePermission, 46  Id<ResourcePermissionId> resourcePermissionId) { 47  if (resourcePermission.isSystemPermission()) { 48  throw new IllegalArgumentException("Permission: " + resourcePermission + " is not a non-system permission"); 49  } 50  51  SQLStatement statement = null; 52  try { 53  // get the list of objects of the specified type that the session has access to via direct permissions 54  SQLResult resultSet; 55  Set<Resource> resources = new HashSet<>(); 56  57  statement = connection.prepareStatement(sqlStrings.SQL_findInGrantResourcePermission_ResourceID_ExternalID_BY_AccessorID_ResourceClassID_PermissionID_IsWithGrant); 58  statement.setResourceId(1, accessorResource); 59  statement.setResourceClassId(2, resourceClassId); 60  statement.setResourcePermissionId(3, resourcePermissionId); 61  statement.setBoolean(4, resourcePermission.isWithGrantOption()); 62  resultSet = statement.executeQuery(); 63  64  while (resultSet.next()) { 65  resources.add(resultSet.getResource("ResourceId", "ExternalId")); 66  } 67  resultSet.close(); 68  69  return resources; 70  71  } 72  catch (SQLException e) { 73  throw new RuntimeException(e); 74  } 75  finally { 76  closeStatement(statement); 77  } 78  } 79  80  @Override 81  public Set<Resource> getResourcesByResourcePermission(SQLConnection connection, 82  Resource accessorResource, 83  Id<ResourceClassId> resourceClassId, 84  Id<DomainId> resourceDomainId, 85  ResourcePermission resourcePermission, 86  Id<ResourcePermissionId> resourcePermissionId) { 87  if (resourcePermission.isSystemPermission()) { 88  throw new IllegalArgumentException("Permission: " + resourcePermission + " is not a non-system permission"); 89  } 90  91  SQLStatement statement = null; 92  try { 93  // get the list of objects of the specified type that the session has access to via direct permissions 94  SQLResult resultSet; 95  Set<Resource> resources = new HashSet<>(); 96  97  statement = connection.prepareStatement(sqlStrings.SQL_findInGrantResourcePermission_ResourceID_ExternalID_BY_AccessorID_DomainID_ResourceClassID_PermissionID_IsWithGrant); 98  statement.setResourceId(1, accessorResource); 99  statement.setResourceDomainId(2, resourceDomainId); 100  statement.setResourceClassId(3, resourceClassId); 101  statement.setResourcePermissionId(4, resourcePermissionId); 102  statement.setBoolean(5, resourcePermission.isWithGrantOption()); 103  resultSet = statement.executeQuery(); 104  105  while (resultSet.next()) { 106  resources.add(resultSet.getResource("ResourceId", "ExternalId")); 107  } 108  resultSet.close(); 109  110  return resources; 111  } 112  catch (SQLException e) { 113  throw new RuntimeException(e); 114  } 115  finally { 116  closeStatement(statement); 117  } 118  } 119  120  @Override 121  public Set<ResourcePermission> getResourcePermissionsIncludeInherited(SQLConnection connection, 122  Resource accessorResource, 123  Resource accessedResource) { 124  SQLStatement statement = null; 125  try { 126  SQLResult resultSet; 127  Set<ResourcePermission> resourcePermissions = new HashSet<>(); 128  129  statement = connection.prepareStatement(sqlStrings.SQL_findInGrantResourcePermission_ResourceClassName_PermissionName_IsWithGrant_BY_AccessorID_AccessedID); 130  statement.setResourceId(1, accessorResource); 131  statement.setResourceId(2, accessedResource); 132  resultSet = statement.executeQuery(); 133  134  while (resultSet.next()) { 135  resourcePermissions.add(getResourcePermission(resultSet)); 136  } 137  resultSet.close(); 138  139  return resourcePermissions; 140  } 141  catch (SQLException e) { 142  throw new RuntimeException(e); 143  } 144  finally { 145  closeStatement(statement); 146  } 147  } 148 }