pg_audit_log 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,12 +23,24 @@ module PgAuditLog
23
23
  "last_accessed_at"
24
24
  end
25
25
 
26
+ def pg_audit_log_old_style_user_id
27
+ defined?(Rails) && Rails.configuration.pg_audit_log_old_style_user_id rescue false
28
+ end
29
+
26
30
  def user_identifier_temporary_function(user_id)
27
- "CREATE OR REPLACE FUNCTION pg_temp.pg_audit_log_user_identifier() RETURNS integer AS 'SELECT #{user_id}' LANGUAGE SQL STABLE;"
31
+ if pg_audit_log_old_style_user_id
32
+ "SET audit.user_id = #{user_id || -1};"
33
+ else
34
+ "CREATE OR REPLACE FUNCTION pg_temp.pg_audit_log_user_identifier() RETURNS integer AS 'SELECT #{user_id}' LANGUAGE SQL STABLE;"
35
+ end
28
36
  end
29
37
 
30
38
  def user_unique_name_temporary_function(username)
31
- "CREATE OR REPLACE FUNCTION pg_temp.pg_audit_log_user_unique_name() RETURNS varchar AS $_$ SELECT '#{PGconn.escape_bytea(username)}'::varchar $_$ LANGUAGE SQL STABLE;"
39
+ if pg_audit_log_old_style_user_id
40
+ "SET audit.user_unique_name = '#{PGconn.escape_bytea(username)}';"
41
+ else
42
+ "CREATE OR REPLACE FUNCTION pg_temp.pg_audit_log_user_unique_name() RETURNS varchar AS $_$ SELECT '#{PGconn.escape_bytea(username)}'::varchar $_$ LANGUAGE SQL STABLE;"
43
+ end
32
44
  end
33
45
 
34
46
  def install
@@ -47,11 +59,11 @@ module PgAuditLog
47
59
  unique_name varchar;
48
60
  column_name varchar;
49
61
  BEGIN
50
- user_identifier := pg_temp.pg_audit_log_user_identifier();
62
+ user_identifier := #{pg_audit_log_old_style_user_id ? "current_setting('audit.user_id')" : 'pg_temp.pg_audit_log_user_identifier()'};
51
63
  IF user_identifier = #{DISABLED_USER} THEN
52
64
  RETURN NULL;
53
65
  END IF;
54
- unique_name := pg_temp.pg_audit_log_user_unique_name();
66
+ unique_name := #{pg_audit_log_old_style_user_id ? "current_setting('audit.user_unique_name')" : 'pg_temp.pg_audit_log_user_unique_name()'};
55
67
  primary_key_column := NULL;
56
68
  EXECUTE 'SELECT pg_attribute.attname
57
69
  FROM pg_index, pg_class, pg_attribute
@@ -1,3 +1,3 @@
1
1
  module PgAuditLog
2
- VERSION = "0.5.4"
2
+ VERSION = "0.5.5"
3
3
  end
@@ -22,8 +22,28 @@ describe PgAuditLog::Function do
22
22
  subject { PgAuditLog::Function.user_unique_name_temporary_function(email) }
23
23
  let(:email) { "o'connell@fred.com" }
24
24
 
25
- it "escapes the email" do
26
- subject.should match("'o''connell@fred.com'::varchar")
25
+ context "new style" do
26
+ it "escapes the email" do
27
+ subject.should_not match("SET")
28
+
29
+ subject.should match("FUNCTION")
30
+ subject.should match("'o''connell@fred.com'::varchar")
31
+ end
32
+ end
33
+
34
+ context "old style" do
35
+ before do
36
+ Rails = double
37
+ Rails.stub_chain(:configuration, :pg_audit_log_old_style_user_id).and_return(true)
38
+ end
39
+ after { Object.send(:remove_const, :Rails) }
40
+ it "escapes the email" do
41
+ subject.should match("SET")
42
+ subject.should match("'o''connell@fred.com'")
43
+
44
+ subject.should_not match("FUNCTION")
45
+ subject.should_not match("'o''connell@fred.com'::varchar")
46
+ end
27
47
  end
28
48
  end
29
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_audit_log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-04 00:00:00.000000000 Z
12
+ date: 2012-06-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  version: '0'
144
144
  segments:
145
145
  - 0
146
- hash: 3807464180291971898
146
+ hash: 4555192677821680411
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  none: false
149
149
  requirements:
@@ -152,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  version: '0'
153
153
  segments:
154
154
  - 0
155
- hash: 3807464180291971898
155
+ hash: 4555192677821680411
156
156
  requirements: []
157
157
  rubyforge_project:
158
158
  rubygems_version: 1.8.19