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

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