acts_as_authoritah 1.0.5 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.gitignore +15 -3
  2. data/.rspec +2 -0
  3. data/Gemfile +6 -0
  4. data/LICENSE +4 -2
  5. data/README.md +29 -0
  6. data/Rakefile +2 -56
  7. data/acts_as_authoritah.gemspec +15 -44
  8. data/lib/acts_as_authoritah.rb +15 -16
  9. data/lib/acts_as_authoritah/access_control_list.rb +32 -0
  10. data/lib/acts_as_authoritah/access_rule.rb +31 -0
  11. data/lib/acts_as_authoritah/core.rb +37 -0
  12. data/lib/acts_as_authoritah/identifier_parser.rb +28 -0
  13. data/lib/acts_as_authoritah/matchers/controller_matcher.rb +10 -0
  14. data/lib/acts_as_authoritah/matchers/direct_matcher.rb +9 -0
  15. data/lib/acts_as_authoritah/matchers/scope_matcher.rb +15 -0
  16. data/lib/acts_as_authoritah/spreadsheets/access_rights_mapper.rb +14 -0
  17. data/lib/acts_as_authoritah/spreadsheets/spreadsheet_header_parser.rb +9 -0
  18. data/lib/acts_as_authoritah/spreadsheets/spreadsheet_reader.rb +37 -0
  19. data/lib/acts_as_authoritah/spreadsheets/spreadsheet_row_parser.rb +23 -0
  20. data/lib/acts_as_authoritah/spreadsheets/spreadsheet_wrapper.rb +16 -0
  21. data/lib/acts_as_authoritah/version.rb +3 -0
  22. data/spec/acts_as_authoritah/access_control_list_spec.rb +78 -0
  23. data/spec/acts_as_authoritah/access_rule_spec.rb +39 -0
  24. data/spec/acts_as_authoritah/core_spec.rb +63 -0
  25. data/spec/acts_as_authoritah/identifier_parser_spec.rb +111 -0
  26. data/spec/acts_as_authoritah/matchers/controller_matcher_spec.rb +20 -0
  27. data/spec/acts_as_authoritah/matchers/direct_matcher_spec.rb +20 -0
  28. data/spec/acts_as_authoritah/matchers/scope_matcher_spec.rb +25 -0
  29. data/spec/acts_as_authoritah/spreadsheets/access_rights_mapper_spec.rb +13 -0
  30. data/spec/acts_as_authoritah/spreadsheets/spreadsheet_header_parser_spec.rb +8 -0
  31. data/spec/acts_as_authoritah/spreadsheets/spreadsheet_reader_spec.rb +29 -0
  32. data/spec/acts_as_authoritah/spreadsheets/spreadsheet_row_parser_spec.rb +24 -0
  33. data/spec/acts_as_authoritah/spreadsheets/spreadsheet_wrapper_spec.rb +15 -0
  34. data/spec/data/default.xls +0 -0
  35. data/spec/spec_helper.rb +5 -0
  36. metadata +91 -72
  37. data/.document +0 -5
  38. data/README.rdoc +0 -123
  39. data/VERSION +0 -1
  40. data/lib/access_control.rb +0 -30
  41. data/lib/access_rights.rb +0 -88
  42. data/lib/custom_exceptions.rb +0 -8
  43. data/lib/handler.rb +0 -38
  44. data/lib/loader.rb +0 -27
  45. data/test/acts_as_authoritah_test.rb +0 -23
  46. data/test/test_helper.rb +0 -22
data/.gitignore CHANGED
@@ -1,5 +1,17 @@
1
- *.sw?
2
- .DS_Store
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
3
9
  coverage
4
- rdoc
10
+ doc/
11
+ lib/bundler/man
5
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,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_authoritah.gemspec
4
+ gemspec
5
+ gem 'spreadsheet'
6
+
data/LICENSE CHANGED
@@ -1,4 +1,6 @@
1
- Copyright (c) 2009 Unnikrishnan KP
1
+ Copyright (c) 2012 Unnikrishnan KP
2
+
3
+ MIT License
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
@@ -17,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ActsAsAuthoritah
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'acts_as_authoritah'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install acts_as_authoritah
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 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile CHANGED
@@ -1,56 +1,2 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "acts_as_authoritah"
8
- gem.summary = %Q{role based access rights for a user specified via spreadsheet}
9
- gem.description = %Q{TODO: longer description of your gem}
10
- gem.email = "unni@bangthetable.com"
11
- gem.homepage = "https://github.com/bangthetable/acts_as_authoritah/"
12
- gem.authors = ["Unnikrishnan KP"]
13
- gem.add_development_dependency "thoughtbot-shoulda"
14
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
- end
16
- rescue LoadError
17
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
- end
19
-
20
- require 'rake/testtask'
21
- Rake::TestTask.new(:test) do |test|
22
- test.libs << 'lib' << 'test'
23
- test.pattern = 'test/**/*_test.rb'
24
- test.verbose = true
25
- end
26
-
27
- begin
28
- require 'rcov/rcovtask'
29
- Rcov::RcovTask.new do |test|
30
- test.libs << 'test'
31
- test.pattern = 'test/**/*_test.rb'
32
- test.verbose = true
33
- end
34
- rescue LoadError
35
- task :rcov do
36
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
- end
38
- end
39
-
40
- task :test => :check_dependencies
41
-
42
- task :default => :test
43
-
44
- require 'rake/rdoctask'
45
- Rake::RDocTask.new do |rdoc|
46
- if File.exist?('VERSION')
47
- version = File.read('VERSION')
48
- else
49
- version = ""
50
- end
51
-
52
- rdoc.rdoc_dir = 'rdoc'
53
- rdoc.title = "acts_as_authoritah #{version}"
54
- rdoc.rdoc_files.include('README*')
55
- rdoc.rdoc_files.include('lib/**/*.rb')
56
- end
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -1,48 +1,19 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
1
  # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/acts_as_authoritah/version', __FILE__)
5
3
 
6
- Gem::Specification.new do |s|
7
- s.name = %q{acts_as_authoritah}
8
- s.version = "1.0.5"
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Unnikrishnan KP"]
6
+ gem.email = ["unni.tallman@gmail.com"]
7
+ gem.description = %q{Define user capabilities in your app}
8
+ gem.summary = %q{Define user capabilities in your app}
9
+ gem.homepage = ""
10
+ gem.add_development_dependency "rspec"
11
+ gem.add_development_dependency "rake"
9
12
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["BangTheTable","Unnikrishnan KP"]
12
- s.date = %q{2010-08-10}
13
- s.description = %q{role based access rights for a user specified via spreadsheet}
14
- s.email = %q{devteam@bangthetable.com, unni.tallman@gmail.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".gitignore",
22
- "LICENSE",
23
- "README.rdoc",
24
- "Rakefile",
25
- "VERSION",
26
- "acts_as_authoritah.gemspec",
27
- "lib/acts_as_authoritah.rb",
28
- "lib/loader.rb",
29
- "lib/handler.rb",
30
- "lib/access_rights.rb",
31
- "lib/access_control.rb",
32
- "lib/custom_exceptions.rb",
33
- "test/acts_as_authoritah_test.rb",
34
- "test/test_helper.rb"
35
- ]
36
- s.homepage = %q{https://github.com/bangthetable/acts_as_authoritah/}
37
- s.rdoc_options = ["--charset=UTF-8"]
38
- s.require_paths = ["lib"]
39
- s.rubygems_version = %q{1.3.7}
40
- s.summary = %q{role based access rights for a user specified via spreadsheet}
41
- s.test_files = [
42
- "test/acts_as_authoritah_test.rb",
43
- "test/test_helper.rb"
44
- ]
45
-
46
- s.add_dependency(%q<spreadsheet>, [">= 0"])
47
-
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = "acts_as_authoritah"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = ActsAsAuthoritah::VERSION
48
19
  end
@@ -1,22 +1,21 @@
1
- require "active_record"
2
- require "active_support"
3
- require "spreadsheet"
1
+ require "acts_as_authoritah/version"
4
2
 
5
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ module ActsAsAuthoritah
4
+ module Matchers
5
+ end
6
+ end
6
7
 
7
- require "loader"
8
- require "handler"
9
- require "access_rights"
10
- require "access_control"
11
- require "custom_exceptions"
8
+ require "acts_as_authoritah/core"
12
9
 
13
- $LOAD_PATH.shift
10
+ require "acts_as_authoritah/access_rule"
11
+ require "acts_as_authoritah/access_control_list"
12
+ require "acts_as_authoritah/identifier_parser"
14
13
 
14
+ require 'spreadsheet'
15
15
  Spreadsheet.client_encoding = 'UTF-8'
16
16
 
17
-
18
- if defined?(ActiveRecord::Base)
19
- ActiveRecord::Base.extend ActsAsAuthoritah::Loader
20
- ActiveRecord::Base.send :include, ActsAsAuthoritah::Handler
21
- end
22
-
17
+ require "acts_as_authoritah/spreadsheets/access_rights_mapper"
18
+ require "acts_as_authoritah/spreadsheets/spreadsheet_wrapper"
19
+ require "acts_as_authoritah/spreadsheets/spreadsheet_reader"
20
+ require "acts_as_authoritah/spreadsheets/spreadsheet_header_parser"
21
+ require "acts_as_authoritah/spreadsheets/spreadsheet_row_parser"
@@ -0,0 +1,32 @@
1
+ require "acts_as_authoritah/matchers/direct_matcher.rb"
2
+ require "acts_as_authoritah/matchers/controller_matcher.rb"
3
+ require "acts_as_authoritah/matchers/scope_matcher.rb"
4
+
5
+
6
+ class ActsAsAuthoritah::AccessControlList
7
+ include ActsAsAuthoritah::Matchers
8
+
9
+ attr_reader :store
10
+
11
+ def initialize(access_rules)
12
+ @store = {}
13
+ access_rules.each do |access_rule|
14
+ @store.merge! access_rule.to_rule
15
+ end
16
+
17
+ @matchers = [DirectMatcher, ControllerMatcher, ScopeMatcher]
18
+ end
19
+
20
+ def match(identifier)
21
+ @matchers.each do |matcher|
22
+ access_rights = matcher.new(@store).match(identifier)
23
+ return access_rights unless access_rights.nil?
24
+ end
25
+ nil
26
+ end
27
+
28
+ def match_identifier(identifier)
29
+ match(identifier) || {}
30
+ end
31
+
32
+ end
@@ -0,0 +1,31 @@
1
+ class ActsAsAuthoritah::AccessRule
2
+ attr_reader :scope, :controller, :action, :access_rights
3
+
4
+ def initialize(scope, controller, action, access_rights)
5
+ @scope = scope
6
+ @controller = controller
7
+ @action = action
8
+ @access_rights = access_rights
9
+ end
10
+
11
+ def action?
12
+ @action && @action != ""
13
+ end
14
+
15
+ def controller?
16
+ @controller && @controller != ""
17
+ end
18
+
19
+ def scope?
20
+ @scope && @scope != ""
21
+ end
22
+
23
+ def to_rule
24
+ key = []
25
+ key << @scope if scope?
26
+ key << "#{@controller}Controller" if controller?
27
+ key = key.join('::')
28
+ key += "##{action}" if action?
29
+ { key => access_rights }
30
+ end
31
+ end
@@ -0,0 +1,37 @@
1
+ module ActsAsAuthoritah
2
+ module Core
3
+ def self.included(base)
4
+ base.send :include, ActsAsAuthoritah::Core::InstanceMethods
5
+ base.extend ActsAsAuthoritah::Core::ClassMethods
6
+ end
7
+ end
8
+ end
9
+
10
+
11
+ module ActsAsAuthoritah
12
+ module Core
13
+ module InstanceMethods
14
+ def can?(identifier, options={})
15
+ klass = self.class
16
+ klass.send(:default_acl).match_identifier(identifier)[self.usertype(options)] ||= klass.send(:whitelist)
17
+ end
18
+ end
19
+
20
+ module ClassMethods
21
+ def acts_as_authoritah(path, options={})
22
+ rules = ActsAsAuthoritah::SpreadsheetWrapper.new(path).to_access_rules
23
+ @@whitelist = options[:whitelist] ||= false
24
+ @@default_acl = ActsAsAuthoritah::AccessControlList.new(rules)
25
+ end
26
+
27
+ def default_acl
28
+ @@default_acl ||= ActsAsAuthoritah::AccessControlList.new(rules)
29
+ end
30
+
31
+ def whitelist
32
+ !@@whitelist
33
+ end
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,28 @@
1
+ class ActsAsAuthoritah::IdentifierParser
2
+ attr_reader :action, :controller, :scopes
3
+
4
+ def initialize(identifier)
5
+ @identifier = identifier
6
+ end
7
+
8
+ def action
9
+ return nil if @identifier.nil?
10
+ return nil unless @identifier.include?("#")
11
+ return nil if @identifier.split('').last == '#'
12
+ @identifier.split("#").last
13
+ end
14
+
15
+ def controller
16
+ return nil if @identifier.nil?
17
+ return nil if @identifier == ""
18
+ return @identifier.split('#').first
19
+ end
20
+
21
+ def scopes
22
+ return [] if @identifier.nil?
23
+ return [] if @identifier == ""
24
+ arr = @identifier.split('::')
25
+ arr.length > 1 ? arr[0..-2] : []
26
+ end
27
+
28
+ end
@@ -0,0 +1,10 @@
1
+ class ActsAsAuthoritah::Matchers::ControllerMatcher
2
+ def initialize(store)
3
+ @store = store
4
+ end
5
+
6
+ def match(identifier)
7
+ controller = ActsAsAuthoritah::IdentifierParser.new(identifier).controller
8
+ @store[controller]
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ class ActsAsAuthoritah::Matchers::DirectMatcher
2
+ def initialize(store)
3
+ @store = store
4
+ end
5
+
6
+ def match(identifier)
7
+ @store[identifier]
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ class ActsAsAuthoritah::Matchers::ScopeMatcher
2
+ def initialize(store)
3
+ @store = store
4
+ end
5
+
6
+ def match(identifier)
7
+ scopes = ActsAsAuthoritah::IdentifierParser.new(identifier).scopes
8
+ scopes.length.times do |i|
9
+ key = scopes[0..scopes.length-(i+1)].join("::")
10
+ val = @store[key]
11
+ return val if val
12
+ end
13
+ nil
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ class ActsAsAuthoritah::AccessRightsMapper
2
+ def initialize(roles, access_rights)
3
+ @roles = roles
4
+ @access_rights = access_rights
5
+ end
6
+
7
+ def map
8
+ h = {}
9
+ @roles.each_with_index do |role, i|
10
+ h[role] = @access_rights[i]
11
+ end
12
+ h
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ class ActsAsAuthoritah::SpreadsheetHeaderParser
2
+ def initialize(header_row)
3
+ @header_row = header_row
4
+ end
5
+
6
+ def roles
7
+ @header_row[4..-1]
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ class ActsAsAuthoritah::SpreadsheetReader
2
+ def initialize(path)
3
+ if File.exists?(path)
4
+ @book = Spreadsheet.open path
5
+ @sheet = @book.worksheets.first
6
+ else
7
+ @error = "File not found"
8
+ end
9
+ end
10
+
11
+ def valid?
12
+ @error.nil?
13
+ end
14
+
15
+ def valid_rows
16
+ i = 0
17
+ rows = []
18
+ @sheet.each{|row|
19
+ i = i + 1
20
+ next if i == 1
21
+ row = row.to_a
22
+ rows << row unless row.compact.empty?
23
+ }
24
+ rows
25
+ end
26
+
27
+ def header
28
+ i = 0
29
+ rows = []
30
+ @sheet.each{|row|
31
+ i = i + 1
32
+ break if i > 1
33
+ rows << row.to_a
34
+ }
35
+ rows.first
36
+ end
37
+ end