1 21 22 package org.apache.derby.iapi.sql.dictionary; 23 24 import org.apache.derby.iapi.error.StandardException; 25 import org.apache.derby.catalog.UUID; 26 import org.apache.derby.iapi.sql.conn.Authorizer; 27 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 28 import org.apache.derby.iapi.reference.SQLState; 29 30 33 34 public class StatementTablePermission extends StatementPermission 35 { 36 UUID tableUUID; 37 int privType; 39 47 public StatementTablePermission(UUID tableUUID, int privType) 48 { 49 this.tableUUID = tableUUID; 50 this.privType = privType; 51 } 52 53 58 public int getPrivType() 59 { 60 return privType; 61 } 62 63 68 public UUID getTableUUID() 69 { 70 return tableUUID; 71 } 72 73 82 public boolean equals( Object obj) 83 { 84 if( obj == null) 85 return false; 86 if( getClass().equals( obj.getClass())) 87 { 88 StatementTablePermission other = (StatementTablePermission) obj; 89 return privType == other.privType && tableUUID.equals( other.tableUUID); 90 } 91 return false; 92 } 94 100 public int hashCode() 101 { 102 return privType + tableUUID.hashCode(); 103 } 104 105 108 public void check( LanguageConnectionContext lcc, 109 String authorizationId, 110 boolean forGrant) 111 throws StandardException 112 { 113 DataDictionary dd = lcc.getDataDictionary(); 114 115 if( ! hasPermissionOnTable( dd, authorizationId, forGrant)) 116 { 117 TableDescriptor td = getTableDescriptor( dd); 118 throw StandardException.newException( forGrant ? SQLState.AUTH_NO_TABLE_PERMISSION_FOR_GRANT 119 : SQLState.AUTH_NO_TABLE_PERMISSION, 120 authorizationId, 121 getPrivName(), 122 td.getSchemaName(), 123 td.getName()); 124 } 125 } 127 protected TableDescriptor getTableDescriptor(DataDictionary dd) throws StandardException 128 { 129 TableDescriptor td = dd.getTableDescriptor( tableUUID); 130 if( td == null) 131 throw StandardException.newException(SQLState.AUTH_INTERNAL_BAD_UUID, "table"); 132 return td; 133 } 135 138 protected boolean hasPermissionOnTable(DataDictionary dd, String authorizationId, boolean forGrant) 139 throws StandardException 140 { 141 return oneAuthHasPermissionOnTable( dd, Authorizer.PUBLIC_AUTHORIZATION_ID, forGrant) 142 || oneAuthHasPermissionOnTable( dd, authorizationId, forGrant); 143 } 144 145 protected boolean oneAuthHasPermissionOnTable(DataDictionary dd, String authorizationId, boolean forGrant) 146 throws StandardException 147 { 148 TablePermsDescriptor perms = dd.getTablePermissions( tableUUID, authorizationId); 149 if( perms == null) 150 return false; 151 152 String priv = null; 153 154 switch( privType) 155 { 156 case Authorizer.SELECT_PRIV: 157 priv = perms.getSelectPriv(); 158 break; 159 case Authorizer.UPDATE_PRIV: 160 priv = perms.getUpdatePriv(); 161 break; 162 case Authorizer.REFERENCES_PRIV: 163 priv = perms.getReferencesPriv(); 164 break; 165 case Authorizer.INSERT_PRIV: 166 priv = perms.getInsertPriv(); 167 break; 168 case Authorizer.DELETE_PRIV: 169 priv = perms.getDeletePriv(); 170 break; 171 case Authorizer.TRIGGER_PRIV: 172 priv = perms.getTriggerPriv(); 173 break; 174 } 175 176 return "Y".equals(priv) || (!forGrant) && "y".equals( priv); 177 } 179 182 public PermissionsDescriptor getPermissionDescriptor(String authid, DataDictionary dd) 183 throws StandardException 184 { 185 if (oneAuthHasPermissionOnTable( dd, authid, false)) 188 return dd.getTablePermissions(tableUUID, authid); 189 else return null; 190 } 191 192 197 public String getPrivName( ) 198 { 199 switch( privType) 200 { 201 case Authorizer.SELECT_PRIV: 202 return "select"; 203 case Authorizer.UPDATE_PRIV: 204 return "update"; 205 case Authorizer.REFERENCES_PRIV: 206 return "references"; 207 case Authorizer.INSERT_PRIV: 208 return "insert"; 209 case Authorizer.DELETE_PRIV: 210 return "delete"; 211 case Authorizer.TRIGGER_PRIV: 212 return "trigger"; 213 } 214 return "?"; 215 } } 217 | Popular Tags |