activerecord-tenant-level-security 0.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8741075a67af652cbb2d63bd9b51a133567e8c4afaee412a0613a83ab691574b
4
- data.tar.gz: dd8568928a4ef717ff14414999c30ab338779bd7442eb235c65241812c7b709e
3
+ metadata.gz: e847b09c2072b1d43800f05548532cbea63ee65700b80a50f48ce212ce1128c4
4
+ data.tar.gz: '0884eb072cb058b099b449cbc940a373abb247f6c01a396df64486941b091167'
5
5
  SHA512:
6
- metadata.gz: e6804c43306e3f42dc848f77ecc626d48dcd6be423c3b00f360f2958df3ad20fa7c1eed89bd3558493928fb9fd3166f7297f4f03b3326d6c28a8b580de1823f8
7
- data.tar.gz: 7557ed07df0d49fd04f38a230a03a7a4d9548c02c4583e0c074c03758b016ad30e8aa88131c01730788409d5897c9c7660fa058fb3ecf2f6941efb04f530c90f
6
+ metadata.gz: a2761438baef9b4e0c3ea159121f28da51f537bb5c70bcb8d4c81b6d22283d2d0bab9eea784ba84d0b68635d68954cc27eb3bc36f56f6c92508914998e548422
7
+ data.tar.gz: 33e9fbbc489d73f12418e7c53483989b404417051aff14a28ce0aa77598aef9547d89a08a6476e908cdb26972963b876c37a15d79e145f14a6c514f55dfc5310
data/.circleci/config.yml CHANGED
@@ -11,15 +11,16 @@ jobs:
11
11
  type: string
12
12
  docker:
13
13
  - image: << parameters.ruby >>
14
- - image: circleci/<< parameters.postgres >>
14
+ environment:
15
+ BUNDLE_GEMFILE: gemfiles/<< parameters.rails >>.gemfile
16
+ - image: cimg/<< parameters.postgres >>
15
17
  environment:
16
18
  POSTGRES_HOST_AUTH_METHOD: trust
17
19
  steps:
18
20
  - checkout
19
21
  - run: gem install bundler
20
22
  - run: bundle install
21
- - run: bundle exec appraisal << parameters.rails >> bundle install
22
- - run: bundle exec appraisal << parameters.rails >> rspec
23
+ - run: bundle exec rspec
23
24
 
24
25
  workflows:
25
26
  all-tests:
@@ -27,6 +28,6 @@ workflows:
27
28
  - test:
28
29
  matrix:
29
30
  parameters:
30
- ruby: ['ruby:2.7.4', 'ruby:3.0.2']
31
- rails: ['rails-6.0', 'rails-6.1']
32
- postgres: ['postgres:11', 'postgres:12', 'postgres:13']
31
+ ruby: ['ruby:3.0', 'ruby:3.1', 'ruby:3.2']
32
+ rails: ['rails_6.0', 'rails_6.1', 'rails_7.0']
33
+ postgres: ['postgres:11.19', 'postgres:12.14', 'postgres:13.10', 'postgres:14.7', 'postgres:15.2']
data/Appraisals CHANGED
@@ -5,3 +5,7 @@ end
5
5
  appraise "rails-6.1" do
6
6
  gem "rails", "~> 6.1.4"
7
7
  end
8
+
9
+ appraise "rails-7.0" do
10
+ gem "rails", "~> 7.0.2"
11
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,30 @@
1
+ ## v0.2.0 (2023-04-14)
2
+
3
+ This release includes changes to policies created by `create_policy`. Migrations that have already been performed will not be affected, so it is safe to upgrade as-is, but for performance reasons it is recommended to update existing policies to the new format. See https://github.com/kufu/activerecord-tenant-level-security/pull/16 for details.
4
+
5
+ ### Breaking Changes
6
+
7
+ - [#16](https://github.com/kufu/activerecord-tenant-level-security/pull/16): Cast `current_setting` instead of `tenant_id`
8
+ - [#17](https://github.com/kufu/activerecord-tenant-level-security/pull/17): CI against Ruby 3.2 and PostgreSQL 15, drop Ruby 2.7
9
+
10
+ ### Chores
11
+
12
+ - [#13](https://github.com/kufu/activerecord-tenant-level-security/pull/13): Mention multiple databases support status
13
+ - [#15](https://github.com/kufu/activerecord-tenant-level-security/pull/15): Update License and CoC files
14
+
15
+ ## v0.1.0 (2023-01-23)
16
+
17
+ ### BugFixes
18
+
19
+ - [#12](https://github.com/kufu/activerecord-tenant-level-security/pull/12): Clear query cache after switching
20
+
21
+ ### Chores
22
+
23
+ - [#8](https://github.com/kufu/activerecord-tenant-level-security/pull/8): Project tweaks
24
+ - [#9](https://github.com/kufu/activerecord-tenant-level-security/pull/9): fix email
25
+ - [#10](https://github.com/kufu/activerecord-tenant-level-security/pull/10): CI against Rails 7.0/Ruby 3.1/PostgreSQL 14
26
+ - [#11](https://github.com/kufu/activerecord-tenant-level-security/pull/11): Fix typos in README.md
27
+
28
+ ## v0.0.1 (2021-11-10)
29
+
30
+ Initial release 🥳
@@ -0,0 +1,86 @@
1
+ # コントリビューター行動規範
2
+
3
+ ## 私たちの約束
4
+ メンバー、コントリビューター、およびリーダーとして、年齢、体の大きさ、目に見えるまたは目に見えない障害、民族性、性別、
5
+ 性同一性、表現、経験のレベル、教育、社会経済的地位、国籍、人格、人種、宗教、または性的同一性と指向に関係なく、
6
+ コミュニティへの参加をハラスメントのない体験にすることを誓います。
7
+
8
+ 私たちは、オープンで親しみやすく、多様で包括的で健全なコミュニティに貢献する方法で行動し、交流することを誓います。
9
+
10
+ ## 私たちの標準
11
+
12
+ 前向きな環境を作り上げることに貢献する行動の例:
13
+
14
+ * 他人への共感と優しさを示す
15
+
16
+ * 異なる意見、視点、経験を尊重する
17
+
18
+ * 建設的なフィードバックを与え、優雅に受け入れる
19
+
20
+ * 私たちの過ちの影響を受けた人々に責任を受け入れ、謝罪し、そしてその経験から学ぶ
21
+
22
+ * 個人としてだけでなく、コミュニティ全体にとっても最善であることに焦点を当てる
23
+
24
+ 許容できない行動の例は次のとおりです。
25
+
26
+ * 性的な言葉や画像の使用、および性的な注意またはその他あらゆる種類の問題行為
27
+
28
+ * トローリング、侮辱的または中傷的なコメント、個人的または政治的攻撃
29
+
30
+ * 公的またはプライベートの嫌がらせ
31
+
32
+ * 明示的な許可なしに、住所や電子メールアドレスなど、他者の個人情報を公開する
33
+
34
+ * 職業上不適切と合理的に考えられるその他の行為
35
+
36
+ ## 執行責任
37
+
38
+ コミュニティリーダーは、許容される行動の基準を明確にし、実施する責任があり、不適切、脅迫的、攻撃的、または有害と見なされる行動に応じて、適切で公正な是正措置を講じます。
39
+
40
+ コミュニティリーダーは、コメント、コミット、コード、wikiの編集、問題、およびこの行動規範に沿っていないその他の貢献を削除、編集、または拒否する権利と責任を持ち、適切な場合はモデレーションの決定の理由を伝えます。
41
+
42
+ ## 適用範囲
43
+
44
+ この行動規範は、すべてのコミュニティスペース内で適用され、個人がパブリックスペースでコミュニティを公式に代表している場合にも適用されます。
45
+ 私たちのコミュニティを代表する例には、公式の電子メールアドレスの使用、公式のソーシャルメディアアカウントを介した投稿、オンラインまたはオフラインのイベントでの指定代理人としての行動などがあります。
46
+
47
+ ## 執行
48
+
49
+ 虐待的、嫌がらせ、またはその他の許容できない行動の事例は、執行を担当するコミュニティリーダーに対して `oss@smarthr.co.jp` で報告される場合があります。
50
+ すべての苦情は迅速かつ公正にレビューおよび調査されます。
51
+
52
+ すべてのコミュニティリーダーは、問題の報告者のプライバシーとセキュリティを尊重する義務があります。
53
+
54
+ ## 執行ガイドライン
55
+
56
+ コミュニティリーダーは、この行動規範に違反していると見なした行動への帰結を判断する際に、これらのコミュニティガイドラインに従います。
57
+
58
+ ### 1. 更生
59
+
60
+ **コミュニティへの影響**: コミュニティで専門家にふさわしくない、または歓迎されないと思われる不適切な言葉の使用やその他の不適切な行動をすること。
61
+
62
+ **帰結**: コミュニティリーダーからの非公開の書面による警告。違反の理由を明確にし、行動が不適切だった理由を説明します。 公の謝罪が要求される場合があります。
63
+
64
+ ### 2. 警告
65
+
66
+ **コミュニティへの影響**: 単一の出来事または一連の動作による違反。
67
+
68
+ **帰結**: 持続的な行動の結果を伴う警告。 指定された期間、行動規範の実施者との一方的な対話を含め、関係者との対話はありません。 これには、コミュニティスペースやソーシャルメディアなどの外部チャネルでの相互作用の回避が含まれます。 これらの条件に違反すると、一時的または永続的に禁止される場合があります。
69
+
70
+ ### 3. 一時的な禁止
71
+ **コミュニティへの影響**: 持続的で不適切な行動を含む、コミュニティ標準の重大な違反。
72
+
73
+ **帰結**: 指定された期間のコミュニティとのあらゆる種類の相互関係または公的なコミュニケーションの一時的な禁止。 この期間中、行動規範を実施する人々との一方的な対話を含め、関係する人々との公的または私的な対話は許可されません。
74
+ これらの条件に違反すると、永久的に禁止される場合があります。
75
+ ### 4. 永久的な禁止
76
+ **コミュニティへの影響**: 連続的な不適切な行動、個人への嫌がらせ、または個人の集団に対する攻撃または名誉毀損を含む、コミュニティの標準への違反のパターンを示す。
77
+
78
+ **帰結**: コミュニティ内でのあらゆる種類の公的な相互関係の永久的な禁止。
79
+
80
+ ## 帰属
81
+ この行動規範は、https://www.contributor-covenant.org/version/2/0/code_of_conduct.html で利用可能な [Contributor Covenant][homepage] バージョン2.0を基に作成されています。
82
+
83
+ コミュニティへの影響ガイドラインは[Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity)に適合しています。
84
+
85
+ [homepage]: https://www.contributor-covenant.org
86
+ この行動規範に関する一般的な質問への回答については、https://www.contributor-covenant.org/faq のFAQを参照してください。翻訳はhttps://www.contributor-covenant.org/translations で入手できます。
data/CODE_OF_CONDUCT.md CHANGED
@@ -1,74 +1,133 @@
1
+
1
2
  # Contributor Covenant Code of Conduct
2
3
 
3
4
  ## Our Pledge
4
5
 
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
6
+ We as members, contributors, and leaders pledge to make participation in our
7
+ community a harassment-free experience for everyone, regardless of age, body
8
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
9
+ identity and expression, level of experience, education, socio-economic status,
10
+ nationality, personal appearance, race, religion, or sexual identity
11
+ and orientation.
12
+
13
+ We pledge to act and interact in ways that contribute to an open, welcoming,
14
+ diverse, inclusive, and healthy community.
11
15
 
12
16
  ## Our Standards
13
17
 
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
18
+ Examples of behavior that contributes to a positive environment for our
19
+ community include:
16
20
 
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
21
+ * Demonstrating empathy and kindness toward other people
22
+ * Being respectful of differing opinions, viewpoints, and experiences
23
+ * Giving and gracefully accepting constructive feedback
24
+ * Accepting responsibility and apologizing to those affected by our mistakes,
25
+ and learning from the experience
26
+ * Focusing on what is best not just for us as individuals, but for the
27
+ overall community
22
28
 
23
- Examples of unacceptable behavior by participants include:
29
+ Examples of unacceptable behavior include:
24
30
 
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
31
+ * The use of sexualized language or imagery, and sexual attention or
32
+ advances of any kind
33
+ * Trolling, insulting or derogatory comments, and personal or political attacks
28
34
  * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
35
+ * Publishing others' private information, such as a physical or email
36
+ address, without their explicit permission
31
37
  * Other conduct which could reasonably be considered inappropriate in a
32
38
  professional setting
33
39
 
34
- ## Our Responsibilities
40
+ ## Enforcement Responsibilities
35
41
 
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
42
+ Community leaders are responsible for clarifying and enforcing our standards of
43
+ acceptable behavior and will take appropriate and fair corrective action in
44
+ response to any behavior that they deem inappropriate, threatening, offensive,
45
+ or harmful.
39
46
 
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
47
+ Community leaders have the right and responsibility to remove, edit, or reject
48
+ comments, commits, code, wiki edits, issues, and other contributions that are
49
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
50
+ decisions when appropriate.
45
51
 
46
52
  ## Scope
47
53
 
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
+ This Code of Conduct applies within all community spaces, and also applies when
55
+ an individual is officially representing the community in public spaces.
56
+ Examples of representing our community include using an official e-mail address,
57
+ posting via an official social media account, or acting as an appointed
58
+ representative at an online or offline event.
54
59
 
55
60
  ## Enforcement
56
61
 
57
62
  Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at watassbass@gmail.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
+ reported to the community leaders responsible for enforcement at
64
+ `oss@smarthr.co.jp`.
65
+ All complaints will be reviewed and investigated promptly and fairly.
66
+
67
+ All community leaders are obligated to respect the privacy and security of the
68
+ reporter of any incident.
69
+
70
+ ## Enforcement Guidelines
71
+
72
+ Community leaders will follow these Community Impact Guidelines in determining
73
+ the consequences for any action they deem in violation of this Code of Conduct:
74
+
75
+ ### 1. Correction
76
+
77
+ **Community Impact**: Use of inappropriate language or other behavior deemed
78
+ unprofessional or unwelcome in the community.
79
+
80
+ **Consequence**: A private, written warning from community leaders, providing
81
+ clarity around the nature of the violation and an explanation of why the
82
+ behavior was inappropriate. A public apology may be requested.
83
+
84
+ ### 2. Warning
63
85
 
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
86
+ **Community Impact**: A violation through a single incident or series
87
+ of actions.
88
+
89
+ **Consequence**: A warning with consequences for continued behavior. No
90
+ interaction with the people involved, including unsolicited interaction with
91
+ those enforcing the Code of Conduct, for a specified period of time. This
92
+ includes avoiding interactions in community spaces as well as external channels
93
+ like social media. Violating these terms may lead to a temporary or
94
+ permanent ban.
95
+
96
+ ### 3. Temporary Ban
97
+
98
+ **Community Impact**: A serious violation of community standards, including
99
+ sustained inappropriate behavior.
100
+
101
+ **Consequence**: A temporary ban from any sort of interaction or public
102
+ communication with the community for a specified period of time. No public or
103
+ private interaction with the people involved, including unsolicited interaction
104
+ with those enforcing the Code of Conduct, is allowed during this period.
105
+ Violating these terms may lead to a permanent ban.
106
+
107
+ ### 4. Permanent Ban
108
+
109
+ **Community Impact**: Demonstrating a pattern of violation of community
110
+ standards, including sustained inappropriate behavior, harassment of an
111
+ individual, or aggression toward or disparagement of classes of individuals.
112
+
113
+ **Consequence**: A permanent ban from any sort of public interaction within
114
+ the community.
67
115
 
68
116
  ## Attribution
69
117
 
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
118
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
119
+ version 2.0, available at
120
+ [https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][v2.0].
121
+
122
+ Community Impact Guidelines were inspired by
123
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
124
+
125
+ For answers to common questions about this code of conduct, see the FAQ at
126
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available
127
+ at [https://www.contributor-covenant.org/translations][translations].
72
128
 
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/
129
+ [homepage]: https://www.contributor-covenant.org
130
+ [v2.0]: https://www.contributor-covenant.org/version/2/0/code_of_conduct.html
131
+ [Mozilla CoC]: https://github.com/mozilla/diversity
132
+ [FAQ]: https://www.contributor-covenant.org/faq
133
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt CHANGED
@@ -1,21 +1,7 @@
1
- The MIT License (MIT)
1
+ Copyright 2023 SmartHR, Inc.
2
2
 
3
- Copyright (c) 2019 SmartHR, Inc.
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11
6
 
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # activerecord-tenant-level-security
2
+ [![CircleCI](https://circleci.com/gh/kufu/activerecord-tenant-level-security/tree/master.svg?style=svg)](https://circleci.com/gh/kufu/activerecord-tenant-level-security/tree/master)
3
+ [![gem-version](https://img.shields.io/gem/v/activerecord-tenant-level-security.svg)](https://rubygems.org/gems/activerecord-tenant-level-security)
4
+ [![License](https://img.shields.io/github/license/kufu/activerecord-tenant-level-security.svg?color=blue)](https://github.com/kufu/activerecord-tenant-level-security/blob/master/LICENSE.txt)
2
5
 
3
6
  An Active Record extension for Multitenancy with PostgreSQL Row Level Security.
4
7
 
@@ -57,7 +60,7 @@ When testing, be aware of the database user you are connecting to. The default u
57
60
  If you want to use this gem, you first need to register a callback which gets the current tenant. This callback is invoked when checking out a new connection from a connection pool. Create an initializer and tell how it should resolve the current tenant like the following:
58
61
 
59
62
  ```ruby
60
- TenantLevelSecuriy.current_tenant_id { RequestStore.store[:current_tenant_id] }
63
+ TenantLevelSecurity.current_tenant_id { RequestStore.store[:current_tenant_id] }
61
64
  ```
62
65
 
63
66
  The above is an example of getting the current tenant stored using [RequestStore](https://github.com/steveklabnik/request_store). You are responsible for storing the current tenant, such as at the beginning of the request.
@@ -65,7 +68,7 @@ The above is an example of getting the current tenant stored using [RequestStore
65
68
  We strongly recommend using the [activerecord-multi-tenant](https://github.com/citusdata/activerecord-multi-tenant) for this config. activerecord-multi-tenant provides multi-tenant data isolation at the application layer by rewriting queries. On the other hand, this gem provides the isolation at the database layer by RLS. Multi-layered security is important.
66
69
 
67
70
  ```ruby
68
- TenantLevelSecuriy.current_tenant_id { MultiTenant.current_tenant_id }
71
+ TenantLevelSecurity.current_tenant_id { MultiTenant.current_tenant_id }
69
72
  ```
70
73
 
71
74
  Do not query the database in this callback. As mentioned above, this callback is invoked at checking out a connection, so it may be called recursively.
@@ -79,15 +82,15 @@ CREATE POLICY tenant_policy ON employees
79
82
  AS PERMISSIVE
80
83
  FOR ALL
81
84
  TO PUBLIC
82
- USING (tenant_id::text = current_setting('tenant_level_security.tenant_id'))
83
- WITH CHECK (tenant_id::text = current_setting('tenant_level_security.tenant_id'))
85
+ USING (tenant_id = NULLIF(current_setting('tenant_level_security.tenant_id'), '')::integer)
86
+ WITH CHECK (tenant_id = NULLIF(current_setting('tenant_level_security.tenant_id'), '')::integer)
84
87
  ```
85
88
 
86
89
  In the table in which the policy is created, only the rows that match the current setting of `tenant_level_security.tenant_id` can be referenced. This value is set by `TenantLevelSecurity.with` etc.
87
90
 
88
91
  ```ruby
89
92
  # Set default tenant to "tenant2"
90
- TenantLevelSecuriy.current_tenant_id { tenant2.id }
93
+ TenantLevelSecurity.current_tenant_id { tenant2.id }
91
94
 
92
95
  TenantLevelSecurity.with(tenant1.id) do # => SET tenant_level_security.tenant_id = '1'
93
96
  Employee.pluck(:name)
@@ -121,6 +124,12 @@ end
121
124
 
122
125
  The middleware propagates the current tenant to the job through the session. This allows RLS to be enabled even within workers.
123
126
 
127
+ ## Multiple Databases
128
+
129
+ Active Record 6+ adds support for [multiple databases](https://guides.rubyonrails.org/active_record_multiple_databases.html). Note that when using multiple databases with this gem, you need to explicitly switch when connecting other databases.
130
+
131
+ In multiple databases, Active Record creates a connection pool for each connection, but `TenantLevelSecurity.switch` only switches for the current connection.
132
+
124
133
  ## Development
125
134
 
126
135
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -28,8 +28,8 @@ Gem::Specification.new do |spec|
28
28
  spec.add_dependency "activesupport", ">= 6.0"
29
29
  spec.add_dependency "pg", ">= 1.0"
30
30
 
31
- spec.add_development_dependency "bundler", "~> 2.0"
32
- spec.add_development_dependency "rake", "~> 10.0"
33
- spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "bundler", ">= 2.0"
32
+ spec.add_development_dependency "rake", ">= 10.0"
33
+ spec.add_development_dependency "rspec", ">= 3.0"
34
34
  spec.add_development_dependency "appraisal", "~> 2.4"
35
35
  end
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 7.0.2"
6
+
7
+ gemspec path: "../"
@@ -5,13 +5,14 @@ module TenantLevelSecurity
5
5
  ALTER TABLE #{table_name} ENABLE ROW LEVEL SECURITY;
6
6
  ALTER TABLE #{table_name} FORCE ROW LEVEL SECURITY;
7
7
  SQL
8
+ tenant_id_data_type = get_tenant_id_data_type(table_name)
8
9
  execute <<~SQL
9
10
  CREATE POLICY tenant_policy ON #{table_name}
10
11
  AS PERMISSIVE
11
12
  FOR ALL
12
13
  TO PUBLIC
13
- USING (tenant_id::text = current_setting('tenant_level_security.tenant_id'))
14
- WITH CHECK (tenant_id::text = current_setting('tenant_level_security.tenant_id'))
14
+ USING (tenant_id = NULLIF(current_setting('tenant_level_security.tenant_id'), '')::#{tenant_id_data_type})
15
+ WITH CHECK (tenant_id = NULLIF(current_setting('tenant_level_security.tenant_id'), '')::#{tenant_id_data_type})
15
16
  SQL
16
17
  end
17
18
 
@@ -24,5 +25,14 @@ module TenantLevelSecurity
24
25
  DROP POLICY tenant_policy ON #{table_name}
25
26
  SQL
26
27
  end
28
+
29
+ private
30
+ def get_tenant_id_data_type(table_name)
31
+ tenant_id_column = columns(table_name)
32
+ .find{|column| column.name == 'tenant_id'}
33
+ raise "tenant_id column is missing in #{table_name}" if tenant_id_column.nil?
34
+
35
+ tenant_id_column.sql_type
36
+ end
27
37
  end
28
38
  end
@@ -28,6 +28,8 @@ module TenantLevelSecurity
28
28
  end
29
29
 
30
30
  def switch_with_connection!(conn, tenant_id)
31
+ conn.clear_query_cache
32
+
31
33
  if tenant_id.present?
32
34
  conn.execute("SET tenant_level_security.tenant_id = '#{tenant_id}'")
33
35
  else
@@ -1,3 +1,3 @@
1
1
  module TenantLevelSecurity
2
- VERSION = '0.0.1'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-tenant-level-security
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SmartHR
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-10 00:00:00.000000000 Z
11
+ date: 2023-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -56,42 +56,42 @@ dependencies:
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '2.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '2.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '10.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '10.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '3.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '3.0'
97
97
  - !ruby/object:Gem::Dependency
@@ -120,6 +120,8 @@ files:
120
120
  - ".gitignore"
121
121
  - ".rspec"
122
122
  - Appraisals
123
+ - CHANGELOG.md
124
+ - CODE_OF_CONDUCT.jp.md
123
125
  - CODE_OF_CONDUCT.md
124
126
  - Gemfile
125
127
  - LICENSE.txt
@@ -130,6 +132,7 @@ files:
130
132
  - bin/setup
131
133
  - gemfiles/rails_6.0.gemfile
132
134
  - gemfiles/rails_6.1.gemfile
135
+ - gemfiles/rails_7.0.gemfile
133
136
  - lib/activerecord-tenant-level-security.rb
134
137
  - lib/activerecord-tenant-level-security/command_recorder.rb
135
138
  - lib/activerecord-tenant-level-security/schema_dumper.rb
@@ -143,8 +146,8 @@ licenses:
143
146
  metadata:
144
147
  homepage_uri: https://github.com/kufu/activerecord-tenant-level-security
145
148
  source_code_uri: https://github.com/kufu/activerecord-tenant-level-security
146
- changelog_uri: https://github.com/kufu/activerecord-tenant-level-security/blob/v0.0.1/CHANGELOG.md
147
- post_install_message:
149
+ changelog_uri: https://github.com/kufu/activerecord-tenant-level-security/blob/v0.2.0/CHANGELOG.md
150
+ post_install_message:
148
151
  rdoc_options: []
149
152
  require_paths:
150
153
  - lib
@@ -159,8 +162,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
162
  - !ruby/object:Gem::Version
160
163
  version: '0'
161
164
  requirements: []
162
- rubygems_version: 3.1.2
163
- signing_key:
165
+ rubygems_version: 3.2.33
166
+ signing_key:
164
167
  specification_version: 4
165
168
  summary: An Active Record extension for Multitenancy with PostgreSQL Row Level Security
166
169
  test_files: []