cantango-api 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -5
- data/Gemfile.lock +20 -24
- data/README.mdown +73 -0
- data/VERSION +1 -1
- data/cantango-api.gemspec +42 -23
- data/lib/cantango/api/ability/account.rb +6 -6
- data/lib/cantango/api/ability/user.rb +1 -0
- data/lib/cantango/api/account.rb +9 -11
- data/lib/cantango/api/attributes.rb +1 -1
- data/lib/cantango/api/can/account.rb +2 -2
- data/lib/cantango/api/can/dsl/relation.rb +67 -0
- data/lib/cantango/api/can/dsl/scope.rb +24 -0
- data/lib/cantango/api/can/dsl.rb +26 -0
- data/lib/cantango/api/can/user.rb +1 -1
- data/lib/cantango/api/can.rb +1 -1
- data/lib/cantango/api/common.rb +16 -3
- data/lib/cantango/api/model/account.rb +9 -16
- data/lib/cantango/api/model/user.rb +10 -17
- data/lib/cantango/api/model.rb +3 -1
- data/lib/cantango/api/scope/account.rb +8 -5
- data/lib/cantango/api/scope/user.rb +1 -0
- data/lib/cantango/api/session/account.rb +1 -1
- data/lib/cantango/api/user.rb +9 -11
- data/lib/cantango/api.rb +7 -4
- data/lib/cantango/api_ext/macros/account.rb +8 -0
- data/lib/cantango/api_ext/macros/clazz.rb +17 -0
- data/lib/cantango/api_ext/macros/user.rb +8 -0
- data/lib/cantango/api_ext/macros.rb +12 -0
- data/lib/cantango/api_ext.rb +5 -0
- data/spec/cantango/api/ability/account_spec.rb +14 -15
- data/spec/cantango/api/ability/user_spec.rb +18 -22
- data/spec/cantango/api/account_spec.rb +40 -0
- data/spec/cantango/api/attributes_spec.rb +13 -14
- data/spec/cantango/api/can/account_spec.rb +40 -47
- data/spec/cantango/api/can/user_spec.rb +56 -83
- data/spec/cantango/api/common_spec.rb +37 -0
- data/spec/cantango/api/model/account_spec.rb +38 -0
- data/spec/cantango/api/model/user_spec.rb +35 -0
- data/spec/cantango/api/options_spec.rb +23 -0
- data/spec/cantango/api/scope/account_spec.rb +43 -0
- data/spec/cantango/api/scope/user_spec.rb +19 -51
- data/spec/cantango/api/session/account_spec.rb +0 -0
- data/spec/cantango/api/session/user_spec.rb +0 -0
- data/spec/cantango/api/user_spec.rb +40 -0
- data/spec/cantango/api_ext/macros/account_spec.rb +16 -0
- data/spec/cantango/api_ext/macros/clazz_spec.rb +26 -0
- data/spec/cantango/api_ext/macros/user_spec.rb +17 -0
- data/spec/fixtures/models/user_account.rb +1 -15
- data/spec/helpers/current_user_accounts.rb +1 -1
- data/spec/spec_helper.rb +0 -1
- metadata +55 -28
@@ -1,113 +1,86 @@
|
|
1
|
-
require '
|
2
|
-
require 'cantango'
|
3
|
-
require 'simple_roles'
|
1
|
+
require 'cantango/config'
|
4
2
|
require 'fixtures/models'
|
5
|
-
require 'cantango/api/current_users'
|
6
|
-
# require 'cantango/configuration/engines/store_engine_shared'
|
7
|
-
|
8
|
-
class User
|
9
|
-
include CanTango::Users::Masquerade
|
10
|
-
include_and_extend SimpleRoles
|
11
|
-
|
12
|
-
tango_user
|
13
|
-
end
|
14
|
-
|
15
|
-
class Admin < User
|
16
|
-
tango_user
|
17
|
-
end
|
18
3
|
|
19
4
|
CanTango.configure do |config|
|
20
|
-
config.
|
21
|
-
config.
|
5
|
+
config.users.register :user, User
|
6
|
+
config.users.register :admin, Admin
|
7
|
+
|
8
|
+
config.accounts.register :user, UserAccount
|
9
|
+
config.accounts.register :admin, AdminAccount
|
10
|
+
|
11
|
+
config.modes.register :no_cache, CanTango::Ability::Mode::NoCache
|
12
|
+
config.ability.mode = :no_cache
|
22
13
|
end
|
23
14
|
|
24
|
-
|
15
|
+
require 'spec_helper'
|
16
|
+
require 'helpers/current_user_accounts'
|
25
17
|
|
26
|
-
class
|
27
|
-
|
28
|
-
super
|
29
|
-
end
|
18
|
+
class Context
|
19
|
+
include CanTango::Api::Can::User
|
30
20
|
|
31
|
-
|
32
|
-
can :edit, Article
|
33
|
-
cannot :edit, User
|
34
|
-
end
|
21
|
+
include_and_extend ::CurrentUserAccounts
|
35
22
|
end
|
36
23
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
can :edit, Article
|
44
|
-
cannot :edit, User
|
24
|
+
module CanTango::Ability::Mode
|
25
|
+
class NoCache
|
26
|
+
def calculate_rules
|
27
|
+
can :edit, Article
|
28
|
+
cannot :edit, User
|
29
|
+
end
|
45
30
|
end
|
46
31
|
end
|
47
32
|
|
48
|
-
|
49
|
-
include CanTango::Api::User::Can
|
50
|
-
|
51
|
-
include_and_extend ::CurrentUsers
|
52
|
-
end
|
53
|
-
|
54
|
-
describe CanTango::Api::User::Can do
|
33
|
+
describe CanTango::Api::Can::Account do
|
55
34
|
subject { Context.new }
|
56
35
|
|
57
|
-
describe '
|
58
|
-
specify
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
describe 'current_user_ability :user' do
|
63
|
-
specify { subject.current_user_ability(:user).should be_a CanTango::Ability }
|
64
|
-
|
65
|
-
it 'should set the :user user correctly on ability' do
|
66
|
-
subject.current_user_ability(:user).user.should == subject.current_user
|
36
|
+
describe 'user_account' do
|
37
|
+
specify do
|
38
|
+
subject.current_user_ability(:user).should be_a CanTango::Ability::Executor::Modal
|
67
39
|
end
|
68
|
-
end
|
69
40
|
|
70
|
-
|
71
|
-
|
41
|
+
specify do
|
42
|
+
subject.current_user_ability(:user).modes.should == [:no_cache]
|
43
|
+
end
|
72
44
|
|
73
|
-
|
74
|
-
subject.current_user_ability(:
|
45
|
+
specify do
|
46
|
+
subject.current_user_ability(:user).should respond_to(:can?)
|
75
47
|
end
|
76
|
-
end
|
77
48
|
|
78
|
-
|
79
|
-
|
49
|
+
specify do
|
50
|
+
subject.current_user_ability(:user).rules.should_not be_empty
|
51
|
+
end
|
80
52
|
|
53
|
+
specify do
|
54
|
+
subject.current_user_ability(:user).can?(:edit, Article).should be_true
|
55
|
+
end
|
56
|
+
|
57
|
+
specify do
|
58
|
+
CanTango.config.users.registered.should include(:user)
|
59
|
+
end
|
60
|
+
|
81
61
|
# user can edit Article, not Admin
|
82
|
-
specify
|
83
|
-
|
62
|
+
specify do
|
63
|
+
subject.user_can?(:edit, Article).should be_true
|
64
|
+
subject.user_can?(:edit, User).should be_false
|
84
65
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
describe 'admin' do
|
90
|
-
specify { subject.current_admin.role.should == 'admin' }
|
91
|
-
|
92
|
-
specify { subject.admin_can?(:edit, Article).should be_true }
|
93
|
-
specify { subject.admin_can?(:edit, User).should be_false }
|
94
|
-
|
95
|
-
specify { subject.admin_cannot?(:edit, User).should be_true }
|
96
|
-
specify { subject.admin_cannot?(:edit, Article).should be_false }
|
66
|
+
subject.user_cannot?(:edit, User).should be_true
|
67
|
+
subject.user_cannot?(:edit, Article).should be_false
|
68
|
+
end
|
97
69
|
end
|
98
70
|
|
99
|
-
describe '
|
100
|
-
|
101
|
-
|
71
|
+
describe 'admin_user' do
|
72
|
+
specify do
|
73
|
+
CanTango.config.users.registered.should include(:admin)
|
102
74
|
end
|
103
75
|
|
104
|
-
|
105
|
-
|
76
|
+
specify do
|
77
|
+
subject.admin_can?(:edit, Article).should be_true
|
78
|
+
subject.admin_can?(:edit, User).should be_false
|
106
79
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
specify { subject.admin_cannot?(:edit, Article).should be_false }
|
80
|
+
subject.admin_cannot?(:edit, User).should be_true
|
81
|
+
subject.admin_cannot?(:edit, Article).should be_false
|
82
|
+
end
|
111
83
|
end
|
112
84
|
end
|
113
85
|
|
86
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fixtures/models'
|
3
|
+
|
4
|
+
class CommonContext
|
5
|
+
include CanTango::Api::Common
|
6
|
+
end
|
7
|
+
|
8
|
+
class Wheat
|
9
|
+
end
|
10
|
+
|
11
|
+
class Barley
|
12
|
+
end
|
13
|
+
|
14
|
+
describe CanTango::Api::Common do
|
15
|
+
before do
|
16
|
+
@user = User.new 'kris', 'kris@mail.ru'
|
17
|
+
end
|
18
|
+
|
19
|
+
subject { CommonContext.new }
|
20
|
+
|
21
|
+
describe 'create_ability(candidate)' do
|
22
|
+
specify do
|
23
|
+
subject.create_ability(@user).should be_a CanTango::Ability::Executor
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'category(label)' do
|
28
|
+
before do
|
29
|
+
CanTango.config.categories.register :grains => ['Wheat', 'Barley']
|
30
|
+
end
|
31
|
+
|
32
|
+
specify do
|
33
|
+
lambda { subject.category('unknown') }.should raise_error
|
34
|
+
end
|
35
|
+
specify { subject.category('grains').should_not be_empty }
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'cantango/config'
|
2
|
+
require 'fixtures/models'
|
3
|
+
|
4
|
+
CanTango.configure do |config|
|
5
|
+
config.users.register :user, User
|
6
|
+
config.users.register :admin, Admin
|
7
|
+
|
8
|
+
config.accounts.register :user, UserAccount
|
9
|
+
config.accounts.register :admin, AdminAccount
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'spec_helper'
|
13
|
+
|
14
|
+
class UserAccount
|
15
|
+
tango_account
|
16
|
+
end
|
17
|
+
|
18
|
+
class AdminAccount
|
19
|
+
tango_account
|
20
|
+
end
|
21
|
+
|
22
|
+
class Context
|
23
|
+
include CanTango::Api::Model::Account
|
24
|
+
end
|
25
|
+
|
26
|
+
describe CanTango::Api::Model::Account do
|
27
|
+
subject { Context.new }
|
28
|
+
|
29
|
+
specify do
|
30
|
+
CanTango.config.accounts.registered.should include(:admin, :user)
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'Model API included' do
|
34
|
+
[:active_user, :can?, :cannot?].each do |name|
|
35
|
+
specify { subject.should respond_to name }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'cantango/config'
|
2
|
+
require 'fixtures/models'
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
class Context
|
7
|
+
include CanTango::Api::Model::User
|
8
|
+
end
|
9
|
+
|
10
|
+
class User
|
11
|
+
tango_user
|
12
|
+
end
|
13
|
+
|
14
|
+
class Editor
|
15
|
+
tango_user
|
16
|
+
end
|
17
|
+
|
18
|
+
class BadUser
|
19
|
+
tango_user
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
describe CanTango::Api::Model::User do
|
24
|
+
subject { Context.new }
|
25
|
+
|
26
|
+
specify do
|
27
|
+
CanTango.config.users.registered.should include(:editor, :user, :bad)
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'Model API included' do
|
31
|
+
[:active_user, :can?, :cannot?].each do |name|
|
32
|
+
specify { subject.should respond_to name }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Context
|
4
|
+
include CanTango::Api::Options
|
5
|
+
|
6
|
+
def session
|
7
|
+
{}
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe CanTango::Api::Options do
|
12
|
+
subject { Context.new }
|
13
|
+
|
14
|
+
describe 'ability_options' do
|
15
|
+
specify do
|
16
|
+
subject.ability_options[:session].should == {}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'options_list' do
|
21
|
+
specify { subject.options_list.should == [:session, :request, :params, :controller, :domain, :cookies] }
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fixtures/models'
|
3
|
+
require 'helpers/current_user_accounts'
|
4
|
+
|
5
|
+
class Context
|
6
|
+
include CanTango::Api::Scope::Account
|
7
|
+
include CanTango::Api::Masquerade::Account
|
8
|
+
|
9
|
+
include_and_extend ::CurrentUserAccounts
|
10
|
+
end
|
11
|
+
|
12
|
+
class UserAccount
|
13
|
+
tango_account
|
14
|
+
end
|
15
|
+
|
16
|
+
class AdminAccount
|
17
|
+
cantango_account
|
18
|
+
end
|
19
|
+
|
20
|
+
describe CanTango::Api::Scope::Account do
|
21
|
+
subject { Context.new }
|
22
|
+
|
23
|
+
before do
|
24
|
+
subject.masquerade_as subject.current_admin_account
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'scope_account(scope, options)' do
|
28
|
+
specify do
|
29
|
+
subject.scope_account :user do |user|
|
30
|
+
user.candidate.class.should == subject.current_admin_account.class
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'real_account(scope, options)' do
|
36
|
+
specify do
|
37
|
+
subject.real_account :user do |user|
|
38
|
+
user.candidate.class.should == subject.current_user_account.class
|
39
|
+
user.candidate.user.should == subject.current_user_account.user
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -1,73 +1,41 @@
|
|
1
|
-
require '
|
2
|
-
require 'cantango'
|
3
|
-
require 'simple_roles'
|
1
|
+
require 'spec_helper'
|
4
2
|
require 'fixtures/models'
|
5
|
-
require '
|
6
|
-
# require 'cantango/configuration/engines/store_engine_shared'
|
3
|
+
require 'helpers/current_users'
|
7
4
|
|
8
|
-
|
9
|
-
|
10
|
-
config.users.register :admin, Admin
|
5
|
+
class Context
|
6
|
+
include CanTango::Api::Scope::User
|
11
7
|
|
12
|
-
|
13
|
-
config.permit_engine.set :on
|
8
|
+
include_and_extend ::CurrentUsers
|
14
9
|
end
|
15
10
|
|
16
|
-
# puts "#{CanTango.config.users.registered_classes} : #{CanTango.config.users.registered}"
|
17
|
-
|
18
11
|
class User
|
19
|
-
|
20
|
-
include_and_extend ::SimpleRoles
|
12
|
+
cantango
|
21
13
|
end
|
22
14
|
|
23
|
-
class
|
24
|
-
|
25
|
-
include CanTango::Api::User::Scope
|
26
|
-
|
27
|
-
include_and_extend ::CurrentUsers
|
15
|
+
class AdminUser
|
16
|
+
cantango
|
28
17
|
end
|
29
18
|
|
30
|
-
describe CanTango::Api::User
|
19
|
+
describe CanTango::Api::Scope::User do
|
31
20
|
subject { Context.new }
|
32
21
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
22
|
+
before do
|
23
|
+
subject.current_user.active_account = subject.current_admin
|
24
|
+
end
|
37
25
|
|
26
|
+
describe 'scope_user(scope, options)' do
|
38
27
|
specify do
|
39
|
-
subject.scope_user
|
40
|
-
user.should
|
41
|
-
user.ability.user.name.should == 'admin'
|
28
|
+
subject.scope_user :user do |user|
|
29
|
+
user.candidate.should == subject.current_admin
|
42
30
|
end
|
43
31
|
end
|
44
|
-
|
45
|
-
specify do
|
46
|
-
admin = subject.scope_user(:user)
|
47
|
-
admin.should be_a CanTango::Ability::Scope
|
48
|
-
admin.ability.user.name.should == 'stan'
|
49
|
-
end
|
50
32
|
end
|
51
33
|
|
52
|
-
describe 'real_user' do
|
53
|
-
before do
|
54
|
-
subject.current_user.masquerade_as subject.current_admin
|
55
|
-
end
|
56
|
-
|
34
|
+
describe 'real_user(scope, options)' do
|
57
35
|
specify do
|
58
|
-
subject.real_user
|
59
|
-
user.should
|
60
|
-
user.ability.user.name.should == 'stan'
|
36
|
+
subject.real_user :user do |user|
|
37
|
+
user.candidate.should == subject.current_user
|
61
38
|
end
|
62
39
|
end
|
63
|
-
|
64
|
-
specify do
|
65
|
-
admin = subject.real_user(:admin)
|
66
|
-
admin.should be_a CanTango::Ability::Scope
|
67
|
-
admin.ability.user.name.should == 'admin'
|
68
|
-
end
|
69
40
|
end
|
70
|
-
end
|
71
|
-
|
72
|
-
|
73
|
-
|
41
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'cantango/config'
|
2
|
+
require 'fixtures/models'
|
3
|
+
|
4
|
+
CanTango.configure do |config|
|
5
|
+
config.users.register :user, User
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'spec_helper'
|
9
|
+
|
10
|
+
class UserContext
|
11
|
+
include CanTango::Api::User
|
12
|
+
end
|
13
|
+
|
14
|
+
describe CanTango::Api::User do
|
15
|
+
subject { UserContext.new }
|
16
|
+
|
17
|
+
describe 'Ability API included' do
|
18
|
+
[:user_ability, :current_user_ability].each do |name|
|
19
|
+
specify { subject.should respond_to name }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'Can API included' do
|
24
|
+
[:user_can?, :user_cannot?].each do |name|
|
25
|
+
specify { subject.should respond_to name }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'Scope API included' do
|
30
|
+
[:scope_user, :user_scope, :real_user].each do |name|
|
31
|
+
specify { subject.should respond_to name }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'Session API included' do
|
36
|
+
[:any_user, :active_user, :active_user=].each do |name|
|
37
|
+
specify { subject.should respond_to name }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'cantango/config'
|
2
|
+
require 'fixtures/models'
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
class UserAccount
|
7
|
+
tango_account
|
8
|
+
end
|
9
|
+
|
10
|
+
class AdminAccount
|
11
|
+
cantango_account
|
12
|
+
end
|
13
|
+
|
14
|
+
describe CanTango::Macros::Account do
|
15
|
+
specify { CanTango.config.accounts.registered.should include(:user, :admin) }
|
16
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'cantango/config'
|
2
|
+
require 'fixtures/models'
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
class UserAccount
|
7
|
+
cantango
|
8
|
+
end
|
9
|
+
|
10
|
+
class AdminAccount
|
11
|
+
cantango
|
12
|
+
end
|
13
|
+
|
14
|
+
class AdminUser
|
15
|
+
cantango
|
16
|
+
end
|
17
|
+
|
18
|
+
class Publisher
|
19
|
+
cantango
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
describe CanTango::Macros::Account do
|
24
|
+
specify { CanTango.config.users.registered.should include(:publisher, :admin) }
|
25
|
+
specify { CanTango.config.accounts.registered.should include(:user, :admin) }
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'cantango/config'
|
2
|
+
require 'fixtures/models'
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
class User
|
7
|
+
tango_user
|
8
|
+
end
|
9
|
+
|
10
|
+
class EditorUser
|
11
|
+
tango_user
|
12
|
+
end
|
13
|
+
|
14
|
+
describe CanTango::Macros::User do
|
15
|
+
specify { CanTango.config.users.registered.should include(:user, :editor) }
|
16
|
+
end
|
17
|
+
|
@@ -1,21 +1,7 @@
|
|
1
1
|
class UserAccount
|
2
|
-
attr_accessor :user
|
2
|
+
attr_accessor :user
|
3
3
|
|
4
4
|
def initialize user, options = {}
|
5
5
|
@user = user
|
6
|
-
@roles = options[:roles]
|
7
|
-
@role_groups = options[:role_groups]
|
8
|
-
end
|
9
|
-
|
10
|
-
def has_role? name
|
11
|
-
true
|
12
|
-
end
|
13
|
-
|
14
|
-
def roles_list
|
15
|
-
roles
|
16
|
-
end
|
17
|
-
|
18
|
-
def role_groups_list
|
19
|
-
role_groups
|
20
6
|
end
|
21
7
|
end
|