lite_access_control 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
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 ADDED
@@ -0,0 +1,43 @@
1
+ LiteAccessControl
2
+ =================
3
+
4
+ Simple access crontrol system
5
+
6
+ Install
7
+ =======
8
+
9
+
10
+
11
+
12
+ Example
13
+ =======
14
+
15
+ Controller:
16
+
17
+ class ApplicationController < ActionController::Base
18
+ include LiteAccessControl
19
+
20
+ before_filter :check_users_rights
21
+
22
+ set_rights(
23
+ :view_notifications => {:controller => "main", :actions => ["dashboard"]},
24
+ :manage_invoices => {:controller => "invoices", :actions => :all}
25
+ )
26
+
27
+
28
+
29
+ protected
30
+ def check_users_rights
31
+ access_control(current_user) if current_user
32
+ end
33
+ end
34
+
35
+ class User < AR
36
+
37
+ def permissions
38
+ [:manage_invoices, :view_notifications]
39
+ end
40
+ end
41
+
42
+
43
+ Copyright (c) 2009 Dmitry Penkin, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the lite_access_control plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the lite_access_control plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'LiteAccessControl'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ # Include hook code here
2
+ require File.dirname(__FILE__) + '/lib/lite_access_control'
3
+
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,62 @@
1
+ module LiteAccessControl
2
+
3
+ class AccessError < Exception; end
4
+ class AccessDenied < AccessError; end
5
+
6
+ module ClassMethods
7
+ def set_rights(rights)
8
+ write_inheritable_attribute :rights, rights
9
+ end
10
+ end
11
+
12
+ def self.included(base)
13
+ base.extend(ClassMethods)
14
+ end
15
+
16
+ def access_control(user)
17
+ required_rights = lookup_by_controller_and_action(controller_name, action_name)
18
+ return true if required_rights.blank?
19
+ raise AccessDenied if user.permissions.blank? || (user.permissions & required_rights).empty?
20
+ true
21
+ end
22
+
23
+ private
24
+ def rights
25
+ @rights ||= self.class.read_inheritable_attribute(:rights) || {}
26
+ end
27
+
28
+ def reversed_rights
29
+ reversed_rights = {}
30
+ rights.each do |r|
31
+ permission_name = r[0]
32
+ controller_name = r[1][:controller]
33
+ action_names = r[1][:actions]
34
+
35
+ tmp_rules_set = reversed_rights.fetch(controller_name, {})
36
+
37
+ if action_names == :all
38
+ tmp_rules_set[:all_controller_actions]= (tmp_rules_set[:all_controller_actions]||[] << permission_name.to_sym)
39
+ else
40
+ action_names.each do |a|
41
+ tmp_rules_set[a] = (tmp_rules_set[a] || [] ) << permission_name
42
+ end
43
+ end
44
+
45
+ reversed_rights[controller_name] = tmp_rules_set
46
+ end
47
+
48
+ reversed_rights
49
+ end
50
+
51
+ # Lookup rights name by controller name
52
+ def lookup_by_controller_and_action(controller, action)
53
+ actions = reversed_rights[controller.to_s]
54
+ if actions
55
+ required_actions = []
56
+ required_actions += actions[action] if actions[action]
57
+ required_actions += actions[:all_controller_actions] if actions[:all_controller_actions]
58
+ required_actions
59
+ end
60
+ end
61
+
62
+ end# LiteAccessControl
data/lib/version.rb ADDED
@@ -0,0 +1,9 @@
1
+ module LiteAccessControl #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ PKG_FILES = FileList[ '[a-zA-Z]*', 'generators/**/*', 'lib/**/*', 'rails/**/*', 'tasks/**/*', 'test/**/*' ]
5
+
6
+ spec = Gem::Specification.new do |s|
7
+ s.platform = Gem::Platform::RUBY
8
+
9
+ s.name = "lite_access_control"
10
+ s.version = "0.0.1"
11
+ s.summary = "Simple access control"
12
+
13
+ s.homepage = 'http://github.com/dmitryp/lite_access_control'
14
+ s.author = "Dmitry Penkin"
15
+ s.email = "dr.demax@gmail.com"
16
+
17
+ s.files = PKG_FILES.to_a
18
+ s.require_path = "lib"
19
+ s.has_rdoc = false
20
+ s.extra_rdoc_files = ["README"]
21
+
22
+ s.description = <<EOF
23
+ Simple access control system
24
+ EOF
25
+ end
26
+
27
+ desc 'Turn this plugin into a gem.'
28
+ Rake::GemPackageTask.new(spec) do |pkg|
29
+ pkg.gem_spec = spec
30
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__),'../init.rb')
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :lite_access_control do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class LiteAccessControlTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lite_access_control
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Penkin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-09 00:00:00 +07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: |
17
+ Simple access control system
18
+
19
+ email: dr.demax@gmail.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files:
25
+ - README
26
+ files:
27
+ - init.rb
28
+ - install.rb
29
+ - lite_access_control.gemspec
30
+ - MIT-LICENSE
31
+ - Rakefile
32
+ - README
33
+ - uninstall.rb
34
+ - lib/lite_access_control.rb
35
+ - lib/version.rb
36
+ - rails/init.rb
37
+ - tasks/lite_access_control_tasks.rake
38
+ - test/lite_access_control_test.rb
39
+ - test/test_helper.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/dmitryp/lite_access_control
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Simple access control
68
+ test_files: []
69
+