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

Class Class, % Method, % Line, %
CommonGrantResourceCreatePermissionPostCreatePersister 100% (1/ 1) 100% (9/ 9) 87.8% (101/ 115)


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