Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit 25d291b

Browse files
Update setting permissions on org creation
[#175042021] Co-authored-by: Jenna Goldstrich <jgoldstrich@pivotal.io> Co-authored-by: Merric de Launey <mdelauney@pivotal.io>
1 parent a567562 commit 25d291b

4 files changed

Lines changed: 91 additions & 19 deletions

File tree

app/actions/organization_create.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'role_create'
2+
13
module VCAP::CloudController
24
class OrganizationCreate
35
class Error < ::StandardError
@@ -8,7 +10,7 @@ def initialize(perm_client:, user_audit_info:)
810
@user_audit_info = user_audit_info
911
end
1012

11-
def create(message)
13+
def create(message, user)
1214
org = nil
1315
Organization.db.transaction do
1416
org = VCAP::CloudController::Organization.create(
@@ -19,8 +21,10 @@ def create(message)
1921
MetadataUpdate.update(org, message)
2022
end
2123

22-
VCAP::CloudController::Roles::ORG_ROLE_NAMES.each do |role|
23-
perm_client.create_org_role(role: role, org_id: org.guid)
24+
VCAP::CloudController::RoleTypes::ORGANIZATION_ROLES.each do |role|
25+
VCAP::CloudController::RoleCreate.new(message, @user_audit_info).create_organization_role(type: role,
26+
user: user,
27+
organization: org)
2428
end
2529

2630
Repositories::OrganizationEventRepository.new.record_organization_create(org, @user_audit_info, message.audit_hash)

app/controllers/v3/organizations_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def create
4747
message = VCAP::CloudController::OrganizationCreateMessage.new(hashed_params[:body])
4848
unprocessable!(message.errors.full_messages) unless message.valid?
4949

50-
org = OrganizationCreate.new(perm_client: perm_client, user_audit_info: user_audit_info).create(message)
50+
org = OrganizationCreate.new(perm_client: perm_client, user_audit_info: user_audit_info).create(message, current_user)
5151

5252
render json: Presenters::V3::OrganizationPresenter.new(org), status: :created
5353
rescue OrganizationCreate::Error => e

spec/request/organizations_spec.rb

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,24 @@ module VCAP::CloudController
1010
let!(:organization2) { Organization.make name: 'Dungeon World' }
1111
let!(:organization3) { Organization.make name: 'The Sprawl' }
1212
let!(:inaccessible_organization) { Organization.make name: 'D&D' }
13+
let(:uaa_client) { instance_double(VCAP::CloudController::UaaClient) }
1314

1415
before do
1516
organization1.add_user(user)
1617
organization2.add_user(user)
1718
organization3.add_user(user)
1819
Domain.dataset.destroy # this will clean up the seeded test domains
1920
TestConfig.override(kubernetes: {})
21+
22+
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
23+
allow(uaa_client).to receive(:usernames_for_ids).with([user.guid]).and_return(
24+
{ user.guid => 'Ragnaros' }
25+
)
2026
end
2127

2228
describe 'POST /v3/organizations' do
23-
it 'creates a new organization with the given name' do
24-
request_body = {
29+
let(:request_body) {
30+
{
2531
name: 'org1',
2632
metadata: {
2733
labels: {
@@ -34,7 +40,8 @@ module VCAP::CloudController
3440
}
3541
}
3642
}.to_json
37-
43+
}
44+
it 'creates a new organization with the given name' do
3845
expect {
3946
post '/v3/organizations', request_body, admin_header
4047
}.to change {
@@ -68,12 +75,12 @@ module VCAP::CloudController
6875
end
6976

7077
it 'allows creating a suspended org' do
71-
request_body = {
78+
suspended_request_body = {
7279
name: 'suspended-org',
7380
suspended: true
7481
}.to_json
7582

76-
post '/v3/organizations', request_body, admin_header
83+
post '/v3/organizations', suspended_request_body, admin_header
7784
expect(last_response.status).to eq(201)
7885

7986
created_org = Organization.last
@@ -96,6 +103,58 @@ module VCAP::CloudController
96103
}
97104
)
98105
end
106+
107+
context 'when "user_org_creation" feature flag is enabled' do
108+
before do
109+
VCAP::CloudController::FeatureFlag.make(name: 'user_org_creation', enabled: true)
110+
end
111+
112+
it 'lets ALL users create orgs' do
113+
expect {
114+
post '/v3/organizations', request_body, user_header
115+
}.to change {
116+
Organization.count
117+
}.by 1
118+
119+
created_org = Organization.last
120+
121+
expect(last_response.status).to eq(201)
122+
expect(parsed_response).to be_a_response_like(
123+
{
124+
'guid' => created_org.guid,
125+
'created_at' => iso8601,
126+
'updated_at' => iso8601,
127+
'name' => 'org1',
128+
'links' => {
129+
'self' => { 'href' => "#{link_prefix}/v3/organizations/#{created_org.guid}" },
130+
'domains' => { 'href' => "http://api2.vcap.me/v3/organizations/#{created_org.guid}/domains" },
131+
'default_domain' => { 'href' => "http://api2.vcap.me/v3/organizations/#{created_org.guid}/domains/default" },
132+
'quota' => { 'href' => "http://api2.vcap.me/v3/organization_quotas/#{created_org.quota_definition.guid}" }
133+
},
134+
'relationships' => { 'quota' => { 'data' => { 'guid' => created_org.quota_definition.guid } } },
135+
'metadata' => {
136+
'labels' => { 'freaky' => 'friday' },
137+
'annotations' => { 'make' => 'subaru', 'model' => 'xv crosstrek', 'color' => 'orange' }
138+
},
139+
'suspended' => false
140+
}
141+
)
142+
end
143+
it 'gives the user all org roles associated with the new org' do
144+
expect {
145+
post '/v3/organizations', request_body, user_header
146+
}.to change {
147+
Organization.count
148+
}.by 1
149+
150+
created_org = Organization.last
151+
expect(OrganizationManager.first(organization_id: created_org.id, user_id: user.id)).to be_present
152+
expect(OrganizationBillingManager.first(organization_id: created_org.id, user_id: user.id)).to be_present
153+
expect(OrganizationAuditor.first(organization_id: created_org.id, user_id: user.id)).to be_present
154+
expect(OrganizationUser.first(organization_id: created_org.id, user_id: user.id)).to be_present
155+
expect(last_response.status).to eq(201)
156+
end
157+
end
99158
end
100159

101160
describe 'GET /v3/organizations' do

spec/unit/actions/organization_create_spec.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ module VCAP::CloudController
88
let(:user_email) { 'user@example.com' }
99
let(:user_audit_info) { UserAuditInfo.new(user_guid: user.guid, user_email: user_email) }
1010
let(:perm_client) { instance_spy(VCAP::CloudController::Perm::Client) }
11+
let(:uaa_client) { instance_double(VCAP::CloudController::UaaClient) }
1112
subject(:org_create) { OrganizationCreate.new(perm_client: perm_client, user_audit_info: user_audit_info) }
1213

14+
before do
15+
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
16+
allow(uaa_client).to receive(:usernames_for_ids).with([user.guid]).and_return(
17+
{ user.guid => 'Ragnaros' }
18+
)
19+
end
20+
1321
context 'when creating a non-suspended organization' do
1422
let(:message) do
1523
VCAP::CloudController::OrganizationUpdateMessage.new({
@@ -28,7 +36,7 @@ module VCAP::CloudController
2836
end
2937

3038
it 'creates a organization' do
31-
organization = org_create.create(message)
39+
organization = org_create.create(message, user)
3240

3341
expect(organization.name).to eq('my-organization')
3442

@@ -44,10 +52,11 @@ module VCAP::CloudController
4452
end
4553

4654
it 'creates an audit event' do
47-
created_org = org_create.create(message)
48-
expect(VCAP::CloudController::Event.count).to eq(1)
49-
event = VCAP::CloudController::Event.first
50-
expect(event.values).to include(
55+
created_org = org_create.create(message, user)
56+
expect(VCAP::CloudController::Event.count).to eq(5)
57+
org_create_event = VCAP::CloudController::Event.find(type: 'audit.organization.create')
58+
expect(org_create_event).to exist
59+
expect(org_create_event.values).to include(
5160
type: 'audit.organization.create',
5261
actor: user_audit_info.user_guid,
5362
actor_type: 'user',
@@ -58,8 +67,8 @@ module VCAP::CloudController
5867
actee_name: 'my-organization',
5968
organization_guid: created_org.guid
6069
)
61-
expect(event.metadata).to eq({ 'request' => message.audit_hash })
62-
expect(event.timestamp).to be
70+
expect(org_create_event.metadata).to eq({ 'request' => message.audit_hash })
71+
expect(org_create_event.timestamp).to be
6372
end
6473
end
6574

@@ -68,7 +77,7 @@ module VCAP::CloudController
6877
name: 'my-organization',
6978
suspended: true
7079
})
71-
organization = org_create.create(message)
80+
organization = org_create.create(message, user)
7281

7382
expect(organization.name).to eq('my-organization')
7483
expect(organization.suspended?).to be true
@@ -83,7 +92,7 @@ module VCAP::CloudController
8392

8493
message = VCAP::CloudController::OrganizationUpdateMessage.new(name: 'foobar')
8594
expect {
86-
org_create.create(message)
95+
org_create.create(message, user)
8796
}.to raise_error(OrganizationCreate::Error, 'blork is busted')
8897
end
8998

@@ -97,7 +106,7 @@ module VCAP::CloudController
97106
it 'raises a human-friendly error' do
98107
message = VCAP::CloudController::OrganizationUpdateMessage.new(name: name)
99108
expect {
100-
org_create.create(message)
109+
org_create.create(message, user)
101110
}.to raise_error(OrganizationCreate::Error, "Organization '#{name}' already exists.")
102111
end
103112
end

0 commit comments

Comments
 (0)