caber 0.3.0 → 0.4.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: 71093a528267100a6e810a9906de6dc3bde29357d54f9b4557926c0d9ce5852b
4
- data.tar.gz: cdcfda7d716bc8e5f9fd307d803ce8eba968477e724a872e03f8e088c02da0a5
3
+ metadata.gz: f6bda935480f7b69fe515d693078cec12122b527fee405b593f83bda85dc6ba8
4
+ data.tar.gz: afde1293ded688ea93031794a4197465e9d799aa17c6ac8c8b0dca7897135e60
5
5
  SHA512:
6
- metadata.gz: 47d31508f48f5390523980e2507e6654412f4eca34e5af152cbb704fb4b7b72a35020189edf1afe41e04db3f88cd080f69f815a0546578e1719734c88a147b06
7
- data.tar.gz: 2b48730f693d2ca3425ae70d2df0f950dfaa6dc24a353cbcba06bd08696772ed3cf3c668b2cce7c28e2b99d536ef87ac79ba76adb342fc03e4d6bccd164f4f73
6
+ metadata.gz: fb3c8cbb9113114a4bb9d96686c839007b4809c445ead3a583d1f966a5ddb0aa1e824a612a49f31d517dcf167a56509327150182671b0d76ecee632a139c541e
7
+ data.tar.gz: 0dfa0445dbcfb9f78cf410b048b222809c6899ee31efe8ad12257468c1aac991ff552087913288638094e6f078bc853bf48db65ec551673dd99a76aa413e1e65
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Caber
2
2
 
3
- A simple ReBAC / Zanzibar gem for Rails apps.
3
+ ![Gem Downloads (for latest version)](https://img.shields.io/gem/dtv/caber)
4
+ ![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/rubygems/caber)
5
+ ![Dependent repos (via libraries.io)](https://img.shields.io/librariesio/dependent-repos/rubygems/caber)
6
+
7
+
8
+ A simple [ReBAC](https://en.wikipedia.org/wiki/Relationship-based_access_control) / [Zanzibar](https://research.google/pubs/zanzibar-googles-consistent-global-authorization-system/) backend plugin for Rails apps. Allows you to easily specify permission relationships between pairs of objects, e.g. granting edit permission on a document to a specific user, like in Google Docs.
4
9
 
5
10
  ## Installation
6
11
 
@@ -123,6 +128,49 @@ Document.granted_to "viewer", user
123
128
  # => All the documents that user has "viewer" permission on
124
129
  ```
125
130
 
131
+ ## Usage with other gems
132
+
133
+ ### Pundit
134
+
135
+ Caber makes for nice clear [Pundit](https://github.com/varvet/pundit) policies:
136
+
137
+ ```
138
+ class DocumentPolicy < ApplicationPolicy
139
+ class Scope < ApplicationPolicy::Scope
140
+ def resolve
141
+ scope.granted_to(["viewer", "editor", "owner"], user)
142
+ end
143
+ end
144
+
145
+ def update?
146
+ record.grants_permission_to? ["editor", "owner"], user
147
+ end
148
+ end
149
+ ```
150
+
151
+ ### Rolify
152
+
153
+ Caber doesn't include groups specifically, but you can integrate it easily with a role management gem like [Rolify](https://github.com/RolifyCommunity/rolify) pretty easily. Make your Role class a subject, and you can grant permissions to roles:
154
+
155
+ ```
156
+ class Document < ApplicationRecord
157
+ include Caber::Object
158
+ can_grant_permissions_to Role
159
+ end
160
+
161
+ class Role < ApplicationRecord
162
+ include Caber::Subject
163
+ can_have_permissions_on Document
164
+
165
+ scopify
166
+ end
167
+
168
+ document.grant_permission_to "editor", Role.find_by(name: "editor")
169
+
170
+ User.with_role(document.permitted_roles.with_permission("editor"))
171
+ # => all users with a role that can edit the document
172
+ ```
173
+
126
174
  ## Development
127
175
 
128
176
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
@@ -131,11 +179,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
131
179
 
132
180
  ## Contributing
133
181
 
134
- Bug reports and pull requests are welcome on GitHub at https://github.com/manyfold3d/caber. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
135
-
136
- ## Code of Conduct
137
-
138
- Everyone interacting in the Caber project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/manyfold3d/caber/blob/master/CODE_OF_CONDUCT.md).
182
+ Bug reports and pull requests are welcome on GitHub at https://github.com/manyfold3d/caber. This project is intended to be a safe, welcoming space for collaboration; everyone interacting in the Caber project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/manyfold3d/caber/blob/master/CODE_OF_CONDUCT.md).
139
183
 
140
184
  ## Acknowledgements
141
185
 
@@ -11,15 +11,16 @@ module Caber::Object
11
11
 
12
12
  scope :granted_to, ->(permission, subject) {
13
13
  includes(:caber_relations).where(
14
- "caber_relations.subject_id": subject.id,
15
- "caber_relations.subject_type": subject.class.name,
14
+ "caber_relations.subject": subject,
16
15
  "caber_relations.permission": permission
17
16
  )
18
17
  }
19
18
  end
20
19
 
21
20
  def grant_permission_to(permission, subject)
22
- Caber::Relation.create!(subject: subject, permission: permission, object: self)
21
+ rel = Caber::Relation.find_or_initialize_by(subject: subject, object: self)
22
+ rel.permission = permission
23
+ rel.save!
23
24
  end
24
25
 
25
26
  def grants_permission_to?(permission, subject)
data/lib/caber/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Caber
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -6,6 +6,7 @@ class CreateCaberRelations < ActiveRecord::Migration<%= migration_version %>
6
6
  t.references :object, polymorphic: true, null: false
7
7
 
8
8
  t.timestamps
9
+ t.index [:subject_id, :subject_type, :object_id, :object_type], unique: true
9
10
  end
10
11
  end
11
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-29 00:00:00.000000000 Z
11
+ date: 2024-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails