zero_authorization 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 950cc7e7cd5bfbc2957da19480b4cbeba39c6978
4
+ data.tar.gz: 2e5b43a11b77759a3544f430c95199e909731a9c
5
+ SHA512:
6
+ metadata.gz: b803db0659e93cc599c6c0b6a8a45c728e8a6677568ddcc0dcef277dae2dac24d5197f362312be7f1db05dc8eb8edd45f88823592866ba0c44823a3d1246ab50
7
+ data.tar.gz: f83f3552b4456fdd7444087bdc3598b1e9b4557b826085139d8cfad3820efd0b9af8d788ee11dff79136cc324b97dacb51d171b09cfa37ee0e5e5c90bc4360fe
data/.gitignore ADDED
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zero_authorization.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Rajeev Kannav Sharma
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 ADDED
@@ -0,0 +1 @@
1
+ This file was created by JetBrains RubyMine 5.4.3.2.1 for binding GitHub repository
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ZeroAuthorization
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'zero_authorization'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install zero_authorization
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,189 @@
1
+ require "zero_authorization/version"
2
+
3
+ module ZeroAuthorization
4
+
5
+ class Role
6
+ cattr_writer :role
7
+ # Initializing role
8
+ def initialize(role_name)
9
+ @role_name = role_name
10
+ end
11
+
12
+ def to_s
13
+ @role_name.to_s
14
+ end
15
+
16
+ # Getting rule_set(s) for the role
17
+ def rule_set
18
+ self.class.roles_n_privileges_hash["role_#{@role_name}".to_sym]
19
+ end
20
+
21
+ #Returns role if role can be formed/included in parsed hash's keys of parse_roles_n_privileges_yml
22
+ def self.role
23
+ roles_n_privileges_hash.keys.collect { |key| key.to_s.gsub(/^role_/, '') }.include?(@@role) ? new(@@role) : nil
24
+ end
25
+
26
+ # role_privileges_hash in place of yml
27
+ #TODO: Read it from YML and also provide functionality to reload it after caching
28
+ def self.roles_n_privileges_hash
29
+ @roles_n_privileges_hash ||= YAML::load_file(File.join(Rails.root, 'config', 'roles_n_privileges.yml'))
30
+ @roles_n_privileges_hash
31
+ end
32
+
33
+ def self.roles_n_privileges_hash_reload
34
+ @roles_n_privileges_hash = YAML::load_file(File.join(Rails.root, 'config', 'roles_n_privileges.yml'))
35
+ end
36
+
37
+ end
38
+
39
+ module Engine
40
+ def self.included(base)
41
+ puts "Initializing ZeroAuthorization for #{base.name}"
42
+
43
+ base.extend(ClassMethods)
44
+
45
+ # Initializing authentication mode. Options are
46
+ # :strict =>'raise exception and deny operation if not authorized' ,
47
+ # :warning => 'display only warning without exception',
48
+ # :superficial =>'allow operation without authorization'
49
+ base.send(:initialize_authorization_mode)
50
+
51
+ # Applying restriction on methods
52
+ base.send(:initialize_methods_restriction)
53
+
54
+ # Applying restriction on crud write operations
55
+ base.send(:before_save, :is_zero_authorized_4_save)
56
+ base.send(:before_create, :is_zero_authorized_4_create)
57
+ base.send(:before_update, :is_zero_authorized_4_update)
58
+ base.send(:before_destroy, :is_zero_authorized_4_destroy)
59
+
60
+
61
+ private
62
+
63
+ # Authorization for authorization mode :strict
64
+ def authorize_strictly(action)
65
+ role = ZeroAuthorization::Role.role
66
+ raise 'ZeroAuthorizationRoleNotAvailable' if role.nil?
67
+
68
+ if zero_authorized_core(role, action)
69
+ return true
70
+ else
71
+ logger.info 'ZeroAuthorization: Not authorized to perform activity.'
72
+ raise 'NotAuthorized'
73
+ end
74
+
75
+ false
76
+ end
77
+
78
+ # Authorization for authorization mode :warning
79
+ def authorize_with_warning(action)
80
+ role = ZeroAuthorization::Role.role
81
+ raise 'ZeroAuthorizationRoleNotAvailable' if role.nil?
82
+
83
+ if zero_authorized_core(role, action)
84
+ return true
85
+ else
86
+ logger.info 'ERROR: ZeroAuthorization: Not authorized to perform activity.'
87
+ end
88
+
89
+ false
90
+ end
91
+
92
+ # Authorization for authorization mode :superficial
93
+ def authorize_superficially(action)
94
+ logger.info 'ZeroAuthorizationMode: superficial. By passing authorization.'
95
+ return true
96
+ end
97
+
98
+ # Return authorization mode
99
+ def zero_authorized_checker(action)
100
+ if self.class.authorization_mode == :strict
101
+ return authorize_strictly(action)
102
+ elsif self.class.authorization_mode == :warning
103
+ return authorize_with_warning(action)
104
+ elsif self.class.authorization_mode == :superficial
105
+ return authorize_superficially(action)
106
+ else
107
+ raise 'InvalidAuthorizationMode'
108
+ end
109
+ end
110
+
111
+ # Core of authorization after reading/parsing rule set for current role
112
+ def zero_authorized_core(role, action)
113
+ _auth_flag = false
114
+ unless role.rule_set[:can_do].nil?
115
+ if role.rule_set[:can_do] == :anything
116
+ _auth_flag = true
117
+ elsif role.rule_set[:can_do].is_a?(Hash)
118
+ _auth_flag = true if (role.rule_set[:can_do][self.class.name.to_sym] || []).include?(action)
119
+ end
120
+ end
121
+ unless role.rule_set[:cant_do].nil?
122
+ if role.rule_set[:cant_do] == :anything
123
+ _auth_flag = false
124
+ elsif role.rule_set[:cant_do].is_a?(Hash)
125
+ _auth_flag = false if (role.rule_set[:cant_do][self.class.name.to_sym] || []).include?(action)
126
+ end
127
+ end
128
+ _auth_flag
129
+ end
130
+
131
+ def is_zero_authorized_4_save
132
+ zero_authorized_checker(:save)
133
+ end
134
+
135
+ def is_zero_authorized_4_create
136
+ zero_authorized_checker(:create)
137
+ end
138
+
139
+ def is_zero_authorized_4_update
140
+ zero_authorized_checker(:update)
141
+ end
142
+
143
+ def is_zero_authorized_4_destroy
144
+ zero_authorized_checker(:destroy)
145
+ end
146
+ end
147
+
148
+ module ClassMethods
149
+ attr_accessor :authorization_mode
150
+
151
+ def list_of_methods_to_guard
152
+ _model_methods_set = {}
153
+ Role.roles_n_privileges_hash.each do |role_key, permission_value|
154
+ unless permission_value[:can_do].nil?
155
+ _model_methods_set = _model_methods_set.merge(permission_value[:can_do]) { |key, oval, nval| ([oval] << [nval]).flatten.compact.uniq } if permission_value[:can_do].is_a?(Hash)
156
+ end
157
+ unless permission_value[:cant_do].nil?
158
+ _model_methods_set = _model_methods_set.merge(permission_value[:cant_do]) { |key, oval, nval| ([oval] << [nval]).flatten.compact.uniq } if permission_value[:cant_do].is_a?(Hash)
159
+ end
160
+ end
161
+
162
+ (_model_methods_set[self.name.to_sym] || []).clone.delete_if { |x| [:create, :save, :update, :destroy].include?(x) }
163
+ end
164
+
165
+ private
166
+ def initialize_authorization_mode
167
+ @authorization_mode = :strict # :strict, :warning and :superficial
168
+ end
169
+
170
+ # applying restriction on methods TODO 'it will be done by yml'
171
+ def initialize_methods_restriction
172
+ list_of_methods_to_guard.each do |method_name|
173
+ send(:alias_method, "za_#{method_name}", method_name)
174
+ define_method "#{method_name}" do |*args|
175
+ puts 'Restricted method call..'
176
+ send("za_#{method_name}", *args) if zero_authorized_checker(method_name)
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
182
+
183
+ end
184
+
185
+
186
+ Rails.application.eager_load!
187
+ ActiveRecord::Base.descendants.each do |descendant|
188
+ descendant.send(:include, ZeroAuthorization::Engine)
189
+ end
@@ -0,0 +1,6 @@
1
+ module Exceptions
2
+
3
+ #class ActivatedAlreadyError < StandardError;
4
+ #end
5
+
6
+ end
@@ -0,0 +1,3 @@
1
+ module ZeroAuthorization
2
+ VERSION = "0.0.1"
3
+ end
@@ -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 'zero_authorization/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zero_authorization"
8
+ spec.version = ZeroAuthorization::VERSION
9
+ spec.authors = ["Rajeev Kannav Sharma"]
10
+ spec.email = ["rajeevsharma86@gmail.com"]
11
+ spec.description = %q{Write a gem description}
12
+ spec.summary = %q{Write a gem summary}
13
+ spec.homepage = ""
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
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zero_authorization
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rajeev Kannav Sharma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-10 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: Write a gem description
42
+ email:
43
+ - rajeevsharma86@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README
52
+ - README.md
53
+ - Rakefile
54
+ - lib/zero_authorization.rb
55
+ - lib/zero_authorization/exceptions.rb
56
+ - lib/zero_authorization/version.rb
57
+ - zero_authorization.gemspec
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.3
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Write a gem summary
82
+ test_files: []