permify 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 490f5fb31ff867f2325a630790d2b45b9432f4d8
4
+ data.tar.gz: 25f219fb4cd5c68f88fb6bc06d4c90d5da689439
5
+ SHA512:
6
+ metadata.gz: aacb25ffb1f6592ca5feaa81ff927863fc73c8a8620f531e90a9ec40e5eaa4d5e654223bc291d3a675c6003f48f862286585d11c2c3eafada4880843154d6b6d
7
+ data.tar.gz: a5293ea18930c766c16091b43829988706401c11c8a472573122a523741599dd542182fbfa2ea697bdeea8b1e85bb2ccfa5b861e333951d509ff3bb3b50f4940
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in permify.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Maarten Claes
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Permify
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'permify'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install permify
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/permify/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :mutant do
9
+ system "bundle exec mutant --include lib -r permify --use rspec \"Permify*\""
10
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_support/all'
2
+
3
+ module Permify
4
+ end
5
+
6
+ require "permify/version"
7
+ require "permify/authorization"
8
+ require "permify/clearance"
9
+ require "permify/permission"
10
+ require "permify/permission_map"
11
+ require "permify/permission/dynamic"
12
+ require "permify/permission/null"
13
+ require "permify/permission/static"
14
+ require "permify/permission/combination"
15
+ require "permify/permission/combination_map"
16
+ require "permify/permission/combination/any"
17
+ require "permify/repository"
18
+
@@ -0,0 +1,17 @@
1
+ class Permify::Authorization
2
+ attr_reader :candidate
3
+ def initialize(candidate)
4
+ @candidate = candidate
5
+ end
6
+
7
+ def can?(action, resource)
8
+ permission = find_permission(resource, action)
9
+ permission.granted?(candidate, resource)
10
+ end
11
+
12
+ protected
13
+
14
+ def find_permission(resource, action)
15
+ candidate.permissions.find(resource, action)
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ # Clearance: Official authorization for something to proceed or take place
2
+ #
3
+ class Permify::Clearance
4
+ def initialize(repo)
5
+ @repo = repo
6
+ @permissions = Permify::PermissionMap.new
7
+ end
8
+
9
+ def add(resource, action)
10
+ permission = resolve_to_permission(resource, action)
11
+ permissions.store(resource, action, permission)
12
+ end
13
+
14
+ def find(resource, action)
15
+ if !repo.permission_combination?(resource, action)
16
+ permissions.find(resource, action) || Permify::Permission::Null.new
17
+ else
18
+ combination = repo.find_permission_combination(resource, action)
19
+ combination.resolve(self)
20
+ end
21
+ end
22
+
23
+ private
24
+ attr_reader :permissions, :repo
25
+
26
+ def resolve_to_permission(resource, action_name)
27
+ repo.find_permission(resource, action_name)
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ module Permify
2
+ class Permission
3
+ def initialize(*args)
4
+ end
5
+
6
+ def granted?(candidate, context)
7
+ false
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ class Permify::Permission
2
+ class Combination < self
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ class Permify::Permission::Combination
2
+ class Any < self
3
+ def initialize(permissions)
4
+ @permissions = permissions
5
+ end
6
+
7
+ def granted?(candidate, resource)
8
+ @permissions.any? { |perm|
9
+ perm.granted?(candidate, resource)
10
+ }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ class Permify::Permission::CombinationMap
2
+ def initialize(combinations)
3
+ @combinations = combinations
4
+ end
5
+
6
+ def resolve(clearance)
7
+ permissions = list_permissions(clearance)
8
+ Permify::Permission::Combination::Any.new(permissions)
9
+ end
10
+
11
+ private
12
+ attr_reader :combinations
13
+
14
+ def list_permissions(clearance)
15
+ permissions = []
16
+ combinations.each do |resource, actions|
17
+ [actions].flatten.each do |action|
18
+ permissions << clearance.find(resource, action)
19
+ end
20
+ end
21
+ permissions
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ class Permify::Permission
2
+ class Dynamic < self
3
+ def initialize(checks)
4
+ @checks = checks
5
+ end
6
+
7
+ def granted?(candidate, resource)
8
+ @checks.all? { |check|
9
+ check.new(candidate, resource).success?
10
+ }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ class Permify::Permission
2
+ class Null < self
3
+ def granted?(candidate, resource)
4
+ false
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class Permify::Permission
2
+ class Static < self
3
+ def granted?(candidate, resource)
4
+ true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ # Provides a convenient way to store and retrieve permission data
2
+ class Permify::PermissionMap
3
+ def initialize
4
+ @data = {}
5
+ end
6
+
7
+ def store(resource, action, info)
8
+ resource_key, action_key = to_keys(resource, action)
9
+ data[resource_key] ||= {}
10
+ data[resource_key][action_key] = info
11
+ end
12
+
13
+ def find(resource, action)
14
+ resource_key, action_key = to_keys(resource, action)
15
+ data.fetch(resource_key, {})[action_key]
16
+ end
17
+
18
+ private
19
+ attr_reader :data
20
+
21
+ # Transforms resource and action into keys for use in internal hashes
22
+ #
23
+ # @param resource [Class, Object] resource object or its class
24
+ # @param action [String, #to_sym]
25
+ #
26
+ # @return keys [Array<Symbol>] array with resource and action key
27
+ #
28
+ def to_keys(resource, action)
29
+ resource_class = resource.is_a?(Class) ? resource : resource.class
30
+ [resource_class.name, action].map(&:to_sym)
31
+ end
32
+ end
@@ -0,0 +1,68 @@
1
+ class Permify::Repository
2
+
3
+ # Define a static permission
4
+ #
5
+ # @param resource_class [Class]
6
+ # @param action [Symbol, String]
7
+ #
8
+ # @return [undefined]
9
+ #
10
+ def static(resource_class, action)
11
+ permission = Permify::Permission::Static.new
12
+ permissions.store(resource_class, action, permission)
13
+ end
14
+
15
+ # Define a dynamic permission
16
+ #
17
+ # @param resource_class [Class]
18
+ # @param action [Symbol, String]
19
+ #
20
+ # @return [undefined]
21
+ #
22
+ def dynamic(resource_class, action, *args)
23
+ permission = Permify::Permission::Dynamic.new args.flatten
24
+ permissions.store(resource_class, action, permission)
25
+ end
26
+
27
+ # Creates a permission that's granted whenever any of the combined
28
+ # permissions is granted
29
+ #
30
+ # @param resource_class [Class]
31
+ # @param action [String, #to_sym]
32
+ #
33
+ # @return [undefined]
34
+ #
35
+ def any(resource_class, action, combinations)
36
+ combination_map = Permify::Permission::CombinationMap.new(combinations)
37
+ permission_combinations.store(resource_class, action, combination_map)
38
+ end
39
+
40
+ # Finds permission in this repository
41
+ #
42
+ # @param resource [Class]
43
+ # @param action [String, #to_sym]
44
+ #
45
+ # @return permission [Permify::Permission, nil]
46
+ #
47
+ def find_permission(resource, action)
48
+ permissions.find(resource, action)
49
+ end
50
+
51
+ def permission_combination?(resource_class, action)
52
+ !!find_permission_combination(resource_class, action)
53
+ end
54
+
55
+ def find_permission_combination(resource, action)
56
+ permission_combinations.find(resource, action)
57
+ end
58
+
59
+ protected
60
+
61
+ def permissions
62
+ @permissions ||= Permify::PermissionMap.new
63
+ end
64
+
65
+ def permission_combinations
66
+ @permission_combinations ||= Permify::PermissionMap.new
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module Permify
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'permify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "permify"
8
+ spec.version = Permify::VERSION
9
+ spec.authors = ["Maarten Claes"]
10
+ spec.email = ["maartencls@gmail.com"]
11
+ spec.summary = %q{Manageable permissions}
12
+ spec.description = %q{Manageable permissions}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "mutant", "~> 0.5.6"
25
+ spec.add_development_dependency "mutant-rspec"
26
+
27
+ spec.add_dependency "activesupport"
28
+ end
@@ -0,0 +1,114 @@
1
+ require "spec_helper"
2
+
3
+ class Post
4
+ def initialize(owner)
5
+ @owner = owner
6
+ end
7
+
8
+ def owner?(user)
9
+ @owner == user
10
+ end
11
+ end
12
+
13
+ class OwnerCheck
14
+ attr_reader :candidate, :resource
15
+
16
+ def initialize(candidate, resource)
17
+ @candidate = candidate
18
+ @resource = resource
19
+ end
20
+
21
+ def success?
22
+ resource.owner?(candidate)
23
+ end
24
+ end
25
+
26
+ REPO = Permify::Repository.new
27
+
28
+ REPO.static(Post, :show)
29
+ REPO.static(Post, :create)
30
+
31
+ # :edit permission granted if either of its dependencies (:edit_all or
32
+ # :edit_own) is granted
33
+ REPO.any(Post, :edit, { Post => [:edit_all, :edit_own] })
34
+
35
+ REPO.static(Post, :edit_all)
36
+ REPO.dynamic(Post, :edit_own, [OwnerCheck])
37
+
38
+ class Admin
39
+ def permissions
40
+ Permify::Clearance.new(REPO).tap do |c|
41
+ c.add Post, :show
42
+ c.add Post, :create
43
+ c.add Post, :edit_all
44
+ end
45
+ end
46
+ end
47
+
48
+ class User
49
+ def permissions
50
+ Permify::Clearance.new(REPO).tap do |c|
51
+ c.add Post, :show
52
+ c.add Post, :create
53
+ c.add Post, :edit_own
54
+ end
55
+ end
56
+ end
57
+
58
+ describe Permify::Authorization, "integration" do
59
+ let(:post) { Post.new(post_owner) }
60
+ let(:post_owner) { user }
61
+ let(:admin) { Admin.new }
62
+ let(:user) { User.new }
63
+
64
+ describe "admin" do
65
+ let(:auth) { Permify::Authorization.new(admin) }
66
+
67
+ describe "static create" do
68
+ subject { auth.can?(:create, post)}
69
+ it { should be_true }
70
+ end
71
+
72
+ describe "static show" do
73
+ subject { auth.can?(:show, post)}
74
+ it { should be_true }
75
+ end
76
+
77
+ describe "static edit" do
78
+ subject { auth.can?(:edit, post)}
79
+ it { should be_true }
80
+ end
81
+ end
82
+
83
+ describe "user" do
84
+ let(:auth) { Permify::Authorization.new(user) }
85
+
86
+ describe "Post :edit_own" do
87
+ subject { auth.can?(:edit_own, post)}
88
+
89
+ context "when owner" do
90
+ let(:post_owner) { user }
91
+ it { should be_true }
92
+ end
93
+
94
+ context "when not owner" do
95
+ let(:post_owner) { admin }
96
+ it { should_not be_true }
97
+ end
98
+ end
99
+
100
+ describe "Post :edit" do
101
+ subject { auth.can?(:edit, post)}
102
+
103
+ context "when owner" do
104
+ let(:post_owner) { user }
105
+ it { should be_true }
106
+ end
107
+
108
+ context "when not owner" do
109
+ let(:post_owner) { admin }
110
+ it { should_not be_true }
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,13 @@
1
+ require 'permify'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+
8
+ # Run specs in random order to surface order dependencies. If you find an
9
+ # order dependency and want to debug it, you can fix the order by providing
10
+ # the seed, which is printed after each run.
11
+ # --seed 1234
12
+ config.order = 'random'
13
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: permify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Maarten Claes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-16 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mutant
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.6
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.6
69
+ - !ruby/object:Gem::Dependency
70
+ name: mutant-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Manageable permissions
98
+ email:
99
+ - maartencls@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/permify.rb
111
+ - lib/permify/authorization.rb
112
+ - lib/permify/clearance.rb
113
+ - lib/permify/permission.rb
114
+ - lib/permify/permission/combination.rb
115
+ - lib/permify/permission/combination/any.rb
116
+ - lib/permify/permission/combination_map.rb
117
+ - lib/permify/permission/dynamic.rb
118
+ - lib/permify/permission/null.rb
119
+ - lib/permify/permission/static.rb
120
+ - lib/permify/permission_map.rb
121
+ - lib/permify/repository.rb
122
+ - lib/permify/version.rb
123
+ - permify.gemspec
124
+ - spec/integration_spec.rb
125
+ - spec/spec_helper.rb
126
+ homepage: ''
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.2.2
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Manageable permissions
150
+ test_files:
151
+ - spec/integration_spec.rb
152
+ - spec/spec_helper.rb
153
+ has_rdoc: