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

Class Class, % Method, % Line, %
CommonGrantGlobalResourcePermissionPersister 100% (1/ 1) 100% (9/ 9) 87.3% (96/ 110)


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.HashMap; 32 import java.util.HashSet; 33 import java.util.Map; 34 import java.util.Set; 35  36 public abstract class CommonGrantGlobalResourcePermissionPersister extends Persister implements GrantGlobalResourcePermissionPersister, Serializable { 37  private static final long serialVersionUID = 1L; 38  39  protected final SQLProfile sqlProfile; 40  protected final SQLStrings sqlStrings; 41  42  public CommonGrantGlobalResourcePermissionPersister(SQLProfile sqlProfile, 43  SQLStrings sqlStrings) { 44  this.sqlProfile = sqlProfile; 45  this.sqlStrings = sqlStrings; 46  } 47  48  @Override 49  public abstract Set<Resource> getResourcesByGlobalResourcePermission(SQLConnection connection, 50  Resource accessorResource, 51  Id<ResourceClassId> resourceClassId, 52  ResourcePermission resourcePermission, 53  Id<ResourcePermissionId> resourcePermissionId); 54  55  @Override 56  public abstract Set<Resource> getResourcesByGlobalResourcePermission(SQLConnection connection, 57  Resource accessorResource, 58  Id<ResourceClassId> resourceClassId, 59  Id<DomainId> resourceDomainId, 60  ResourcePermission resourcePermission, 61  Id<ResourcePermissionId> resourcePermissionId); 62  63  @Override 64  public abstract Set<ResourcePermission> getGlobalResourcePermissionsIncludeInherited(SQLConnection connection, 65  Resource accessorResource, 66  Id<ResourceClassId> resourceClassId, 67  Id<DomainId> resourceDomainId); 68  69  @Override 70  public Set<ResourcePermission> getGlobalResourcePermissions(SQLConnection connection, 71  Resource accessorResource, 72  Id<ResourceClassId> resourceClassId, 73  Id<DomainId> resourceDomainId) { 74  SQLStatement statement = null; 75  try { 76  // collect the system permissions that the accessor has to the accessed resource directly 77  SQLResult resultSet; 78  Set<ResourcePermission> resourcePermissions = new HashSet<>(); 79  80  statement = connection.prepareStatement(sqlStrings.SQL_findInGrantGlobalResourcePermission_withoutInheritance_PermissionName_IsWithGrant_BY_AccessorID_AccessedDomainID_ResourceClassID); 81  statement.setResourceId(1, accessorResource); 82  statement.setResourceDomainId(2, resourceDomainId); 83  statement.setResourceClassId(3, resourceClassId); 84  resultSet = statement.executeQuery(); 85  86  while (resultSet.next()) { 87  resourcePermissions.add(getResourcePermission(resultSet)); 88  } 89  resultSet.close(); 90  91  return resourcePermissions; 92  } 93  catch (SQLException e) { 94  throw new RuntimeException(e); 95  } 96  finally { 97  closeStatement(statement); 98  } 99  } 100  101  protected static ResourcePermission getResourcePermission(SQLResult resultSet) throws SQLException { 102  final String permissionName = resultSet.getString("PermissionName"); 103  104  if (resultSet.getBoolean("IsWithGrant")) { 105  return ResourcePermissions.getInstanceWithGrantOption(permissionName); 106  } 107  else { 108  return ResourcePermissions.getInstance(permissionName); 109  } 110  } 111  112  @Override 113  public abstract Map<String, Map<String, Set<ResourcePermission>>> getGlobalResourcePermissionsIncludeInherited(SQLConnection connection, 114  Resource accessorResource); 115  116  @Override 117  public Map<String, Map<String, Set<ResourcePermission>>> getGlobalResourcePermissions(SQLConnection connection, 118  Resource accessorResource) { 119  SQLStatement statement = null; 120  try { 121  // collect the non-system permissions that the accessor has 122  SQLResult resultSet; 123  final Map<String, Map<String, Set<ResourcePermission>>> globalPermissionsMap = new HashMap<>(); 124  125  statement = connection.prepareStatement(sqlStrings.SQL_findInGrantGlobalResourcePermission_withoutInheritance_ResourceDomainName_ResourceClassName_PermissionName_IsWithGrant_BY_AccessorID); 126  statement.setResourceId(1, accessorResource); 127  resultSet = statement.executeQuery(); 128  129  while (resultSet.next()) { 130  final String resourceDomainName; 131  final String resourceClassName; 132  Map<String, Set<ResourcePermission>> permissionsForResourceDomain; 133  Set<ResourcePermission> resourcePermissionsForResourceClass; 134  135  resourceDomainName = resultSet.getString("DomainName"); 136  resourceClassName = resultSet.getString("ResourceClassName"); 137  138  if ((permissionsForResourceDomain = globalPermissionsMap.get(resourceDomainName)) == null) { 139  globalPermissionsMap.put(resourceDomainName, 140  permissionsForResourceDomain = new HashMap<>()); 141  } 142  143  if ((resourcePermissionsForResourceClass = permissionsForResourceDomain.get(resourceClassName)) == null) { 144  permissionsForResourceDomain.put(resourceClassName, 145  resourcePermissionsForResourceClass = new HashSet<>()); 146  } 147  148  resourcePermissionsForResourceClass.add(getResourcePermission(resultSet)); 149  } 150  resultSet.close(); 151  152  return globalPermissionsMap; 153  } 154  catch (SQLException e) { 155  throw new RuntimeException(e); 156  } 157  finally { 158  closeStatement(statement); 159  } 160  } 161  162  @Override 163  public void addGlobalResourcePermissions(SQLConnection connection, 164  Resource accessorResource, 165  Id<ResourceClassId> accessedResourceClassId, 166  Id<DomainId> accessedResourceDomainId, 167  Set<ResourcePermission> requestedResourcePermissions, 168  Resource grantorResource) { 169  SQLStatement statement = null; 170  try { 171  // add the new non-system permissions 172  statement = connection.prepareStatement(sqlStrings.SQL_createInGrantGlobalResourcePermission_WITH_AccessorID_GrantorID_AccessedDomainID_IsWithGrant_ResourceClassID_PermissionName); 173  for (ResourcePermission resourcePermission : requestedResourcePermissions) { 174  if (!resourcePermission.isSystemPermission()) { 175  statement.setResourceId(1, accessorResource); 176  statement.setResourceId(2, grantorResource); 177  statement.setResourceDomainId(3, accessedResourceDomainId); 178  statement.setBoolean(4, resourcePermission.isWithGrantOption()); 179  statement.setResourceClassId(5, accessedResourceClassId); 180  statement.setString(6, resourcePermission.getPermissionName()); 181  182  assertOneRowInserted(statement.executeUpdate()); 183  } 184  } 185  } 186  catch (SQLException e) { 187  throw new RuntimeException(e); 188  } 189  finally { 190  closeStatement(statement); 191  } 192  } 193  194  @Override 195  public void updateGlobalResourcePermissions(SQLConnection connection, 196  Resource accessorResource, 197  Id<ResourceClassId> accessedResourceClassId, 198  Id<DomainId> accessedResourceDomainId, 199  Set<ResourcePermission> requestedResourcePermissions, 200  Resource grantorResource) { 201  SQLStatement statement = null; 202  try { 203  // add the new non-system permissions 204  statement = connection.prepareStatement(sqlStrings.SQL_updateInGrantGlobalResourcePermission_SET_GrantorID_IsWithGrant_BY_AccessorID_AccessedDomainID_ResourceClassID_PermissionName); 205  for (ResourcePermission resourcePermission : requestedResourcePermissions) { 206  if (!resourcePermission.isSystemPermission()) { 207  statement.setResourceId(1, grantorResource); 208  statement.setBoolean(2, resourcePermission.isWithGrantOption()); 209  statement.setResourceId(3, accessorResource); 210  statement.setResourceDomainId(4, accessedResourceDomainId); 211  statement.setResourceClassId(5, accessedResourceClassId); 212  statement.setString(6, resourcePermission.getPermissionName()); 213  214  assertOneRowUpdated(statement.executeUpdate()); 215  } 216  } 217  } 218  catch (SQLException e) { 219  throw new RuntimeException(e); 220  } 221  finally { 222  closeStatement(statement); 223  } 224  } 225  226  @Override 227  public void removeAllGlobalResourcePermissions(SQLConnection connection, 228  Resource accessorResource) { 229  230  SQLStatement statement = null; 231  try { 232  // revoke any existing non-system permissions this accessor has to any domain + resource class 233  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantGlobalResourcePermission_BY_AccessorID); 234  statement.setResourceId(1, accessorResource); 235  statement.executeUpdate(); 236  } 237  catch (SQLException e) { 238  throw new RuntimeException(e); 239  } 240  finally { 241  closeStatement(statement); 242  } 243  } 244  245  @Override 246  public abstract void removeAllGlobalResourcePermissions(SQLConnection connection, 247  Id<DomainId> accessedDomainId); 248  249  @Override 250  public void removeGlobalResourcePermissions(SQLConnection connection, 251  Resource accessorResource, 252  Id<ResourceClassId> accessedResourceClassId, 253  Id<DomainId> accessedResourceDomainId) { 254  255  SQLStatement statement = null; 256  try { 257  // revoke any existing non-system permissions this accessor has to this domain + resource class 258  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantGlobalResourcePermission_BY_AccessorID_AccessedDomainID_ResourceClassID); 259  statement.setResourceId(1, accessorResource); 260  statement.setResourceDomainId(2, accessedResourceDomainId); 261  statement.setResourceClassId(3, accessedResourceClassId); 262  statement.executeUpdate(); 263  } 264  catch (SQLException e) { 265  throw new RuntimeException(e); 266  } 267  finally { 268  closeStatement(statement); 269  } 270  } 271  272  @Override 273  public void removeGlobalResourcePermissions(SQLConnection connection, 274  Resource accessorResource, 275  Id<ResourceClassId> accessedResourceClassId, 276  Id<DomainId> accessedResourceDomainId, 277  Set<ResourcePermission> requestedResourcePermissions) { 278  SQLStatement statement = null; 279  try { 280  // remove the specified non-system permissions 281  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantGlobalResourcePermission_BY_AccessorID_AccessedDomainID_ResourceClassID_PermissionName); 282  for (ResourcePermission resourcePermission : requestedResourcePermissions) { 283  if (!resourcePermission.isSystemPermission()) { 284  statement.setResourceId(1, accessorResource); 285  statement.setResourceDomainId(2, accessedResourceDomainId); 286  statement.setResourceClassId(3, accessedResourceClassId); 287  statement.setString(4, resourcePermission.getPermissionName()); 288  289  assertOneRowUpdated(statement.executeUpdate()); 290  } 291  } 292  } 293  catch (SQLException e) { 294  throw new RuntimeException(e); 295  } 296  finally { 297  closeStatement(statement); 298  } 299  } 300 }