caber 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cf2b829a8c4545b9421f75c2dc2fbf08bbfa45e03dc01d6cc14cd9bc91c742ff
4
+ data.tar.gz: 66c199b0d561879e11467ba60723a7d5489c5e1824961a41a2497bb4e64654ac
5
+ SHA512:
6
+ metadata.gz: fb15e64a7fe5ba982b6d55eea3be6d7294f4064f1bb3b31641bc9577b36e3eafd2767442dcf4b287d6382aaf1fd0a26868fccb169a4c11dd436c4d217612eb0a
7
+ data.tar.gz: f1503f5ed8203e44b807529bc9028bc961437d6d684f11017e43536422a8dddcf06bf1e2f680116c7696d2ecc4983dee7eddfef7f19b440d112a4a36aee863d9
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Manyfold
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:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Caber
2
+
3
+ A simple ReBAC / Zanzibar gem for Rails apps.
4
+
5
+ ## Installation
6
+
7
+ Add caber to your Rails application's Gemfile:
8
+
9
+ ```
10
+ bundle add caber
11
+ ```
12
+
13
+ Then, run the installer:
14
+
15
+ ```
16
+ rails g caber:install
17
+ ```
18
+
19
+ This will create an initializer and a migration to add the `caber_relations` to your database.
20
+
21
+ Set up the permission types you want in the `config/initializers/caber.rb` file - a default is provided, but you can change it to whatever combination of permissions you'd like.
22
+
23
+ ## Usage
24
+
25
+ To use Caber, include `Caber::Subject` in any of your models that can be given permissions (e.g. Users), and include `Caber::Object` in the things that subjects are given permission TO (e.g. documents):
26
+
27
+ ```
28
+ class User < ApplicationRecord
29
+ include Caber::Subject
30
+ end
31
+
32
+ class Document < ApplicationRecord
33
+ include Caber::Object
34
+ end
35
+ ```
36
+
37
+ Now you're ready to grant some permissions! To give someone permission on something:
38
+
39
+ ```
40
+ document.grant_permission_to :view, user
41
+ ```
42
+
43
+ You can query permissions in both directions:
44
+ ```
45
+ document.grants_permission_to? :view, user
46
+ user.has_permission_to? :view, document
47
+ ```
48
+
49
+ ### Global permissions
50
+
51
+ To grant or query permissions globally (for instance, for a public view permission), you can use a `nil` subject:
52
+
53
+ ```
54
+ document.grant_permission_to :view, nil
55
+ ```
56
+
57
+ ## Development
58
+
59
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
60
+
61
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+
63
+ ## Contributing
64
+
65
+ 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.
66
+
67
+ ## Code of Conduct
68
+
69
+ 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).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
4
+ load "rails/tasks/engine.rake"
5
+ Bundler::GemHelper.install_tasks
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "rspec/core"
9
+ require "rspec/core/rake_task"
10
+
11
+ desc "Run all specs in spec directory (excluding plugin specs)"
12
+ RSpec::Core::RakeTask.new(spec: "app:db:test:prepare")
13
+
14
+ task default: :spec
@@ -0,0 +1,6 @@
1
+ class Caber::Relation < ActiveRecord::Base
2
+ belongs_to :subject, polymorphic: true, required: false
3
+ belongs_to :object, polymorphic: true
4
+
5
+ validates :permission, inclusion: {in: Caber.configuration.permissions}
6
+ end
@@ -0,0 +1,14 @@
1
+ module Caber::Object
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ end
6
+
7
+ def grant_permission_to(permission, subject)
8
+ Caber::Relation.create!(subject: subject, permission: permission, object: self)
9
+ end
10
+
11
+ def grants_permission_to?(permission, subject)
12
+ Caber::Relation.where(object: self, subject: [subject, nil], permission: permission).present?
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ module Caber::Subject
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ end
6
+
7
+ def has_permission_on?(permission, object)
8
+ object.grants_permission_to? permission, self
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ module Caber
2
+ class Configuration
3
+ @permissions = []
4
+ attr_accessor :permissions
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ module Caber
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Caber
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec, fixture: false
7
+ g.fixture_replacement :factory_bot, dir: "spec/factories"
8
+ g.assets false
9
+ g.helper false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Caber
2
+ VERSION = "0.1.0"
3
+ end
data/lib/caber.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "caber/version"
2
+ require "caber/engine"
3
+ require "caber/configuration"
4
+
5
+ module Caber
6
+ @@configuration = Caber::Configuration.new
7
+ mattr_reader :configuration
8
+
9
+ def self.configure
10
+ yield @@configuration
11
+ end
12
+ end
@@ -0,0 +1,27 @@
1
+ require "rails/generators"
2
+ require "rails/generators/active_record"
3
+
4
+ class Caber::InstallGenerator < Rails::Generators::Base
5
+ include ActiveRecord::Generators::Migration
6
+
7
+ source_root File.expand_path("templates/install", __dir__)
8
+
9
+ class_option :database, type: :string, aliases: %i[--db], desc: "The database for your migration. By default, the current environment's primary database is used."
10
+ class_option :skip_migrations, type: :boolean, default: nil, desc: "Skip migrations"
11
+
12
+ def create_initializer_file
13
+ copy_file "initializers/caber.rb", "config/initializers/caber.rb"
14
+ end
15
+
16
+ def create_migration_file
17
+ return if options[:skip_migrations]
18
+
19
+ migration_template "migrations/01_create_caber_relations.rb.erb", File.join("db/migrate/create_caber_relations.rb")
20
+ end
21
+
22
+ private
23
+
24
+ def migration_version
25
+ "[#{ActiveRecord::VERSION::STRING.to_f}]"
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ # Autogenerated configuration for Caber
2
+ Caber.configure do |config|
3
+ # List the object-level permissions you want
4
+ config.permissions = [
5
+ "viewer",
6
+ "editor",
7
+ "owner"
8
+ ]
9
+ end
@@ -0,0 +1,11 @@
1
+ class CreateCaberRelations < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :caber_relations do |t|
4
+ t.references :subject, polymorphic: true, null: true
5
+ t.string :permission
6
+ t.references :object, polymorphic: true, null: false
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: caber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Smith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.1.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.1.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: factory_bot_rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '6.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '6.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: standardrb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A simple ReBAC / Zanzibar gem for Rails apps.
70
+ email:
71
+ - james@floppy.org.uk
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - app/models/caber/relation.rb
80
+ - app/models/concerns/caber/object.rb
81
+ - app/models/concerns/caber/subject.rb
82
+ - lib/caber.rb
83
+ - lib/caber/configuration.rb
84
+ - lib/caber/engine.rb
85
+ - lib/caber/version.rb
86
+ - lib/generators/caber/install_generator.rb
87
+ - lib/generators/caber/templates/install/initializers/caber.rb
88
+ - lib/generators/caber/templates/install/migrations/01_create_caber_relations.rb.erb
89
+ homepage: https://github.com/manyfold3d/caber
90
+ licenses:
91
+ - MIT
92
+ metadata:
93
+ homepage_uri: https://github.com/manyfold3d/caber
94
+ source_code_uri: https://github.com/manyfold3d/caber
95
+ changelog_uri: https://github.com/manyfold3d/caber/releases
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.5.11
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: A simple ReBAC / Zanzibar gem for Rails apps.
115
+ test_files: []