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

Class Class, % Method, % Line, %
CommonGrantResourcePermissionPersister 100% (1/ 1) 100% (9/ 9) 86% (92/ 107)


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.ResourcePermissions; 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 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 abstract class CommonGrantResourcePermissionPersister extends Persister implements GrantResourcePermissionPersister, Serializable { 35  private static final long serialVersionUID = 1L; 36  37  protected final SQLProfile sqlProfile; 38  protected final SQLStrings sqlStrings; 39  40  public CommonGrantResourcePermissionPersister(SQLProfile sqlProfile, 41  SQLStrings sqlStrings) { 42  this.sqlProfile = sqlProfile; 43  this.sqlStrings = sqlStrings; 44  } 45  46  @Override 47  public abstract Set<Resource> getResourcesByResourcePermission(SQLConnection connection, 48  Resource accessorResource, 49  Id<ResourceClassId> resourceClassId, 50  ResourcePermission resourcePermission, 51  Id<ResourcePermissionId> resourcePermissionId); 52  53  @Override 54  public abstract Set<Resource> getResourcesByResourcePermission(SQLConnection connection, 55  Resource accessorResource, 56  Id<ResourceClassId> resourceClassId, 57  Id<DomainId> resourceDomainId, 58  ResourcePermission resourcePermission, 59  Id<ResourcePermissionId> resourcePermissionId); 60  61  @Override 62  public Set<Resource> getAccessorResourcesByResourcePermission(SQLConnection connection, 63  Resource accessedResource, 64  Id<ResourceClassId> resourceClassId, 65  ResourcePermission resourcePermission, 66  Id<ResourcePermissionId> resourcePermissionId) { 67  if (resourcePermission.isSystemPermission()) { 68  throw new IllegalArgumentException("Permission: " + resourcePermission + " is not a non-system permission"); 69  } 70  71  SQLStatement statement = null; 72  try { 73  // get the list of resources of the specified type that direct permissions to the specified accessed resource 74  SQLResult resultSet; 75  Set<Resource> resources = new HashSet<>(); 76  77  statement = connection.prepareStatement(sqlStrings.SQL_findInGrantResourcePermission_ResourceID_ExternalID_BY_AccessedID_ResourceClassID_PermissionID_IsWithGrant); 78  statement.setResourceId(1, accessedResource); 79  statement.setResourceClassId(2, resourceClassId); 80  statement.setResourcePermissionId(3, resourcePermissionId); 81  statement.setBoolean(4, resourcePermission.isWithGrantOption()); 82  resultSet = statement.executeQuery(); 83  84  while (resultSet.next()) { 85  resources.add(resultSet.getResource("ResourceId", "ExternalId")); 86  } 87  resultSet.close(); 88  89  return resources; 90  } 91  catch (SQLException e) { 92  throw new RuntimeException(e); 93  } 94  finally { 95  closeStatement(statement); 96  } 97  } 98  99  @Override 100  public abstract Set<ResourcePermission> getResourcePermissionsIncludeInherited(SQLConnection connection, 101  Resource accessorResource, 102  Resource accessedResource); 103  104  @Override 105  public Set<ResourcePermission> getResourcePermissions(SQLConnection connection, 106  Resource accessorResource, 107  Resource accessedResource) { 108  SQLStatement statement = null; 109  try { 110  SQLResult resultSet; 111  Set<ResourcePermission> resourcePermissions = new HashSet<>(); 112  113  statement = connection.prepareStatement(sqlStrings.SQL_findInGrantResourcePermission_withoutInheritance_ResourceClassName_PermissionName_IsWithGrant_BY_AccessorID_AccessedID); 114  statement.setResourceId(1, accessorResource); 115  statement.setResourceId(2, accessedResource); 116  resultSet = statement.executeQuery(); 117  118  while (resultSet.next()) { 119  resourcePermissions.add(getResourcePermission(resultSet)); 120  } 121  resultSet.close(); 122  123  return resourcePermissions; 124  } 125  catch (SQLException e) { 126  throw new RuntimeException(e); 127  } 128  finally { 129  closeStatement(statement); 130  } 131  } 132  133  protected static ResourcePermission getResourcePermission(SQLResult resultSet) throws SQLException { 134  final String permissionName = resultSet.getString("PermissionName"); 135  136  if (resultSet.getBoolean("IsWithGrant")) { 137  return ResourcePermissions.getInstanceWithGrantOption(permissionName); 138  } 139  else { 140  return ResourcePermissions.getInstance(permissionName); 141  } 142  } 143  144  @Override 145  public void addResourcePermissions(SQLConnection connection, 146  Resource accessorResource, 147  Resource accessedResource, 148  Id<ResourceClassId> accessedResourceClassId, 149  Set<ResourcePermission> requestedResourcePermissions, 150  Resource grantorResource) { 151  SQLStatement statement = null; 152  try { 153  statement = connection.prepareStatement(sqlStrings.SQL_createInGrantResourcePermission_WITH_AccessorID_GrantorID_AccessedID_IsWithGrant_ResourceClassID_PermissionName); 154  for (ResourcePermission resourcePermission : requestedResourcePermissions) { 155  if (!resourcePermission.isSystemPermission()) { 156  statement.setResourceId(1, accessorResource); 157  statement.setResourceId(2, grantorResource); 158  statement.setResourceId(3, accessedResource); 159  statement.setBoolean(4, resourcePermission.isWithGrantOption()); 160  statement.setResourceClassId(5, accessedResourceClassId); 161  statement.setString(6, resourcePermission.getPermissionName()); 162  163  assertOneRowInserted(statement.executeUpdate()); 164  } 165  } 166  } 167  catch (SQLException e) { 168  throw new RuntimeException(e); 169  } 170  finally { 171  closeStatement(statement); 172  } 173  } 174  175  @Override 176  public void updateResourcePermissions(SQLConnection connection, 177  Resource accessorResource, 178  Resource accessedResource, 179  Id<ResourceClassId> accessedResourceClassId, 180  Set<ResourcePermission> requestedResourcePermissions, 181  Resource grantorResource) { 182  SQLStatement statement = null; 183  try { 184  statement = connection.prepareStatement(sqlStrings.SQL_updateInGrantResourcePermission_SET_GrantorID_IsWithGrant_BY_AccessorID_AccessedID_ResourceClassID_PermissionName); 185  for (ResourcePermission resourcePermission : requestedResourcePermissions) { 186  if (!resourcePermission.isSystemPermission()) { 187  statement.setResourceId(1, grantorResource); 188  statement.setBoolean(2, resourcePermission.isWithGrantOption()); 189  statement.setResourceId(3, accessorResource); 190  statement.setResourceId(4, accessedResource); 191  statement.setResourceClassId(5, accessedResourceClassId); 192  statement.setString(6, resourcePermission.getPermissionName()); 193  194  assertOneRowUpdated(statement.executeUpdate()); 195  } 196  } 197  } 198  catch (SQLException e) { 199  throw new RuntimeException(e); 200  } 201  finally { 202  closeStatement(statement); 203  } 204  } 205  206  @Override 207  public void removeAllResourcePermissionsAsAccessorOrAccessed(SQLConnection connection, 208  Resource resource) { 209  SQLStatement statement = null; 210  try { 211  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantResourcePermission_BY_AccessorID_OR_AccessedID); 212  statement.setResourceId(1, resource); 213  statement.setResourceId(2, resource); 214  statement.executeUpdate(); 215  } 216  catch (SQLException e) { 217  throw new RuntimeException(e); 218  } 219  finally { 220  closeStatement(statement); 221  } 222  } 223  224  @Override 225  public void removeResourcePermissions(SQLConnection connection, 226  Resource accessorResource, 227  Resource accessedResource) { 228  SQLStatement statement = null; 229  try { 230  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantResourcePermission_BY_AccessorID_AccessedID); 231  statement.setResourceId(1, accessorResource); 232  statement.setResourceId(2, accessedResource); 233  statement.executeUpdate(); 234  } 235  catch (SQLException e) { 236  throw new RuntimeException(e); 237  } 238  finally { 239  closeStatement(statement); 240  } 241  } 242  243  @Override 244  public void removeResourcePermissions(SQLConnection connection, 245  Resource accessorResource, 246  Resource accessedResource, 247  Id<ResourceClassId> accessedResourceClassId, 248  Set<ResourcePermission> requestedResourcePermissions) { 249  SQLStatement statement = null; 250  try { 251  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantResourcePermission_BY_AccessorID_AccessedID_ResourceClassID_PermissionName); 252  for (ResourcePermission resourcePermission : requestedResourcePermissions) { 253  if (!resourcePermission.isSystemPermission()) { 254  statement.setResourceId(1, accessorResource); 255  statement.setResourceId(2, accessedResource); 256  statement.setResourceClassId(3, accessedResourceClassId); 257  statement.setString(4, resourcePermission.getPermissionName()); 258  259  assertOneRowUpdated(statement.executeUpdate()); 260  } 261  } 262  } 263  catch (SQLException e) { 264  throw new RuntimeException(e); 265  } 266  finally { 267  closeStatement(statement); 268  } 269  } 270 }