active_permission 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +39 -0
- data/lib/active_permission.rb +6 -4
- data/lib/active_permission/controller_additions.rb +6 -6
- data/lib/active_permission/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4822a5239730d4ab16ad3ad140ad83a42d316386
|
4
|
+
data.tar.gz: 9e72ad4a57f7fcf80ed87a123ccebaf507db91b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c23dd91e7b24ffcfcbf2581a9d02862a2cc05a766aaf4f31e9a6666059ea22c4197ed6b693633c8969030b619b81e1b0c75dba7145d02d5a0ca9ea03dd915b7
|
7
|
+
data.tar.gz: c55fe1ecaf350a2d76aa01d187a63de3645d03a068e03c9812fec4270de8ba61b75857c2eb63171f5c91df833bc199890aaa1ddd5020ade825a4bd204d50af7a
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# ActivePermission
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/active_permission.svg)](http://badge.fury.io/rb/active_permission)
|
4
|
+
|
3
5
|
This gem allow you load and authorize resource in Ruby on Rails inside controllers or views using rules with described permissions of user.
|
4
6
|
|
5
7
|
## Installation
|
@@ -40,6 +42,19 @@ end
|
|
40
42
|
|
41
43
|
### Load Resource and authorization examples
|
42
44
|
|
45
|
+
```
|
46
|
+
class ApplicationController < ActionController::Base
|
47
|
+
include ActivePermission::ControllerAdditions
|
48
|
+
private
|
49
|
+
def current_user
|
50
|
+
@current_user ||= User.find(session[:user_id]) if session[:user_id]
|
51
|
+
end
|
52
|
+
def current_permissions
|
53
|
+
@permission ||= Permission.new(current_user)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
43
58
|
```
|
44
59
|
class BooksController < ApplicationController
|
45
60
|
resource :book, object: 'Book'
|
@@ -95,6 +110,30 @@ end
|
|
95
110
|
<% end %>
|
96
111
|
```
|
97
112
|
|
113
|
+
### Rescue from ActivePermission::AccessDenied
|
114
|
+
|
115
|
+
```
|
116
|
+
rescue_from ActivePermission::AccessDenied do |error|
|
117
|
+
if @current_user
|
118
|
+
logger.warn "#{@current_user.class}(#{@current_user.id}): #{error}"
|
119
|
+
flash[:warning] = t('Access denied')
|
120
|
+
redirect_to root_path
|
121
|
+
else
|
122
|
+
logger.warn "Anonymous: #{error}"
|
123
|
+
flash[:warning] = t('Must be signin')
|
124
|
+
redirect_to signin_path
|
125
|
+
end
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
129
|
+
```
|
130
|
+
rescue_from ActivePermission::AccessDenied do |error|
|
131
|
+
logger.warn "Controller: #{error.controller} Action: #{error.action} Object: #{error.object}"
|
132
|
+
flash[:warning] = t('Access denied')
|
133
|
+
redirect_to root_path
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
98
137
|
|
99
138
|
## Contributing
|
100
139
|
|
data/lib/active_permission.rb
CHANGED
@@ -4,10 +4,12 @@ require 'active_permission/base'
|
|
4
4
|
|
5
5
|
module ActivePermission
|
6
6
|
class AccessDenied < RuntimeError
|
7
|
-
attr_reader :
|
8
|
-
def initialize(
|
9
|
-
|
10
|
-
@
|
7
|
+
attr_reader :controller, :action, :object
|
8
|
+
def initialize(controller = nil , action = nil , object = nil)
|
9
|
+
@controller = controller
|
10
|
+
@action = action
|
11
|
+
@object = object
|
12
|
+
super("Access denied in #{@controller}::#{@action} - #{object.inspect}")
|
11
13
|
end
|
12
14
|
end
|
13
15
|
end
|
@@ -49,7 +49,7 @@ module ActivePermission
|
|
49
49
|
if options[:through] and options[:association]
|
50
50
|
object = instance_variable_get("@#{options[:through]}").send(options[:association])
|
51
51
|
elsif options[:object].nil?
|
52
|
-
raise AccessDenied.new(
|
52
|
+
raise AccessDenied.new(controller.params[:controller], controller.params[:action], object)
|
53
53
|
elsif options[:object].kind_of? Symbol
|
54
54
|
object = send(options[:object])
|
55
55
|
elsif options[:object].kind_of? String
|
@@ -78,27 +78,27 @@ module ActivePermission
|
|
78
78
|
current_permissions.can!(controller.params[:controller], controller.params[:action], *objects)
|
79
79
|
end
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
def current_permissions
|
83
83
|
@permissions ||= ActivePermission::Base.new
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
87
|
module InstanceMethods
|
88
|
-
def authorize!(resource, options = {})
|
88
|
+
def authorize!(resource = nil, options = {})
|
89
89
|
options = params.merge(options)
|
90
90
|
current_permissions.can!(options[:controller], options[:action], resource)
|
91
91
|
end
|
92
92
|
|
93
|
-
def authorize?(resource, options = {})
|
93
|
+
def authorize?(resource = nil, options = {})
|
94
94
|
options = params.merge(options)
|
95
95
|
current_permissions.can?(options[:controller], options[:action], resource)
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
99
99
|
def self.included(base)
|
100
|
-
base.extend ClassMethods
|
101
|
-
base.include InstanceMethods
|
100
|
+
base.send :extend, ClassMethods
|
101
|
+
base.send :include, InstanceMethods
|
102
102
|
base.delegate :can?, :can!, :to => :current_permissions
|
103
103
|
base.helper_method :can?, :can!
|
104
104
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_permission
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evgeniy Shurmin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|