1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 import org.apache.derby.iapi.services.io.FormatableBitSet; 26 import org.apache.derby.iapi.sql.Activation; 27 import org.apache.derby.iapi.error.StandardException; 28 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 29 import org.apache.derby.iapi.store.access.TransactionController; 30 import org.apache.derby.iapi.sql.depend.DependencyManager; 31 import org.apache.derby.iapi.sql.dictionary.AliasDescriptor; 32 import org.apache.derby.iapi.sql.dictionary.PermissionsDescriptor; 33 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 34 import org.apache.derby.iapi.sql.dictionary.TablePermsDescriptor; 35 import org.apache.derby.iapi.sql.dictionary.ColPermsDescriptor; 36 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 37 import org.apache.derby.iapi.sql.dictionary.ViewDescriptor; 38 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 39 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 40 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor; 41 import org.apache.derby.iapi.reference.SQLState; 42 43 import java.util.List ; 44 import java.util.Iterator ; 45 46 public class TablePrivilegeInfo extends PrivilegeInfo 47 { 48 public static final int SELECT_ACTION = 0; 50 public static final int DELETE_ACTION = 1; 51 public static final int INSERT_ACTION = 2; 52 public static final int UPDATE_ACTION = 3; 53 public static final int REFERENCES_ACTION = 4; 54 public static final int TRIGGER_ACTION = 5; 55 public static final int ACTION_COUNT = 6; 56 57 private static final String YES_WITH_GRANT_OPTION = "Y"; 58 private static final String YES_WITHOUT_GRANT_OPTION = "y"; 59 private static final String NO = "N"; 60 61 private static final String [][] actionString = 62 {{"s", "S"}, {"d", "D"}, {"i", "I"}, {"u", "U"}, {"r", "R"}, {"t", "T"}}; 63 64 private TableDescriptor td; 65 private boolean[] actionAllowed; 66 private FormatableBitSet[] columnBitSets; 67 private List descriptorList; 68 69 72 public TablePrivilegeInfo( TableDescriptor td, 73 boolean[] actionAllowed, 74 FormatableBitSet[] columnBitSets, 75 List descriptorList) 76 { 77 this.actionAllowed = actionAllowed; 78 this.columnBitSets = columnBitSets; 79 this.td = td; 80 this.descriptorList = descriptorList; 81 } 82 83 98 protected void checkOwnership( String user, 99 TableDescriptor td, 100 SchemaDescriptor sd, 101 DataDictionary dd, 102 LanguageConnectionContext lcc, 103 boolean grant) 104 throws StandardException 105 { 106 super.checkOwnership(user, td, sd, dd); 107 108 if (grant) 110 { 111 checkPrivileges(user, td, sd, dd, lcc); 112 } 113 } 114 115 131 private void checkPrivileges( String user, 132 TableDescriptor td, 133 SchemaDescriptor sd, 134 DataDictionary dd, 135 LanguageConnectionContext lcc) 136 throws StandardException 137 { 138 if (user.equals(dd.getAuthorizationDatabaseOwner())) return; 139 140 if (td.getTableType() == TableDescriptor.VIEW_TYPE) 142 { 143 if (descriptorList != null ) 144 { 145 TransactionController tc = lcc.getTransactionExecute(); 146 int siz = descriptorList.size(); 147 for (int i=0; i < siz; i++) 148 { 149 TupleDescriptor p; 150 SchemaDescriptor s = null; 151 152 p = (TupleDescriptor)descriptorList.get(i); 153 if (p instanceof TableDescriptor) 154 { 155 TableDescriptor t = (TableDescriptor)p; 156 s = t.getSchemaDescriptor(); 157 } 158 else if (p instanceof ViewDescriptor) 159 { 160 ViewDescriptor v = (ViewDescriptor)p; 161 s = dd.getSchemaDescriptor(v.getCompSchemaId(), tc); 162 } 163 else if (p instanceof AliasDescriptor) 164 { 165 AliasDescriptor a = (AliasDescriptor)p; 166 s = dd.getSchemaDescriptor( a.getSchemaUUID(), tc); 167 } 168 169 if (s != null && !user.equals(s.getAuthorizationId()) ) 170 { 171 throw StandardException.newException( 172 SQLState.AUTH_NO_OBJECT_PERMISSION, 173 user, 174 "grant", 175 sd.getSchemaName(), 176 td.getName()); 177 } 178 179 } 182 } 183 } 184 } 185 186 195 public void executeGrantRevoke( Activation activation, 196 boolean grant, 197 List grantees) 198 throws StandardException 199 { 200 LanguageConnectionContext lcc = activation.getLanguageConnectionContext(); 201 DataDictionary dd = lcc.getDataDictionary(); 202 String currentUser = lcc.getAuthorizationId(); 203 TransactionController tc = lcc.getTransactionExecute(); 204 SchemaDescriptor sd = td.getSchemaDescriptor(); 205 206 checkOwnership( currentUser, td, sd, dd, lcc, grant); 208 209 DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator(); 210 211 TablePermsDescriptor tablePermsDesc = 212 ddg.newTablePermsDescriptor( td, 213 getPermString( SELECT_ACTION, false), 214 getPermString( DELETE_ACTION, false), 215 getPermString( INSERT_ACTION, false), 216 getPermString( UPDATE_ACTION, false), 217 getPermString( REFERENCES_ACTION, false), 218 getPermString( TRIGGER_ACTION, false), 219 currentUser); 220 221 ColPermsDescriptor[] colPermsDescs = new ColPermsDescriptor[ columnBitSets.length]; 222 for( int i = 0; i < columnBitSets.length; i++) 223 { 224 if( columnBitSets[i] != null || 225 (!grant) && hasColumnPermissions(i) && actionAllowed[i] 229 ) 230 { 231 colPermsDescs[i] = ddg.newColPermsDescriptor( td, 232 getActionString(i, false), 233 columnBitSets[i], 234 currentUser); 235 } 236 } 237 238 239 dd.startWriting(lcc); 240 for( Iterator itr = grantees.iterator(); itr.hasNext();) 242 { 243 boolean privileges_revoked = false; 252 253 String grantee = (String ) itr.next(); 254 if( tablePermsDesc != null) 255 { 256 if (dd.addRemovePermissionsDescriptor( grant, tablePermsDesc, grantee, tc)) 257 { 258 privileges_revoked = true; 259 dd.getDependencyManager().invalidateFor(tablePermsDesc, DependencyManager.REVOKE_PRIVILEGE, lcc); 260 } 261 } 262 for( int i = 0; i < columnBitSets.length; i++) 263 { 264 if( colPermsDescs[i] != null) 265 { 266 if (dd.addRemovePermissionsDescriptor( grant, colPermsDescs[i], grantee, tc)) 267 { 268 privileges_revoked = true; 269 dd.getDependencyManager().invalidateFor(colPermsDescs[i], DependencyManager.REVOKE_PRIVILEGE, lcc); 270 } 271 } 272 } 273 274 addWarningIfPrivilegeNotRevoked(activation, grant, privileges_revoked, grantee); 275 } 276 } 278 private String getPermString( int action, boolean forGrantOption) 279 { 280 if( actionAllowed[ action] && columnBitSets[action] == null) 281 return forGrantOption ? YES_WITH_GRANT_OPTION : YES_WITHOUT_GRANT_OPTION; 282 else 283 return NO; 284 } 286 private String getActionString( int action, boolean forGrantOption) 287 { 288 return actionString[action][forGrantOption ? 1 : 0]; 289 } 290 291 private boolean hasColumnPermissions( int action) 292 { 293 return action == SELECT_ACTION || action == UPDATE_ACTION || action == REFERENCES_ACTION; 294 } 295 } 296 | Popular Tags |