permitted 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ .rvmrc
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in permitted.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Gabriel Sobrinho
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.
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the permitted plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ end
@@ -0,0 +1,7 @@
1
+ require 'permitted/railtie' if defined? Rails::Railtie
2
+
3
+ module Permitted
4
+ autoload :Ability, 'permitted/ability'
5
+ autoload :AccessDenied, 'permitted/access_denied'
6
+ autoload :ActionController, 'permitted/action_controller'
7
+ end
@@ -0,0 +1,17 @@
1
+ module Permitted
2
+ class Ability
3
+ def initialize(user)
4
+ @user = user
5
+ end
6
+
7
+ def permit!(action, subject)
8
+ raise AccessDenied unless permitted_to?(action, subject)
9
+ end
10
+
11
+ def permitted_to?(action, subject)
12
+ @user.roles.any? do |role|
13
+ role.subject.to_s == subject.to_s && role.action.to_s == action.to_s
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ module Permitted
2
+ class AccessDenied < StandardError
3
+ end
4
+ end
@@ -0,0 +1,11 @@
1
+ module Permitted
2
+ module ActionController
3
+ def self.included(controller)
4
+ controller.delegate :permit!, :permitted_to?, :to => :current_ability
5
+ end
6
+
7
+ def current_ability
8
+ @current_ability ||= Permitted::Ability.new(current_user)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Permitted
2
+ class Railtie < Rails::Railtie
3
+ initializer 'permitted.action_controller' do
4
+ ActiveSupport.on_load(:action_controller) do
5
+ include Permitted::ActionController
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Permitted
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "permitted/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "permitted"
7
+ s.version = Permitted::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Gabriel Sobrinho"]
10
+ s.email = ["gabriel.sobrinho@gmail.com"]
11
+ s.homepage = "https://github.com/sobrinho/permitted"
12
+ s.summary = %q{Authorization for ruby apps using roles concept}
13
+ s.description = %q{Authorization for ruby apps using roles concept}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency %q{railties}, [">= 3.0.0"]
21
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class AbilityTest < Test::Unit::TestCase
4
+ def setup
5
+ @role = Role.new(:action => 'read', :subject => 'users')
6
+ @user = User.new(:name => 'sobrinho', :roles => [@role])
7
+ @ability = Permitted::Ability.new(@user)
8
+ end
9
+
10
+ def test_permitted_to
11
+ assert @ability.permitted_to?(:read, :users)
12
+ assert !@ability.permitted_to?(:read, :profiles)
13
+ end
14
+
15
+ def test_permit
16
+ assert_nothing_raised do
17
+ @ability.permit!(:read, :users)
18
+ end
19
+
20
+ assert_raise Permitted::AccessDenied do
21
+ @ability.permit!(:read, :profiles)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class ActionControllerTest < Test::Unit::TestCase
4
+ def setup
5
+ @controller = UsersController.new
6
+ end
7
+
8
+ def test_current_ability
9
+ assert @controller.respond_to?(:current_ability)
10
+ end
11
+
12
+ def test_permitted_to
13
+ assert @controller.respond_to?(:permitted_to?)
14
+ end
15
+
16
+ def test_permit
17
+ assert @controller.respond_to?(:permit!)
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ module Permitted
2
+ class Application < Rails::Application
3
+ config.active_support.deprecation = :stderr
4
+ end
5
+ end
6
+
7
+ Permitted::Application.initialize!
@@ -0,0 +1,4 @@
1
+ require 'action_controller'
2
+
3
+ class UsersController < ActionController::Base
4
+ end
@@ -0,0 +1,7 @@
1
+ require 'ostruct'
2
+
3
+ class Role < OpenStruct
4
+ end
5
+
6
+ class User < OpenStruct
7
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'test/unit'
4
+ require 'rails'
5
+ require 'permitted'
6
+ require 'support/application'
7
+ require 'support/models'
8
+ require 'support/controllers'
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: permitted
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Gabriel Sobrinho
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-05 00:00:00 -03:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: railties
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 3.0.0
25
+ type: :development
26
+ version_requirements: *id001
27
+ description: Authorization for ruby apps using roles concept
28
+ email:
29
+ - gabriel.sobrinho@gmail.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - MIT-LICENSE
40
+ - Rakefile
41
+ - lib/permitted.rb
42
+ - lib/permitted/ability.rb
43
+ - lib/permitted/access_denied.rb
44
+ - lib/permitted/action_controller.rb
45
+ - lib/permitted/railtie.rb
46
+ - lib/permitted/version.rb
47
+ - permitted.gemspec
48
+ - test/ability_test.rb
49
+ - test/action_controller_test.rb
50
+ - test/support/application.rb
51
+ - test/support/controllers.rb
52
+ - test/support/models.rb
53
+ - test/test_helper.rb
54
+ has_rdoc: true
55
+ homepage: https://github.com/sobrinho/permitted
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.5.2
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Authorization for ruby apps using roles concept
82
+ test_files:
83
+ - test/ability_test.rb
84
+ - test/action_controller_test.rb
85
+ - test/support/application.rb
86
+ - test/support/controllers.rb
87
+ - test/support/models.rb
88
+ - test/test_helper.rb