1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 6 import com.daffodilwoods.daffodildb.server.serversystem.*; 7 import com.daffodilwoods.daffodildb.server.sql99.*; 8 import com.daffodilwoods.daffodildb.server.sql99.common.*; 9 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 10 import com.daffodilwoods.daffodildb.server.sql99.ddl.utility.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 12 import com.daffodilwoods.daffodildb.server.sql99.token.*; 13 import com.daffodilwoods.database.general.*; 14 import com.daffodilwoods.database.resource.*; 15 16 public class grantprivilegestatement implements grantstatement { 17 public SNONRESERVEDWORD136444255SRESERVEDWORD1206543922grantor _OptSNONRESERVEDWORD136444255SRESERVEDWORD1206543922grantor0; 18 public SRESERVEDWORD1206543922SRESERVEDWORD1206543922SNONRESERVEDWORD136444255 _OptSRESERVEDWORD1206543922SRESERVEDWORD1206543922SNONRESERVEDWORD1364442551; 19 public dummyrule _Optdummyrule2; 20 public grantee[] _OptRepScomma94843605grantee3; 21 public grantee _grantee4; 22 public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439225; 23 public privileges _privileges6; 24 public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439227; 25 26 private SchemaDescriptor schemaDescriptor; 27 private _Descriptor objectDescriptor; 28 private Object [] applicableRoles; 29 private String objectSchemaName; 30 boolean isIndependentStt; 31 32 public void setSchemaDescriptor(_Descriptor schemaDes) throws DException { 33 schemaDescriptor = (SchemaDescriptor) schemaDes; 34 } 35 36 public void setObjectDescriptor(_Descriptor objectDes) { 37 objectDescriptor = objectDes; 38 } 39 40 58 59 60 public Object run(Object object) throws DException { 61 _ServerSession currentSession = (_ServerSession) object; 62 ArrayList granteeList = getAllGrantee(); 63 64 checkGranteeExistance(currentSession, granteeList); 65 isIndependentStt = objectDescriptor == null; 66 if (isIndependentStt) { 67 objectSchemaName = getSchemaNameForObject(_privileges6, currentSession); 68 } 69 String grantor = getGrantor(currentSession); 70 ArrayList listOfPrivileges = getListOfPrivileges(currentSession, grantor); 71 if (!grantor.equalsIgnoreCase(SystemTables.SYSTEM) && !grantor.equalsIgnoreCase(ServerSystem.browserUser)) { 72 checkGrantorPrivileges(currentSession, listOfPrivileges, grantor); 73 } 74 String isGrantable = 75 _OptSRESERVEDWORD1206543922SRESERVEDWORD1206543922SNONRESERVEDWORD1364442551 != null 76 ? SqlSchemaConstants.YES : SqlSchemaConstants.NO; 77 String objectType = _privileges6.getObjectType(); 78 if (objectType.equalsIgnoreCase(SqlKeywords.TABLE)) { 79 processTablePrivileges(listOfPrivileges, grantor, isGrantable, 80 currentSession, granteeList); 81 } else if (objectType.equalsIgnoreCase(SqlKeywords.DOMAIN) || 82 objectType.equalsIgnoreCase(SqlKeywords.SPECIFIC)) { 83 processOtherPrivileges(listOfPrivileges, grantor, isGrantable, 84 currentSession, granteeList); 85 } 86 refreshDataDictionary(currentSession, _privileges6.getObjectType(), 87 _privileges6.getQualifiedObjectName()); 88 return null; 89 } 90 91 private String getSchemaNameForObject(privileges _privileges6, _ServerSession currentSession) throws DException { 92 94 String sch_name = _privileges6._objectname0.getSchemaName(); 95 String cat_name = _privileges6._objectname0.getCatalogName(); 96 sch_name = (sch_name != null) ? sch_name : (schemaDescriptor != null) 97 ? schemaDescriptor.schema_name : currentSession.getCurrentSchema(); 98 cat_name = (cat_name != null) ? cat_name : (schemaDescriptor != null) 99 ? schemaDescriptor.catalog_name : currentSession.getCurrentCatalog(); 100 QualifiedIdentifier objectName = new QualifiedIdentifier(cat_name, sch_name, 101 _privileges6._objectname0.getObjectName()); 102 String qualifiedObjectName = objectName.toString(); 103 104 int index0 = qualifiedObjectName.lastIndexOf("."); 105 if (index0 == -1) { 106 return currentSession.getCurrentSchema(); 107 } else { 108 String Sc = qualifiedObjectName.substring(0, index0); 109 int index1 = Sc.lastIndexOf("."); 110 if (index1 == -1) 111 return Sc.trim(); 112 else 113 return Sc.substring(index1 + 1, index0).trim(); 114 } 115 } 116 117 private void checkGranteeExistance(_ServerSession serverSession, ArrayList grantees) throws DException { 118 _Executer userExecuter = ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter().getExecuterForUserValidity(); 119 _Executer rolesExecuter = ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter().getExecuterForRoleValidity(); 120 for (int i = 0, size = grantees.size(); i < size; i++) { 121 String tempGrantee = (String ) grantees.get(i); 122 if (! (tempGrantee.equalsIgnoreCase(SystemTables.SYSTEM) || tempGrantee.equalsIgnoreCase(ServerSystem.browserUser))) { 123 _Iterator iterator = (_Iterator) userExecuter.executeForFresh(new Object [] {tempGrantee}); 124 if (!iterator.first()) { 125 iterator = (_Iterator) rolesExecuter.executeForFresh(new Object [] {tempGrantee}); 126 if (!iterator.first()) { 127 throw new DException("DSE8094", new Object [] {tempGrantee}); 128 } 129 } 130 } 131 } 132 } 133 134 private ArrayList getListOfPrivileges(_ServerSession currentSession, 135 String grantor) throws DException { 136 if (objectDescriptor != null) { 137 _privileges6.setObjectDescriptor(objectDescriptor); 138 } 139 if (schemaDescriptor != null) { 140 _privileges6.setSchemaDescriptor(schemaDescriptor); 141 } 142 _privileges6.setGrantee(grantor); 143 return (ArrayList) _privileges6.run(currentSession); 144 } 145 146 private void processTablePrivileges(ArrayList listOfPrivileges, 147 String grantor, String isGrantable, _ServerSession currentSession, 148 ArrayList granteeList) throws DException { 149 TableDescriptor tableDes = (TableDescriptor) objectDescriptor; 150 if (tableDes == null) { 151 tableDes = (TableDescriptor) _privileges6.getObjectDescriptor(); 152 153 } 154 ArrayList allColumnDesciptor = tableDes.get_all_column_descriptors(); 155 for (int j = 0, temp_size = listOfPrivileges.size(); j < temp_size; j++) { 156 PrivilegeDescriptor privilege_sql = (PrivilegeDescriptor) 157 listOfPrivileges.get(j); 158 privilege_sql.setGrantor(grantor); 159 privilege_sql.is_grantable = isGrantable; 160 if (privilege_sql instanceof TablePrivilegesDescriptor) { 161 TablePrivilegesDescriptor tpd = (TablePrivilegesDescriptor) privilege_sql; 162 tpd.saveColumnsPrivilegeDescriptor(currentSession, allColumnDesciptor, 163 tpd, granteeList); 164 } 165 for (int i = 0; i < granteeList.size(); i++) { 166 privilege_sql.setGrantee(granteeList.get(i)); 167 try { 168 privilege_sql.save(currentSession); 169 } catch (DException ex) { 170 if ( (ex.getDseCode().equals("DSE1255") || ex.getDseCode().equals("DSE1138")) && 171 privilege_sql.is_grantable.equalsIgnoreCase(SqlSchemaConstants.YES)) { 172 privilege_sql.updateIsGrantableValue(currentSession, privilege_sql.is_grantable); 173 } 174 } 175 } 176 } 177 } 178 179 private void processOtherPrivileges(ArrayList listOfPrivileges, 180 String grantor, String isGrantable, _ServerSession currentSession, 181 ArrayList granteeList) throws DException { 182 for (int j = 0, temp_size = listOfPrivileges.size(); j < temp_size; j++) { 183 PrivilegeDescriptor privilege_sql = (PrivilegeDescriptor) 184 listOfPrivileges.get(j); 185 privilege_sql.setGrantor(grantor); 186 privilege_sql.is_grantable = isGrantable; 187 for (int i = 0; i < granteeList.size(); i++) { 188 privilege_sql.setGrantee(granteeList.get(i)); 189 try { 190 privilege_sql.save(currentSession); 191 } catch (DException ex) { 192 if (ex.getDseCode().equals("DSE1255") && 193 privilege_sql.is_grantable.equalsIgnoreCase(SqlSchemaConstants.YES)) { 194 privilege_sql.updateIsGrantableValue(currentSession, 195 privilege_sql.is_grantable); 196 } 197 } 198 } 199 } 200 } 201 202 private void refreshDataDictionary(_ServerSession currentSession, 203 String objectType, 204 QualifiedIdentifier objectName) throws 205 DException { 206 if (objectDescriptor == null) { 207 currentSession.getDataDictionary().refreshPrivilegeCharacteristics( 208 objectName, objectType); 209 } 210 } 211 212 private ArrayList getAllGrantee() throws DException { 213 ArrayList tempGranteeList = new ArrayList(); 214 tempGranteeList.add(_grantee4.run(null)); 215 if (_OptRepScomma94843605grantee3 != null) { 216 for (int i = 0, size = _OptRepScomma94843605grantee3.length; i < size; i++) { 217 String tmpGrantee = (String ) _OptRepScomma94843605grantee3[i].run(null); 218 if (!tempGranteeList.contains(tmpGrantee)) { 219 tempGranteeList.add(tmpGrantee); 220 } 221 } 222 } 223 return tempGranteeList; 224 } 225 226 private String getGrantor(_ServerSession serverSession) throws DException { 227 String tempGrantor = null; 228 if (schemaDescriptor != null) { 229 tempGrantor = schemaDescriptor.schema_owner; 230 } else if (_OptSNONRESERVEDWORD136444255SRESERVEDWORD1206543922grantor0 == null) { 231 String currentUser = (String ) serverSession.getCurrentUser(); 232 if (currentUser != null) { 233 if (currentUser.equalsIgnoreCase(ServerSystem.browserUser)) { 234 StringBuffer sb = new StringBuffer (); 235 sb.append("select schema_owner from ").append(SystemTables.schema_TableName). 236 append(" where schema_name = ").append("?"); 237 _SelectQueryIterator iter = SqlSchemaConstants.getIterator(serverSession, sb.toString(), new Object [] {isIndependentStt ? objectSchemaName : serverSession.getCurrentSchema()}); 238 if (iter.first()) { 239 Object [] ob = (Object []) iter.getColumnValues(); 240 tempGrantor = ob[0].toString(); 241 } 242 } else 243 tempGrantor = currentUser; 244 } else { 245 tempGrantor = serverSession.getCurrentRole(); 246 TreeMap applicableRoleAuthos = GeneralUtility.getApplicableRoles(serverSession, tempGrantor); 247 applicableRoles = applicableRoleAuthos.keySet().toArray(); 248 } 249 } else { 250 tempGrantor = 251 _OptSNONRESERVEDWORD136444255SRESERVEDWORD1206543922grantor0. 252 getGrantor(serverSession); 253 if (tempGrantor == null) { 254 throw new DException("DSE520", null); 255 } 256 if (_OptSNONRESERVEDWORD136444255SRESERVEDWORD1206543922grantor0. 257 _grantor0.toString().equalsIgnoreCase(SqlKeywords.CURRENT_ROLE)) { 258 TreeMap applicableRoleAuthos = GeneralUtility.getApplicableRoles(serverSession, tempGrantor); 259 applicableRoles = applicableRoleAuthos.keySet().toArray(); 260 } 261 } 262 return tempGrantor; 263 } 264 265 269 private void checkGrantorPrivileges(_ServerSession serverSession, 270 ArrayList listOfPrivileges, String grantor) throws 271 DException { 272 int len = applicableRoles == null ? 0 : applicableRoles.length; 273 Object [] allGrantees = new Object [1 + len]; 274 allGrantees[0] = grantor; 275 if (len != 0) { 276 System.arraycopy(applicableRoles, 0, allGrantees, 1, len); 277 } 278 for (int i = 0, size = listOfPrivileges.size(); i < size; i++) { 279 PrivilegeDescriptor pd = (PrivilegeDescriptor) listOfPrivileges.get(i); 280 if (pd instanceof TablePrivilegesDescriptor) { 281 TablePrivilegesDescriptor tpd = (TablePrivilegesDescriptor) pd; 282 String query = "select * from " + 283 SystemTables.table_privileges_TableName 284 + " where table_catalog = ? and table_schema = ? " 285 + " and table_name = ? and grantee IN (?) " 286 + " and privilege_type = ? and is_grantable='YES' "; 287 _Iterator iter = SqlSchemaConstants.getIterator(serverSession, query, 288 new Object [] {tpd.object_catalog, tpd.object_schema, 289 tpd.object_name, allGrantees, tpd.privilege_type}); 290 if (!iter.first()) { 291 throw new DException("DSE1209", null); 292 } 293 } else if (pd instanceof ColumnPrivilegeDescriptor) { 294 ColumnPrivilegeDescriptor cpd = (ColumnPrivilegeDescriptor) pd; 295 String query = "select * from " + 296 SystemTables.column_privileges_TableName 297 + " where table_catalog = ? and table_schema = ? " 298 + " and table_name = ? and grantee IN (?) " 299 + " and privilege_type = ? and is_grantable='YES' " 300 + " and column_name = ? "; 301 _Iterator iter = SqlSchemaConstants.getIterator(serverSession, query, 302 new Object [] {cpd.object_catalog, cpd.object_schema, 303 cpd.object_name, allGrantees, cpd.privilege_type, cpd.column_name}); 304 if (!iter.first()) { 305 throw new DException("DSE1209", null); 306 } 307 } else if (pd instanceof UsagePrivilegesDescriptor) { 308 UsagePrivilegesDescriptor upd = (UsagePrivilegesDescriptor) pd; 309 String query = "select * from " + 310 SystemTables.usage_privileges_TableName 311 + " where object_catalog = ? and object_schema = ? " 312 + " and object_name = ? and grantee IN (?) " 313 + " and object_type = ? and is_grantable='YES' "; 314 _Iterator iter = SqlSchemaConstants.getIterator(serverSession, query, 315 new Object [] {upd.object_catalog, upd.object_schema, 316 upd.object_name, allGrantees, upd.object_type}); 317 if (!iter.first()) { 318 throw new DException("DSE1209", null); 319 } 320 } else if (pd instanceof RoutinePrivilegeDescriptor) { 321 RoutinePrivilegeDescriptor rpd = (RoutinePrivilegeDescriptor) pd; 322 String query = "select * from " + 323 SystemTables.routine_privileges_TableName 324 + " where specific_catalog = ? and specific_schema = ? " 325 + " and specific_name = ? and grantee IN (?) " 326 + " and privilege_type = ? and is_grantable='YES' "; 327 _Iterator iter = SqlSchemaConstants.getIterator(serverSession, query, 328 new Object [] {rpd.object_catalog, rpd.object_schema, 329 rpd.object_name, allGrantees, rpd.privilege_type}); 330 if (!iter.first()) { 331 throw new DException("DSE1209", null); 332 } 333 } 334 } 335 } 336 337 public Object clone() throws CloneNotSupportedException { 338 return this; 339 } 340 341 public String toString() { 342 StringBuffer sb = new StringBuffer (); 343 sb.append(" "); 344 sb.append(_SRESERVEDWORD12065439227); 345 sb.append(" "); 346 sb.append(_privileges6); 347 sb.append(" "); 348 sb.append(_SRESERVEDWORD12065439225); 349 sb.append(" "); 350 sb.append(_grantee4); 351 sb.append(" "); 352 if (_OptRepScomma94843605grantee3 != null) { 353 for (int i = 0; i < _OptRepScomma94843605grantee3.length; i++) { 354 sb.append(",").append(_OptRepScomma94843605grantee3[i]); 355 } 356 } 357 sb.append(" "); 358 if (_Optdummyrule2 != null) { 359 sb.append(_Optdummyrule2); 360 } 361 sb.append(" "); 362 if (_OptSRESERVEDWORD1206543922SRESERVEDWORD1206543922SNONRESERVEDWORD1364442551 != null) { 363 sb.append(_OptSRESERVEDWORD1206543922SRESERVEDWORD1206543922SNONRESERVEDWORD1364442551); 364 } 365 sb.append(" "); 366 if (_OptSNONRESERVEDWORD136444255SRESERVEDWORD1206543922grantor0 != null) { 367 sb.append(_OptSNONRESERVEDWORD136444255SRESERVEDWORD1206543922grantor0); 368 } 369 return sb.toString(); 370 } 371 } 372 | Popular Tags |