break_dance 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjM5MDQ4YzAwY2VhZjlhMTdiYmE5MzEwNjZjOWUyZTcwMGNjZDRhYQ==
5
+ data.tar.gz: !binary |-
6
+ Y2JhNmE0MzgzYzc3ZmJlMjdjYTk2NzJkZjU1OWM0MjgzMjgwMDBjYg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MjRmNjE1OWVkMGEwMDYxNGFhZTkxNjBhZDgwNDkxYWQ4MjA4OTBlYmYxZDk0
10
+ ZmM2NmQ0YzkxN2IwMjE4ODM2OTUyMGZjZTMyZWM4NmZkMjZlYmYyZWE1OGQw
11
+ NjMxNzllZTk0M2U3MjhkZjkyYTA5MmJiMTMzN2IxNGVjNmQwNzU=
12
+ data.tar.gz: !binary |-
13
+ NDE2OWNlNDFjMzY3OWQ0ZWU2NjUyYzYyMmU4MzUxYTE5YTBjOWYwYzQ5MDAx
14
+ MWJlODA3ZGZjMmM4N2JjZjEwMTc5OGM4ZDI3YmRmOTMyOGY5ZjAyOTQ0NGEz
15
+ MTBiNzFiZTFiYjA5ZWJlNTlhMjZjYWY5ZjlmOThlYjYwNGUzNzk=
data/.gitignore ADDED
@@ -0,0 +1,20 @@
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
18
+ .idea
19
+ .ruby-version
20
+ .ruby-gemset
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in break_dance.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Zlatko Zahariev
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # BreakDance
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'break_dance'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install break_dance
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
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
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'break_dance/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "break_dance"
8
+ spec.version = BreakDance::VERSION
9
+ spec.authors = ['Zlatko Zahariev']
10
+ spec.email = ['zlatko.zahariev@gmail.com']
11
+ spec.description = %q{Rails authorization gem.}
12
+ spec.summary = %q{Rails authorization for data-centric applications based on ActiveRecord.}
13
+ spec.homepage = 'https://github.com/notentered/breakdance'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,72 @@
1
+ module BreakDance
2
+ module ControllerAdditions
3
+ module ClassMethods
4
+ def enable_authorization!
5
+ before_filter :prepare_security_policy
6
+ before_filter :access_filter
7
+ end
8
+ end
9
+
10
+ def self.included(base)
11
+ base.extend ClassMethods
12
+ base.helper_method :can?, :cannot?
13
+ end
14
+
15
+ def with_authorization?
16
+ @with_authorization || false
17
+ end
18
+
19
+ def can?(action, resource)
20
+ return true unless with_authorization?
21
+
22
+ allowed_permissions = current_permissions['resources'].select { |_,v| v == '1'}
23
+
24
+ allowed = allowed_permissions.any? do |r|
25
+ Thread.current[:security_policy_holder].resources[r[0].to_sym] and Thread.current[:security_policy_holder].resources[r[0].to_sym][:can].any? do |k,v|
26
+ v = Array.wrap(v)
27
+ k == resource.to_sym && (
28
+ (
29
+ v.include?(:all_actions) &&
30
+ !(
31
+ Thread.current[:security_policy_holder].resources[r[0].to_sym][:except] &&
32
+ Thread.current[:security_policy_holder].resources[r[0].to_sym][:except][resource.to_sym] &&
33
+ Thread.current[:security_policy_holder].resources[r[0].to_sym][:except][resource.to_sym].include?(action.to_sym)
34
+ )
35
+ ) || v.include?(action.to_sym) )
36
+ end
37
+ end
38
+
39
+ allowed
40
+ end
41
+
42
+ def cannot?(action, resource)
43
+ !can?(action, resource)
44
+ end
45
+
46
+ def current_permissions
47
+ Permissions.for_user(current_user)
48
+ end
49
+
50
+ private
51
+
52
+ def prepare_security_policy
53
+ @with_authorization = true
54
+
55
+ Thread.current[:security_policy_holder] = BreakDance::SecurityPoliciesHolder.new
56
+
57
+ SecurityPolicy.new(current_user)
58
+ end
59
+
60
+ def access_filter
61
+ raise BreakDance::AccessDenied.new unless can?(self.action_name ,self.controller_path)
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ if defined? ActionController::Base
69
+ ActionController::Base.class_eval do
70
+ include BreakDance::ControllerAdditions
71
+ end
72
+ end
@@ -0,0 +1,4 @@
1
+ module BreakDance
2
+ class AccessDenied < StandardError
3
+ end
4
+ end
@@ -0,0 +1,10 @@
1
+ module BreakDance
2
+ class SecurityPoliciesHolder < BasicObject
3
+ attr_accessor :policies, :resources, :suppress_security_for
4
+
5
+ def initialize
6
+ @policies = {}
7
+ @resources = {}
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module BreakDance
2
+ module SecurityPolicyAdditions
3
+ def policy(name)
4
+ @policy_name = name
5
+ yield
6
+ end
7
+
8
+ def scope(model)
9
+ model_name = model.name
10
+ if @user and @user.permissions and @user.permissions['models'] and @user.permissions['models'].has_key? model_name and @user.permissions['models'][model_name] == @policy_name
11
+ Thread.current[:security_policy_holder].policies[model.name] = yield(model.unscoped)
12
+ end
13
+ end
14
+
15
+ def resource(key, resource)
16
+ Thread.current[:security_policy_holder].resources[key] = resource
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ module BreakDance
2
+ module SecurityScoping
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def scoped(options = nil)
7
+ scope = super(options)
8
+ return ActiveRecord::Relation.new(self, Arel::Table.new(table_name)) unless scope
9
+
10
+ sph = Thread.current[:security_policy_holder]
11
+ if sph
12
+ if sph.suppress_security_for == self.name
13
+ sph.suppress_security_for = nil
14
+ scope
15
+ else
16
+ scope.merge(sph.policies[self.name]).readonly(false)
17
+ end
18
+ else
19
+ scope
20
+ end
21
+ end
22
+
23
+ def unsecured
24
+ Thread.current[:security_policy_holder].suppress_security_for = self.name
25
+ scoped
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module BreakDance
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'break_dance/controller_additions'
2
+ require 'break_dance/exceptions'
3
+ require 'break_dance/security_policy_additions'
4
+ require 'break_dance/security_policies_holder'
5
+ require 'break_dance/security_scoping'
6
+ require 'break_dance/version'
7
+
8
+ ActiveRecord::Base.send(:include, BreakDance::SecurityScoping)
9
+
10
+ module BreakDance
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: break_dance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Zlatko Zahariev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-11 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: '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
+ description: Rails authorization gem.
42
+ email:
43
+ - zlatko.zahariev@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - break_dance.gemspec
54
+ - lib/break_dance.rb
55
+ - lib/break_dance/controller_additions.rb
56
+ - lib/break_dance/exceptions.rb
57
+ - lib/break_dance/security_policies_holder.rb
58
+ - lib/break_dance/security_policy_additions.rb
59
+ - lib/break_dance/security_scoping.rb
60
+ - lib/break_dance/version.rb
61
+ homepage: https://github.com/notentered/breakdance
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Rails authorization for data-centric applications based on ActiveRecord.
85
+ test_files: []