warrant 3.1.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,23 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Warrant
4
- class Role
4
+ class Role < Warrant::Object
5
5
  OBJECT_TYPE = "role"
6
6
 
7
7
  include Warrant::WarrantObject
8
8
 
9
- attr_reader :role_id, :name, :description
9
+ alias :role_id :object_id
10
10
 
11
11
  # @!visibility private
12
- def initialize(role_id, name = nil, description = nil)
13
- @role_id = role_id
14
- @name = name
15
- @description = description
12
+ def initialize(role_id, meta = {}, created_at = nil)
13
+ super(OBJECT_TYPE, role_id, meta, created_at)
16
14
  end
17
15
 
18
16
  # Creates a role with the given parameters
19
17
  #
20
- # @option params [String] :role_id A string identifier for this new role. The role_id can only be composed of lower-case alphanumeric chars and/or '-' and '_'.
18
+ # @option params [String] :role_id User defined string identifier for this role. If not provided, Warrant will create an id for the role 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 role. Note that roleIds in Warrant must be composed of alphanumeric chars, '-', and/or '_'. (optional)
19
+ # @option params [Hash] :meta A JSON object containing additional information about this role (e.g. name/description) to be persisted to Warrant. (optional)
21
20
  #
22
21
  # @return [Role] created role
23
22
  #
@@ -28,16 +27,9 @@ module Warrant
28
27
  # @raise [Warrant::InternalError]
29
28
  # @raise [Warrant::InvalidRequestError]
30
29
  # @raise [Warrant::UnauthorizedError]
31
- def self.create(params = {})
32
- res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/roles"), Util.normalize_params(params))
33
-
34
- case res
35
- when Net::HTTPSuccess
36
- res_json = JSON.parse(res.body)
37
- Role.new(res_json['roleId'], res_json['name'], res_json['description'])
38
- else
39
- APIOperations.raise_error(res)
40
- end
30
+ def self.create(params = {}, options = {})
31
+ object = Object.create({ object_type: OBJECT_TYPE, object_id: params[:role_id], meta: params[:meta] }, options)
32
+ return Role.new(object.object_id, object.meta, object.created_at)
41
33
  end
42
34
 
43
35
  # Deletes a role with given role id
@@ -53,21 +45,20 @@ module Warrant
53
45
  # @raise [Warrant::MissingRequiredParameterError]
54
46
  # @raise [Warrant::NotFoundError]
55
47
  # @raise [Warrant::UnauthorizedError]
56
- def self.delete(role_id)
57
- res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v1/roles/#{role_id}"))
58
-
59
- case res
60
- when Net::HTTPSuccess
61
- return
62
- else
63
- APIOperations.raise_error(res)
64
- end
48
+ def self.delete(role_id, options = {})
49
+ return Object.delete(OBJECT_TYPE, role_id, options)
65
50
  end
66
51
 
67
52
  # Lists all roles for your organization
68
53
  #
69
- # @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)
70
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
54
+ # @param [Hash] filters Filters to apply to result set
55
+ # @param [Hash] options Options to apply on a per-request basis
56
+ # @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)
57
+ # @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)
58
+ # @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)
59
+ # @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)
60
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
61
+ # @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)
71
62
  #
72
63
  # @return [Array<Role>] all roles for your organization
73
64
  #
@@ -77,16 +68,11 @@ module Warrant
77
68
  # @raise [Warrant::InternalError]
78
69
  # @raise [Warrant::InvalidParameterError]
79
70
  # @raise [Warrant::UnauthorizedError]
80
- def self.list(filters = {})
81
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/roles"), Util.normalize_params(filters))
82
-
83
- case res
84
- when Net::HTTPSuccess
85
- roles = JSON.parse(res.body)
86
- roles.map{ |role| Role.new(role['roleId'], role['name'], role['description']) }
87
- else
88
- APIOperations.raise_error(res)
89
- end
71
+ def self.list(filters = {}, options = {})
72
+ filters.merge({ object_type: "role" })
73
+ list_response = Object.list(filters, options)
74
+ roles = list_response.results.map{ |object| Role.new(object.object_id, object.meta, object.created_at)}
75
+ return ListResponse.new(roles, list_response.prev_cursor, list_response.next_cursor)
90
76
  end
91
77
 
92
78
  # Get a role with the given role_id
@@ -99,24 +85,15 @@ module Warrant
99
85
  # @raise [Warrant::MissingRequiredParameterError]
100
86
  # @raise [Warrant::NotFoundError]
101
87
  # @raise [Warrant::UnauthorizedError]
102
- def self.get(role_id)
103
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/roles/#{role_id}"))
104
-
105
- case res
106
- when Net::HTTPSuccess
107
- role = JSON.parse(res.body)
108
- Role.new(role['roleId'], role['name'], role['description'])
109
- else
110
- APIOperations.raise_error(res)
111
- end
88
+ def self.get(role_id, options = {})
89
+ object = Object.get(OBJECT_TYPE, role_id, options)
90
+ return Role.new(object.object_id, object.meta, object.created_at)
112
91
  end
113
92
 
114
93
  # Updates a role with the given role_id and params
115
94
  #
116
95
  # @param role_id [String] The role_id of the role to be updated.
117
- # @param [Hash] params attributes to update user with
118
- # @option params [String] :name Name for the role. Designed to be used as a UI-friendly identifier. (optional)
119
- # @option params [String] :description Description of the role. Designed to be used as a UI-friendly identifier. (optional)
96
+ # @param meta [Hash] A JSON object containing additional information about this role (e.g. name/description, etc.) to be persisted to Warrant.
120
97
  #
121
98
  # @return [Role] updated role
122
99
  #
@@ -128,65 +105,59 @@ module Warrant
128
105
  # @raise [Warrant::InvalidRequestError]
129
106
  # @raise [Warrant::NotFoundError]
130
107
  # @raise [Warrant::UnauthorizedError]
131
- def self.update(role_id, params = {})
132
- res = APIOperations.put(URI.parse("#{::Warrant.config.api_base}/v1/roles/#{role_id}"), Util.normalize_params(params))
133
-
134
- case res
135
- when Net::HTTPSuccess
136
- res_json = JSON.parse(res.body)
137
- Role.new(res_json['roleId'], res_json['name'], res_json['description'])
138
- else
139
- APIOperations.raise_error(res)
140
- end
108
+ def self.update(role_id, meta, options = {})
109
+ object = Object.update(OBJECT_TYPE, role_id, meta, options)
110
+ return Role.new(object.object_id, object.meta, object.created_at)
141
111
  end
142
112
 
143
113
  # Updates a role with the given params
144
114
  #
145
- # @param [Hash] params attributes to update user with
146
- # @option params [String] :name Name for the role. Designed to be used as a UI-friendly identifier. (optional)
147
- # @option params [String] :description Description of the role. Designed to be used as a UI-friendly identifier. (optional)
115
+ # @param meta [Hash] A JSON object containing additional information about this role (e.g. name/description, etc.) to be persisted to Warrant.
148
116
  #
149
117
  # @return [Role] updated role
150
118
  #
151
119
  # @example Update role "test-role"'s name
152
- # Warrant::Role.update("test-role", { name: "Test Role" })
120
+ # role = Warrant::Role.get("test-role")
121
+ # role.update({ name: "Test Role" })
153
122
  #
154
123
  # @raise [Warrant::InternalError]
155
124
  # @raise [Warrant::InvalidParameterError]
156
125
  # @raise [Warrant::InvalidRequestError]
157
126
  # @raise [Warrant::NotFoundError]
158
127
  # @raise [Warrant::UnauthorizedError]
159
- def update(params = {})
160
- return Role.update(role_id, params)
128
+ def update(meta, options = {})
129
+ return Role.update(role_id, meta)
161
130
  end
162
131
 
163
132
  # List roles for user
164
133
  #
165
134
  # @param user_id [String] The user_id of the user you want to retrieve roles for.
166
- # @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)
167
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
135
+ # @param [Hash] filters Filters to apply to result set
136
+ # @param [Hash] options Options to apply on a per-request basis
137
+ # @option filters [String] :object_type Only return objects with an `objectType` matching this value
138
+ # @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)
139
+ # @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)
140
+ # @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)
141
+ # @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)
142
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
143
+ # @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)
168
144
  #
169
145
  # @return [Array<Role>] all assigned roles for the user
170
146
  #
171
147
  # @raise [Warrant::InternalError]
172
148
  # @raise [Warrant::MissingRequiredParameterError]
173
149
  # @raise [Warrant::UnauthorizedError]
174
- def self.list_for_user(user_id, filters = {})
175
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/roles"), Util.normalize_params(filters))
176
-
177
- case res
178
- when Net::HTTPSuccess
179
- roles = JSON.parse(res.body)
180
- roles.map{ |role| Role.new(role['roleId'], role['name'], role['description']) }
181
- else
182
- APIOperations.raise_error(res)
183
- end
150
+ def self.list_for_user(user_id, filters = {}, options = {})
151
+ query_response = Warrant.query("select role where user:#{user_id} is *", filters: filters, options: options)
152
+ roles = query_response.results.map{ |result| Role.new(result.object_id, result.meta) }
153
+ return ListResponse.new(roles, query_response.prev_cursor, query_response.next_cursor)
184
154
  end
185
155
 
186
156
  # Assign a role to a user
187
157
  #
188
158
  # @param user_id [String] The user_id of the user you want to assign a role to.
189
159
  # @param role_id [String] The role_id of the role you want to assign to a user.
160
+ # @param relation [String] The relation for this role to user association. The relation must be valid as per the +role+ object type definition.
190
161
  #
191
162
  # @return [Warrant] warrant assigning role to user
192
163
  #
@@ -196,14 +167,15 @@ module Warrant
196
167
  # @raise [Warrant::MissingRequiredParameterError]
197
168
  # @raise [Warrant::NotFoundError]
198
169
  # @raise [Warrant::UnauthorizedError]
199
- def self.assign_to_user(user_id, role_id)
200
- Warrant.create({ object_type: Role::OBJECT_TYPE, object_id: role_id }, "member", { object_type: User::OBJECT_TYPE, object_id: user_id })
170
+ def self.assign_to_user(user_id, role_id, relation: "member", options: {})
171
+ Warrant.create({ object_type: Role::OBJECT_TYPE, object_id: role_id }, relation, { object_type: User::OBJECT_TYPE, object_id: user_id }, nil, options)
201
172
  end
202
173
 
203
174
  # Remove a role from a user
204
175
  #
205
176
  # @param user_id [String] The user_id of the role you want to remove a role from.
206
177
  # @param role_id [String] The role_id of the role you want to remove from a user.
178
+ # @param relation [String] The relation for this role to user association. The relation must be valid as per the +role+ object type definition.
207
179
  #
208
180
  # @return [nil] if remove was successful
209
181
  #
@@ -213,27 +185,35 @@ module Warrant
213
185
  # @raise [Warrant::NotFoundError]
214
186
  # @raise [Warrant::UnauthorizedError]
215
187
  # @raise [Warrant::WarrantError]
216
- def self.remove_from_user(user_id, role_id)
217
- Warrant.delete({ object_type: Role::OBJECT_TYPE, object_id: role_id }, "member", { object_type: User::OBJECT_TYPE, object_id: user_id })
188
+ def self.remove_from_user(user_id, role_id, relation: "member", options: {})
189
+ Warrant.delete({ object_type: Role::OBJECT_TYPE, object_id: role_id }, relation, { object_type: User::OBJECT_TYPE, object_id: user_id }, nil, options)
218
190
  end
219
191
 
220
192
  # List assigned permissions for the role
221
193
  #
222
- # @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)
223
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
194
+ # @param [Hash] filters Filters to apply to result set
195
+ # @param [Hash] options Options to apply on a per-request basis
196
+ # @option filters [String] :object_type Only return objects with an `objectType` matching this value
197
+ # @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)
198
+ # @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)
199
+ # @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)
200
+ # @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)
201
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
202
+ # @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)
224
203
  #
225
204
  # @return [Permission] assigned permissions
226
205
  #
227
206
  # @raise [Warrant::InternalError]
228
207
  # @raise [Warrant::MissingRequiredParameterError]
229
208
  # @raise [Warrant::UnauthorizedError]
230
- def list_permissions(filters = {})
231
- return Permission.list_for_role(role_id, filters)
209
+ def list_permissions(filters = {}, options = {})
210
+ return Permission.list_for_role(role_id, filters, options)
232
211
  end
233
212
 
234
213
  # Assign a permission to a role
235
214
  #
236
215
  # @param permission_id [String] The permission_id of the permission you want to assign to the role.
216
+ # @param relation [String] The relation for this permission to user association. The relation must be valid as per the +permission+ object type definition.
237
217
  #
238
218
  # @return [Permission] assigned permission
239
219
  #
@@ -243,13 +223,14 @@ module Warrant
243
223
  # @raise [Warrant::MissingRequiredParameterError]
244
224
  # @raise [Warrant::NotFoundError]
245
225
  # @raise [Warrant::UnauthorizedError]
246
- def assign_permission(permission_id)
247
- return Permission.assign_to_role(role_id, permission_id)
226
+ def assign_permission(permission_id, relation: "member", options: {})
227
+ return Permission.assign_to_role(role_id, permission_id, relation: relation, options: options)
248
228
  end
249
229
 
250
230
  # Remove a permission from a role
251
231
  #
252
232
  # @param permission_id [String] The permission_id of the permission you want to remove from the role.
233
+ # @param relation [String] The relation for this permission to user association. The relation must be valid as per the +permission+ object type definition.
253
234
  #
254
235
  # @return [nil] if remove was successful
255
236
  #
@@ -258,8 +239,8 @@ module Warrant
258
239
  # @raise [Warrant::NotFoundError]
259
240
  # @raise [Warrant::UnauthorizedError]
260
241
  # @raise [Warrant::WarrantError]
261
- def remove_permission(permission_id)
262
- return Permission.remove_from_role(role_id, permission_id)
242
+ def remove_permission(permission_id, relation: "member", options: {})
243
+ return Permission.remove_from_role(role_id, permission_id, relation: relation, options: options)
263
244
  end
264
245
 
265
246
  def warrant_object_type
@@ -15,9 +15,9 @@ module Warrant
15
15
  # @raise [Warrant::MissingRequiredParameterError]
16
16
  # @raise [Warrant::NotFoundError]
17
17
  # @raise [Warrant::UnauthorizedError]
18
- def self.create_authorization_session(params = {})
18
+ def self.create_authorization_session(params = {}, options = {})
19
19
  params = params.merge(type: "sess")
20
- res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/sessions"), Util.normalize_params(params))
20
+ res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/sessions"), params: Util.normalize_params(params), options: options)
21
21
 
22
22
  case res
23
23
  when Net::HTTPSuccess
@@ -45,9 +45,9 @@ module Warrant
45
45
  # @raise [Warrant::MissingRequiredParameterError]
46
46
  # @raise [Warrant::NotFoundError]
47
47
  # @raise [Warrant::UnauthorizedError]
48
- def self.create_self_service_session(redirect_url, params = {})
48
+ def self.create_self_service_session(redirect_url, params = {}, options = {})
49
49
  params = params.merge(type: "ssdash")
50
- res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/sessions"), Util.normalize_params(params))
50
+ res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/sessions"), params: Util.normalize_params(params), options: options)
51
51
 
52
52
  case res
53
53
  when Net::HTTPSuccess