maymay 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.
- data/Gemfile +3 -0
- data/README.md +34 -0
- data/Rakefile +7 -0
- data/init.rb +1 -0
- data/lib/maymay.rb +3 -0
- data/lib/maymay/ability.rb +40 -0
- data/lib/maymay/controller.rb +13 -0
- data/lib/maymay/exceptions.rb +7 -0
- data/spec/maymay/ability_spec.rb +49 -0
- data/spec/spec_helper.rb +6 -0
- metadata +70 -0
data/Gemfile
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# MayMay is a dead simple auth solution for Rails without any magic
|
|
2
|
+
|
|
3
|
+
```ruby
|
|
4
|
+
ability = MayMay::Ability.new(user)
|
|
5
|
+
|
|
6
|
+
ability.may :create, :posts
|
|
7
|
+
ability.may? :create, :posts # => true
|
|
8
|
+
|
|
9
|
+
ability.may :show, :posts do |user, post|
|
|
10
|
+
user.id == post.user_id
|
|
11
|
+
end
|
|
12
|
+
ability.may? :show, :posts, post # => true
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
That's it!
|
|
16
|
+
|
|
17
|
+
## How it works with Rails
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
class ApplicationController < ActionController::Base
|
|
21
|
+
def ability
|
|
22
|
+
ability = MayMay::Ability.new(current_user)
|
|
23
|
+
# define abilities (inline, from db, how you wanna)
|
|
24
|
+
ability
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class PostsController < ApplicationController
|
|
29
|
+
def show
|
|
30
|
+
@post = Post.find(params[:id])
|
|
31
|
+
authorize! :show, :posts, @post
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
```
|
data/Rakefile
ADDED
data/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'maymay'
|
data/lib/maymay.rb
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
module MayMay
|
|
2
|
+
class Ability
|
|
3
|
+
def initialize(user)
|
|
4
|
+
@abilities = Hash.new([])
|
|
5
|
+
@user = user
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def self.reset!
|
|
9
|
+
@abilities = {}
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def may?(action, subject, object = nil)
|
|
13
|
+
actions = @abilities[subject].find_all { |a| a[1] == action }
|
|
14
|
+
raise UndefinedAbility if actions.empty?
|
|
15
|
+
actions.all? do |a|
|
|
16
|
+
a[0] == !!(!a[2] || a[2].call(@user, object))
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def maynot?(action, subject, object = nil)
|
|
21
|
+
!may?(action, subject, object)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def may(action, subject, &block)
|
|
25
|
+
@abilities[subject] << [true, action, block]
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def maynot(action, subject, &block)
|
|
30
|
+
@abilities[subject] << [false, action, block]
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def authorize!(*args)
|
|
35
|
+
if maynot?(*args)
|
|
36
|
+
raise Unauthorized
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module MayMay
|
|
2
|
+
module ControllerMethods
|
|
3
|
+
def ability
|
|
4
|
+
Ability.new(nil)
|
|
5
|
+
end
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
if defined? ActionController::Base
|
|
10
|
+
ActionController::Base.send :include, MayMay::ControllerMethods
|
|
11
|
+
ActionController::Base.helper_method :may?, :maynot?
|
|
12
|
+
ActionController::Base.delegate :may?, :maynot?, :authorize!, to: :ability
|
|
13
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
module MayMay
|
|
4
|
+
describe Ability do
|
|
5
|
+
|
|
6
|
+
subject { Ability.new(nil) }
|
|
7
|
+
|
|
8
|
+
describe 'checking abilities' do
|
|
9
|
+
context 'unexisted' do
|
|
10
|
+
it 'should raise an error' do
|
|
11
|
+
expect { subject.may?(:foo, :bar) }.to raise_error(UndefinedAbility)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
context 'defined with block' do
|
|
16
|
+
before do
|
|
17
|
+
subject.may(:foo, :bar) { |user, object| object }
|
|
18
|
+
subject.maynot(:boo, :moo) { |user, object| object }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it { subject.may?(:foo, :bar, true).should be_true }
|
|
22
|
+
it { subject.may?(:foo, :bar, false).should be_false }
|
|
23
|
+
|
|
24
|
+
it { subject.may?(:boo, :moo, true).should be_false }
|
|
25
|
+
it { subject.may?(:boo, :moo, false).should be_true }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
context 'defined without block' do
|
|
29
|
+
before do
|
|
30
|
+
subject.may :foo, :bar
|
|
31
|
+
subject.maynot :boo, :moo
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it { subject.may?(:foo, :bar).should be_true }
|
|
35
|
+
it { subject.may?(:boo, :moo).should be_false }
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
describe '#authorize!' do
|
|
40
|
+
before do
|
|
41
|
+
subject.maynot :foo, :bar
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it 'should raise an error' do
|
|
45
|
+
expect { subject.authorize! :foo, :bar }.to raise_error(Unauthorized)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: maymay
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Jan Bernacki
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-10-26 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rspec
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ~>
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 2.9.0
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ~>
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: 2.9.0
|
|
30
|
+
description: Abstract authorization solution for Rails.
|
|
31
|
+
email: releu@redstonelabs.cz
|
|
32
|
+
executables: []
|
|
33
|
+
extensions: []
|
|
34
|
+
extra_rdoc_files: []
|
|
35
|
+
files:
|
|
36
|
+
- lib/maymay/ability.rb
|
|
37
|
+
- lib/maymay/controller.rb
|
|
38
|
+
- lib/maymay/exceptions.rb
|
|
39
|
+
- lib/maymay.rb
|
|
40
|
+
- spec/maymay/ability_spec.rb
|
|
41
|
+
- spec/spec_helper.rb
|
|
42
|
+
- Gemfile
|
|
43
|
+
- Rakefile
|
|
44
|
+
- README.md
|
|
45
|
+
- init.rb
|
|
46
|
+
homepage: http://github.com/redstonelabs/maymay
|
|
47
|
+
licenses: []
|
|
48
|
+
post_install_message:
|
|
49
|
+
rdoc_options: []
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
none: false
|
|
54
|
+
requirements:
|
|
55
|
+
- - ! '>='
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: '0'
|
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
|
+
none: false
|
|
60
|
+
requirements:
|
|
61
|
+
- - ! '>='
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: 1.3.4
|
|
64
|
+
requirements: []
|
|
65
|
+
rubyforge_project: maymay
|
|
66
|
+
rubygems_version: 1.8.24
|
|
67
|
+
signing_key:
|
|
68
|
+
specification_version: 3
|
|
69
|
+
summary: Abstract authorization solution for Rails.
|
|
70
|
+
test_files: []
|