-
Notifications
You must be signed in to change notification settings - Fork 359
/
permissions.rb
302 lines (239 loc) · 8.91 KB
/
permissions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
class VCAP::CloudController::Permissions
ROLES_FOR_ORG_READING ||= [
VCAP::CloudController::Membership::ORG_MANAGER,
VCAP::CloudController::Membership::ORG_AUDITOR,
VCAP::CloudController::Membership::ORG_USER,
VCAP::CloudController::Membership::ORG_BILLING_MANAGER,
].freeze
ROLES_FOR_ORG_CONTENT_READING = [
VCAP::CloudController::Membership::ORG_MANAGER,
].freeze
ROLES_FOR_ORG_WRITING = [
VCAP::CloudController::Membership::ORG_MANAGER,
].freeze
ROLES_FOR_SPACE_READING ||= [
VCAP::CloudController::Membership::SPACE_DEVELOPER,
VCAP::CloudController::Membership::SPACE_MANAGER,
VCAP::CloudController::Membership::SPACE_AUDITOR,
VCAP::CloudController::Membership::ORG_MANAGER,
VCAP::CloudController::Membership::SPACE_SUPPORTER,
].freeze
ROLES_FOR_DROPLET_DOWLOAD ||= [
VCAP::CloudController::Membership::SPACE_DEVELOPER,
VCAP::CloudController::Membership::SPACE_MANAGER,
VCAP::CloudController::Membership::SPACE_AUDITOR,
VCAP::CloudController::Membership::ORG_MANAGER,
].freeze
ORG_ROLES_FOR_READING_DOMAINS_FROM_ORGS ||= [
VCAP::CloudController::Membership::ORG_MANAGER,
VCAP::CloudController::Membership::ORG_AUDITOR,
].freeze
SPACE_ROLES ||= [
VCAP::CloudController::Membership::SPACE_DEVELOPER,
VCAP::CloudController::Membership::SPACE_MANAGER,
VCAP::CloudController::Membership::SPACE_AUDITOR,
VCAP::CloudController::Membership::SPACE_SUPPORTER,
].freeze
SPACE_ROLES_FOR_EVENTS ||= [
VCAP::CloudController::Membership::SPACE_AUDITOR,
VCAP::CloudController::Membership::SPACE_DEVELOPER,
VCAP::CloudController::Membership::SPACE_SUPPORTER
].freeze
ROLES_FOR_SPACE_SECRETS_READING ||= [
VCAP::CloudController::Membership::SPACE_DEVELOPER,
].freeze
ROLES_FOR_SPACE_SERVICES_READING ||= [
VCAP::CloudController::Membership::SPACE_DEVELOPER,
VCAP::CloudController::Membership::SPACE_SUPPORTER
].freeze
ROLES_FOR_ROUTE_READING ||= ROLES_FOR_SPACE_READING + [
VCAP::CloudController::Membership::ORG_AUDITOR
].freeze
ROLES_FOR_SPACE_WRITING ||= [
VCAP::CloudController::Membership::SPACE_DEVELOPER,
].freeze
ROLES_FOR_APP_MANAGING ||= (ROLES_FOR_SPACE_WRITING + [
VCAP::CloudController::Membership::SPACE_SUPPORTER,
]).freeze
ROLES_FOR_SPACE_UPDATING ||= [
VCAP::CloudController::Membership::SPACE_MANAGER,
VCAP::CloudController::Membership::ORG_MANAGER,
].freeze
ROLES_FOR_ROUTE_WRITING ||= [
VCAP::CloudController::Membership::SPACE_DEVELOPER,
].freeze
ROLES_FOR_APP_ENVIRONMENT_VARIABLES_READING ||= (ROLES_FOR_SPACE_SECRETS_READING + [
VCAP::CloudController::Membership::SPACE_SUPPORTER,
]).freeze
def initialize(user)
@user = user
end
def can_read_globally?
roles.admin? || roles.admin_read_only? || roles.global_auditor?
end
def can_read_secrets_globally?
roles.admin? || roles.admin_read_only?
end
def can_write_globally?
roles.admin?
end
def readable_org_guids
readable_org_guids_query.select_map(:guid)
end
def readable_org_guids_query
if can_read_globally?
raise 'must not be called for users that can read globally'
else
membership.authorized_org_guids_subquery(ROLES_FOR_ORG_READING)
end
end
def readable_orgs_query
if can_read_globally?
VCAP::CloudController::Organization.select(:id, :guid)
else
membership.authorized_orgs_subquery(ROLES_FOR_ORG_READING)
end
end
def readable_org_guids_for_domains_query
if can_read_globally?
VCAP::CloudController::Organization.select(:guid)
else
membership.authorized_org_guids_subquery(ORG_ROLES_FOR_READING_DOMAINS_FROM_ORGS + SPACE_ROLES)
end
end
def can_read_from_org?(org_id)
can_read_globally? || membership.role_applies?(ROLES_FOR_ORG_READING, nil, org_id)
end
def can_write_to_active_org?(org_id)
return true if can_write_globally?
membership.role_applies?(ROLES_FOR_ORG_WRITING, nil, org_id)
end
def is_org_active?(org_id)
return true if can_write_globally? # admins can modify suspended orgs
!VCAP::CloudController::Organization.
where(id: org_id, status: VCAP::CloudController::Organization::ACTIVE).
empty?
end
def is_space_active?(space_id)
return true if can_write_globally? # admins can modify suspended orgs
!VCAP::CloudController::Organization.
join(:spaces, organization_id: :id).
where(spaces__id: space_id, organizations__status: VCAP::CloudController::Organization::ACTIVE).
empty?
end
def readable_space_guids
readable_space_guids_query.select_map(:guid)
end
def readable_space_guids_query
if can_read_globally?
raise 'must not be called for users that can read globally'
else
membership.authorized_space_guids_subquery(ROLES_FOR_SPACE_READING)
end
end
def can_read_from_space?(space_id, org_id)
can_read_globally? || membership.role_applies?(ROLES_FOR_SPACE_READING, space_id, org_id)
end
def can_download_droplet?(space_id, org_id)
can_read_globally? || membership.role_applies?(ROLES_FOR_DROPLET_DOWLOAD, space_id, org_id)
end
def can_read_secrets_in_space?(space_id, org_id)
can_read_secrets_globally? ||
membership.role_applies?(ROLES_FOR_SPACE_SECRETS_READING, space_id, org_id)
end
def can_read_services_in_space?(space_id, org_id)
can_read_globally? || membership.role_applies?(ROLES_FOR_SPACE_SERVICES_READING, space_id, org_id)
end
def can_write_to_active_space?(space_id)
return true if can_write_globally?
membership.role_applies?(ROLES_FOR_SPACE_WRITING, space_id)
end
def can_manage_apps_in_active_space?(space_id)
return true if can_write_globally?
membership.role_applies?(ROLES_FOR_APP_MANAGING, space_id)
end
def can_update_active_space?(space_id, org_id)
return true if can_write_globally?
membership.role_applies?(ROLES_FOR_SPACE_UPDATING, space_id, org_id)
end
def can_read_from_isolation_segment?(isolation_segment)
can_read_globally? || readable_org_guids_query.where(isolation_segment_models: isolation_segment).any?
end
def readable_route_dataset
if can_read_globally?
VCAP::CloudController::Route.dataset
else
VCAP::CloudController::Route.user_visible(@user, can_read_globally?)
end
end
def readable_services_space_guids
if can_read_secrets_globally?
VCAP::CloudController::Space.select_map(:guid)
else
membership.authorized_space_guids(ROLES_FOR_SPACE_SERVICES_READING)
end
end
def readable_space_scoped_spaces
readable_space_scoped_spaces_query.all
end
def readable_space_scoped_spaces_query
if can_read_globally?
VCAP::CloudController::Space.select(:id, :guid)
else
membership.authorized_spaces_subquery(SPACE_ROLES)
end
end
def can_read_route?(space_id)
return true if can_read_globally?
space = VCAP::CloudController::Space.where(id: space_id).first
space.has_member?(@user) || space.has_supporter?(@user) ||
@user.managed_organizations.map(&:id).include?(space.organization_id) ||
@user.audited_organizations.map(&:id).include?(space.organization_id)
end
def space_guids_with_readable_routes_query
if can_read_globally?
raise 'must not be called for users that can read globally'
else
membership.authorized_space_guids_subquery(ROLES_FOR_ROUTE_READING)
end
end
def can_read_app_environment_variables?(space_id, org_id)
can_read_secrets_globally? ||
membership.role_applies?(ROLES_FOR_APP_ENVIRONMENT_VARIABLES_READING, space_id, org_id)
end
def can_read_system_environment_variables?(space_id, org_id)
can_read_secrets_globally? ||
membership.role_applies?(ROLES_FOR_SPACE_SECRETS_READING, space_id, org_id)
end
def readable_app_guids
VCAP::CloudController::AppModel.user_visible(@user, can_read_globally?).select(:guid).map(&:guid)
end
def readable_space_quota_guids
VCAP::CloudController::SpaceQuotaDefinition.user_visible(@user, can_read_globally?).map(&:guid)
end
def readable_security_group_guids
readable_security_group_guids_query.select_map(:guid)
end
def readable_security_group_guids_query
VCAP::CloudController::SecurityGroup.user_visible(@user, can_read_globally?).select(:guid)
end
def can_update_build_state?
can_write_globally? || roles.build_state_updater?
end
def readable_event_dataset
return VCAP::CloudController::Event.dataset if can_read_globally?
spaces_with_permitted_roles = membership.authorized_space_guids(SPACE_ROLES_FOR_EVENTS)
orgs_with_permitted_roles = membership.authorized_org_guids(VCAP::CloudController::Membership::ORG_AUDITOR)
VCAP::CloudController::Event.dataset.filter(Sequel.or([
[:space_guid, spaces_with_permitted_roles],
[:organization_guid, orgs_with_permitted_roles]
]))
end
private
def membership
@membership ||= VCAP::CloudController::Membership.new(@user)
end
def roles
VCAP::CloudController::SecurityContext.roles
end
end