Skip to content

Commit 7831a95

Browse files
committed
authorization callbacks are working
1 parent 90f69da commit 7831a95

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

ext/sqlite3/database.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,11 @@ static int rb_sqlite3_auth(
411411
*
412412
* Set the authorizer for this database. +auth+ must respond to +call+, and
413413
* +call+ must take 5 arguments.
414+
*
415+
* Installs (or removes) a block that will be invoked for every access
416+
* to the database. If the block returns 0 (or +true+), the statement
417+
* is allowed to proceed. Returning 1 or false causes an authorization error to
418+
* occur, and returning 2 or nil causes the access to be silently denied.
414419
*/
415420
static VALUE set_authorizer(VALUE self, VALUE authorizer)
416421
{

ext/sqlite3/statement.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,17 @@ static VALUE initialize(VALUE self, VALUE db, VALUE sql)
4949
&tail
5050
);
5151

52-
if(SQLITE_OK != status)
53-
rb_raise(rb_path2class("SQLite3::SQLException"), "%s", sqlite3_errmsg(db_ctx->db));
52+
switch(status) {
53+
case SQLITE_OK:
54+
break;
55+
case SQLITE_AUTH:
56+
rb_raise(
57+
rb_path2class("SQLite3::AuthorizationException"), "%s",
58+
sqlite3_errmsg(db_ctx->db));
59+
break;
60+
default:
61+
rb_raise(rb_path2class("SQLite3::SQLException"), "%s", sqlite3_errmsg(db_ctx->db));
62+
}
5463

5564
rb_iv_set(self, "@connection", db);
5665
rb_iv_set(self, "@remainder", rb_str_new2(tail));

lib/sqlite3/database.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ def translator
7070
# to the database. If the block returns 0 (or +nil+), the statement
7171
# is allowed to proceed. Returning 1 causes an authorization error to
7272
# occur, and returning 2 causes the access to be silently denied.
73-
def authorizer( data=nil, &block )
74-
result = @driver.set_authorizer( @handle, data, &block )
75-
Error.check( result, self )
73+
def authorizer &block
74+
self.authorizer = block
7675
end
7776

7877
# Returns a Statement object representing the given SQL. This does not

test/test_integration.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,20 @@ def test_trace
8181
end
8282

8383
def test_authorizer_okay
84-
@db.authorizer( "data" ) { |data,type,a,b,c,d| 0 }
84+
@db.authorizer { |type,a,b,c,d| 0 }
8585
rows = @db.execute "select * from foo"
8686
assert_equal 3, rows.length
8787
end
8888

8989
def test_authorizer_error
90-
@db.authorizer( "data" ) { |data,type,a,b,c,d| 1 }
90+
@db.authorizer { |type,a,b,c,d| 1 }
9191
assert_raise( SQLite3::AuthorizationException ) do
9292
@db.execute "select * from foo"
9393
end
9494
end
9595

9696
def test_authorizer_silent
97-
@db.authorizer( "data" ) { |data,type,a,b,c,d| 2 }
97+
@db.authorizer { |type,a,b,c,d| 2 }
9898
rows = @db.execute "select * from foo"
9999
assert rows.empty?
100100
end

0 commit comments

Comments
 (0)