File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\GrantRevoke.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.command.ddl;
8
9 import java.util.ArrayList;
10 import org.h2.command.CommandInterface;
11 import org.h2.constant.ErrorCode;
12 import org.h2.engine.Database;
13 import org.h2.engine.Right;
14 import org.h2.engine.RightOwner;
15 import org.h2.engine.Role;
16 import org.h2.engine.Session;
17 import org.h2.message.DbException;
18 import org.h2.table.Table;
19 import org.h2.util.New;
20
21 /**
22 * This class represents the statements
23 * GRANT RIGHT,
24 * GRANT ROLE,
25 * REVOKE RIGHT,
26 * REVOKE ROLE
27 */
28 public class GrantRevoke extends DefineCommand {
29
30 private ArrayList<String> roleNames;
31 private int operationType;
32 private int rightMask;
33 private final ArrayList<Table> tables = New.arrayList();
34 private RightOwner grantee;
35
36 public GrantRevoke(Session session) {
37 super(session);
38 }
39
40 public void setOperationType(int operationType) {
41 this.operationType = operationType;
42 }
43
44 /**
45 * Add the specified right bit to the rights bitmap.
46 *
47 * @param right the right bit
48 */
49 public void addRight(int right) {
50 this.rightMask |= right;
51 }
52
53 /**
54 * Add the specified role to the list of roles.
55 *
56 * @param roleName the role
57 */
58 public void addRoleName(String roleName) {
59 if (roleNames == null) {
60 roleNames = New.arrayList();
61 }
62 roleNames.add(roleName);
63 }
64
65 public void setGranteeName(String granteeName) {
66 Database db = session.getDatabase();
67 grantee = db.findUser(granteeName);
68 if (grantee == null) {
69 grantee = db.findRole(granteeName);
70 if (grantee == null) {
71 throw DbException.get(ErrorCode.USER_OR_ROLE_NOT_FOUND_1, granteeName);
72 }
73 }
74 }
75
76 public int update() {
77 session.getUser().checkAdmin();
78 session.commit(true);
79 Database db = session.getDatabase();
80 if (roleNames != null) {
81 for (String name : roleNames) {
82 Role grantedRole = db.findRole(name);
83 if (grantedRole == null) {
84 throw DbException.get(ErrorCode.ROLE_NOT_FOUND_1, name);
85 }
86 if (operationType == CommandInterface.GRANT) {
87 grantRole(grantedRole);
88 } else if (operationType == CommandInterface.REVOKE) {
89 revokeRole(grantedRole);
90 } else {
91 DbException.throwInternalError("type=" + operationType);
92 }
93 }
94 } else {
95 if (operationType == CommandInterface.GRANT) {
96 grantRight();
97 } else if (operationType == CommandInterface.REVOKE) {
98 revokeRight();
99 } else {
100 DbException.throwInternalError("type=" + operationType);
101 }
102 }
103 return 0;
104 }
105
106 private void grantRight() {
107 Database db = session.getDatabase();
108 for (Table table : tables) {
109 Right right = grantee.getRightForTable(table);
110 if (right == null) {
111 int id = getObjectId();
112 right = new Right(db, id, grantee, rightMask, table);
113 grantee.grantRight(table, right);
114 db.addDatabaseObject(session, right);
115 } else {
116 right.setRightMask(right.getRightMask() | rightMask);
117 }
118 }
119 }
120
121 private void grantRole(Role grantedRole) {
122 if (grantedRole != grantee && grantee.isRoleGranted(grantedRole)) {
123 return;
124 }
125 if (grantee instanceof Role) {
126 Role granteeRole = (Role) grantee;
127 if (grantedRole.isRoleGranted(granteeRole)) {
128 // cyclic role grants are not allowed
129 throw DbException.get(ErrorCode.ROLE_ALREADY_GRANTED_1, grantedRole.getSQL());
130 }
131 }
132 Database db = session.getDatabase();
133 int id = getObjectId();
134 Right right = new Right(db, id, grantee, grantedRole);
135 db.addDatabaseObject(session, right);
136 grantee.grantRole(grantedRole, right);
137 }
138
139 private void revokeRight() {
140 for (Table table : tables) {
141 Right right = grantee.getRightForTable(table);
142 if (right == null) {
143 continue;
144 }
145 int mask = right.getRightMask();
146 int newRight = mask & ~rightMask;
147 Database db = session.getDatabase();
148 if (newRight == 0) {
149 db.removeDatabaseObject(session, right);
150 } else {
151 right.setRightMask(newRight);
152 db.update(session, right);
153 }
154 }
155 }
156
157 private void revokeRole(Role grantedRole) {
158 Right right = grantee.getRightForRole(grantedRole);
159 if (right == null) {
160 return;
161 }
162 Database db = session.getDatabase();
163 db.removeDatabaseObject(session, right);
164 }
165
166 public boolean isTransactional() {
167 return false;
168 }
169
170 /**
171 * Add the specified table to the list of tables.
172 *
173 * @param table the table
174 */
175 public void addTable(Table table) {
176 tables.add(table);
177 }
178
179 public int getType() {
180 return operationType;
181 }
182
183 }
184