001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.engine;
007:
008: import java.sql.SQLException;
009: import java.util.HashMap;
010: import java.util.Iterator;
011:
012: import org.h2.constant.ErrorCode;
013: import org.h2.message.Message;
014: import org.h2.table.Table;
015:
016: /**
017: * A right owner (sometimes called principal).
018: */
019: public abstract class RightOwner extends DbObjectBase {
020:
021: /**
022: * The map of granted roles.
023: * The key is the role,
024: * and the value is the right.
025: */
026: private HashMap grantedRoles;
027:
028: /**
029: * The map of granted rights.
030: * The key is the table,
031: * and the value is the right.
032: */
033: private HashMap grantedRights;
034:
035: protected RightOwner(Database database, int id, String name,
036: String traceModule) {
037: super (database, id, name, traceModule);
038: }
039:
040: public boolean isRoleGranted(Role grantedRole) {
041: if (grantedRole == this ) {
042: return true;
043: }
044: if (grantedRoles != null) {
045: Iterator it = grantedRoles.keySet().iterator();
046: while (it.hasNext()) {
047: Role role = (Role) it.next();
048: if (role == grantedRole) {
049: return true;
050: }
051: if (role.isRoleGranted(grantedRole)) {
052: return true;
053: }
054: }
055: }
056: return false;
057: }
058:
059: protected boolean isRightGrantedRecursive(Table table, int rightMask) {
060: Right right;
061: if (grantedRights != null) {
062: right = (Right) grantedRights.get(table);
063: if (right != null) {
064: if ((right.getRightMask() & rightMask) == rightMask) {
065: return true;
066: }
067: }
068: }
069: if (grantedRoles != null) {
070: Iterator it = grantedRoles.keySet().iterator();
071: while (it.hasNext()) {
072: RightOwner role = (RightOwner) it.next();
073: if (role.isRightGrantedRecursive(table, rightMask)) {
074: return true;
075: }
076: }
077: }
078: return false;
079: }
080:
081: public void grantRight(Table table, Right right) {
082: if (grantedRights == null) {
083: grantedRights = new HashMap();
084: }
085: grantedRights.put(table, right);
086: }
087:
088: public void revokeRight(Table table) {
089: if (grantedRights == null) {
090: return;
091: }
092: grantedRights.remove(table);
093: if (grantedRights.size() == 0) {
094: grantedRights = null;
095: }
096: }
097:
098: /**
099: * Grant a role to this object.
100: *
101: * @param session the session
102: * @param role the role
103: * @param right the right to grant
104: */
105: public void grantRole(Session session, Role role, Right right) {
106: if (grantedRoles == null) {
107: grantedRoles = new HashMap();
108: }
109: grantedRoles.put(role, right);
110: }
111:
112: public void revokeRole(Session session, Role role)
113: throws SQLException {
114: if (grantedRoles == null) {
115: throw Message.getSQLException(ErrorCode.RIGHT_NOT_FOUND);
116: }
117: Right right = (Right) grantedRoles.get(role);
118: if (right == null) {
119: throw Message.getSQLException(ErrorCode.RIGHT_NOT_FOUND);
120: }
121: grantedRoles.remove(role);
122: if (grantedRoles.size() == 0) {
123: grantedRoles = null;
124: }
125: }
126:
127: public Right getRightForTable(Table table) {
128: if (grantedRights == null) {
129: return null;
130: }
131: return (Right) grantedRights.get(table);
132: }
133:
134: public Right getRightForRole(Role role) {
135: if (grantedRoles == null) {
136: return null;
137: }
138: return (Right) grantedRoles.get(role);
139: }
140:
141: }
|