asserter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 314aba4c8680198293c6f038d9c9aedec8b409a6
4
+ data.tar.gz: b6b7a13e36b3d7ac633bb3c1af28954eb62aca19
5
+ SHA512:
6
+ metadata.gz: 9bd2443af1b3f3dc489c244c88e5709a5c03e7cae4dd17f55c260522f2fd22ce25a9f8fd4b418a80e7f4517683c362272e09692496932ed4805795aefef6a276
7
+ data.tar.gz: 043fff7e93088cbb9dd263d9ca47505f45d2143a1f41384f846156e9c5b7d4c525f152a1c59d22544b30f3a96517c9a3f632ceb1d02625e99718419c753898b4
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,8 @@
1
+ Metrics/LineLength:
2
+ Enabled: false
3
+
4
+ Metrics/BlockLength:
5
+ Enabled: false
6
+
7
+ Style/Documentation:
8
+ Enabled: false
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.4
5
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in asserter.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Loic Kartono
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
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.
@@ -0,0 +1,79 @@
1
+ # Asserter
2
+
3
+ Lightweight permissions asserter for Ruby applications.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'asserter', git: 'https://github.com/wamland-team/asserter'
11
+ ```
12
+
13
+ Use Bundler to install the dependency:
14
+
15
+ $ bundle install
16
+
17
+ ## Usage
18
+
19
+ Asserter is intended to be a lightweight and flexible way to grant permissions to
20
+ a given subject (a.k.a model/class). It imposes very few requirements on your application.
21
+
22
+ To get started add a `permissions` method to your class. This method must returns an array of permissions associated with that
23
+ subject. Once you have this method, include the `Asserter::Model` module in
24
+ your class to add the `permits?` method.
25
+
26
+ An example implementation is:
27
+
28
+ ```ruby
29
+ # app/models/user.rb
30
+ class User < ActiveRecord::Base
31
+ include Asserter::Model
32
+
33
+ def permissions
34
+ permissions.flat_map { |p| p[:name] }
35
+ end
36
+ end
37
+ ```
38
+
39
+ With this in place, a `Subject` can have a permission check performed, for
40
+ example:
41
+
42
+ ```ruby
43
+ class PartnersController < ApplicationController
44
+ def index
45
+ fail('Not allowed') unless current_user.permits?('wamadmin.partners.*.index')
46
+ end
47
+
48
+ def current_user
49
+ @current_user ||= User.find(session[:user_id])
50
+ end
51
+ end
52
+ ```
53
+
54
+ ## Permission
55
+
56
+ A permission is made of one or more colon-separated parts that
57
+ describe the action being permitted. Each part of the permission can be
58
+ either a wildcard (`*`), or a "term" of characters `a-z A-Z 0-9 _ -`
59
+
60
+ A wildcard in any position will match a single term in that position. A wildcard
61
+ in the last position will match any number of terms in the action string.
62
+
63
+ | permission | action | result |
64
+ |-----------------------|--------------------------|--------|
65
+ | `wamadmin` | `wamadmin` | permit |
66
+ | `wamadmin` | `gamelles` | deny |
67
+ | `wamadmin.users.show` | `wamadmin.users.show` | permit |
68
+ | `wamadmin.users.show` | `wamadmin.users.create` | deny |
69
+ | `*.users.index` | `wamadmin.users.index` | permit |
70
+ | `wamadmin.users.*` | `wamadmin.users.destroy` | permit |
71
+ | `wamadmin.*` | `wamadmin.users.update` | permit |
72
+ | `*.create` | `wamadmin.users.create` | deny |
73
+ | `*` | `wamadmin.users.create` | permit |
74
+ | `wamadmin.users.*` | `wamadmin.login` | deny |
75
+ | `wamadmin.*.*` | `wamadmin.login` | deny |
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wamland-team/asserter.
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'asserter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'asserter'
8
+ spec.version = Asserter::VERSION
9
+ spec.authors = ['Loic Kartono']
10
+ spec.email = ['kartono.loic@gmail.com']
11
+ spec.summary = 'Lightweight permissions asserter for Ruby applications'
12
+ spec.description = 'Lightweight permissions asserter for Ruby and Rails applications'
13
+ spec.homepage = 'https://github.com/wamland-team/asserter'
14
+ spec.license = 'MIT'
15
+
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+
23
+ spec.required_ruby_version = '>= 1.9.3'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.3'
26
+ spec.add_development_dependency 'rake', '~> 11.3'
27
+ spec.add_development_dependency 'rspec', '~> 3.5'
28
+ spec.add_development_dependency 'rubocop', '~> 0.47.1'
29
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'asserter'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ require 'asserter/version'
2
+
3
+ module Asserter
4
+ # Your code goes here...
5
+ end
6
+
7
+ require 'asserter/permission'
8
+ require 'asserter/model'
@@ -0,0 +1,15 @@
1
+ module Asserter
2
+ module Model
3
+ # Valid an action against an
4
+ # ActiveRecord object permissions.
5
+ #
6
+ # ==== Parameters
7
+ # * <tt>action</tt> - Action to validate.
8
+ #
9
+ # ==== Returns
10
+ # * <tt>Boolean</tt> - True if granted, false otherwise.
11
+ def permit?(action)
12
+ permissions.map { |p| Permission.new(p) }.any? { |p| p.permit?(action) }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ module Asserter
2
+ class Permission
3
+ # Initialize a new permission.
4
+ #
5
+ # ==== Parameters
6
+ # * <tt>permission</tt> - Permission
7
+ #
8
+ # ==== Returns
9
+ # * <tt>Asserter::Permission</tt> - New instance.
10
+ def initialize(permission)
11
+ @permission_terms = permission.split('.')
12
+ end
13
+
14
+ # Validate a permission against an action.
15
+ # Check each terms of a given permission against
16
+ # each terms of a given action and compare them.
17
+ #
18
+ # If one of the permission's terms contains a
19
+ # wildcard (*), we don't compare it and skip
20
+ # to the next iteration.
21
+ #
22
+ # ==== Parameters
23
+ # * <tt>action</tt> - Action to validate.
24
+ #
25
+ # ==== Returns
26
+ # * <tt>Boolean</tt> - True if permission by pass action.
27
+ def permit?(action)
28
+ action_terms = action.split('.', @permission_terms.length)
29
+ return false if action_terms.length != @permission_terms.length
30
+
31
+ @permission_terms.zip(action_terms).each do |p_term, a_term|
32
+ next if p_term == '*'
33
+ return false if p_term != a_term
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Asserter
2
+ VERSION = '1.0.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asserter
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Loic Kartono
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-09 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: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '11.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '11.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.47.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.47.1
69
+ description: Lightweight permissions asserter for Ruby and Rails applications
70
+ email:
71
+ - kartono.loic@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - asserter.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/asserter.rb
88
+ - lib/asserter/model.rb
89
+ - lib/asserter/permission.rb
90
+ - lib/asserter/version.rb
91
+ homepage: https://github.com/wamland-team/asserter
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 1.9.3
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Lightweight permissions asserter for Ruby applications
115
+ test_files: []