warrant 1.2.0 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Warrant
4
4
  class User
5
+ include Warrant::WarrantObject
6
+
5
7
  attr_reader :user_id, :email, :created_at
6
8
 
7
9
  # @!visibility private
@@ -25,10 +27,8 @@ module Warrant
25
27
  # @raise [Warrant::InternalError]
26
28
  # @raise [Warrant::InvalidParameterError]
27
29
  # @raise [Warrant::InvalidRequestError]
28
- # @raise [Warrant::MissingRequiredParameterError]
29
30
  # @raise [Warrant::NotFoundError]
30
31
  # @raise [Warrant::UnauthorizedError]
31
- # @raise [Warrant::WarrantError]
32
32
  def self.create(params = {})
33
33
  res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/users"), Util.normalize_params(params))
34
34
 
@@ -41,6 +41,35 @@ module Warrant
41
41
  end
42
42
  end
43
43
 
44
+ # Batch creates multiple users with given parameters
45
+ #
46
+ # @param [Array] Array of users to create.
47
+ # * user_id User defined string identifier for this user. If not provided, Warrant will create an id for the user and return it. In this case, you should store the id in your system for future reference. Note that tenantIds in Warrant must be composed of alphanumeric chars and/or '-', '_', and '@'. (optional)
48
+ # * email Email address for this user. Designed to be used as a UI-friendly identifier. (optional)
49
+ #
50
+ # @return [Array<User>] all created users
51
+ #
52
+ # @example Create two new users with user ids "test-user-1" and "test-user-2"
53
+ # Warrant::User.batch_create([{ user_id: "test-user-1" }, { user_id: "test-user-2" }])
54
+ #
55
+ # @raise [Warrant::DuplicateRecordError]
56
+ # @raise [Warrant::InternalError]
57
+ # @raise [Warrant::InvalidParameterError]
58
+ # @raise [Warrant::InvalidRequestError]
59
+ # @raise [Warrant::NotFoundError]
60
+ # @raise [Warrant::UnauthorizedError]
61
+ def self.batch_create(users = [])
62
+ res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/users"), Util.normalize_params(users))
63
+
64
+ case res
65
+ when Net::HTTPSuccess
66
+ users = JSON.parse(res.body)
67
+ users.map{ |user| User.new(user['userId'], user['email'], user['createdAt']) }
68
+ else
69
+ APIOperations.raise_error(res)
70
+ end
71
+ end
72
+
44
73
  # Deletes a user with given user id
45
74
  #
46
75
  # @param user_id [String] User defined string identifier for this user. If not provided, Warrant will create an id for the user and return it. In this case, you should store the id in your system as you will need to provide it for any authorization requests for that user. Note that userIds in Warrant must be composed of alphanumeric chars and/or '-', '_', and '@'.
@@ -51,10 +80,9 @@ module Warrant
51
80
  # Warrant::User.delete("test-customer")
52
81
  #
53
82
  # @raise [Warrant::InternalError]
54
- # @raise [Warrant::InvalidRequestError]
83
+ # @raise [Warrant::InvalidParameterError]
55
84
  # @raise [Warrant::NotFoundError]
56
85
  # @raise [Warrant::UnauthorizedError]
57
- # @raise [Warrant::WarrantError]
58
86
  def self.delete(user_id)
59
87
  res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}"))
60
88
 
@@ -68,18 +96,19 @@ module Warrant
68
96
 
69
97
  # Lists all users for your organization
70
98
  #
99
+ # @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
100
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
101
+ #
71
102
  # @return [Array<User>] all users for your organization
72
103
  #
73
104
  # @example List all users
74
105
  # Warrant::User.list()
75
106
  #
76
107
  # @raise [Warrant::InternalError]
77
- # @raise [Warrant::InvalidRequestError]
78
- # @raise [Warrant::NotFoundError]
108
+ # @raise [Warrant::InvalidParameterError]
79
109
  # @raise [Warrant::UnauthorizedError]
80
- # @raise [Warrant::WarrantError]
81
110
  def self.list(filters = {})
82
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/users"))
111
+ res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/users"), Util.normalize_params(filters))
83
112
 
84
113
  case res
85
114
  when Net::HTTPSuccess
@@ -98,10 +127,8 @@ module Warrant
98
127
  #
99
128
  # @raise [Warrant::InternalError]
100
129
  # @raise [Warrant::InvalidParameterError]
101
- # @raise [Warrant::InvalidRequestError]
102
130
  # @raise [Warrant::NotFoundError]
103
131
  # @raise [Warrant::UnauthorizedError]
104
- # @raise [Warrant::WarrantError]
105
132
  def self.get(user_id)
106
133
  res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}"))
107
134
 
@@ -125,13 +152,11 @@ module Warrant
125
152
  # @example Update user "test-user"'s email
126
153
  # Warrant::User.update("test-user", { email: "my-new-email@example.com" })
127
154
  #
128
- # @raise [Warrant::DuplicateRecordError]
129
155
  # @raise [Warrant::InternalError]
130
156
  # @raise [Warrant::InvalidParameterError]
131
157
  # @raise [Warrant::InvalidRequestError]
132
158
  # @raise [Warrant::NotFoundError]
133
159
  # @raise [Warrant::UnauthorizedError]
134
- # @raise [Warrant::WarrantError]
135
160
  def self.update(user_id, params = {})
136
161
  res = APIOperations.put(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}"), Util.normalize_params(params))
137
162
 
@@ -154,35 +179,27 @@ module Warrant
154
179
  # user = Warrant::User.get("test-user")
155
180
  # user.update(email: "my-new-email@example.com")
156
181
  #
157
- # @raise [Warrant::DuplicateRecordError]
158
182
  # @raise [Warrant::InternalError]
159
183
  # @raise [Warrant::InvalidParameterError]
160
184
  # @raise [Warrant::InvalidRequestError]
161
185
  # @raise [Warrant::NotFoundError]
162
186
  # @raise [Warrant::UnauthorizedError]
163
- # @raise [Warrant::WarrantError]
164
187
  def update(params = {})
165
188
  return User.update(user_id, params)
166
189
  end
167
190
 
168
191
  # List all roles for a user.
169
192
  #
193
+ # @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
194
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
195
+ #
170
196
  # @return [Array<Role>] all roles for the user
171
197
  #
172
198
  # @raise [Warrant::InternalError]
173
- # @raise [Warrant::InvalidRequestError]
199
+ # @raise [Warrant::MissingRequiredParameterError]
174
200
  # @raise [Warrant::UnauthorizedError]
175
- # @raise [Warrant::WarrantError]
176
- def list_roles
177
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/roles"))
178
-
179
- case res
180
- when Net::HTTPSuccess
181
- roles = JSON.parse(res.body)
182
- roles.map{ |role| Role.new(role['roleId']) }
183
- else
184
- APIOperations.raise_error(res)
185
- end
201
+ def list_roles(filters = {})
202
+ return Role.list_for_user(user_id, filters)
186
203
  end
187
204
 
188
205
  # Assign a role to a user
@@ -192,16 +209,16 @@ module Warrant
192
209
  #
193
210
  # @return [Permission] assigned role
194
211
  #
212
+ # @example
213
+ # user = Warrant::User.get("fawa324nfa")
214
+ # user.assign_role("admin")
215
+ #
216
+ # @raise [Warrant::DuplicateRecordError]
195
217
  # @raise [Warrant::InternalError]
196
- # @raise [Warrant::InvalidRequestError]
218
+ # @raise [Warrant::InvalidParameterError]
197
219
  # @raise [Warrant::MissingRequiredParameterError]
198
220
  # @raise [Warrant::NotFoundError]
199
221
  # @raise [Warrant::UnauthorizedError]
200
- # @raise [Warrant::WarrantError]
201
- #
202
- # @example
203
- # user = Warrant::User.get("fawa324nfa")
204
- # user.assign_role("admin")
205
222
  def assign_role(role_id)
206
223
  return Role.assign_to_user(user_id, role_id)
207
224
  end
@@ -213,38 +230,31 @@ module Warrant
213
230
  #
214
231
  # @return [nil] if remove was successful
215
232
  #
233
+ # @example
234
+ # user = Warrant::User.get("fawa324nfa")
235
+ # user.remove_role("admin")
236
+ #
237
+ # @raise [Warrant::ForbiddenError]
216
238
  # @raise [Warrant::InternalError]
217
- # @raise [Warrant::InvalidRequestError]
218
239
  # @raise [Warrant::MissingRequiredParameterError]
219
240
  # @raise [Warrant::NotFoundError]
220
241
  # @raise [Warrant::UnauthorizedError]
221
- # @raise [Warrant::WarrantError]
222
- #
223
- # @example
224
- # user = Warrant::User.get("fawa324nfa")
225
- # user.remove_role("admin")
226
242
  def remove_role(role_id)
227
243
  return Role.remove_from_user(user_id, role_id)
228
244
  end
229
245
 
230
246
  # List all permissions for a user
231
247
  #
248
+ # @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
249
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
250
+ #
232
251
  # @return [Array<Permission>] all permissions for the user
233
252
  #
234
253
  # @raise [Warrant::InternalError]
235
- # @raise [Warrant::InvalidRequestError]
254
+ # @raise [Warrant::MissingRequiredParameterError]
236
255
  # @raise [Warrant::UnauthorizedError]
237
- # @raise [Warrant::WarrantError]
238
- def list_permissions
239
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/permissions"))
240
-
241
- case res
242
- when Net::HTTPSuccess
243
- permissions = JSON.parse(res.body)
244
- permissions.map{ |permission| Permission.new(permission['permissionId']) }
245
- else
246
- APIOperations.raise_error(res)
247
- end
256
+ def list_permissions(filters = {})
257
+ return Permission.list_for_user(user_id, filters)
248
258
  end
249
259
 
250
260
  # Assign a permission to a user
@@ -253,16 +263,16 @@ module Warrant
253
263
  #
254
264
  # @return [Permission] assigned permission
255
265
  #
266
+ # @example
267
+ # user = Warrant::User.get("fawa324nfa")
268
+ # user.assign_permission("edit-report")
269
+ #
270
+ # @raise [Warrant::DuplicateRecordError]
256
271
  # @raise [Warrant::InternalError]
257
- # @raise [Warrant::InvalidRequestError]
272
+ # @raise [Warrant::InvalidParameterError]
258
273
  # @raise [Warrant::MissingRequiredParameterError]
259
274
  # @raise [Warrant::NotFoundError]
260
275
  # @raise [Warrant::UnauthorizedError]
261
- # @raise [Warrant::WarrantError]
262
- #
263
- # @example
264
- # user = Warrant::User.get("fawa324nfa")
265
- # user.assign_permission("edit-report")
266
276
  def assign_permission(permission_id)
267
277
  return Permission.assign_to_user(user_id, permission_id)
268
278
  end
@@ -273,16 +283,14 @@ module Warrant
273
283
  #
274
284
  # @return [nil] if remove was successful
275
285
  #
286
+ # @example
287
+ # user = Warrant::User.get("fawa324nfa")
288
+ # user.remove_permission("edit-report")
289
+ #
276
290
  # @raise [Warrant::InternalError]
277
- # @raise [Warrant::InvalidRequestError]
278
291
  # @raise [Warrant::MissingRequiredParameterError]
279
292
  # @raise [Warrant::NotFoundError]
280
293
  # @raise [Warrant::UnauthorizedError]
281
- # @raise [Warrant::WarrantError]
282
- #
283
- # @example
284
- # user = Warrant::User.get("fawa324nfa")
285
- # user.remove_permission("edit-report")
286
294
  def remove_permission(permission_id)
287
295
  Permission.remove_from_user(user_id, permission_id)
288
296
  end
@@ -290,6 +298,9 @@ module Warrant
290
298
  # Checks whether a user has a given permission
291
299
  #
292
300
  # @param permission_id [String] The permission_id of the permission you want to check whether or not it exists on the user.
301
+ # @option options [Hash] :context Object containing key-value pairs that specifies the context the warrant should be checked in. (optional)
302
+ # @option options [Boolean] :consistent_read Boolean flag indicating whether or not to enforce strict consistency for this access check. Defaults to false. (optional)
303
+ # @option options [Boolean] :debug Boolean flag indicating whether or not to return debug information for this access check. Defaults to false. (optional)
293
304
  #
294
305
  # @return [Boolean] whether or not the user has the given permission
295
306
  #
@@ -299,25 +310,41 @@ module Warrant
299
310
  #
300
311
  # @raise [Warrant::InternalError]
301
312
  # @raise [Warrant::InvalidParameterError]
302
- # @raise [Warrant::InvalidRequestError]
303
- # @raise [Warrant::MissingRequiredParameterError]
304
313
  # @raise [Warrant::NotFoundError]
305
314
  # @raise [Warrant::UnauthorizedError]
306
- # @raise [Warrant::WarrantError]
307
- def has_permission?(permission_id)
308
- return Warrant.is_authorized?(
309
- warrants: [{
310
- object_type: "permission",
311
- object_id: permission_id,
312
- relation: "member",
313
- subject: {
314
- object_type: "user",
315
- object_id: user_id
316
- }
317
- }]
315
+ def has_permission?(permission_id, opts = {})
316
+ return Warrant.user_has_permission?(
317
+ permission_id: permission_id,
318
+ user_id: user_id,
319
+ context: opts[:context],
320
+ consistent_read: opts[:consistent_read],
321
+ debug: opts[:debug]
318
322
  )
319
323
  end
320
324
 
325
+ # List all users for a tenant
326
+ #
327
+ # @param tenant_id [String] The tenant_id of the tenant from which to fetch users
328
+ # @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
329
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
330
+ #
331
+ # @return [Array<User>] all users for the tenant
332
+ #
333
+ # @raise [Warrant::InternalError]
334
+ # @raise [Warrant::InvalidParameterError]
335
+ # @raise [Warrant::UnauthorizedError]
336
+ def self.list_for_tenant(tenant_id, filters = {})
337
+ res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/tenants/#{tenant_id}/users"), Util.normalize_params(filters))
338
+
339
+ case res
340
+ when Net::HTTPSuccess
341
+ users = JSON.parse(res.body)
342
+ users.map{ |user| User.new(user['userId'], user['email'], user['createdAt']) }
343
+ else
344
+ APIOperations.raise_error(res)
345
+ end
346
+ end
347
+
321
348
  # Add a user to a tenant
322
349
  #
323
350
  # @param tenant_id [String] The tenant_id of the tenant you want to assign a user to.
@@ -325,13 +352,12 @@ module Warrant
325
352
  #
326
353
  # @return [Warrant] warrant assigning user to the tenant
327
354
  #
355
+ # @raise [Warrant::DuplicateRecordError]
328
356
  # @raise [Warrant::InternalError]
329
357
  # @raise [Warrant::InvalidParameterError]
330
- # @raise [Warrant::InvalidRequestError]
331
358
  # @raise [Warrant::NotFoundError]
332
359
  # @raise [Warrant::UnauthorizedError]
333
- # @raise [Warrant::WarrantError]
334
- def self.add_to_tenant(tenant_id, user_id)
360
+ def self.assign_to_tenant(tenant_id, user_id)
335
361
  res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/tenants/#{tenant_id}/users/#{user_id}"))
336
362
 
337
363
  case res
@@ -352,8 +378,6 @@ module Warrant
352
378
  # @return [nil] if remove was successful
353
379
  #
354
380
  # @raise [Warrant::InternalError]
355
- # @raise [Warrant::InvalidParameterError]
356
- # @raise [Warrant::InvalidRequestError]
357
381
  # @raise [Warrant::NotFoundError]
358
382
  # @raise [Warrant::UnauthorizedError]
359
383
  # @raise [Warrant::WarrantError]
@@ -368,38 +392,141 @@ module Warrant
368
392
  end
369
393
  end
370
394
 
371
- # List all users for a tenant
395
+ # List all tenants for a user
372
396
  #
373
- # @param tenant_id [String] The tenant_id of the tenant from which to fetch users
397
+ # @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
398
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
374
399
  #
375
- # @return [Array<User>] all users for the tenant
400
+ # @return [Array<Tenant>] all tenants for the user
376
401
  #
377
402
  # @raise [Warrant::InternalError]
378
403
  # @raise [Warrant::InvalidRequestError]
379
404
  # @raise [Warrant::UnauthorizedError]
380
405
  # @raise [Warrant::WarrantError]
381
- def self.list_for_tenant(tenant_id)
382
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/tenants/#{tenant_id}/users"))
406
+ def list_tenants(filters = {})
407
+ return Tenant.list_for_user(user_id, filters)
408
+ end
383
409
 
384
- case res
385
- when Net::HTTPSuccess
386
- users = JSON.parse(res.body)
387
- users.map{ |user| User.new(user['userId'], user['email'], user['createdAt']) }
388
- else
389
- APIOperations.raise_error(res)
390
- end
410
+ # List pricing tiers for a user
411
+ #
412
+ # @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
413
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
414
+ #
415
+ # @return [Array<PricingTier>] assigned pricing tiers for the user
416
+ #
417
+ # @raise [Warrant::InternalError]
418
+ # @raise [Warrant::InvalidParameterError]
419
+ # @raise [Warrant::UnauthorizedError]
420
+ def list_pricing_tiers(filters = {})
421
+ return PricingTier.list_for_user(user_id, filters)
391
422
  end
392
423
 
393
- # List all tenants for a user
424
+ # Assign a pricing tier to a user
394
425
  #
395
- # @return [Array<Tenant>] all tenants for the user
426
+ # @param pricing_tier_id [String] The pricing_tier_id of the pricing tier you want to assign to the user.
427
+ #
428
+ # @return [PricingTier] assigned pricing tier
396
429
  #
430
+ # @raise [Warrant::DuplicateRecordError]
397
431
  # @raise [Warrant::InternalError]
398
- # @raise [Warrant::InvalidRequestError]
432
+ # @raise [Warrant::InvalidParameterError]
433
+ # @raise [Warrant::NotFoundError]
434
+ # @raise [Warrant::UnauthorizedError]
435
+ def assign_pricing_tier(pricing_tier_id)
436
+ return PricingTier.assign_to_user(user_id, pricing_tier_id)
437
+ end
438
+
439
+ # Remove a pricing tier from a user
440
+ #
441
+ # @param pricing_tier_id [String] The pricing_tier_id of the pricing tier you want to assign from the user.
442
+ #
443
+ # @return [nil] if remove was successful
444
+ #
445
+ # @raise [Warrant::InternalError]
446
+ # @raise [Warrant::InvalidParameterError]
447
+ # @raise [Warrant::NotFoundError]
399
448
  # @raise [Warrant::UnauthorizedError]
400
449
  # @raise [Warrant::WarrantError]
401
- def list_tenants
402
- return Tenant.list_for_user(user_id)
450
+ def remove_pricing_tier(pricing_tier_id)
451
+ return PricingTier.remove_from_user(user_id, pricing_tier_id)
452
+ end
453
+
454
+ # List features for a user
455
+ #
456
+ # @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
457
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
458
+ #
459
+ # @return [Array<Feature>] assigned features for the user
460
+ #
461
+ # @raise [Warrant::InternalError]
462
+ # @raise [Warrant::InvalidParameterError]
463
+ # @raise [Warrant::UnauthorizedError]
464
+ def list_features(filters = {})
465
+ return Feature.list_for_user(user_id, filters)
466
+ end
467
+
468
+ # Assign a feature to a user
469
+ #
470
+ # @param feature_id [String] The feature_id of the feature you want to assign to the user.
471
+ #
472
+ # @return [Feature] assigned feature
473
+ #
474
+ # @raise [Warrant::DuplicateRecordError]
475
+ # @raise [Warrant::InternalError]
476
+ # @raise [Warrant::InvalidParameterError]
477
+ # @raise [Warrant::NotFoundError]
478
+ # @raise [Warrant::UnauthorizedError]
479
+ def assign_feature(feature_id)
480
+ return Feature.assign_to_user(user_id, feature_id)
481
+ end
482
+
483
+ # Remove a feature from a user
484
+ #
485
+ # @param feature_id [String] The feature_id of the feature you want to assign from the user.
486
+ #
487
+ # @return [nil] if remove was successful
488
+ #
489
+ # @raise [Warrant::InternalError]
490
+ # @raise [Warrant::InvalidParameterError]
491
+ # @raise [Warrant::NotFoundError]
492
+ # @raise [Warrant::UnauthorizedError]
493
+ # @raise [Warrant::WarrantError]
494
+ def remove_feature(feature_id)
495
+ return Feature.remove_from_user(user_id, feature_id)
496
+ end
497
+
498
+ # Check whether a user has a given feature
499
+ #
500
+ # @param feature_id [String] The feature_id of the feature to check whether the user has access to.
501
+ # @option options [Hash] :context Object containing key-value pairs that specifies the context the warrant should be checked in. (optional)
502
+ # @option options [Boolean] :consistent_read Boolean flag indicating whether or not to enforce strict consistency for this access check. Defaults to false. (optional)
503
+ # @option options [Boolean] :debug Boolean flag indicating whether or not to return debug information for this access check. Defaults to false. (optional)
504
+ #
505
+ # @ return [Boolean] whether or not the user has the given feature
506
+ #
507
+ # @raise [Warrant::InternalError]
508
+ # @raise [Warrant::InvalidParameterError]
509
+ # @raise [Warrant::NotFoundError]
510
+ # @raise [Warrant::UnauthorizedError]
511
+ def has_feature?(feature_id, opts = {})
512
+ return Warrant.has_feature?(
513
+ feature_id: feature_id,
514
+ subject: {
515
+ object_type: "user",
516
+ object_id: user_id
517
+ },
518
+ context: opts[:context],
519
+ consistent_read: opts[:consistent_read],
520
+ debug: opts[:debug]
521
+ )
522
+ end
523
+
524
+ def warrant_object_type
525
+ "user"
526
+ end
527
+
528
+ def warrant_object_id
529
+ user_id
403
530
  end
404
531
  end
405
532
  end