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

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