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

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