safety_patrol_padrino 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
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 safety_patrol_padrino.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Thomas Brewer
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.md ADDED
@@ -0,0 +1,63 @@
1
+ # SafetyPatrol Padrino
2
+ This is a set of helpers to make using SafeyPatrol in Padrino a little nicer
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'safety_patrol_padrino'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install safety_patrol_padrino
17
+
18
+ ## Usage
19
+
20
+ #checkout the Usage of the SafetyPatrol gem to see how to configure the basic authorization setup
21
+ #below is the usage specfic to Padrino
22
+
23
+ #in your app/app.rb file add:
24
+ register SafetyPatrol::Padrino
25
+
26
+ #in the config/boot.rb file inside the Padrino.before_load block add:
27
+ SafetyPatrol.configure do |config|
28
+
29
+ #set what method you application uses to get the currently logged in user
30
+ config.user_method = :current_user
31
+
32
+ #also set any extra abilities your application needs here
33
+ config.abilities[:send] = :sendable
34
+
35
+ end
36
+
37
+
38
+ #inside of a route definition call the can? or can! methods
39
+
40
+ post :create, map: '/users' do
41
+
42
+ user = User.new(params[:user])
43
+
44
+ halt 403 unless can?(:create, user)
45
+
46
+ or pass it a block
47
+ can?(:create, user) do |can_perform_action|
48
+ halt 403 unless can_perform_action
49
+ end
50
+
51
+ or use the can! method, it will raise a SafetyPatrol::Padrino::SafetyViolation error
52
+ can!(:create, user)
53
+
54
+ end
55
+
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ require 'safety_patrol'
2
+ require_relative 'padrino/helpers'
3
+ require_relative 'padrino/safety_violation'
4
+
5
+ module SafetyPatrol
6
+
7
+ module Padrino
8
+
9
+ VERSION = '0.5.0'
10
+
11
+ class << self
12
+ def registered(app)
13
+ app.helpers SafetyPatrol::Padrino::Helpers
14
+ end
15
+ alias :included :registered
16
+ end
17
+
18
+ end
19
+
20
+ class Configuration
21
+ attr_accessor :user_method
22
+ end
23
+
24
+ end
25
+
26
+
@@ -0,0 +1,27 @@
1
+ module SafetyPatrol
2
+
3
+ module Padrino
4
+
5
+ module Helpers
6
+
7
+ def can?(action, resource, &block)
8
+ can_perform_action = safety_patrol_user.send(("can_#{action.to_s}?").intern, resource)
9
+ yield(can_perform_action) if block_given?
10
+ can_perform_action
11
+ end
12
+
13
+ def can!(action, resource)
14
+ raise "can! cannot be passed a block" if block_given?
15
+ can_perform_action = can?(action, resource)
16
+ raise SafetyPatrol::Padrino::SafteyViolation.new(safety_patrol_user, action.to_s, resource) unless can_perform_action
17
+ end
18
+
19
+ def safety_patrol_user
20
+ send(SafetyPatrol.configuration.user_method)
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,23 @@
1
+ module SafetyPatrol
2
+
3
+ module Padrino
4
+
5
+ class SafteyViolation < StandardError
6
+
7
+ attr_reader :user, :action, :resource
8
+
9
+ def initialize(user, action, resource)
10
+ @user = user
11
+ @action = action
12
+ @resource = resource
13
+ end
14
+
15
+ def message
16
+ "#{@user.class} is not authorized to #{@action} this resource: #{@resource.class}"
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/safety_patrol/padrino', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Thomas Brewer"]
6
+ gem.email = ["tom@21purple.com"]
7
+ gem.description = %q{A few helpers for SafetyPatrol and Pardino}
8
+ gem.summary = gem.description
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "safety_patrol_padrino"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = SafetyPatrol::Padrino::VERSION
17
+ gem.add_development_dependency('rspec')
18
+ gem.add_dependency('safety_patrol')
19
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safety_patrol_padrino
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Brewer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70358359440340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70358359440340
25
+ - !ruby/object:Gem::Dependency
26
+ name: safety_patrol
27
+ requirement: &70358359439760 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70358359439760
36
+ description: A few helpers for SafetyPatrol and Pardino
37
+ email:
38
+ - tom@21purple.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - lib/safety_patrol/padrino.rb
49
+ - lib/safety_patrol/padrino/helpers.rb
50
+ - lib/safety_patrol/padrino/safety_violation.rb
51
+ - safety_patrol_padrino.gemspec
52
+ homepage: ''
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.10
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A few helpers for SafetyPatrol and Pardino
76
+ test_files: []