File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\engine\Right.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.engine;
8
9 import org.h2.message.DbException;
10 import org.h2.message.Trace;
11 import org.h2.table.Table;
12
13 /**
14 * An access right. Rights are regular database objects, but have generated
15 * names.
16 */
17 public class Right extends DbObjectBase {
18
19 /**
20 * The right bit mask that means: selecting from a table is allowed.
21 */
22 public static final int SELECT = 1;
23
24 /**
25 * The right bit mask that means: deleting rows from a table is allowed.
26 */
27 public static final int DELETE = 2;
28
29 /**
30 * The right bit mask that means: inserting rows into a table is allowed.
31 */
32 public static final int INSERT = 4;
33
34 /**
35 * The right bit mask that means: updating data is allowed.
36 */
37 public static final int UPDATE = 8;
38
39 /**
40 * The right bit mask that means: select, insert, update, delete, and update
41 * for this object is allowed.
42 */
43 public static final int ALL = SELECT | DELETE | INSERT | UPDATE;
44
45 private Role grantedRole;
46 private int grantedRight;
47 private Table grantedTable;
48 private RightOwner grantee;
49
50 public Right(Database db, int id, RightOwner grantee, Role grantedRole) {
51 initDbObjectBase(db, id, "RIGHT_"+id, Trace.USER);
52 this.grantee = grantee;
53 this.grantedRole = grantedRole;
54 }
55
56 public Right(Database db, int id, RightOwner grantee, int grantedRight, Table grantedRightOnTable) {
57 initDbObjectBase(db, id, "" + id, Trace.USER);
58 this.grantee = grantee;
59 this.grantedRight = grantedRight;
60 this.grantedTable = grantedRightOnTable;
61 }
62
63 private static boolean appendRight(StringBuilder buff, int right, int mask, String name, boolean comma) {
64 if ((right & mask) != 0) {
65 if (comma) {
66 buff.append(", ");
67 }
68 buff.append(name);
69 return true;
70 }
71 return comma;
72 }
73
74 public String getRights() {
75 StringBuilder buff = new StringBuilder();
76 if (grantedRight == ALL) {
77 buff.append("ALL");
78 } else {
79 boolean comma = false;
80 comma = appendRight(buff, grantedRight, SELECT, "SELECT", comma);
81 comma = appendRight(buff, grantedRight, DELETE, "DELETE", comma);
82 comma = appendRight(buff, grantedRight, INSERT, "INSERT", comma);
83 appendRight(buff, grantedRight, UPDATE, "UPDATE", comma);
84 }
85 return buff.toString();
86 }
87
88 public Role getGrantedRole() {
89 return grantedRole;
90 }
91
92 public Table getGrantedTable() {
93 return grantedTable;
94 }
95
96 public DbObject getGrantee() {
97 return grantee;
98 }
99
100 public String getDropSQL() {
101 return null;
102 }
103
104 public String getCreateSQLForCopy(Table table, String quotedName) {
105 StringBuilder buff = new StringBuilder();
106 buff.append("GRANT ");
107 if (grantedRole != null) {
108 buff.append(grantedRole.getSQL());
109 } else {
110 buff.append(getRights()).append(" ON ").append(table.getSQL());
111 }
112 buff.append(" TO ").append(grantee.getSQL());
113 return buff.toString();
114 }
115
116 public String getCreateSQL() {
117 return getCreateSQLForCopy(grantedTable, null);
118 }
119
120 public int getType() {
121 return DbObject.RIGHT;
122 }
123
124 public void removeChildrenAndResources(Session session) {
125 if (grantedTable != null) {
126 grantee.revokeRight(grantedTable);
127 } else {
128 grantee.revokeRole(grantedRole);
129 }
130 database.removeMeta(session, getId());
131 grantedRole = null;
132 grantedTable = null;
133 grantee = null;
134 invalidate();
135 }
136
137 public void checkRename() {
138 DbException.throwInternalError();
139 }
140
141 public void setRightMask(int rightMask) {
142 grantedRight = rightMask;
143 }
144
145 public int getRightMask() {
146 return grantedRight;
147 }
148
149 }
150