Debian Codegraph @

src/main/org/h2/command/ddl/GrantRevoke.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.command.ddl;
7
8 import java.util.ArrayList;
9
10 import org.h2.api.ErrorCode;
11 import org.h2.command.CommandInterface;
12 import org.h2.engine.Database;
13 import org.h2.engine.DbObject;
14 import org.h2.engine.Right;
15 import org.h2.engine.RightOwner;
16 import org.h2.engine.Role;
17 import org.h2.engine.SessionLocal;
18 import org.h2.engine.User;
19 import org.h2.message.DbException;
20 import org.h2.schema.Schema;
21 import org.h2.table.Table;
22 import org.h2.util.Utils;
23
24 /**
25 * This class represents the statements
26 * GRANT RIGHT,
27 * GRANT ROLE,
28 * REVOKE RIGHT,
29 * REVOKE ROLE
30 */
31 public class GrantRevoke extends DefineCommand {
32
33 private ArrayList<String> roleNames;
34 private int operationType;
35 private int rightMask;
36 private final ArrayList<Table> tables = Utils.newSmallArrayList();
37 private Schema schema;
38 private RightOwner grantee;
39
40 public GrantRevoke(SessionLocal session) {
41 super(session);
42 }
43
44 public void setOperationType(int operationType) {
45 this.operationType = operationType;
46 }
47
48 /**
49 * Add the specified right bit to the rights bitmap.
50 *
51 * @param right the right bit
52 */
53 public void addRight(int right) {
54 this.rightMask |= right;
55 }
56
57 /**
58 * Add the specified role to the list of roles.
59 *
60 * @param roleName the role
61 */
62 public void addRoleName(String roleName) {
63 if (roleNames == null) {
64 roleNames = Utils.newSmallArrayList();
65 }
66 roleNames.add(roleName);
67 }
68
69 public void setGranteeName(String granteeName) {
70 Database db = getDatabase();
71 grantee = db.findUserOrRole(granteeName);
72 if (grantee == null) {
73 throw DbException.get(ErrorCode.USER_OR_ROLE_NOT_FOUND_1, granteeName);
74 }
75 }
76
77 @Override
78 public long update() {
79 Database db = getDatabase();
80 User user = session.getUser();
81 if (roleNames != null) {
82 user.checkAdmin();
83 for (String name : roleNames) {
84 Role grantedRole = db.findRole(name);
85 if (grantedRole == null) {
86 throw DbException.get(ErrorCode.ROLE_NOT_FOUND_1, name);
87 }
88 if (operationType == CommandInterface.GRANT) {
89 grantRole(grantedRole);
90 } else if (operationType == CommandInterface.REVOKE) {
91 revokeRole(grantedRole);
92 } else {
93 throw DbException.getInternalError("type=" + operationType);
94 }
95 }
96 } else {
97 if ((rightMask & Right.ALTER_ANY_SCHEMA) != 0) {
98 user.checkAdmin();
99 } else {
100 if (schema != null) {
101 user.checkSchemaOwner(schema);
102 }
103 for (Table table : tables) {
104 user.checkSchemaOwner(table.getSchema());
105 }
106 }
107 if (operationType == CommandInterface.GRANT) {
108 grantRight();
109 } else if (operationType == CommandInterface.REVOKE) {
110 revokeRight();
111 } else {
112 throw DbException.getInternalError("type=" + operationType);
113 }
114 }
115 return 0;
116 }
117
118 private void grantRight() {
119 if (schema != null) {
120 grantRight(schema);
121 }
122 for (Table table : tables) {
123 grantRight(table);
124 }
125 }
126
127 private void grantRight(DbObject object) {
128 Database db = getDatabase();
129 Right right = grantee.getRightForObject(object);
130 if (right == null) {
131 int id = getPersistedObjectId();
132 if (id == 0) {
133 id = getDatabase().allocateObjectId();
134 }
135 right = new Right(db, id, grantee, rightMask, object);
136 grantee.grantRight(object, right);
137 db.addDatabaseObject(session, right);
138 } else {
139 right.setRightMask(right.getRightMask() | rightMask);
140 db.updateMeta(session, right);
141 }
142 }
143
144 private void grantRole(Role grantedRole) {
145 if (grantedRole != grantee && grantee.isRoleGranted(grantedRole)) {
146 return;
147 }
148 if (grantee instanceof Role) {
149 Role granteeRole = (Role) grantee;
150 if (grantedRole.isRoleGranted(granteeRole)) {
151 // cyclic role grants are not allowed
152 throw DbException.get(ErrorCode.ROLE_ALREADY_GRANTED_1, grantedRole.getTraceSQL());
153 }
154 }
155 Database db = getDatabase();
156 int id = getObjectId();
157 Right right = new Right(db, id, grantee, grantedRole);
158 db.addDatabaseObject(session, right);
159 grantee.grantRole(grantedRole, right);
160 }
161
162 private void revokeRight() {
163 if (schema != null) {
164 revokeRight(schema);
165 }
166 for (Table table : tables) {
167 revokeRight(table);
168 }
169 }
170
171 private void revokeRight(DbObject object) {
172 Right right = grantee.getRightForObject(object);
173 if (right == null) {
174 return;
175 }
176 int mask = right.getRightMask();
177 int newRight = mask & ~rightMask;
178 Database db = getDatabase();
179 if (newRight == 0) {
180 db.removeDatabaseObject(session, right);
181 } else {
182 right.setRightMask(newRight);
183 db.updateMeta(session, right);
184 }
185 }
186
187
188 private void revokeRole(Role grantedRole) {
189 Right right = grantee.getRightForRole(grantedRole);
190 if (right == null) {
191 return;
192 }
193 Database db = getDatabase();
194 db.removeDatabaseObject(session, right);
195 }
196
197 @Override
198 public boolean isTransactional() {
199 return false;
200 }
201
202 /**
203 * Add the specified table to the list of tables.
204 *
205 * @param table the table
206 */
207 public void addTable(Table table) {
208 tables.add(table);
209 }
210
211 /**
212 * Set the specified schema
213 *
214 * @param schema the schema
215 */
216 public void setSchema(Schema schema) {
217 this.schema = schema;
218 }
219
220 @Override
221 public int getType() {
222 return operationType;
223 }
224
225 }