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:
010: import org.h2.message.Message;
011: import org.h2.message.Trace;
012: import org.h2.table.Table;
013:
014: /**
015: * An access right. Rights are regular database objects, but have generated
016: * names.
017: */
018: public class Right extends DbObjectBase {
019:
020: public static final int SELECT = 1, DELETE = 2, INSERT = 4,
021: UPDATE = 8, ALL = 15;
022:
023: private Role grantedRole;
024: private int grantedRight;
025: private Table grantedTable;
026: private RightOwner grantee;
027:
028: public Right(Database db, int id, RightOwner grantee,
029: Role grantedRole) {
030: super (db, id, "RIGHT_" + id, Trace.USER);
031: this .grantee = grantee;
032: this .grantedRole = grantedRole;
033: }
034:
035: public Right(Database db, int id, RightOwner grantee,
036: int grantedRight, Table grantedRightOnTable) {
037: super (db, id, "" + id, Trace.USER);
038: this .grantee = grantee;
039: this .grantedRight = grantedRight;
040: this .grantedTable = grantedRightOnTable;
041: }
042:
043: private boolean appendRight(StringBuffer buff, int right, int mask,
044: String name, boolean comma) {
045: if ((right & mask) != 0) {
046: if (comma) {
047: buff.append(", ");
048: }
049: buff.append(name);
050: return true;
051: }
052: return comma;
053: }
054:
055: public String getRights() {
056: StringBuffer buff = new StringBuffer();
057: if (grantedRight == ALL) {
058: buff.append("ALL");
059: } else {
060: boolean comma = false;
061: comma = appendRight(buff, grantedRight, SELECT, "SELECT",
062: comma);
063: comma = appendRight(buff, grantedRight, DELETE, "DELETE",
064: comma);
065: comma = appendRight(buff, grantedRight, INSERT, "INSERT",
066: comma);
067: appendRight(buff, grantedRight, UPDATE, "UPDATE", comma);
068: }
069: return buff.toString();
070: }
071:
072: public Role getGrantedRole() {
073: return grantedRole;
074: }
075:
076: public Table getGrantedTable() {
077: return grantedTable;
078: }
079:
080: public DbObject getGrantee() {
081: return grantee;
082: }
083:
084: public String getDropSQL() {
085: return null;
086: }
087:
088: public String getCreateSQLForCopy(Table table, String quotedName) {
089: StringBuffer buff = new StringBuffer();
090: buff.append("GRANT ");
091: if (grantedRole != null) {
092: buff.append(grantedRole.getSQL());
093: } else {
094: buff.append(getRights());
095: buff.append(" ON ");
096: buff.append(table.getSQL());
097: }
098: buff.append(" TO ");
099: // TODO rights: need role 'PUBLIC'
100: buff.append(grantee.getSQL());
101: return buff.toString();
102: }
103:
104: public String getCreateSQL() {
105: return getCreateSQLForCopy(grantedTable, null);
106: }
107:
108: public int getType() {
109: return DbObject.RIGHT;
110: }
111:
112: public void removeChildrenAndResources(Session session)
113: throws SQLException {
114: if (grantedTable != null) {
115: grantee.revokeRight(grantedTable);
116: } else {
117: grantee.revokeRole(session, grantedRole);
118: }
119: database.removeMeta(session, getId());
120: grantedRole = null;
121: grantedTable = null;
122: grantee = null;
123: invalidate();
124: }
125:
126: public void checkRename() throws SQLException {
127: throw Message.getInternalError();
128: }
129:
130: public void setRightMask(int rightMask) {
131: grantedRight = rightMask;
132: }
133:
134: public int getRightMask() {
135: return grantedRight;
136: }
137:
138: }
|