1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://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.table.Table; |
11 | |
12 | /** |
13 | * Represents a role. Roles can be granted to users, and to other roles. |
14 | */ |
15 | public class Role extends RightOwner { |
16 | |
17 | private final boolean system; |
18 | |
19 | public Role(Database database, int id, String roleName, boolean system) { |
20 | super(database, id, roleName, Trace.USER); |
21 | this.system = system; |
22 | } |
23 | |
24 | @Override |
25 | public String getCreateSQLForCopy(Table table, String quotedName) { |
26 | throw DbException.throwInternalError(); |
27 | } |
28 | |
29 | @Override |
30 | public String getDropSQL() { |
31 | return null; |
32 | } |
33 | |
34 | /** |
35 | * Get the CREATE SQL statement for this object. |
36 | * |
37 | * @param ifNotExists true if IF NOT EXISTS should be used |
38 | * @return the SQL statement |
39 | */ |
40 | public String getCreateSQL(boolean ifNotExists) { |
41 | if (system) { |
42 | return null; |
43 | } |
44 | StringBuilder buff = new StringBuilder("CREATE ROLE "); |
45 | if (ifNotExists) { |
46 | buff.append("IF NOT EXISTS "); |
47 | } |
48 | buff.append(getSQL()); |
49 | return buff.toString(); |
50 | } |
51 | |
52 | @Override |
53 | public String getCreateSQL() { |
54 | return getCreateSQL(false); |
55 | } |
56 | |
57 | @Override |
58 | public int getType() { |
59 | return DbObject.ROLE; |
60 | } |
61 | |
62 | @Override |
63 | public void removeChildrenAndResources(Session session) { |
64 | for (User user : database.getAllUsers()) { |
65 | Right right = user.getRightForRole(this); |
66 | if (right != null) { |
67 | database.removeDatabaseObject(session, right); |
68 | } |
69 | } |
70 | for (Role r2 : database.getAllRoles()) { |
71 | Right right = r2.getRightForRole(this); |
72 | if (right != null) { |
73 | database.removeDatabaseObject(session, right); |
74 | } |
75 | } |
76 | for (Right right : database.getAllRights()) { |
77 | if (right.getGrantee() == this) { |
78 | database.removeDatabaseObject(session, right); |
79 | } |
80 | } |
81 | database.removeMeta(session, getId()); |
82 | invalidate(); |
83 | } |
84 | |
85 | @Override |
86 | public void checkRename() { |
87 | // ok |
88 | } |
89 | |
90 | } |