1 30 31 32 package org.hsqldb; 33 34 import org.hsqldb.HsqlNameManager.HsqlName; 35 import org.hsqldb.lib.HashSet; 36 import org.hsqldb.lib.IntValueHashMap; 37 import org.hsqldb.lib.Iterator; 38 import org.hsqldb.lib.Set; 39 40 68 public class Grantee { 69 70 boolean isRole; 71 72 76 private boolean isAdminDirect = false; 77 78 79 private boolean isAdmin = false; 80 81 82 private IntValueHashMap fullRightsMap = new IntValueHashMap(); 83 84 87 private String granteeName; 88 89 90 private IntValueHashMap rightsMap; 91 92 93 HashSet roles = new HashSet(); 94 95 101 private Grantee pubGrantee; 102 103 104 private GranteeManager granteeManager; 105 106 113 Grantee(String name, Grantee inGrantee, 114 GranteeManager man) throws HsqlException { 115 116 rightsMap = new IntValueHashMap(); 117 granteeName = name; 118 granteeManager = man; 119 pubGrantee = inGrantee; 120 } 121 122 String getName() { 123 return granteeName; 124 } 125 126 144 IntValueHashMap getRights() { 145 146 return rightsMap; 148 } 149 150 153 public void grant(String role) throws HsqlException { 154 roles.add(role); 155 } 156 157 160 public void revoke(String role) throws HsqlException { 161 162 if (!hasRoleDirect(role)) { 163 throw Trace.error(Trace.DONT_HAVE_ROLE, role); 164 } 165 166 roles.remove(role); 167 } 168 169 172 public HashSet getDirectRoles() { 173 return roles; 174 } 175 176 String getDirectRolesString() { 177 return setToString(roles); 178 } 179 180 String getAllRolesString() { 181 return setToString(getAllRoles()); 182 } 183 184 public String setToString(Set set) { 185 186 Iterator it = set.iterator(); 189 StringBuffer sb = new StringBuffer (); 190 191 while (it.hasNext()) { 192 if (sb.length() > 0) { 193 sb.append(','); 194 } 195 196 sb.append(it.next()); 197 } 198 199 return sb.toString(); 200 } 201 202 205 public HashSet getAllRoles() { 206 207 HashSet newSet = new HashSet(); 208 209 addGranteeAndRoles(newSet); 210 211 newSet.remove(granteeName); 213 214 return newSet; 215 } 216 217 222 private HashSet addGranteeAndRoles(HashSet set) { 223 224 String candidateRole; 225 226 set.add(granteeName); 227 228 Iterator it = roles.iterator(); 229 230 while (it.hasNext()) { 231 candidateRole = (String ) it.next(); 232 233 if (!set.contains(candidateRole)) { 234 try { 235 granteeManager.getRole(candidateRole).addGranteeAndRoles( 236 set); 237 } catch (HsqlException he) { 238 throw new RuntimeException (he.getMessage()); 239 } 240 } 241 } 242 243 return set; 244 } 245 246 public boolean hasRoleDirect(String role) { 247 return roles.contains(role); 248 } 249 250 public boolean hasRole(String role) { 251 return getAllRoles().contains(role); 252 } 253 254 public String allRolesString() { 255 256 HashSet allRoles = getAllRoles(); 257 258 if (allRoles.size() < 1) { 259 return null; 260 } 261 262 Iterator it = getAllRoles().iterator(); 263 StringBuffer sb = new StringBuffer (); 264 265 while (it.hasNext()) { 266 if (sb.length() > 0) { 267 sb.append(','); 268 } 269 270 sb.append((String ) it.next()); 271 } 272 273 return sb.toString(); 274 } 275 276 282 void grant(Object dbobject, int rights) { 283 284 if (rights == 0) { 285 return; 286 } 287 288 int n = rightsMap.get(dbobject, 0); 289 290 n |= rights; 291 292 rightsMap.put(dbobject, n); 293 } 294 295 302 void revoke(Object dbobject, int rights) { 303 304 if (rights == 0) { 305 return; 306 } 307 308 int n = rightsMap.get(dbobject, 0); 309 310 if (n == 0) { 311 return; 312 } 313 314 rights = n & (GranteeManager.ALL - rights); 315 316 if (rights == 0) { 317 rightsMap.remove(dbobject); 318 } else { 319 rightsMap.put(dbobject, rights); 320 } 321 } 322 323 328 void revokeDbObject(Object dbobject) { 329 rightsMap.remove(dbobject); 330 fullRightsMap.remove(dbobject); 331 } 332 333 337 void clearPrivileges() { 338 339 roles.clear(); 340 rightsMap.clear(); 341 fullRightsMap.clear(); 342 343 isAdminDirect = false; 344 } 345 346 354 void check(HsqlName dbobject, int rights) throws HsqlException { 355 356 if (!isAccessible(dbobject, rights)) { 357 throw Trace.error(Trace.ACCESS_IS_DENIED); 358 } 359 } 360 361 void check(String dbobject) throws HsqlException { 362 363 if (!isAccessible(dbobject)) { 364 throw Trace.error(Trace.ACCESS_IS_DENIED); 365 } 366 } 367 368 379 boolean isAccessible(HsqlName dbObject, int rights) throws HsqlException { 380 381 if (isAdmin) { 382 return true; 383 } 384 385 if (pubGrantee != null && pubGrantee.isAccessible(dbObject, rights)) { 386 return true; 387 } 388 389 int n = fullRightsMap.get(dbObject, 0); 390 391 if (n != 0) { 392 return (n & rights) != 0; 393 } 394 395 return false; 396 } 397 398 402 boolean isAccessible(String functionName) throws HsqlException { 403 404 if (functionName.startsWith("org.hsqldb.Library") 405 || functionName.startsWith("java.lang.Math")) { 406 return true; 407 } 408 409 if (isAdmin) { 410 return true; 411 } 412 413 if (pubGrantee != null && pubGrantee.isAccessible(functionName)) { 414 return true; 415 } 416 417 int n = fullRightsMap.get(functionName, 0); 418 419 return n != 0; 420 } 421 422 434 protected boolean isDirectlyAccessible(Object dbObject, 435 int rights) throws HsqlException { 436 437 int n = rightsMap.get(dbObject, 0); 438 439 if (n != 0) { 440 return (n & rights) != 0; 441 } 442 443 return false; 444 } 445 446 450 boolean isAccessible(HsqlName dbObject) throws HsqlException { 451 return isAccessible(dbObject, GranteeManager.ALL); 452 } 453 454 458 void checkAdmin() throws HsqlException { 459 460 if (!isAdmin()) { 461 throw Trace.error(Trace.ACCESS_IS_DENIED); 462 } 463 } 464 465 469 boolean isAdmin() { 470 return isAdmin; 471 } 472 473 479 boolean isAdminDirect() { 480 return isAdminDirect; 481 } 482 483 496 HashSet getGrantedClassNames(boolean andToPublic) throws HsqlException { 497 498 IntValueHashMap rights; 499 Object key; 500 int right; 501 Iterator i; 502 503 rights = rightsMap; 504 505 HashSet out = getGrantedClassNamesDirect(); 506 507 if (andToPublic && pubGrantee != null) { 508 rights = pubGrantee.rightsMap; 509 i = rights.keySet().iterator(); 510 511 while (i.hasNext()) { 512 key = i.next(); 513 514 if (key instanceof String ) { 515 right = rights.get(key, 0); 516 517 if (right == GranteeManager.ALL) { 518 out.add(key); 519 } 520 } 521 } 522 } 523 524 Iterator it = getAllRoles().iterator(); 525 526 while (it.hasNext()) { 527 out.addAll( 528 ((Grantee) granteeManager.getRole( 529 (String ) it.next())).getGrantedClassNamesDirect()); 530 } 531 532 return out; 533 } 534 535 546 HashSet getGrantedClassNamesDirect() throws HsqlException { 547 548 IntValueHashMap rights; 549 HashSet out; 550 Object key; 551 int right; 552 Iterator i; 553 554 rights = rightsMap; 555 out = new HashSet(); 556 i = rightsMap.keySet().iterator(); 557 558 while (i.hasNext()) { 559 key = i.next(); 560 561 if (key instanceof String ) { 562 right = rights.get(key, 0); 563 564 if (right == GranteeManager.ALL) { 565 out.add(key); 566 } 567 } 568 } 569 570 return out; 571 } 572 573 584 String [] listGrantedTablePrivileges(HsqlName name) { 585 return GranteeManager.getRightsArray(rightsMap.get(name, 0)); 586 } 587 588 592 void setAdminDirect() { 593 isAdmin = isAdminDirect = true; 594 } 595 596 624 boolean updateNestedRoles(String role) { 625 626 boolean hasNested = false; 627 boolean isSelf = role.equals(granteeName); 628 629 if (!isSelf) { 630 Iterator it = roles.iterator(); 631 632 while (it.hasNext()) { 633 String roleName = (String ) it.next(); 634 635 try { 636 Grantee currentRole = granteeManager.getRole(roleName); 637 638 hasNested |= currentRole.updateNestedRoles(role); 639 } catch (HsqlException e) {} 640 } 641 } 642 643 if (hasNested) { 644 updateAllRights(); 645 } 646 647 return hasNested || isSelf; 648 } 649 650 655 void updateAllRights() { 656 657 fullRightsMap.clear(); 658 659 isAdmin = isAdminDirect; 660 661 Iterator it = roles.iterator(); 662 663 while (it.hasNext()) { 664 String roleName = (String ) it.next(); 665 666 try { 667 Grantee currentRole = granteeManager.getRole(roleName); 668 669 fullRightsMap.putAll(currentRole.fullRightsMap); 670 671 isAdmin |= currentRole.isAdmin(); 672 } catch (HsqlException e) {} 673 } 674 675 fullRightsMap.putAll(rightsMap); 676 } 677 } 678 | Popular Tags |