| 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 |
|
| 10 |
import org.h2.message.Trace; |
| 11 |
import org.h2.schema.Schema; |
| 12 |
|
| 13 |
/** |
| 14 |
* Represents a role. Roles can be granted to users, and to other roles. |
| 15 |
*/ |
| 16 |
public final class Role extends RightOwner { |
| 17 |
|
| 18 |
private final boolean system; |
| 19 |
|
| 20 |
public Role(Database database, int id, String roleName, boolean system) { |
| 21 |
super(database, id, roleName, Trace.USER); |
| 22 |
this.system = system; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get the CREATE SQL statement for this object. |
| 27 |
* |
| 28 |
* @param ifNotExists true if IF NOT EXISTS should be used |
| 29 |
* @return the SQL statement |
| 30 |
*/ |
| 31 |
public String getCreateSQL(boolean ifNotExists) { |
| 32 |
if (system) { |
| 33 |
return null; |
| 34 |
} |
| 35 |
StringBuilder builder = new StringBuilder("CREATE ROLE "); |
| 36 |
if (ifNotExists) { |
| 37 |
builder.append("IF NOT EXISTS "); |
| 38 |
} |
| 39 |
return getSQL(builder, DEFAULT_SQL_FLAGS).toString(); |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public String getCreateSQL() { |
| 44 |
return getCreateSQL(false); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public int getType() { |
| 49 |
return DbObject.ROLE; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public ArrayList<DbObject> getChildren() { |
| 54 |
ArrayList<DbObject> children = new ArrayList<>(); |
| 55 |
for (Schema schema : database.getAllSchemas()) { |
| 56 |
if (schema.getOwner() == this) { |
| 57 |
children.add(schema); |
| 58 |
} |
| 59 |
} |
| 60 |
return children; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void removeChildrenAndResources(SessionLocal session) { |
| 65 |
for (RightOwner rightOwner : database.getAllUsersAndRoles()) { |
| 66 |
Right right = rightOwner.getRightForRole(this); |
| 67 |
if (right != null) { |
| 68 |
database.removeDatabaseObject(session, right); |
| 69 |
} |
| 70 |
} |
| 71 |
for (Right right : database.getAllRights()) { |
| 72 |
if (right.getGrantee() == this) { |
| 73 |
database.removeDatabaseObject(session, right); |
| 74 |
} |
| 75 |
} |
| 76 |
database.removeMeta(session, getId()); |
| 77 |
invalidate(); |
| 78 |
} |
| 79 |
|
| 80 |
} |