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

Class Class, % Method, % Line, %
RecursiveGrantResourceCreatePermissionPostCreateSysPersister 100% (1/ 1) 100% (4/ 4) 69.6% (39/ 56)


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.sql.SQLProfile; 23 import com.acciente.oacc.sql.internal.persister.id.DomainId; 24 import com.acciente.oacc.sql.internal.persister.id.Id; 25 import com.acciente.oacc.sql.internal.persister.id.ResourceClassId; 26  27 import java.io.Serializable; 28 import java.sql.SQLException; 29 import java.util.ArrayList; 30 import java.util.HashMap; 31 import java.util.HashSet; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.Set; 35  36 public class RecursiveGrantResourceCreatePermissionPostCreateSysPersister extends CommonGrantResourceCreatePermissionPostCreateSysPersister implements Serializable { 37  private static final long serialVersionUID = 1L; 38  39  public RecursiveGrantResourceCreatePermissionPostCreateSysPersister(SQLProfile sqlProfile, 40  SQLStrings sqlStrings) { 41  super(sqlProfile, sqlStrings); 42  } 43  44  @Override 45  public Set<ResourceCreatePermission> getResourceCreatePostCreateSysPermissionsIncludeInherited(SQLConnection connection, 46  Resource accessorResource, 47  Id<ResourceClassId> resourceClassId, 48  Id<DomainId> resourceDomainId) { 49  SQLStatement statement = null; 50  try { 51  SQLResult resultSet; 52  Set<ResourceCreatePermission> resourceCreatePermissions = new HashSet<>(); 53  54  // collect the system permissions the accessor has to the specified resource class 55  statement = connection.prepareStatement(sqlStrings.SQL_findInGrantResourceCreatePermissionPostCreateSys_PostCreateSysPermissionID_PostCreateIsWithGrant_IsWithGrant_BY_AccessorID_AccessedDomainID_ResourceClassID); 56  statement.setResourceId(1, accessorResource); 57  statement.setResourceDomainId(2, resourceDomainId); 58  statement.setResourceClassId(3, resourceClassId); 59  resultSet = statement.executeQuery(); 60  61  while (resultSet.next()) { 62  resourceCreatePermissions.add(getResourceCreatePostCreateSysPermission(resultSet)); 63  } 64  resultSet.close(); 65  66  return resourceCreatePermissions; 67  } 68  catch (SQLException e) { 69  throw new RuntimeException(e); 70  } 71  finally { 72  closeStatement(statement); 73  } 74  } 75  76  @Override 77  public Map<String, Map<String, Set<ResourceCreatePermission>>> getResourceCreatePostCreateSysPermissionsIncludeInherited( 78  SQLConnection connection, 79  Resource accessorResource) { 80  SQLStatement statement = null; 81  82  try { 83  Map<String, Map<String, Set<ResourceCreatePermission>>> createSysPermissionsMap = new HashMap<>(); 84  SQLResult resultSet; 85  86  // collect the system permissions that the accessor has and add it to createALLPermissionsMap 87  statement = connection.prepareStatement(sqlStrings.SQL_findInGrantResourceCreatePermissionPostCreateSys_ResourceDomainName_ResourceClassName_PostCreateSysPermissionID_PostCreateIsWithGrant_IsWithGrant_BY_AccessorID); 88  statement.setResourceId(1, accessorResource); 89  resultSet = statement.executeQuery(); 90  91  while (resultSet.next()) { 92  final String resourceDomainName; 93  final String resourceClassName; 94  Map<String, Set<ResourceCreatePermission>> permissionsForResourceDomain; 95  Set<ResourceCreatePermission> permissionsForResourceClass; 96  97  resourceDomainName = resultSet.getString("DomainName"); 98  resourceClassName = resultSet.getString("ResourceClassName"); 99  100  if ((permissionsForResourceDomain = createSysPermissionsMap.get(resourceDomainName)) == null) { 101  createSysPermissionsMap.put(resourceDomainName, 102  permissionsForResourceDomain = new HashMap<>()); 103  } 104  105  if ((permissionsForResourceClass = permissionsForResourceDomain.get(resourceClassName)) == null) { 106  permissionsForResourceDomain.put(resourceClassName, 107  permissionsForResourceClass = new HashSet<>()); 108  } 109  110  permissionsForResourceClass.add(getResourceCreatePostCreateSysPermission(resultSet)); 111  } 112  resultSet.close(); 113  114  return createSysPermissionsMap; 115  } 116  catch (SQLException e) { 117  throw new RuntimeException(e); 118  } 119  finally { 120  closeStatement(statement); 121  } 122  } 123  124  @Override 125  public void removeAllResourceCreatePostCreateSysPermissions(SQLConnection connection, 126  Id<DomainId> accessedDomainId) { 127  SQLStatement statement = null; 128  try { 129  // chose strategy to perform recursive delete based on sql profile 130  if (sqlProfile.isRecursiveDeleteEnabled()) { 131  // prepare the standard recursive delete statement for domain and its children 132  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantResourceCreatePermissionPostCreateSys_withDescendants_BY_AccessedDomainID); 133  134  // revoke any existing post-create system permissions any accessor has to this domain + any resource class 135  statement.setResourceDomainId(1, accessedDomainId); 136  statement.executeUpdate(); 137  } 138  else { 139  // DBMS doesn't support recursive deletion, so we have to remove domain's children's accessors first 140  141  // get descendant domain Ids 142  statement = connection.prepareStatement(sqlStrings.SQL_findInDomain_DescendantResourceDomainID_BY_DomainID_ORDERBY_DomainLevel); 143  statement.setResourceDomainId(1, accessedDomainId); 144  SQLResult resultSet = statement.executeQuery(); 145  146  List<Id<DomainId>> descendantDomainIds = new ArrayList<>(); 147  148  while (resultSet.next()) { 149  descendantDomainIds.add(resultSet.getResourceDomainId("DomainId")); 150  } 151  closeStatement(statement); 152  153  // delete domains' accessors (in reverse order of domainLevel, to preserve FK constraints) 154  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantResourceCreatePermissionPostCreateSys_BY_AccessedDomainID); 155  156  for (int i=descendantDomainIds.size()-1; i >= 0; i--) { 157  statement.setResourceDomainId(1, descendantDomainIds.get(i)); 158  statement.executeUpdate(); 159  } 160  } 161  } 162  catch (SQLException e) { 163  throw new RuntimeException(e); 164  } 165  finally { 166  closeStatement(statement); 167  } 168  } 169 }