aptible-auth 0.5.4 → 0.5.5
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.
- checksums.yaml +4 -4
- data/lib/aptible/auth/user.rb +29 -0
- data/lib/aptible/auth/version.rb +1 -1
- data/spec/aptible/auth/user_spec.rb +100 -0
- 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: 19f89d52a24659f1989ace1a8a504f0f5afe20ed
|
4
|
+
data.tar.gz: 07edd32d5ace4f65005eaabfe79b3fdba456d658
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 553557d661afe5da3f45ffed18752d15a397141e2c608a4634139c5e9a07db52e546fcda8561311f1a0d683c08c7bd3a759c5ae349cb51687fabfea9527251b5
|
7
|
+
data.tar.gz: bf0dc5e0292f27c43ce5483145a24dd9d739d0ace1b24b9813f5a7478cbd2d2418adacfc7bb2b3a97f4fbe1634084a8ce9f15ffbdc20f782034c7919dcbc3369
|
data/lib/aptible/auth/user.rb
CHANGED
@@ -12,6 +12,23 @@ module Aptible
|
|
12
12
|
field :created_at, type: Time
|
13
13
|
field :updated_at, type: Time
|
14
14
|
|
15
|
+
# rubocop:disable MethodLength
|
16
|
+
def set_organization_roles(organization, roles)
|
17
|
+
self.roles.each do |role|
|
18
|
+
next unless role.organization.id == organization.id
|
19
|
+
next if roles.map(&:id).include? role.id
|
20
|
+
|
21
|
+
role_membership = role.memberships.find do |membership|
|
22
|
+
membership.user.id == id
|
23
|
+
end
|
24
|
+
|
25
|
+
role_membership.destroy
|
26
|
+
end
|
27
|
+
|
28
|
+
add_to_roles(roles)
|
29
|
+
end
|
30
|
+
# rubocop:enable MethodLength
|
31
|
+
|
15
32
|
def organizations
|
16
33
|
roles.map(&:organization).uniq(&:id)
|
17
34
|
end
|
@@ -34,6 +51,18 @@ module Aptible
|
|
34
51
|
roles.select { |user_role| role.id == user_role.id }.count > 0
|
35
52
|
end
|
36
53
|
# rubocop:enable PredicateName
|
54
|
+
|
55
|
+
def can_manage?(organization)
|
56
|
+
privileged_organizations.map(&:id).include? organization.id
|
57
|
+
end
|
58
|
+
|
59
|
+
def add_to_roles(roles)
|
60
|
+
roles.each { |role| add_to_role(role) }
|
61
|
+
end
|
62
|
+
|
63
|
+
def add_to_role(role)
|
64
|
+
role.create_membership(user: self, token: token) unless has_role? role
|
65
|
+
end
|
37
66
|
end
|
38
67
|
end
|
39
68
|
end
|
data/lib/aptible/auth/version.rb
CHANGED
@@ -1,6 +1,35 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Aptible::Auth::User do
|
4
|
+
describe '#can_manage?' do
|
5
|
+
let(:developer) { double 'Aptible::Auth::Role' }
|
6
|
+
let(:owner) { double 'Aptible::Auth::Role' }
|
7
|
+
let(:org) { double 'Aptible::Auth::Organization' }
|
8
|
+
|
9
|
+
before do
|
10
|
+
org.stub(:id) { 1 }
|
11
|
+
developer.stub(:organization) { org }
|
12
|
+
allow(developer).to receive(:privileged?).and_return(false)
|
13
|
+
owner.stub(:organization) { org }
|
14
|
+
allow(owner).to receive(:privileged?).and_return(true)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should return false if not member of org privileged role' do
|
18
|
+
subject.stub(:roles) { [developer] }
|
19
|
+
expect(subject.can_manage?(org)).to eq false
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should return true if member of org privileged role' do
|
23
|
+
subject.stub(:roles) { [developer, owner] }
|
24
|
+
expect(subject.can_manage?(org)).to eq true
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should return false if member of no roles' do
|
28
|
+
subject.stub(:roles) { [] }
|
29
|
+
expect(subject.can_manage?(org)).to eq false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
4
33
|
describe '#organizations' do
|
5
34
|
let(:so) { double 'Aptible::Auth::Role' }
|
6
35
|
let(:owner) { double 'Aptible::Auth::Role' }
|
@@ -39,4 +68,75 @@ describe Aptible::Auth::User do
|
|
39
68
|
expect(subject.has_role?(owner)).to eq false
|
40
69
|
end
|
41
70
|
end
|
71
|
+
|
72
|
+
describe '#set_organization_roles' do
|
73
|
+
|
74
|
+
let(:so) { double 'Aptible::Auth::Role' }
|
75
|
+
let(:owner) { double 'Aptible::Auth::Role' }
|
76
|
+
let(:org) { double 'Aptible::Auth::Organization' }
|
77
|
+
let(:owner_membership) { double 'Aptible::Auth::Membership' }
|
78
|
+
let(:so_membership) { double 'Aptible::Auth::Membership' }
|
79
|
+
|
80
|
+
before do
|
81
|
+
org.stub(:id) { 1 }
|
82
|
+
|
83
|
+
so.stub(:organization) { org }
|
84
|
+
so.stub(:id) { 1 }
|
85
|
+
|
86
|
+
owner.stub(:organization) { org }
|
87
|
+
owner.stub(:id) { 2 }
|
88
|
+
|
89
|
+
allow(Aptible::Auth::Role).to receive(:find)
|
90
|
+
.with(1, token: 'token').and_return(so)
|
91
|
+
allow(Aptible::Auth::Role).to receive(:find)
|
92
|
+
.with(2, token: 'token').and_return(owner)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should overwrite existing memberships' do
|
96
|
+
subject.stub(:roles) { [so] }
|
97
|
+
subject.stub(:token) { 'token' }
|
98
|
+
subject.stub(:headers) { {} }
|
99
|
+
so_membership.stub(:user) { subject }
|
100
|
+
so_membership.stub(:role) { so }
|
101
|
+
so.stub(:memberships) { [so_membership] }
|
102
|
+
owner.stub(:memberships) { [] }
|
103
|
+
|
104
|
+
expect(so_membership).to receive(:destroy)
|
105
|
+
expect(owner).to receive(:create_membership)
|
106
|
+
.with(user: subject, token: 'token')
|
107
|
+
|
108
|
+
subject.set_organization_roles(org, [owner])
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'should create new memberships' do
|
112
|
+
subject.stub(:roles) { [] }
|
113
|
+
subject.stub(:token) { 'token' }
|
114
|
+
subject.stub(:headers) { {} }
|
115
|
+
so.stub(:memberships) { [] }
|
116
|
+
owner.stub(:memberships) { [] }
|
117
|
+
|
118
|
+
expect(so).to receive(:create_membership)
|
119
|
+
.with(user: subject, token: 'token')
|
120
|
+
expect(owner).to receive(:create_membership)
|
121
|
+
.with(user: subject, token: 'token')
|
122
|
+
|
123
|
+
subject.set_organization_roles(org, [so, owner])
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should delete all existing memberships' do
|
127
|
+
subject.stub(:roles) { [so, owner] }
|
128
|
+
so.stub(:memberships) { [so_membership] }
|
129
|
+
owner.stub(:memberships) { [owner_membership] }
|
130
|
+
so_membership.stub(:user) { subject }
|
131
|
+
so_membership.stub(:role) { so }
|
132
|
+
owner_membership.stub(:user) { subject }
|
133
|
+
owner_membership.stub(:role) { owner }
|
134
|
+
|
135
|
+
expect(so_membership).to receive(:destroy)
|
136
|
+
expect(owner_membership).to receive(:destroy)
|
137
|
+
|
138
|
+
subject.set_organization_roles(org, [])
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
42
142
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aptible-auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frank Macreery
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aptible-resource
|