acts_as_authoritah 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Unnikrishnan KP
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ 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.
data/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ = acts_as_authoritah
2
+
3
+ In config/environment.rb add
4
+ require 'acts_as_authoritah'
5
+
6
+ ActsAsAuthoritah::AccessRights::FileNotFound - if acl file is not found in the specified location
7
+
8
+ include ActsAsAuthoritah in application.rb
9
+
10
+ one file is mandatory - default.xls, or specify a :default =>
11
+
12
+ == Note on Patches/Pull Requests
13
+
14
+ * Fork the project.
15
+ * Make your feature addition or bug fix.
16
+ * Add tests for it. This is important so I don't break it in a
17
+ future version unintentionally.
18
+ * Commit, do not mess with rakefile, version, or history.
19
+ (if you want to have your own version, that is fine but
20
+ bump version in a commit by itself I can ignore when I pull)
21
+ * Send me a pull request. Bonus points for topic branches.
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2010 Bang The Table. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
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
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.1
@@ -0,0 +1,47 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{acts_as_authoritah}
8
+ s.version = "1.0.1"
9
+
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/custom_exceptions.rb",
32
+ "test/acts_as_authoritah_test.rb",
33
+ "test/test_helper.rb"
34
+ ]
35
+ s.homepage = %q{https://github.com/bangthetable/acts_as_authoritah/}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{role based access rights for a user specified via spreadsheet}
40
+ s.test_files = [
41
+ "test/acts_as_authoritah_test.rb",
42
+ "test/test_helper.rb"
43
+ ]
44
+
45
+ #s.add_dependency(%q<spreadsheet>, [">= 0"])
46
+
47
+ end
@@ -0,0 +1,88 @@
1
+ module ActsAsAuthoritah
2
+ def deny_unauthorized_access
3
+ if File.exists?("#{Rails.public_path}/401.html") and !request.xhr?
4
+ render :file => "#{Rails.public_path}/401.html", :status => :unauthorized and return
5
+ else
6
+ render :text => "access denied", :status => :unauthorized and return
7
+ end
8
+ end
9
+
10
+ module AccessRights
11
+ ACL = Hash.new
12
+ Default = Hash.new
13
+ Urls = {}
14
+
15
+ def self.feature_list
16
+ Default.keys.collect(&:downcase)
17
+ end
18
+
19
+ def self.urls(context)
20
+ h = {}
21
+ ACL[context].keys.collect(&:downcase).each do |feature|
22
+ h[feature] = access_url(feature)
23
+ end
24
+ h
25
+ end
26
+
27
+ def self.access_url(feature)
28
+ Urls[feature.downcase]
29
+ end
30
+
31
+ def self.contexts
32
+ ACL.keys.collect(&:downcase)
33
+ end
34
+
35
+ def self.load_all_files(dir,default_file="default.xls")
36
+ Dir.xls_files(dir).each do |file|
37
+ acl_type = file.split(".").first
38
+ ACL[acl_type] = load(File.join(dir,file))
39
+ end
40
+
41
+ (ACL[default_file.split(".").first] || {}).each_pair do |key,value|
42
+ Default[key] = value
43
+ end
44
+ end
45
+
46
+ def self.load(file)
47
+ hash = {}
48
+ book = Spreadsheet.open file
49
+ sheet = book.worksheets.first
50
+
51
+ urls_column_index = -1
52
+
53
+ usertypes = []
54
+
55
+ sheet.each do |row|
56
+ if row[0] == 'name'
57
+ k = 2
58
+ while true
59
+ usertype = row[k]
60
+ break unless usertype
61
+
62
+ usertypes << usertype unless usertype.downcase == "url"
63
+ urls_column_index = k if usertype.downcase == "url"
64
+
65
+ k += 1
66
+ end
67
+ usertypes = usertypes.collect(&:downcase)
68
+ next
69
+ end
70
+
71
+ h = Hash.new
72
+ feature_name = row[0]
73
+
74
+ next unless feature_name
75
+ feature_name.strip!
76
+
77
+ Urls[feature_name] = row[urls_column_index] unless urls_column_index == -1
78
+
79
+ usertypes.each_with_index do |key,i|
80
+ value = (row[i+2] and row[i+2].include?('x')) ? true : false
81
+ h[key]=value
82
+ end
83
+ hash[feature_name] = h
84
+ end
85
+ return hash
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,19 @@
1
+ require "active_record"
2
+ require "spreadsheet"
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+
6
+ require "loader"
7
+ require "handler"
8
+ require "access_rights"
9
+ require "custom_exceptions"
10
+
11
+ $LOAD_PATH.shift
12
+
13
+ Spreadsheet.client_encoding = 'UTF-8'
14
+
15
+ if defined?(ActiveRecord::Base)
16
+ ActiveRecord::Base.extend ActsAsAuthoritah::Loader
17
+ ActiveRecord::Base.send :include, ActsAsAuthoritah::Handler
18
+ end
19
+
@@ -0,0 +1,8 @@
1
+ module ActsAsAuthoritah
2
+ module AccessRights
3
+ class UnknownContext < RuntimeError; end
4
+ class AclFilesNotFound < RuntimeError; end
5
+ class AclFolderNotFound < RuntimeError; end
6
+ class RuleNotDefined < RuntimeError; end
7
+ end
8
+ end
data/lib/handler.rb ADDED
@@ -0,0 +1,38 @@
1
+ module ActsAsAuthoritah
2
+ module Handler
3
+ CAN_METHOD = /^can_(.*)/
4
+ class_eval do
5
+ def handle_can_methods(feature_name,args)
6
+ acl = AccessRights::Default.clone
7
+
8
+ if args.is_a?(Array) and !args.empty? and args.first[:context]
9
+ context = args.first[:context].to_s
10
+ raise ActsAsAuthoritah::AccessRights::UnknownContext unless AccessRights::contexts.include?(context)
11
+ acl.merge! AccessRights::ACL[context]
12
+ end
13
+
14
+ raise ActsAsAuthoritah::AccessRights::RuleNotDefined unless acl.has_key?(feature_name)
15
+
16
+ args.is_a?(Array) and !args.empty? ? acl[feature_name][usertype(args.first)] : acl[feature_name][usertype]
17
+ end
18
+
19
+ def method_missing(method_name,*args)
20
+ r1 = /^can_/
21
+ method_name = method_name.to_s
22
+
23
+ if method_name =~ CAN_METHOD
24
+ method_name.chop! if method_name[-1].chr == "?"
25
+ handle_can_methods(method_name.gsub(r1,""),args)
26
+ else
27
+ super(method_name.to_sym,*args)
28
+ end
29
+ end
30
+
31
+ def respond_to?(method)
32
+ return true if method.to_s =~ CAN_METHOD
33
+ super
34
+ end
35
+
36
+ end
37
+ end
38
+ end
data/lib/loader.rb ADDED
@@ -0,0 +1,27 @@
1
+ module ActsAsAuthoritah
2
+ module Loader
3
+
4
+ def acts_as_authoritah(args={})
5
+ if File.exists?(args[:acl_folder]) and Dir.has_xls_files?(args[:acl_folder])
6
+ if args[:default]
7
+ AccessRights::load_all_files(args[:acl_folder],args[:default])
8
+ else
9
+ AccessRights::load_all_files(args[:acl_folder])
10
+ end
11
+ elsif !Dir.has_xls_files?(args[:acl_folder])
12
+ raise ActsAsAuthoritah::AccessRights::AclFilesNotFound
13
+ else
14
+ raise ActsAsAuthoritah::AccessRights::AclFolderNotFound
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+
21
+ def Dir.xls_files(dir)
22
+ Dir.entries(dir).reject{|f| File.directory?(f)}.select{|x| x.split(".").last.downcase == "xls"}
23
+ end
24
+
25
+ def Dir.has_xls_files?(dir)
26
+ !Dir.xls_files(dir).empty?
27
+ end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ class ActsAsAuthoritahTest < Test::Unit::TestCase
4
+ def test_responds_to_all_can_methods
5
+ can_methods = ActsAsAuthoritah::AccessRights::feature_list.collect{|feature| 'can_' + feature + '?'}
6
+ assert_respond_to_all Factory.build(:anonymous_user),can_methods
7
+ end
8
+
9
+ def test_should_not_respond_to_any_non_existing_methods
10
+ assert !Factory.build(:anonymous_user).respond_to?(:non_existant_method)
11
+ end
12
+
13
+ def test_can_methods_should_function_as_per_the_access_rights_specified_in_spead_sheet
14
+ spreadsheet_hash = ActsAsAuthoritah::AccessRights::Default
15
+
16
+ spreadsheet_hash.each_pair {|feature,hash|
17
+ can_method = 'can_' + feature + '?'
18
+ hash.keys.each do |usertype|
19
+ assert_equal Factory.build(usertype + '_user').send(can_method),hash[usertype]
20
+ end
21
+ }
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'factory_girl'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'activerecord_test_connector'
8
+ require 'acts_as_authoritah'
9
+
10
+ ActiveRecordTestConnector.setup
11
+
12
+ #load users factory
13
+ require 'test/factories/users'
14
+
15
+ class Test::Unit::TestCase
16
+ protected
17
+ def assert_respond_to_all object, methods
18
+ methods.each do |method|
19
+ [method.to_s, method.to_sym].each { |m| assert_respond_to object, m }
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_authoritah
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
11
+ platform: ruby
12
+ authors:
13
+ - BangTheTable
14
+ - Unnikrishnan KP
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-08-10 00:00:00 +05:30
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: role based access rights for a user specified via spreadsheet
24
+ email: devteam@bangthetable.com, unni.tallman@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - LICENSE
31
+ - README.rdoc
32
+ files:
33
+ - .document
34
+ - .gitignore
35
+ - LICENSE
36
+ - README.rdoc
37
+ - Rakefile
38
+ - VERSION
39
+ - acts_as_authoritah.gemspec
40
+ - lib/acts_as_authoritah.rb
41
+ - lib/loader.rb
42
+ - lib/handler.rb
43
+ - lib/access_rights.rb
44
+ - lib/custom_exceptions.rb
45
+ - test/acts_as_authoritah_test.rb
46
+ - test/test_helper.rb
47
+ has_rdoc: true
48
+ homepage: https://github.com/bangthetable/acts_as_authoritah/
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.5.2
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: role based access rights for a user specified via spreadsheet
81
+ test_files:
82
+ - test/acts_as_authoritah_test.rb
83
+ - test/test_helper.rb