Debian Codegraph @

src/main/org/h2/engine/Right.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.engine;
7
8 import org.h2.message.DbException;
9 import org.h2.message.Trace;
10 import org.h2.schema.Schema;
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 final class Right extends DbObject {
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: create/alter/drop schema is allowed.
41 */
42 public static final int ALTER_ANY_SCHEMA = 16;
43
44 /**
45 * The right bit mask that means: user is a schema owner. This mask isn't
46 * used in GRANT / REVOKE statements.
47 */
48 public static final int SCHEMA_OWNER = 32;
49
50 /**
51 * The right bit mask that means: select, insert, update, delete, and update
52 * for this object is allowed.
53 */
54 public static final int ALL = SELECT | DELETE | INSERT | UPDATE;
55
56 /**
57 * To whom the right is granted.
58 */
59 private RightOwner grantee;
60
61 /**
62 * The granted role, or null if a right was granted.
63 */
64 private Role grantedRole;
65
66 /**
67 * The granted right.
68 */
69 private int grantedRight;
70
71 /**
72 * The object. If the right is global, this is null.
73 */
74 private DbObject grantedObject;
75
76 public Right(Database db, int id, RightOwner grantee, Role grantedRole) {
77 super(db, id, "RIGHT_" + id, Trace.USER);
78 this.grantee = grantee;
79 this.grantedRole = grantedRole;
80 }
81
82 public Right(Database db, int id, RightOwner grantee, int grantedRight, DbObject grantedObject) {
83 super(db, id, Integer.toString(id), Trace.USER);
84 this.grantee = grantee;
85 this.grantedRight = grantedRight;
86 this.grantedObject = grantedObject;
87 }
88
89 private static boolean appendRight(StringBuilder buff, int right, int mask, String name, boolean comma) {
90 if ((right & mask) != 0) {
91 if (comma) {
92 buff.append(", ");
93 }
94 buff.append(name);
95 return true;
96 }
97 return comma;
98 }
99
100 public String getRights() {
101 StringBuilder buff = new StringBuilder();
102 if (grantedRight == ALL) {
103 buff.append("ALL");
104 } else {
105 boolean comma = false;
106 comma = appendRight(buff, grantedRight, SELECT, "SELECT", comma);
107 comma = appendRight(buff, grantedRight, DELETE, "DELETE", comma);
108 comma = appendRight(buff, grantedRight, INSERT, "INSERT", comma);
109 comma = appendRight(buff, grantedRight, UPDATE, "UPDATE", comma);
110 appendRight(buff, grantedRight, ALTER_ANY_SCHEMA, "ALTER ANY SCHEMA", comma);
111 }
112 return buff.toString();
113 }
114
115 public Role getGrantedRole() {
116 return grantedRole;
117 }
118
119 public DbObject getGrantedObject() {
120 return grantedObject;
121 }
122
123 public DbObject getGrantee() {
124 return grantee;
125 }
126
127 @Override
128 public String getCreateSQLForCopy(Table table, String quotedName) {
129 return getCreateSQLForCopy(table);
130 }
131
132 private String getCreateSQLForCopy(DbObject object) {
133 StringBuilder builder = new StringBuilder();
134 builder.append("GRANT ");
135 if (grantedRole != null) {
136 grantedRole.getSQL(builder, DEFAULT_SQL_FLAGS);
137 } else {
138 builder.append(getRights());
139 if (object != null) {
140 if (object instanceof Schema) {
141 builder.append(" ON SCHEMA ");
142 object.getSQL(builder, DEFAULT_SQL_FLAGS);
143 } else if (object instanceof Table) {
144 builder.append(" ON ");
145 object.getSQL(builder, DEFAULT_SQL_FLAGS);
146 }
147 }
148 }
149 builder.append(" TO ");
150 grantee.getSQL(builder, DEFAULT_SQL_FLAGS);
151 return builder.toString();
152 }
153
154 @Override
155 public String getCreateSQL() {
156 return getCreateSQLForCopy(grantedObject);
157 }
158
159 @Override
160 public int getType() {
161 return DbObject.RIGHT;
162 }
163
164 @Override
165 public void removeChildrenAndResources(SessionLocal session) {
166 if (grantedRole != null) {
167 grantee.revokeRole(grantedRole);
168 } else {
169 grantee.revokeRight(grantedObject);
170 }
171 database.removeMeta(session, getId());
172 grantedRole = null;
173 grantedObject = null;
174 grantee = null;
175 invalidate();
176 }
177
178 @Override
179 public void checkRename() {
180 throw DbException.getInternalError();
181 }
182
183 public void setRightMask(int rightMask) {
184 grantedRight = rightMask;
185 }
186
187 public int getRightMask() {
188 return grantedRight;
189 }
190
191 }