| 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.samples; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.DriverManager; |
| 10 |
import java.sql.PreparedStatement; |
| 11 |
import java.sql.ResultSet; |
| 12 |
import java.sql.SQLException; |
| 13 |
import java.sql.Statement; |
| 14 |
import org.h2.tools.DeleteDbFiles; |
| 15 |
import org.h2.tools.TriggerAdapter; |
| 16 |
|
| 17 |
/** |
| 18 |
* This sample application show how to emulate per-row access rights so that |
| 19 |
* each user can only access rows created by the given user. |
| 20 |
*/ |
| 21 |
public class RowAccessRights extends TriggerAdapter { |
| 22 |
|
| 23 |
private PreparedStatement prepDelete, prepInsert; |
| 24 |
|
| 25 |
/** |
| 26 |
* Called when ran from command line. |
| 27 |
* |
| 28 |
* @param args ignored |
| 29 |
* @throws Exception on failure |
| 30 |
*/ |
| 31 |
public static void main(String... args) throws Exception { |
| 32 |
DeleteDbFiles.execute("~", "test", true); |
| 33 |
|
| 34 |
Class.forName("org.h2.Driver"); |
| 35 |
Connection conn = DriverManager.getConnection( |
| 36 |
"jdbc:h2:~/test"); |
| 37 |
Statement stat = conn.createStatement(); |
| 38 |
|
| 39 |
stat.execute("create table test_data(" + |
| 40 |
"id int, `user` varchar, data varchar, primary key(id, `user`))"); |
| 41 |
stat.execute("create index on test_data(id, `user`)"); |
| 42 |
|
| 43 |
stat.execute("create view test as select id, data " + |
| 44 |
"from test_data where `user` = user"); |
| 45 |
stat.execute("create trigger t_test instead of " + |
| 46 |
"insert, update, delete on test for each row " + |
| 47 |
"call \"" + RowAccessRights.class.getName() + "\""); |
| 48 |
stat.execute("create user a password 'a'"); |
| 49 |
stat.execute("create user b password 'b'"); |
| 50 |
stat.execute("grant all on test to a"); |
| 51 |
stat.execute("grant all on test to b"); |
| 52 |
|
| 53 |
ResultSet rs; |
| 54 |
|
| 55 |
Connection connA = DriverManager.getConnection( |
| 56 |
"jdbc:h2:~/test", "a", "a"); |
| 57 |
Statement statA = connA.createStatement(); |
| 58 |
statA.execute("insert into test values(1, 'Hello'), (2, 'World')"); |
| 59 |
statA.execute("update test set data = 'Hello!' where id = 1"); |
| 60 |
statA.execute("delete from test where id = 2"); |
| 61 |
|
| 62 |
Connection connB = DriverManager.getConnection( |
| 63 |
"jdbc:h2:~/test", "b", "b"); |
| 64 |
Statement statB = connB.createStatement(); |
| 65 |
statB.execute("insert into test values(1, 'Hallo'), (2, 'Welt')"); |
| 66 |
statB.execute("update test set data = 'Hallo!' where id = 1"); |
| 67 |
statB.execute("delete from test where id = 2"); |
| 68 |
|
| 69 |
rs = statA.executeQuery("select * from test"); |
| 70 |
while (rs.next()) { |
| 71 |
System.out.println("a: " + rs.getInt(1) + "/" + rs.getString(2)); |
| 72 |
} |
| 73 |
|
| 74 |
rs = statB.executeQuery("select * from test"); |
| 75 |
while (rs.next()) { |
| 76 |
System.out.println("b: " + |
| 77 |
rs.getInt(1) + "/" + rs.getString(2)); |
| 78 |
} |
| 79 |
|
| 80 |
connA.close(); |
| 81 |
connB.close(); |
| 82 |
|
| 83 |
rs = stat.executeQuery("select * from test_data"); |
| 84 |
while (rs.next()) { |
| 85 |
System.out.println(rs.getInt(1) + "/" + |
| 86 |
rs.getString(2) + "/" + rs.getString(3)); |
| 87 |
} |
| 88 |
conn.close(); |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void init(Connection conn, String schemaName, String triggerName, |
| 94 |
String tableName, boolean before, int type) throws SQLException { |
| 95 |
prepDelete = conn.prepareStatement( |
| 96 |
"delete from test_data where id = ? and `user` = ?"); |
| 97 |
prepInsert = conn.prepareStatement( |
| 98 |
"insert into test_data values(?, ?, ?)"); |
| 99 |
super.init(conn, schemaName, triggerName, tableName, before, type); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void fire(Connection conn, ResultSet oldRow, ResultSet newRow) |
| 104 |
throws SQLException { |
| 105 |
String user = conn.getMetaData().getUserName(); |
| 106 |
if (oldRow != null && oldRow.next()) { |
| 107 |
prepDelete.setInt(1, oldRow.getInt(1)); |
| 108 |
prepDelete.setString(2, user); |
| 109 |
int deleted = prepDelete.executeUpdate(); |
| 110 |
if (deleted == 0 && newRow != null) { |
| 111 |
// update: |
| 112 |
// if deleting failed, insert must fail as well |
| 113 |
newRow = null; |
| 114 |
} |
| 115 |
} |
| 116 |
if (newRow != null && newRow.next()) { |
| 117 |
prepInsert.setInt(1, newRow.getInt(1)); |
| 118 |
prepInsert.setString(2, user); |
| 119 |
prepInsert.setString(3, newRow.getString(2)); |
| 120 |
prepInsert.executeUpdate(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
} |