passpartu 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 864b7f8444aadbd01311482d1863c2c0438e11ac73db3bd041f39fc7f06e84b4
4
+ data.tar.gz: d59e1e169c073738cb8848d29db04ee01496dba9cfea4aa9b06b920ad0bb7ed0
5
+ SHA512:
6
+ metadata.gz: cfa6bc3c9f6a45e7b9f77242629b1220e4047ba675af31c601450c0470ea222c76177c93c01911928850a3180d25c123b0d86c1f03f3c2f6e05114d18b4d5b17
7
+ data.tar.gz: 7504541590f2eae641150f526e5499ce15aa69c6ad9bec2713cf1cc169d45ed3ce002aeaa3de169c8f09ff86ac7b3d6c7b344a6675013c2da60281091720f6f4
@@ -0,0 +1,113 @@
1
+ # Passpartu
2
+
3
+ Passpartu makes policies great again (works awesome with Pundit).
4
+
5
+ Instead of this:
6
+ ```ruby
7
+ class PostPolicy < ApplicationPolicy
8
+ def update?
9
+ user.super_admin? || user.admin? || user.manager? || user.supervisor?
10
+ end
11
+ end
12
+ ```
13
+
14
+ just this:
15
+ ```ruby
16
+ class PostPolicy < ApplicationPolicy
17
+ def update?
18
+ user.can?(:post, :update)
19
+ end
20
+ end
21
+ ```
22
+ ## Usage
23
+ Include `Passpartu` into your policy model.
24
+ ```ruby
25
+ class User
26
+ include Passpartu
27
+ end
28
+ ```
29
+ NOTE: Your `User` model must respond to `role` method that returns a string or a symbol!
30
+
31
+ Keep all your policies in one place.
32
+ Create `./config/passpartu.yml` and start writing your policies.
33
+
34
+ #### Example
35
+ ```yml
36
+ # ./config/passpartu.yml
37
+
38
+ admin:
39
+ post:
40
+ create: false
41
+ update: true
42
+ delete: true
43
+ order:
44
+ create: true
45
+ edit: true
46
+ delete: true
47
+ product:
48
+ create: false
49
+ edit: true
50
+ delete: true
51
+ manager:
52
+ order:
53
+ create: true
54
+ edit: true
55
+ delete: false
56
+ product:
57
+ create: true
58
+ edit: true
59
+ delete: false
60
+
61
+ ```
62
+
63
+
64
+ ## Configuration
65
+
66
+ You can configure Doorman by creating `./config/initializers/passpartu.rb`.
67
+
68
+ #### Default configs are:
69
+
70
+ ```ruby
71
+ Passpartu.configure do |config|
72
+ config.policy_file = './config/passpartu.yml'
73
+ config.raise_policy_missed_error = true
74
+ end
75
+ ```
76
+ ### Raise policy missed errors
77
+ By default Passpartu will raise an error if policy is missed in `passpartu.yml`. Set `config.raise_policy_missed_error = false` in order to return `false` in case when policy is not defined. This is a good approach to write only "positive" policies (only true) and automatically restricts everything that is not mentioned in `passpartu.yml`
78
+
79
+ ## Installation
80
+
81
+ Add this line to your application's Gemfile:
82
+
83
+ ```ruby
84
+ gem 'passpartu'
85
+ ```
86
+
87
+ And then execute:
88
+
89
+ $ bundle
90
+
91
+ Or install it yourself as:
92
+
93
+ $ gem install passpartu
94
+
95
+
96
+
97
+ ## Development
98
+
99
+ 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.
100
+
101
+ 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).
102
+
103
+ ## Contributing
104
+
105
+ Bug reports and pull requests are welcome on GitHub at https://github.com/OrestF/passpartu. 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.
106
+
107
+ ## License
108
+
109
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
110
+
111
+ ## Code of Conduct
112
+
113
+ Everyone interacting in the Doorman project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/OrestF/passpartu/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,49 @@
1
+ require 'passpartu/version'
2
+ require 'yaml'
3
+ require 'byebug'
4
+ require_relative 'passpartu/patcher'
5
+ require_relative 'passpartu/verify'
6
+ require_relative 'passpartu/validate_result'
7
+ require_relative 'passpartu/user' # for testing only
8
+
9
+ module Passpartu
10
+ class Error < StandardError; end
11
+
12
+ def self.included(policy_class)
13
+ Passpartu::Patcher.call(policy_class)
14
+ end
15
+
16
+ def self.policy
17
+ config.policy
18
+ end
19
+
20
+ class << self
21
+ attr_accessor :config
22
+ end
23
+
24
+ def self.configure
25
+ self.config ||= Config.new
26
+ yield(config)
27
+ end
28
+
29
+ class Config
30
+ attr_accessor :policy, :raise_policy_missed_error
31
+ attr_reader :policy_file
32
+
33
+ def initialize
34
+ @policy_file = './config/passpartu.yml'
35
+ @policy = YAML.load_file(policy_file)
36
+ @raise_policy_missed_error = true
37
+ end
38
+
39
+ def policy_file=(file = nil)
40
+ @policy_file = file || './config/passpartu.yml'
41
+ @policy = YAML.load_file(policy_file)
42
+ end
43
+ end
44
+
45
+ configure {}
46
+ end
47
+
48
+ initializer = './config/initializers/passpartu.rb'
49
+ require initializer if File.exist?(initializer)
@@ -0,0 +1,22 @@
1
+ require 'byebug'
2
+
3
+ module Passpartu
4
+ class Patcher
5
+ attr_reader :klass
6
+ def initialize(klass)
7
+ @klass = klass
8
+ end
9
+
10
+ def self.call(klass)
11
+ new(klass).call
12
+ end
13
+
14
+ def call
15
+ klass.class_eval do
16
+ define_method('can?') do |*keys|
17
+ Passpartu::Verify.call(role, keys)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ # for testing only
2
+
3
+ module Passpartu
4
+ class User
5
+ attr_reader :role
6
+
7
+ def initialize(role)
8
+ @role = role
9
+ end
10
+ end
11
+
12
+ class Person
13
+ attr_reader :role
14
+
15
+ def initialize(role)
16
+ @role = role
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ # require 'byebug'
2
+
3
+ module Passpartu
4
+ class ValidateResult
5
+ class PolicyMissedError < StandardError; end
6
+
7
+ attr_reader :result
8
+ def initialize(result)
9
+ @result = result
10
+ end
11
+
12
+ def self.call(result)
13
+ new(result).call
14
+ end
15
+
16
+ def call
17
+ raise PolicyMissedError if raise_error?
18
+ return false unless boolean?
19
+
20
+ result
21
+ end
22
+
23
+ private
24
+
25
+ def boolean?
26
+ [TrueClass, FalseClass].include?(result.class)
27
+ end
28
+
29
+ def raise_error?
30
+ !boolean? && Passpartu.config.raise_policy_missed_error
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ # require 'byebug'
2
+
3
+ module Passpartu
4
+ class Verify
5
+ attr_reader :role, :keys
6
+ def initialize(role, keys)
7
+ @role = role
8
+ @keys = keys.map(&:to_s)
9
+ end
10
+
11
+ def self.call(role, keys)
12
+ new(role, keys).call
13
+ end
14
+
15
+ def call
16
+ Passpartu::ValidateResult.call(Passpartu.policy.dig(role.to_s, *keys))
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Passpartu
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: passpartu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - OrestF
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: |-
70
+ Passpartu is a great tool to manage your policies. \n
71
+ Keep all your policy rules in one file - doorman.yml. \n
72
+ And ask user not about it's role, but about it's permissions
73
+ email:
74
+ - falchuko@gmail.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - README.md
80
+ - lib/passpartu.rb
81
+ - lib/passpartu/patcher.rb
82
+ - lib/passpartu/user.rb
83
+ - lib/passpartu/validate_result.rb
84
+ - lib/passpartu/verify.rb
85
+ - lib/passpartu/version.rb
86
+ homepage: https://github.com/OrestF/passpartu
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 2.0.0
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.7.7
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Passpartu makes policies great again
110
+ test_files: []