marble_gate 0.0.1

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: ff34e67ac11212e2ca1b858eddb3b66082dba526
4
+ data.tar.gz: 36f4bdc022609b488950462dd5b5dc5aa4ec8ca4
5
+ SHA512:
6
+ metadata.gz: 8f7422219e6b1f071fba1eec3d410cd3761fc63be977ca120519a87679159d7779c01b4ebff8b8145ce7e6b38b63ddf75b9eec8d2eedc3d8da6579de72065a2a
7
+ data.tar.gz: 8095b1a31dc710a4846d6a493617b52f777d1070ecacc438980b76de7b82165f7ab161d9ea99f9ca924e362f2efb8ba9c9a79a39cad1b3e32a369539b8a2dde6
@@ -0,0 +1,19 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/config/database.yml
5
+ spec/dummy/db/*.sqlite3
6
+ spec/dummy/db/*.sqlite3-journal
7
+ spec/dummy/log/*.log
8
+ spec/dummy/tmp/
9
+ spec/dummy/.sass-cache
10
+
11
+ .rvmrc
12
+ doc/
13
+ .yardoc/
14
+ Gemfile.lock
15
+
16
+ tmp/
17
+
18
+ .ruby-gemset
19
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gate.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dmitry Novikov
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
+ # MarbleGate
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'marble_gate'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install marble_gate
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/marble_gate/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 a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ require "marble_gate/version"
2
+
3
+ require 'marble_gate/authorizer'
4
+ require 'marble_gate/railtie' if defined?(Rails)
5
+
6
+
7
+ module MarbleGate
8
+ # Your code goes here...
9
+ mattr_accessor :alias_map
10
+ @@alias_map = {
11
+ index: 'read',
12
+ show: 'read',
13
+ action_new: 'create',
14
+ edit: 'update'
15
+ }
16
+
17
+ # config.current_user_method_name = :current_user
18
+ mattr_accessor :current_user_method_name
19
+ @@current_user_method_name = :current_user
20
+
21
+ def self.setup
22
+ yield self
23
+ end
24
+ end
@@ -0,0 +1,59 @@
1
+ require 'active_support/inflector'
2
+
3
+ require 'marble_gate/errors'
4
+
5
+ module MarbleGate
6
+ def self.access(context, action, *args)
7
+ Access.new(context, (action == 'new') ? 'action_new' : action, *args)
8
+ end
9
+
10
+ class Access
11
+ attr_reader :context
12
+
13
+ def initialize(context, action, *args)
14
+ @context, @action = context, action.to_sym
15
+ @args = args
16
+ end
17
+
18
+ def for_model?(model)
19
+ return for_authorizer?(model) if model.is_a?(Class)
20
+
21
+ obj = auth_class(model.class).new(model)
22
+ obj.send(choose_action(obj), *@args)
23
+ end
24
+
25
+ def for_authorizer?(model_class)
26
+ cls = auth_class(model_class)
27
+ cls.send(choose_action(cls), *@args)
28
+ end
29
+
30
+ def user
31
+ context.send(MarbleGate.current_user_method_name)
32
+ end
33
+
34
+ private
35
+
36
+ def auth_class(model_class)
37
+ return @auth_class if @auth_class
38
+
39
+ _auth_class = "#{model_class.name}Authorizer"
40
+ @auth_class = _auth_class.safe_constantize
41
+ raise MarbleGate::MissingAuthorizer unless @auth_class
42
+
43
+ %i(user context).each { |m| @auth_class.send("#{m}=", send(m)) }
44
+ @auth_class.model_class = model_class
45
+ @auth_class
46
+ end
47
+
48
+ def choose_action(auth)
49
+ return @action if auth.respond_to?(@action)
50
+
51
+ new_action = MarbleGate.alias_map[@action]
52
+ return new_action if new_action && auth.respond_to?(new_action)
53
+
54
+ return :manage if auth.respond_to?(:manage)
55
+
56
+ :default
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,21 @@
1
+ require 'active_support/core_ext'
2
+
3
+ module MarbleGate
4
+ class Authorizer
5
+ class_attribute :user, :model_class, :context
6
+
7
+ attr_reader :model
8
+
9
+ def initialize(model)
10
+ @model = model
11
+ end
12
+
13
+ def self.default(*)
14
+ false
15
+ end
16
+
17
+ def default(*)
18
+ false
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ require 'active_support/inflector'
2
+
3
+ require 'marble_gate/authorizer'
4
+ require 'marble_gate/access'
5
+ require 'marble_gate/errors'
6
+
7
+ module MarbleGate
8
+ module BaseMethods
9
+ def authorize!(model = nil, *args)
10
+ unless model
11
+ raise MarbleGate::MissingModel unless respond_to?(:controller_name)
12
+ model = controller_name.classify.constantize
13
+ end
14
+ raise MarbleGate::AccessDenied unless can?(params[:action], model, *args)
15
+ end
16
+
17
+ def can?(action, model, *args)
18
+ # p "#{model.class.to_s}Authorizer".constantize
19
+ MarbleGate.access(self, action, *args).for_model?(model)
20
+ end
21
+
22
+ def cannot?(action, model, *args)
23
+ !can?(action, model, *args)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_support/inflector'
2
+
3
+ require 'marble_gate/authorizer'
4
+ require 'marble_gate/access'
5
+ require 'marble_gate/errors'
6
+
7
+ module MarbleGate
8
+ module ControllerClassMethods
9
+ def authorize!(cls = nil, *args, only: [], except: [])
10
+ cls = cls || controller_name.classify.constantize
11
+
12
+ before_action(only: only, except: except) do
13
+ res = MarbleGate.access(self, params[:action], *args).for_authorizer?(cls)
14
+ raise MarbleGate::AccessDenied unless res
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ module MarbleGate
2
+ class AccessDenied < RuntimeError
3
+ end
4
+
5
+ class MissingModel < StandardError
6
+ end
7
+
8
+ class MissingAuthorizer < StandardError
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ require 'marble_gate/base_methods'
2
+ require 'marble_gate/controller_class_methods'
3
+
4
+ module Dome
5
+ class Railtie < Rails::Railtie
6
+ initializer "action_view.initialize_marble_gate" do
7
+ ActiveSupport.on_load(:action_view) do
8
+ ActionView::Base.send :include, MarbleGate::BaseMethods
9
+ end
10
+ ActiveSupport.on_load(:action_controller) do
11
+ ActionController::Base.send :include, MarbleGate::BaseMethods
12
+ ActionController::Base.send :extend, MarbleGate::ControllerClassMethods
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module MarbleGate
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'marble_gate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "marble_gate"
8
+ spec.version = MarbleGate::VERSION
9
+ spec.authors = ["Dmitry Novikov"]
10
+ spec.email = ["glowinglens@gmail.com"]
11
+ spec.summary = %q{Simple ruby authorization library.}
12
+ spec.description = %q{Simple ruby authorization library.}
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_dependency "activesupport"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'marble_gate'
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: marble_gate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Novikov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec
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: Simple ruby authorization library.
70
+ email:
71
+ - glowinglens@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/marble_gate.rb
82
+ - lib/marble_gate/access.rb
83
+ - lib/marble_gate/authorizer.rb
84
+ - lib/marble_gate/base_methods.rb
85
+ - lib/marble_gate/controller_class_methods.rb
86
+ - lib/marble_gate/errors.rb
87
+ - lib/marble_gate/railtie.rb
88
+ - lib/marble_gate/version.rb
89
+ - marble_gate.gemspec
90
+ - spec/spec_helper.rb
91
+ homepage: ''
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: '0'
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.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Simple ruby authorization library.
115
+ test_files:
116
+ - spec/spec_helper.rb