vinyl 0.0.1 → 0.0.3
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.
- data/README.md +35 -12
- data/lib/vinyl/base.rb +10 -6
- data/lib/vinyl/rule.rb +7 -4
- data/lib/vinyl/version.rb +1 -1
- data/tests/rules_spec.rb +5 -5
- metadata +2 -2
data/README.md
CHANGED
@@ -5,25 +5,35 @@ _by: Federico Saravia Barrantes [@fsaravia](https://github.com/fsaravia) and Lau
|
|
5
5
|
|
6
6
|
## Introduction
|
7
7
|
|
8
|
-
"Vinyl" is a simple gem that allows
|
9
|
-
It works by
|
8
|
+
"Vinyl" is a simple gem that allows developers to define different access levels for a resource and a series of local and global validators to gain access to those levels.
|
9
|
+
It works by analyzing a series of validators defined by you and returning a number representing the access level a particular request is able to reach.
|
10
10
|
|
11
11
|
## What is it useful for?
|
12
12
|
|
13
|
-
This gem is useful when you need to control the output depending on who wants to access to a resource.
|
13
|
+
This gem is useful when you need to control the output of a process depending on who wants to access to a resource.
|
14
14
|
|
15
15
|
For example: user A wants to get user B's profile.
|
16
16
|
|
17
|
-
1. If A == B, A
|
18
|
-
2. If A is friend of B, A can see private data but not config data
|
19
|
-
3. If A is not friend of B, A only can see public data
|
17
|
+
1. If A == B, then A has full access to data
|
18
|
+
2. If A is friend of B, than A can see private data but not config data
|
19
|
+
3. If A is not friend of B, then A only can see public data
|
20
|
+
|
21
|
+
In this example we have 3 different levels of access to B's information.
|
22
|
+
|
23
|
+
## Install
|
24
|
+
|
25
|
+
```
|
26
|
+
gem install vinyl
|
27
|
+
|
28
|
+
```
|
20
29
|
|
21
|
-
In the example we have 3 different levels of access to B information.
|
22
30
|
|
23
31
|
## Basic Config
|
24
32
|
|
25
33
|
```ruby
|
26
|
-
|
34
|
+
require 'vinyl'
|
35
|
+
|
36
|
+
Vinyl.configure do |config|
|
27
37
|
config.api_acl_mode = Vinyl::Configuration::STRATEGY_DESCENDING
|
28
38
|
config.force_access_control = true
|
29
39
|
config.warn_on_missing_validators = true
|
@@ -41,7 +51,7 @@ Deny access if no validators are given for a route/method combination and no glo
|
|
41
51
|
|
42
52
|
__:warn_on_missing_validators true/false__
|
43
53
|
|
44
|
-
Display a warning on STDOUT when calling a
|
54
|
+
Display a warning on STDOUT when calling a non-existent validator (Missing validators output always default to false)
|
45
55
|
|
46
56
|
|
47
57
|
## Defining Rules
|
@@ -68,13 +78,26 @@ Vinyl.when_route '/profiles.json',
|
|
68
78
|
:get_access_level => 2,
|
69
79
|
:if_pass => ['is_admin']
|
70
80
|
```
|
81
|
+
You can define your routes using a string, they may include wildcards or you can also use regular expressions:
|
71
82
|
|
83
|
+
__Example:__
|
84
|
+
```ruby
|
85
|
+
Vinyl.when_route '/profiles*',
|
86
|
+
:with_method => 'GET',
|
87
|
+
:get_access_level => 1,
|
88
|
+
:if_pass => ['is_user']
|
89
|
+
|
90
|
+
Vinyl.when_route /^\/profiles\/(\d+)\.(.+)/,
|
91
|
+
:with_method => 'GET',
|
92
|
+
:get_access_level => 1,
|
93
|
+
:if_pass => ['is_user']
|
94
|
+
```
|
72
95
|
|
73
96
|
## Defining validators
|
74
97
|
|
75
|
-
There are two
|
98
|
+
There are two kinds of validators: global and normal validators. All validators you define must return true or false.
|
76
99
|
|
77
|
-
Global validators will be applied to
|
100
|
+
Global validators will be applied to every rule. You can add a global validator using add_global_validator method:
|
78
101
|
|
79
102
|
```ruby
|
80
103
|
Vinyl.add_global_validator("name_of_global_validator", lambda {
|
@@ -119,7 +142,7 @@ Vinyl.reset_variables
|
|
119
142
|
```
|
120
143
|
## Getting Access Level
|
121
144
|
|
122
|
-
At this point you had defined your rules, validators and variables. Now you are ready to
|
145
|
+
At this point you had defined your rules, validators and variables. Now you are ready to check for the access level.
|
123
146
|
|
124
147
|
```ruby
|
125
148
|
access_level = Vinyl.check_level('/call/route','call_method')
|
data/lib/vinyl/base.rb
CHANGED
@@ -13,12 +13,12 @@ module Vinyl
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.check_level(route,method)
|
16
|
-
|
17
|
-
|
18
|
-
validators_to_call =
|
19
|
-
if
|
20
|
-
if
|
21
|
-
return Vinyl.
|
16
|
+
method_routes = Vinyl.acl_routes_collection[method]
|
17
|
+
return Vinyl.access_level_with_no_validators if method_routes.nil? || method_routes.empty?
|
18
|
+
validators_to_call = method_routes[route]
|
19
|
+
if validators_to_call.nil? || validators_to_call.empty?
|
20
|
+
if global_validators.empty?
|
21
|
+
return Vinyl.access_level_with_no_validators
|
22
22
|
else
|
23
23
|
validators_to_call = {1 => []} #No access level defined but global validators must be called
|
24
24
|
end
|
@@ -37,6 +37,10 @@ module Vinyl
|
|
37
37
|
return highest_level
|
38
38
|
end
|
39
39
|
|
40
|
+
def self.access_level_with_no_validators
|
41
|
+
return Vinyl.config.force_access_control ? 0 : 1
|
42
|
+
end
|
43
|
+
|
40
44
|
class Configuration
|
41
45
|
|
42
46
|
attr_accessor :api_acl_mode, :force_access_control, :warn_on_missing_validators
|
data/lib/vinyl/rule.rb
CHANGED
@@ -37,11 +37,14 @@ module Vinyl
|
|
37
37
|
puts e.backtrace
|
38
38
|
end
|
39
39
|
pattern = generate_pattern(route)
|
40
|
-
@@acl_routes_collection[
|
41
|
-
@@acl_routes_collection[
|
42
|
-
@@acl_routes_collection[
|
40
|
+
@@acl_routes_collection[method] ||= RegExpHash.new
|
41
|
+
@@acl_routes_collection[method][pattern] ||= Hash.new
|
42
|
+
@@acl_routes_collection[method][pattern][access_level] = validators
|
43
43
|
end
|
44
44
|
|
45
|
+
#The ideas for this method were extracted from Sinatra's source
|
46
|
+
#https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb
|
47
|
+
#Copyright (c) 2007, 2008, 2009, 2010, 2011 Blake Mizerany
|
45
48
|
def self.generate_pattern(path)
|
46
49
|
if path.respond_to? :to_str
|
47
50
|
ignore = ""
|
@@ -85,4 +88,4 @@ module Vinyl
|
|
85
88
|
end
|
86
89
|
|
87
90
|
class InvalidAclRule < StandardError; end
|
88
|
-
end
|
91
|
+
end
|
data/lib/vinyl/version.rb
CHANGED
data/tests/rules_spec.rb
CHANGED
@@ -16,17 +16,17 @@ describe Vinyl::Rule do
|
|
16
16
|
Vinyl::when_route 'test', :with_method => 'POST', :get_access_level => 3,
|
17
17
|
:if_pass => []
|
18
18
|
|
19
|
-
Vinyl.acl_routes_collection['
|
19
|
+
Vinyl.acl_routes_collection['POST'].should_not be_empty
|
20
20
|
Vinyl.acl_routes_collection.length.should == 1
|
21
|
-
Vinyl.acl_routes_collection['test'].should
|
22
|
-
Vinyl.acl_routes_collection['
|
21
|
+
Vinyl.acl_routes_collection['POST']['test'].should be_instance_of(Hash)
|
22
|
+
Vinyl.acl_routes_collection['POST']['test'].length.should == 3
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should add a route with a non-existent validator" do
|
26
26
|
Vinyl::when_route 'test', :with_method => 'GET', :get_access_level => 1,
|
27
27
|
:if_pass => ['fake_validator']
|
28
|
-
Vinyl.acl_routes_collection['test'].should
|
29
|
-
Vinyl.acl_routes_collection['
|
28
|
+
Vinyl.acl_routes_collection['GET']['test'].should be_instance_of(Hash)
|
29
|
+
Vinyl.acl_routes_collection['GET']['test'].length.should == 1
|
30
30
|
Vinyl.execute('fake_validator').should be_false
|
31
31
|
end
|
32
32
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vinyl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-10-
|
13
|
+
date: 2012-10-27 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|