| 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 java.util.ArrayList; |
| 9 |
import java.util.HashMap; |
| 10 |
import java.util.List; |
| 11 |
import java.util.Map.Entry; |
| 12 |
|
| 13 |
import org.h2.api.ErrorCode; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.schema.Schema; |
| 16 |
import org.h2.table.Table; |
| 17 |
import org.h2.util.StringUtils; |
| 18 |
|
| 19 |
/** |
| 20 |
* A right owner (sometimes called principal). |
| 21 |
*/ |
| 22 |
public abstract class RightOwner extends DbObject { |
| 23 |
|
| 24 |
/** |
| 25 |
* The map of granted roles. |
| 26 |
*/ |
| 27 |
private HashMap<Role, Right> grantedRoles; |
| 28 |
|
| 29 |
/** |
| 30 |
* The map of granted rights. |
| 31 |
*/ |
| 32 |
private HashMap<DbObject, Right> grantedRights; |
| 33 |
|
| 34 |
protected RightOwner(Database database, int id, String name, int traceModuleId) { |
| 35 |
super(database, id, StringUtils.toUpperEnglish(name), traceModuleId); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void rename(String newName) { |
| 40 |
super.rename(StringUtils.toUpperEnglish(newName)); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Check if a role has been granted for this right owner. |
| 45 |
* |
| 46 |
* @param grantedRole the role |
| 47 |
* @return true if the role has been granted |
| 48 |
*/ |
| 49 |
public boolean isRoleGranted(Role grantedRole) { |
| 50 |
if (grantedRole == this) { |
| 51 |
return true; |
| 52 |
} |
| 53 |
if (grantedRoles != null) { |
| 54 |
for (Role role : grantedRoles.keySet()) { |
| 55 |
if (role == grantedRole) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
if (role.isRoleGranted(grantedRole)) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Checks if a right is already granted to this object or to objects that |
| 68 |
* were granted to this object. The rights of schemas will be valid for |
| 69 |
* every each table in the related schema. The ALTER ANY SCHEMA right gives |
| 70 |
* all rights to all tables. |
| 71 |
* |
| 72 |
* @param table |
| 73 |
* the table to check |
| 74 |
* @param rightMask |
| 75 |
* the right mask to check |
| 76 |
* @return true if the right was already granted |
| 77 |
*/ |
| 78 |
final boolean isTableRightGrantedRecursive(Table table, int rightMask) { |
| 79 |
Schema schema = table.getSchema(); |
| 80 |
if (schema.getOwner() == this) { |
| 81 |
return true; |
| 82 |
} |
| 83 |
if (grantedRights != null) { |
| 84 |
Right right = grantedRights.get(null); |
| 85 |
if (right != null && (right.getRightMask() & Right.ALTER_ANY_SCHEMA) == Right.ALTER_ANY_SCHEMA) { |
| 86 |
return true; |
| 87 |
} |
| 88 |
right = grantedRights.get(schema); |
| 89 |
if (right != null && (right.getRightMask() & rightMask) == rightMask) { |
| 90 |
return true; |
| 91 |
} |
| 92 |
right = grantedRights.get(table); |
| 93 |
if (right != null && (right.getRightMask() & rightMask) == rightMask) { |
| 94 |
return true; |
| 95 |
} |
| 96 |
} |
| 97 |
if (grantedRoles != null) { |
| 98 |
for (Role role : grantedRoles.keySet()) { |
| 99 |
if (role.isTableRightGrantedRecursive(table, rightMask)) { |
| 100 |
return true; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Checks if a schema owner right is already granted to this object or to |
| 109 |
* objects that were granted to this object. The ALTER ANY SCHEMA right |
| 110 |
* gives rights to all schemas. |
| 111 |
* |
| 112 |
* @param schema |
| 113 |
* the schema to check, or {@code null} to check for ALTER ANY |
| 114 |
* SCHEMA right only |
| 115 |
* @return true if the right was already granted |
| 116 |
*/ |
| 117 |
final boolean isSchemaRightGrantedRecursive(Schema schema) { |
| 118 |
if (schema != null && schema.getOwner() == this) { |
| 119 |
return true; |
| 120 |
} |
| 121 |
if (grantedRights != null) { |
| 122 |
Right right = grantedRights.get(null); |
| 123 |
if (right != null && (right.getRightMask() & Right.ALTER_ANY_SCHEMA) == Right.ALTER_ANY_SCHEMA) { |
| 124 |
return true; |
| 125 |
} |
| 126 |
} |
| 127 |
if (grantedRoles != null) { |
| 128 |
for (Role role : grantedRoles.keySet()) { |
| 129 |
if (role.isSchemaRightGrantedRecursive(schema)) { |
| 130 |
return true; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Grant a right for the given table. Only one right object per table is |
| 139 |
* supported. |
| 140 |
* |
| 141 |
* @param object the object (table or schema) |
| 142 |
* @param right the right |
| 143 |
*/ |
| 144 |
public void grantRight(DbObject object, Right right) { |
| 145 |
if (grantedRights == null) { |
| 146 |
grantedRights = new HashMap<>(); |
| 147 |
} |
| 148 |
grantedRights.put(object, right); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Revoke the right for the given object (table or schema). |
| 153 |
* |
| 154 |
* @param object the object |
| 155 |
*/ |
| 156 |
void revokeRight(DbObject object) { |
| 157 |
if (grantedRights == null) { |
| 158 |
return; |
| 159 |
} |
| 160 |
grantedRights.remove(object); |
| 161 |
if (grantedRights.size() == 0) { |
| 162 |
grantedRights = null; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Grant a role to this object. |
| 168 |
* |
| 169 |
* @param role the role |
| 170 |
* @param right the right to grant |
| 171 |
*/ |
| 172 |
public void grantRole(Role role, Right right) { |
| 173 |
if (grantedRoles == null) { |
| 174 |
grantedRoles = new HashMap<>(); |
| 175 |
} |
| 176 |
grantedRoles.put(role, right); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Remove the right for the given role. |
| 181 |
* |
| 182 |
* @param role the role to revoke |
| 183 |
*/ |
| 184 |
void revokeRole(Role role) { |
| 185 |
if (grantedRoles == null) { |
| 186 |
return; |
| 187 |
} |
| 188 |
Right right = grantedRoles.get(role); |
| 189 |
if (right == null) { |
| 190 |
return; |
| 191 |
} |
| 192 |
grantedRoles.remove(role); |
| 193 |
if (grantedRoles.size() == 0) { |
| 194 |
grantedRoles = null; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Remove all the temporary rights granted on roles |
| 200 |
*/ |
| 201 |
public void revokeTemporaryRightsOnRoles() { |
| 202 |
if (grantedRoles == null) { |
| 203 |
return; |
| 204 |
} |
| 205 |
List<Role> rolesToRemove= new ArrayList<>(); |
| 206 |
for (Entry<Role,Right> currentEntry : grantedRoles.entrySet()) { |
| 207 |
if ( currentEntry.getValue().isTemporary() || !currentEntry.getValue().isValid()) { |
| 208 |
rolesToRemove.add(currentEntry.getKey()); |
| 209 |
} |
| 210 |
} |
| 211 |
for (Role currentRoleToRemove : rolesToRemove) { |
| 212 |
revokeRole(currentRoleToRemove); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* Get the 'grant schema' right of this object. |
| 220 |
* |
| 221 |
* @param object the granted object (table or schema) |
| 222 |
* @return the right or null if the right has not been granted |
| 223 |
*/ |
| 224 |
public Right getRightForObject(DbObject object) { |
| 225 |
if (grantedRights == null) { |
| 226 |
return null; |
| 227 |
} |
| 228 |
return grantedRights.get(object); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Get the 'grant role' right of this object. |
| 233 |
* |
| 234 |
* @param role the granted role |
| 235 |
* @return the right or null if the right has not been granted |
| 236 |
*/ |
| 237 |
public Right getRightForRole(Role role) { |
| 238 |
if (grantedRoles == null) { |
| 239 |
return null; |
| 240 |
} |
| 241 |
return grantedRoles.get(role); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Check that this right owner does not own any schema. An exception is |
| 246 |
* thrown if it owns one or more schemas. |
| 247 |
* |
| 248 |
* @throws DbException |
| 249 |
* if this right owner owns a schema |
| 250 |
*/ |
| 251 |
public final void checkOwnsNoSchemas() { |
| 252 |
for (Schema s : database.getAllSchemas()) { |
| 253 |
if (this == s.getOwner()) { |
| 254 |
throw DbException.get(ErrorCode.CANNOT_DROP_2, getName(), s.getName()); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
} |