gate_police 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/app/helpers/gate_police_helper.rb +44 -0
- data/gate_police.gemspec +17 -0
- data/lib/gate_police.rb +9 -0
- data/lib/gate_police/engine.rb +4 -0
- data/lib/gate_police/exceptions.rb +13 -0
- data/lib/gate_police/permission_handler.rb +88 -0
- data/lib/gate_police/version.rb +3 -0
- metadata +55 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e08061a3539a5fb0e2fd045e346556d2eb554ff6
|
4
|
+
data.tar.gz: f2c8f366e6e45c870a9c38ce6c4e587d356c5e17
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 00b9d4565138d046284f2c9312d279e00005e521cc1c327d0dda094ee2ee43f792f0f880a03fd66b09cf03e7b8247853e0a105cc67a923630a41a14513a86a67
|
7
|
+
data.tar.gz: 41103d4595b909b2ddec428d827300fd20a9589a56351309590c0d45d53b33893bfb6e8665260d6587fad45a2ac2db4278cd0540d57528c13d948e555ad08096
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 miio
|
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,29 @@
|
|
1
|
+
# GatePolice
|
2
|
+
|
3
|
+
Permission for rails library.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'gate_police'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install gate_police
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO
|
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
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module GatePoliceHelper
|
2
|
+
# Check permission
|
3
|
+
#
|
4
|
+
# @note
|
5
|
+
# You need define check user
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# alias_method :permission_user, :current_user
|
9
|
+
#
|
10
|
+
# How to use
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# class MaterialsController < InheritedResources::Base
|
14
|
+
# before_filter do
|
15
|
+
# require_permission :material, params[:id]
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# @param [Symbol] model_sym target model and handlers prefix
|
20
|
+
# @param [Integer] object_id target primary key
|
21
|
+
# @raise [PermissionDenied] raise for hasn't permissiona
|
22
|
+
def require_permission model_sym, object_id=nil
|
23
|
+
model_name = "#{model_sym.capitalize.to_s}"
|
24
|
+
class_name = "#{model_name}PermissionHandler"
|
25
|
+
permission = params[:action].to_sym
|
26
|
+
require "#{Rails.root}/app/permissions/application_permission_handler"
|
27
|
+
require "#{Rails.root}/app/permissions/#{model_sym.capitalize.to_s}_permission_handler"
|
28
|
+
handler = Module.const_get(class_name).new
|
29
|
+
handler.user = self.permission_user
|
30
|
+
handler.require_permission Module.const_get(model_name), permission, object_id, params
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Render for permission denied action
|
35
|
+
#
|
36
|
+
# @note
|
37
|
+
# You need append ApplicationController
|
38
|
+
# rescue_from GatePolice::PermissionDenied, with: :call_denied_action
|
39
|
+
#
|
40
|
+
# @param [Exception] exception exception instance
|
41
|
+
def call_denied_action exception=nil
|
42
|
+
exception.handler.denied_action self
|
43
|
+
end
|
44
|
+
end
|
data/gate_police.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/gate_police/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["miio"]
|
6
|
+
gem.email = ["info@miio.info"]
|
7
|
+
gem.description = %q{Permission for rails library}
|
8
|
+
gem.summary = %q{Permission for rails library}
|
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 = "gate_police"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = GatePolice::VERSION
|
17
|
+
end
|
data/lib/gate_police.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
module GatePolice
|
2
|
+
# Permission handling classes.
|
3
|
+
#
|
4
|
+
# Check permission for has_perm? method.
|
5
|
+
# Call from require_permission.
|
6
|
+
#
|
7
|
+
# How call you'r permission handler?
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
#
|
11
|
+
# class MaterialsController < ApplicationController
|
12
|
+
# require_permission :material
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# call MaterialPermissionHandler.
|
16
|
+
#
|
17
|
+
# How to define permission logic.
|
18
|
+
#
|
19
|
+
# @example
|
20
|
+
#
|
21
|
+
# class MaterialPermissionHandler < ApplicationPermissionHandler
|
22
|
+
# CODE_NAMES = [
|
23
|
+
# :show, :update, :delete
|
24
|
+
# ]
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# @note
|
28
|
+
# CODE_NAMES is check target action. if need check permission. You need define action into CODE_NAMES.
|
29
|
+
#
|
30
|
+
# Permission logic need implements has_perm? method
|
31
|
+
#
|
32
|
+
# @example
|
33
|
+
#
|
34
|
+
# class MaterialPermissionHandler < ApplicationPermissionHandler
|
35
|
+
# CODE_NAMES = [
|
36
|
+
# :show, :update, :delete, :edit
|
37
|
+
# ]
|
38
|
+
# def has_perm? user, code_name, obj, params
|
39
|
+
# # Accept author.
|
40
|
+
# return true if user == obj.user
|
41
|
+
# case code_name
|
42
|
+
# when :show
|
43
|
+
# return true if obj and obj.scope == Material::SCOPE_PUBLIC
|
44
|
+
# return true if obj.scope == Material::SCOPE_GROUP and ProjectMember.where(project_id: obj.project_id, user_id: user.id).first
|
45
|
+
# when :edit, :update, :delete
|
46
|
+
# # Reject guest
|
47
|
+
# return false if user.nil?
|
48
|
+
# # Accept project member if public or group
|
49
|
+
# if obj and (obj.scope == Material::SCOPE_PUBLIC or obj.scope == Material::SCOPE_GROUP)
|
50
|
+
# return true if obj and ProjectMember.where(project_id: obj.project_id, user_id: user.id).first
|
51
|
+
# end
|
52
|
+
# end
|
53
|
+
# false
|
54
|
+
# end
|
55
|
+
# end
|
56
|
+
class PermissionHandler
|
57
|
+
# Check permission methos.
|
58
|
+
# @param [ActiveRecord::Base] model_class check target object class
|
59
|
+
# @param [Symbol] permission permission code_name
|
60
|
+
# @param [Integer] object_id check target object primary key
|
61
|
+
# @param [Hash] params requests
|
62
|
+
# @raise [PermissionDenied] raise for hasn't permission
|
63
|
+
def require_permission model_class, permission, object_id, params
|
64
|
+
return true unless self.class::CODE_NAMES.include? permission
|
65
|
+
primary_key = model_class.primary_key
|
66
|
+
obj = model_class.where(primary_key => object_id).first_or_initialize
|
67
|
+
raise PermissionDenied, self unless self.has_perm? self.user, permission, obj, params
|
68
|
+
end
|
69
|
+
|
70
|
+
# Render for PermissionDenied Error
|
71
|
+
# @param [ActionController] controller current controller instance
|
72
|
+
def denied_action controller
|
73
|
+
controller.respond_to do |format|
|
74
|
+
format.html { controller.render file: "#{Rails.root}/public/403", handlers: [:html], status: 403, layout: true }
|
75
|
+
format.json { controller.render json: { error: true, message: "Permission Denied" } }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def user
|
80
|
+
@user
|
81
|
+
end
|
82
|
+
|
83
|
+
def user= user
|
84
|
+
@user = user
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gate_police
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- miio
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-02-24 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Permission for rails library
|
14
|
+
email:
|
15
|
+
- info@miio.info
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- app/helpers/gate_police_helper.rb
|
26
|
+
- gate_police.gemspec
|
27
|
+
- lib/gate_police.rb
|
28
|
+
- lib/gate_police/engine.rb
|
29
|
+
- lib/gate_police/exceptions.rb
|
30
|
+
- lib/gate_police/permission_handler.rb
|
31
|
+
- lib/gate_police/version.rb
|
32
|
+
homepage: ''
|
33
|
+
licenses: []
|
34
|
+
metadata: {}
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 2.0.0
|
52
|
+
signing_key:
|
53
|
+
specification_version: 4
|
54
|
+
summary: Permission for rails library
|
55
|
+
test_files: []
|