warrant 4.0.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Warrant
4
- class User
4
+ class User < Warrant::Object
5
5
  OBJECT_TYPE = "user"
6
6
 
7
7
  include Warrant::WarrantObject
8
8
 
9
- attr_reader :user_id, :email, :created_at
9
+ alias :user_id :object_id
10
10
 
11
11
  # @!visibility private
12
- def initialize(user_id, email, created_at)
13
- @user_id = user_id
14
- @email = email
15
- @created_at = created_at
12
+ def initialize(user_id, meta = {}, created_at = nil)
13
+ super(OBJECT_TYPE, user_id, meta, created_at)
16
14
  end
17
15
 
18
16
  # Creates a user with the given parameters
19
17
  #
20
18
  # @option params [String] :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 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 '@'. (optional)
21
- # @option params [String] :email Email address for this user. Designed to be used as a UI-friendly identifier. (optional)
19
+ # @option params [Hash] :meta A JSON object containing additional information about this user (e.g. email/name) to be persisted to Warrant. (optional)
22
20
  #
23
21
  # @return [User] created user
24
22
  #
@@ -31,23 +29,16 @@ module Warrant
31
29
  # @raise [Warrant::InvalidRequestError]
32
30
  # @raise [Warrant::NotFoundError]
33
31
  # @raise [Warrant::UnauthorizedError]
34
- def self.create(params = {})
35
- res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/users"), Util.normalize_params(params))
36
-
37
- case res
38
- when Net::HTTPSuccess
39
- res_json = JSON.parse(res.body)
40
- User.new(res_json['userId'], res_json['email'], res_json['createdAt'])
41
- else
42
- APIOperations.raise_error(res)
43
- end
32
+ def self.create(params = {}, options = {})
33
+ object = Object.create({ object_type: OBJECT_TYPE, object_id: params[:user_id], meta: params[:meta] }, options)
34
+ return User.new(object.object_id, object.meta, object.created_at)
44
35
  end
45
36
 
46
37
  # Batch creates multiple users with given parameters
47
38
  #
48
- # @param [Array] Array of users to create.
49
- # * 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)
50
- # * email Email address for this user. Designed to be used as a UI-friendly identifier. (optional)
39
+ # @param [Array<Hash>] users Array of users to create.
40
+ # @option users [String] :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)
41
+ # @option users [Hash] :meta A JSON object containing additional information about the user (e.g. name/description, etc.) to be persisted to Warrant. (optional)
51
42
  #
52
43
  # @return [Array<User>] all created users
53
44
  #
@@ -60,21 +51,14 @@ module Warrant
60
51
  # @raise [Warrant::InvalidRequestError]
61
52
  # @raise [Warrant::NotFoundError]
62
53
  # @raise [Warrant::UnauthorizedError]
63
- def self.batch_create(users = [])
64
- res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/users"), Util.normalize_params(users))
65
-
66
- case res
67
- when Net::HTTPSuccess
68
- users = JSON.parse(res.body)
69
- users.map{ |user| User.new(user['userId'], user['email'], user['createdAt']) }
70
- else
71
- APIOperations.raise_error(res)
72
- end
54
+ def self.batch_create(users, options = {})
55
+ res = Object.batch_create(users.map{ |user| { object_type: OBJECT_TYPE, object_id: user[:user_id], meta: user[:meta] }}, options)
56
+ return res.map{ |obj| User.new(obj.object_id, obj.meta, obj.created_at)}
73
57
  end
74
58
 
75
59
  # Deletes a user with given user id
76
60
  #
77
- # @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 '@'.
61
+ # @param user_id [String] User defined string identifier for this user.
78
62
  #
79
63
  # @return [nil] if delete was successful
80
64
  #
@@ -85,27 +69,44 @@ module Warrant
85
69
  # @raise [Warrant::InvalidParameterError]
86
70
  # @raise [Warrant::NotFoundError]
87
71
  # @raise [Warrant::UnauthorizedError]
88
- def self.delete(user_id)
89
- res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}"))
90
-
91
- case res
92
- when Net::HTTPSuccess
93
- return
94
- else
95
- APIOperations.raise_error(res)
96
- end
72
+ def self.delete(user_id, options = {})
73
+ return Object.delete(OBJECT_TYPE, user_id, options)
74
+ end
75
+
76
+ # Batch deletes multiple users with given parameters
77
+ #
78
+ # @param [Array<Hash, User>] users Array of users to delete.
79
+ # @option users [String] :user_id Customer defined string identifier for this user.
80
+ #
81
+ # @return [nil] if delete was successful
82
+ #
83
+ # @example Delete two users with ids "test-user-1" and "test-user-2"
84
+ # Warrant::User.batch_delete([{ user_id: "test-user-1" }, { user_id: "test-user-2" }])
85
+ #
86
+ # @raise [Warrant::InternalError]
87
+ # @raise [Warrant::InvalidParameterError]
88
+ # @raise [Warrant::NotFoundError]
89
+ # @raise [Warrant::UnauthorizedError]
90
+ def self.batch_delete(users, options = {})
91
+ return Object.batch_delete(users.map{ |user|
92
+ if user.instance_of? User
93
+ { object_type: OBJECT_TYPE, object_id: user.object_id }
94
+ else
95
+ { object_type: OBJECT_TYPE, object_id: user[:user_id] }
96
+ end
97
+ }, options)
97
98
  end
98
99
 
99
100
  # Lists all users for your organization
100
101
  #
101
- # @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)
102
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
103
- # @option filters [String] :beforeId A string representing a cursor value in the form of a userId. If provided, the results returned are immediately before the provided value. (optional)
104
- # @option filters [String] :beforeValue A string representing a cursor value in the form of the `sortBy` value. If provided, the results returned are immediately before the provided value. (optional)
105
- # @option filters [String] :afterId A string representing a cursor value in the form of a userId. If provided, the results returned are immediately after the provided value. (optional)
106
- # @option filters [String] :afterValue A string representing a cursor value in the form of the `sortBy` value. If provided, the results returned are immediately after the provided value. (optional)
107
- # @option filters [String] :sortBy A string representing the field to sort results by. Default value is userId. (optional)
108
- # @option filters [String] :sortOrder A string representing whether to sort results in ascending or descending order. Must be ASC or DESC. (optional)
102
+ # @param [Hash] filters Filters to apply to result set
103
+ # @param [Hash] options Options to apply on a per-request basis
104
+ # @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
105
+ # @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
106
+ # @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
107
+ # @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
108
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
109
+ # @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
109
110
  #
110
111
  # @return [Array<User>] all users for your organization
111
112
  #
@@ -115,16 +116,11 @@ module Warrant
115
116
  # @raise [Warrant::InternalError]
116
117
  # @raise [Warrant::InvalidParameterError]
117
118
  # @raise [Warrant::UnauthorizedError]
118
- def self.list(filters = {})
119
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/users"), Util.normalize_params(filters))
120
-
121
- case res
122
- when Net::HTTPSuccess
123
- users = JSON.parse(res.body)
124
- users.map{ |user| User.new(user['userId'], user['email'], user['createdAt']) }
125
- else
126
- APIOperations.raise_error(res)
127
- end
119
+ def self.list(filters = {}, options = {})
120
+ filters.merge({ object_type: "user" })
121
+ list_response = Object.list(filters, options)
122
+ users = list_response.results.map{ |object| User.new(object.object_id, object.meta, object.created_at)}
123
+ return ListResponse.new(users, list_response.prev_cursor, list_response.next_cursor)
128
124
  end
129
125
 
130
126
  # Get a user with the given user_id
@@ -137,23 +133,15 @@ module Warrant
137
133
  # @raise [Warrant::InvalidParameterError]
138
134
  # @raise [Warrant::NotFoundError]
139
135
  # @raise [Warrant::UnauthorizedError]
140
- def self.get(user_id)
141
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}"))
142
-
143
- case res
144
- when Net::HTTPSuccess
145
- user = JSON.parse(res.body)
146
- User.new(user['userId'], user['email'], user['createdAt'])
147
- else
148
- APIOperations.raise_error(res)
149
- end
136
+ def self.get(user_id, options = {})
137
+ object = Object.get(OBJECT_TYPE, user_id, options)
138
+ return User.new(object.object_id, object.meta, object.created_at)
150
139
  end
151
140
 
152
- # Updates a user with the given user_id and params
141
+ # Updates a user with the given user_id
153
142
  #
154
- # @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 '@'.
155
- # @param [Hash] params attributes to update user with
156
- # @option params [String] :email Email address for this user. Designed to be used as a UI-friendly identifier. (optional)
143
+ # @param user_id [String] User defined string identifier for this user.
144
+ # @param meta [Hash] A JSON object containing additional information about this user (e.g. name/description, etc.) to be persisted to Warrant.
157
145
  #
158
146
  # @return [User] updated user
159
147
  #
@@ -165,55 +153,56 @@ module Warrant
165
153
  # @raise [Warrant::InvalidRequestError]
166
154
  # @raise [Warrant::NotFoundError]
167
155
  # @raise [Warrant::UnauthorizedError]
168
- def self.update(user_id, params = {})
169
- res = APIOperations.put(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}"), Util.normalize_params(params))
170
-
171
- case res
172
- when Net::HTTPSuccess
173
- res_json = JSON.parse(res.body)
174
- User.new(res_json['userId'], res_json['email'], res_json['createdAt'])
175
- else
176
- APIOperations.raise_error(res)
177
- end
156
+ def self.update(user_id, meta, options = {})
157
+ object = Object.update(OBJECT_TYPE, user_id, meta, options)
158
+ return User.new(object.object_id, object.meta, object.created_at)
178
159
  end
179
160
 
180
161
  # Updates the user with the given params
181
162
  #
182
- # @option params [String] :email Email address for this user. Designed to be used as a UI-friendly identifier. (optional)
163
+ # @param meta [Hash] A JSON object containing additional information about this user (e.g. name/description, etc.) to be persisted to Warrant.
183
164
  #
184
165
  # @return [User] updated user
185
166
  #
186
167
  # @example Update user "test-user"'s email
187
168
  # user = Warrant::User.get("test-user")
188
- # user.update(email: "my-new-email@example.com")
169
+ # user.update({ email: "my-new-email@example.com" })
189
170
  #
190
171
  # @raise [Warrant::InternalError]
191
172
  # @raise [Warrant::InvalidParameterError]
192
173
  # @raise [Warrant::InvalidRequestError]
193
174
  # @raise [Warrant::NotFoundError]
194
175
  # @raise [Warrant::UnauthorizedError]
195
- def update(params = {})
196
- return User.update(user_id, params)
176
+ def update(meta, options = {})
177
+ return User.update(user_id, meta, options)
197
178
  end
198
179
 
199
180
  # List all roles for a user.
200
181
  #
201
- # @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)
202
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
182
+ # @param [Hash] filters Filters to apply to result set
183
+ # @param [Hash] options Options to apply on a per-request basis
184
+ # @option filters [String] :object_type Only return objects with an `objectType` matching this value
185
+ # @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
186
+ # @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
187
+ # @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
188
+ # @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
189
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
190
+ # @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
203
191
  #
204
192
  # @return [Array<Role>] all roles for the user
205
193
  #
206
194
  # @raise [Warrant::InternalError]
207
195
  # @raise [Warrant::MissingRequiredParameterError]
208
196
  # @raise [Warrant::UnauthorizedError]
209
- def list_roles(filters = {})
210
- return Role.list_for_user(user_id, filters)
197
+ def list_roles(filters = {}, options = {})
198
+ return Role.list_for_user(user_id, filters, options)
211
199
  end
212
200
 
213
201
  # Assign a role to a user
214
202
  #
215
203
  # @param user_id [String] The user_id of the user you want to assign a role to.
216
204
  # @param role_id [String] The role_id of the role you want to assign to a user.
205
+ # @param relation [String] The relation for this role to user association. The relation must be valid as per the +role+ object type definition.
217
206
  #
218
207
  # @return [Permission] assigned role
219
208
  #
@@ -227,14 +216,15 @@ module Warrant
227
216
  # @raise [Warrant::MissingRequiredParameterError]
228
217
  # @raise [Warrant::NotFoundError]
229
218
  # @raise [Warrant::UnauthorizedError]
230
- def assign_role(role_id)
231
- return Role.assign_to_user(user_id, role_id)
219
+ def assign_role(role_id, relation: "member", options: {})
220
+ return Role.assign_to_user(user_id, role_id, relation: relation, options: options)
232
221
  end
233
222
 
234
223
  # Remove a role from a user
235
224
  #
236
225
  # @param user_id [String] The user_id of the role you want to assign a role to.
237
226
  # @param role_id [String] The role_id of the role you want to assign to a user.
227
+ # @param relation [String] The relation for this role to user association. The relation must be valid as per the +role+ object type definition.
238
228
  #
239
229
  # @return [nil] if remove was successful
240
230
  #
@@ -247,27 +237,35 @@ module Warrant
247
237
  # @raise [Warrant::MissingRequiredParameterError]
248
238
  # @raise [Warrant::NotFoundError]
249
239
  # @raise [Warrant::UnauthorizedError]
250
- def remove_role(role_id)
251
- return Role.remove_from_user(user_id, role_id)
240
+ def remove_role(role_id, relation: "member", options: {})
241
+ return Role.remove_from_user(user_id, role_id, relation: relation, options: options)
252
242
  end
253
243
 
254
244
  # List all permissions for a user
255
245
  #
256
- # @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)
257
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
246
+ # @param [Hash] filters Filters to apply to result set
247
+ # @param [Hash] options Options to apply on a per-request basis
248
+ # @option filters [String] :object_type Only return objects with an `objectType` matching this value
249
+ # @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
250
+ # @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
251
+ # @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
252
+ # @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
253
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
254
+ # @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
258
255
  #
259
256
  # @return [Array<Permission>] all permissions for the user
260
257
  #
261
258
  # @raise [Warrant::InternalError]
262
259
  # @raise [Warrant::MissingRequiredParameterError]
263
260
  # @raise [Warrant::UnauthorizedError]
264
- def list_permissions(filters = {})
265
- return Permission.list_for_user(user_id, filters)
261
+ def list_permissions(filters = {}, options = {})
262
+ return Permission.list_for_user(user_id, filters, options)
266
263
  end
267
264
 
268
265
  # Assign a permission to a user
269
266
  #
270
267
  # @param permission_id [String] The permission_id of the permission you want to assign to a user.
268
+ # @param relation [String] The relation for this permission to user association. The relation must be valid as per the +permission+ object type definition.
271
269
  #
272
270
  # @return [Permission] assigned permission
273
271
  #
@@ -281,13 +279,14 @@ module Warrant
281
279
  # @raise [Warrant::MissingRequiredParameterError]
282
280
  # @raise [Warrant::NotFoundError]
283
281
  # @raise [Warrant::UnauthorizedError]
284
- def assign_permission(permission_id)
285
- return Permission.assign_to_user(user_id, permission_id)
282
+ def assign_permission(permission_id, relation: "member", options: {})
283
+ return Permission.assign_to_user(user_id, permission_id, relation: relation, options: options)
286
284
  end
287
285
 
288
286
  # Remove a permission from a user
289
287
  #
290
288
  # @param permission_id [String] The permission_id of the permission you want to assign to a user.
289
+ # @param relation [String] The relation for this permission to user association. The relation must be valid as per the +permission+ object type definition.
291
290
  #
292
291
  # @return [nil] if remove was successful
293
292
  #
@@ -299,13 +298,14 @@ module Warrant
299
298
  # @raise [Warrant::MissingRequiredParameterError]
300
299
  # @raise [Warrant::NotFoundError]
301
300
  # @raise [Warrant::UnauthorizedError]
302
- def remove_permission(permission_id)
303
- Permission.remove_from_user(user_id, permission_id)
301
+ def remove_permission(permission_id, relation: "member", options: {})
302
+ Permission.remove_from_user(user_id, permission_id, relation: relation, options: options)
304
303
  end
305
304
 
306
305
  # Checks whether a user has a given permission
307
306
  #
308
307
  # @param permission_id [String] The permission_id of the permission you want to check whether or not it exists on the user.
308
+ # @param relation [String] The relation for this permission to user association. The relation must be valid as per the +permission+ object type definition.
309
309
  # @option options [Hash] :context Object containing key-value pairs that specifies the context the warrant should be checked in. (optional)
310
310
  # @option options [Boolean] :debug Boolean flag indicating whether or not to return debug information for this access check. Defaults to false. (optional)
311
311
  #
@@ -319,48 +319,45 @@ module Warrant
319
319
  # @raise [Warrant::InvalidParameterError]
320
320
  # @raise [Warrant::NotFoundError]
321
321
  # @raise [Warrant::UnauthorizedError]
322
- def has_permission?(permission_id, opts = {})
323
- return Warrant.user_has_permission?(
322
+ def has_permission?(permission_id, relation: "member", options: {})
323
+ return Warrant.user_has_permission?({
324
324
  permission_id: permission_id,
325
+ relation: relation,
325
326
  user_id: user_id,
326
- context: opts[:context],
327
- debug: opts[:debug]
328
- )
327
+ context: options[:context],
328
+ debug: options[:debug]
329
+ }, options)
329
330
  end
330
331
 
331
332
  # List all users for a tenant
332
333
  #
333
334
  # @param tenant_id [String] The tenant_id of the tenant from which to fetch users
334
- # @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)
335
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
336
- # @option filters [String] :beforeId A string representing a cursor value in the form of a userId. If provided, the results returned are immediately before the provided value. (optional)
337
- # @option filters [String] :beforeValue A string representing a cursor value in the form of the `sortBy` value. If provided, the results returned are immediately before the provided value. (optional)
338
- # @option filters [String] :afterId A string representing a cursor value in the form of a userId. If provided, the results returned are immediately after the provided value. (optional)
339
- # @option filters [String] :afterValue A string representing a cursor value in the form of the `sortBy` value. If provided, the results returned are immediately after the provided value. (optional)
340
- # @option filters [String] :sortBy A string representing the field to sort results by. Default value is userId. (optional)
341
- # @option filters [String] :sortOrder A string representing whether to sort results in ascending or descending order. Must be ASC or DESC. (optional)
335
+ # @param [Hash] filters Filters to apply to result set
336
+ # @param [Hash] options Options to apply on a per-request basis
337
+ # @option filters [String] :object_type Only return objects with an `objectType` matching this value
338
+ # @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
339
+ # @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
340
+ # @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
341
+ # @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
342
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
343
+ # @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
342
344
  #
343
345
  # @return [Array<User>] all users for the tenant
344
346
  #
345
347
  # @raise [Warrant::InternalError]
346
348
  # @raise [Warrant::InvalidParameterError]
347
349
  # @raise [Warrant::UnauthorizedError]
348
- def self.list_for_tenant(tenant_id, filters = {})
349
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/tenants/#{tenant_id}/users"), Util.normalize_params(filters))
350
-
351
- case res
352
- when Net::HTTPSuccess
353
- users = JSON.parse(res.body)
354
- users.map{ |user| User.new(user['userId'], user['email'], user['createdAt']) }
355
- else
356
- APIOperations.raise_error(res)
357
- end
350
+ def self.list_for_tenant(tenant_id, filters = {}, options = {})
351
+ query_response = Warrant.query("select * of type user for tenant:#{tenant_id}", filters: filters, options: options)
352
+ users = query_response.results.map{ |result| User.new(result.object_id, result.meta) }
353
+ return ListResponse.new(users, query_response.prev_cursor, query_response.next_cursor)
358
354
  end
359
355
 
360
356
  # Add a user to a tenant
361
357
  #
362
358
  # @param tenant_id [String] The tenant_id of the tenant you want to assign a user to.
363
359
  # @param user_id [String] The user_id of the user you want to add to the tenant.
360
+ # @param relation [String] The relation for this tenant to user association. The relation must be valid as per the +tenant+ object type definition.
364
361
  #
365
362
  # @return [Warrant] warrant assigning user to the tenant
366
363
  #
@@ -369,14 +366,15 @@ module Warrant
369
366
  # @raise [Warrant::InvalidParameterError]
370
367
  # @raise [Warrant::NotFoundError]
371
368
  # @raise [Warrant::UnauthorizedError]
372
- def self.assign_to_tenant(tenant_id, user_id)
373
- Warrant.create({ object_type: Tenant::OBJECT_TYPE, object_id: tenant_id }, "member", { object_type: User::OBJECT_TYPE, object_id: user_id })
369
+ def self.assign_to_tenant(tenant_id, user_id, relation: "member", options: {})
370
+ Warrant.create({ object_type: Tenant::OBJECT_TYPE, object_id: tenant_id }, relation, { object_type: User::OBJECT_TYPE, object_id: user_id }, nil, options)
374
371
  end
375
372
 
376
373
  # Remove a user from a tenant
377
374
  #
378
375
  # @param tenant_id [String] The tenant_id of the tenant you want to remove the user from.
379
376
  # @param user_id [String] The user_id of the user you want to remove from the tenant.
377
+ # @param relation [String] The relation for this tenant to user association. The relation must be valid as per the +tenant+ object type definition.
380
378
  #
381
379
  # @return [nil] if remove was successful
382
380
  #
@@ -384,14 +382,21 @@ module Warrant
384
382
  # @raise [Warrant::NotFoundError]
385
383
  # @raise [Warrant::UnauthorizedError]
386
384
  # @raise [Warrant::WarrantError]
387
- def self.remove_from_tenant(tenant_id, user_id)
388
- Warrant.delete({ object_type: Tenant::OBJECT_TYPE, object_id: tenant_id }, "member", { object_type: User::OBJECT_TYPE, object_id: user_id })
385
+ def self.remove_from_tenant(tenant_id, user_id, relation: "member", options: {})
386
+ Warrant.delete({ object_type: Tenant::OBJECT_TYPE, object_id: tenant_id }, relation, { object_type: User::OBJECT_TYPE, object_id: user_id }, nil, options)
389
387
  end
390
388
 
391
389
  # List all tenants for a user
392
390
  #
393
- # @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)
394
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
391
+ # @param [Hash] filters Filters to apply to result set
392
+ # @param [Hash] options Options to apply on a per-request basis
393
+ # @option filters [String] :object_type Only return objects with an `objectType` matching this value
394
+ # @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
395
+ # @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
396
+ # @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
397
+ # @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
398
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
399
+ # @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
395
400
  #
396
401
  # @return [Array<Tenant>] all tenants for the user
397
402
  #
@@ -399,27 +404,35 @@ module Warrant
399
404
  # @raise [Warrant::InvalidRequestError]
400
405
  # @raise [Warrant::UnauthorizedError]
401
406
  # @raise [Warrant::WarrantError]
402
- def list_tenants(filters = {})
403
- return Tenant.list_for_user(user_id, filters)
407
+ def list_tenants(filters = {}, options = {})
408
+ return Tenant.list_for_user(user_id, filters, options)
404
409
  end
405
410
 
406
411
  # List pricing tiers for a user
407
412
  #
408
- # @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)
409
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
413
+ # @param [Hash] filters Filters to apply to result set
414
+ # @param [Hash] options Options to apply on a per-request basis
415
+ # @option filters [String] :object_type Only return objects with an `objectType` matching this value
416
+ # @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
417
+ # @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
418
+ # @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
419
+ # @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
420
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
421
+ # @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
410
422
  #
411
423
  # @return [Array<PricingTier>] assigned pricing tiers for the user
412
424
  #
413
425
  # @raise [Warrant::InternalError]
414
426
  # @raise [Warrant::InvalidParameterError]
415
427
  # @raise [Warrant::UnauthorizedError]
416
- def list_pricing_tiers(filters = {})
417
- return PricingTier.list_for_user(user_id, filters)
428
+ def list_pricing_tiers(filters = {}, options = {})
429
+ return PricingTier.list_for_user(user_id, filters, options)
418
430
  end
419
431
 
420
432
  # Assign a pricing tier to a user
421
433
  #
422
434
  # @param pricing_tier_id [String] The pricing_tier_id of the pricing tier you want to assign to the user.
435
+ # @param relation [String] The relation for this pricing tier to user association. The relation must be valid as per the +pricing tier+ object type definition.
423
436
  #
424
437
  # @return [PricingTier] assigned pricing tier
425
438
  #
@@ -428,13 +441,14 @@ module Warrant
428
441
  # @raise [Warrant::InvalidParameterError]
429
442
  # @raise [Warrant::NotFoundError]
430
443
  # @raise [Warrant::UnauthorizedError]
431
- def assign_pricing_tier(pricing_tier_id)
432
- return PricingTier.assign_to_user(user_id, pricing_tier_id)
444
+ def assign_pricing_tier(pricing_tier_id, relation: "member", options: {})
445
+ return PricingTier.assign_to_user(user_id, pricing_tier_id, relation: relation, options: options)
433
446
  end
434
447
 
435
448
  # Remove a pricing tier from a user
436
449
  #
437
450
  # @param pricing_tier_id [String] The pricing_tier_id of the pricing tier you want to assign from the user.
451
+ # @param relation [String] The relation for this pricing tier to user association. The relation must be valid as per the +pricing tier+ object type definition.
438
452
  #
439
453
  # @return [nil] if remove was successful
440
454
  #
@@ -443,27 +457,35 @@ module Warrant
443
457
  # @raise [Warrant::NotFoundError]
444
458
  # @raise [Warrant::UnauthorizedError]
445
459
  # @raise [Warrant::WarrantError]
446
- def remove_pricing_tier(pricing_tier_id)
447
- return PricingTier.remove_from_user(user_id, pricing_tier_id)
460
+ def remove_pricing_tier(pricing_tier_id, relation: "member", options: {})
461
+ return PricingTier.remove_from_user(user_id, pricing_tier_id, relation: relation, options: options)
448
462
  end
449
463
 
450
464
  # List features for a user
451
465
  #
452
- # @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)
453
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
466
+ # @param [Hash] filters Filters to apply to result set
467
+ # @param [Hash] options Options to apply on a per-request basis
468
+ # @option filters [String] :object_type Only return objects with an `objectType` matching this value
469
+ # @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
470
+ # @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
471
+ # @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
472
+ # @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
473
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
474
+ # @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
454
475
  #
455
476
  # @return [Array<Feature>] assigned features for the user
456
477
  #
457
478
  # @raise [Warrant::InternalError]
458
479
  # @raise [Warrant::InvalidParameterError]
459
480
  # @raise [Warrant::UnauthorizedError]
460
- def list_features(filters = {})
461
- return Feature.list_for_user(user_id, filters)
481
+ def list_features(filters = {}, options = {})
482
+ return Feature.list_for_user(user_id, filters, options)
462
483
  end
463
484
 
464
485
  # Assign a feature to a user
465
486
  #
466
487
  # @param feature_id [String] The feature_id of the feature you want to assign to the user.
488
+ # @param relation [String] The relation for this feature to user association. The relation must be valid as per the +feature+ object type definition.
467
489
  #
468
490
  # @return [Feature] assigned feature
469
491
  #
@@ -472,13 +494,14 @@ module Warrant
472
494
  # @raise [Warrant::InvalidParameterError]
473
495
  # @raise [Warrant::NotFoundError]
474
496
  # @raise [Warrant::UnauthorizedError]
475
- def assign_feature(feature_id)
476
- return Feature.assign_to_user(user_id, feature_id)
497
+ def assign_feature(feature_id, relation: "member", options: {})
498
+ return Feature.assign_to_user(user_id, feature_id, relation: relation, options: options)
477
499
  end
478
500
 
479
501
  # Remove a feature from a user
480
502
  #
481
503
  # @param feature_id [String] The feature_id of the feature you want to assign from the user.
504
+ # @param relation [String] The relation for this feature to user association. The relation must be valid as per the +feature+ object type definition.
482
505
  #
483
506
  # @return [nil] if remove was successful
484
507
  #
@@ -487,32 +510,34 @@ module Warrant
487
510
  # @raise [Warrant::NotFoundError]
488
511
  # @raise [Warrant::UnauthorizedError]
489
512
  # @raise [Warrant::WarrantError]
490
- def remove_feature(feature_id)
491
- return Feature.remove_from_user(user_id, feature_id)
513
+ def remove_feature(feature_id, relation: "member", options: {})
514
+ return Feature.remove_from_user(user_id, feature_id, relation: relation, options: options)
492
515
  end
493
516
 
494
517
  # Check whether a user has a given feature
495
518
  #
496
519
  # @param feature_id [String] The feature_id of the feature to check whether the user has access to.
520
+ # @param relation [String] The relation for this feature to user association. The relation must be valid as per the +feature+ object type definition.
497
521
  # @option options [Hash] :context Object containing key-value pairs that specifies the context the warrant should be checked in. (optional)
498
522
  # @option options [Boolean] :debug Boolean flag indicating whether or not to return debug information for this access check. Defaults to false. (optional)
499
523
  #
500
- # @ return [Boolean] whether or not the user has the given feature
524
+ # @return [Boolean] whether or not the user has the given feature
501
525
  #
502
526
  # @raise [Warrant::InternalError]
503
527
  # @raise [Warrant::InvalidParameterError]
504
528
  # @raise [Warrant::NotFoundError]
505
529
  # @raise [Warrant::UnauthorizedError]
506
- def has_feature?(feature_id, opts = {})
507
- return Warrant.has_feature?(
530
+ def has_feature?(feature_id, relation: "member", options: {})
531
+ return Warrant.has_feature?({
508
532
  feature_id: feature_id,
533
+ relation: relation,
509
534
  subject: {
510
535
  object_type: "user",
511
536
  object_id: user_id
512
537
  },
513
- context: opts[:context],
514
- debug: opts[:debug]
515
- )
538
+ context: options[:context],
539
+ debug: options[:debug]
540
+ }, options)
516
541
  end
517
542
 
518
543
  def warrant_object_type