|
11 | 11 | )
|
12 | 12 |
|
13 | 13 |
|
| 14 | +class UserObj: |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + login_id: str, |
| 18 | + email: Optional[str] = None, |
| 19 | + phone: Optional[str] = None, |
| 20 | + display_name: Optional[str] = None, |
| 21 | + given_name: Optional[str] = None, |
| 22 | + middle_name: Optional[str] = None, |
| 23 | + family_name: Optional[str] = None, |
| 24 | + role_names: Optional[List[str]] = None, |
| 25 | + user_tenants: Optional[List[AssociatedTenant]] = None, |
| 26 | + picture: Optional[str] = None, |
| 27 | + custom_attributes: Optional[dict] = None, |
| 28 | + verified_email: Optional[bool] = None, |
| 29 | + verified_phone: Optional[bool] = None, |
| 30 | + additional_login_ids: Optional[List[str]] = None, |
| 31 | + ): |
| 32 | + self.login_id = login_id |
| 33 | + self.email = email |
| 34 | + self.phone = phone |
| 35 | + self.display_name = display_name |
| 36 | + self.given_name = given_name |
| 37 | + self.middle_name = middle_name |
| 38 | + self.family_name = family_name |
| 39 | + self.role_names = role_names |
| 40 | + self.user_tenants = user_tenants |
| 41 | + self.picture = picture |
| 42 | + self.custom_attributes = custom_attributes |
| 43 | + self.verified_email = verified_email |
| 44 | + self.verified_phone = verified_phone |
| 45 | + self.additional_login_ids = additional_login_ids |
| 46 | + |
| 47 | + |
14 | 48 | class User(AuthBase):
|
15 | 49 | def create(
|
16 | 50 | self,
|
@@ -181,13 +215,13 @@ def invite(
|
181 | 215 | additional_login_ids: Optional[List[str]] = None,
|
182 | 216 | ) -> dict:
|
183 | 217 | """
|
184 |
| - Create a new user and invite them via an email message. |
| 218 | + Create a new user and invite them via an email / text message. |
185 | 219 |
|
186 | 220 | Functions exactly the same as the `create` function with the additional invitation
|
187 | 221 | behavior. See the documentation above for the general creation behavior.
|
188 | 222 |
|
189 |
| - IMPORTANT: Since the invitation is sent by email, make sure either |
190 |
| - the email is explicitly set, or the login_id itself is an email address. |
| 223 | + IMPORTANT: Since the invitation is sent by email / phone, make sure either |
| 224 | + the email / phone is explicitly set, or the login_id itself is an email address / phone number. |
191 | 225 | You must configure the invitation URL in the Descope console prior to
|
192 | 226 | calling the method.
|
193 | 227 | """
|
@@ -221,6 +255,41 @@ def invite(
|
221 | 255 | )
|
222 | 256 | return response.json()
|
223 | 257 |
|
| 258 | + def invite_batch( |
| 259 | + self, |
| 260 | + users: List[UserObj], |
| 261 | + invite_url: Optional[str] = None, |
| 262 | + send_mail: Optional[ |
| 263 | + bool |
| 264 | + ] = None, # send invite via mail, default is according to project settings |
| 265 | + send_sms: Optional[ |
| 266 | + bool |
| 267 | + ] = None, # send invite via text message, default is according to project settings |
| 268 | + ) -> dict: |
| 269 | + """ |
| 270 | + Create users in batch and invite them via an email / text message. |
| 271 | +
|
| 272 | + Functions exactly the same as the `create` function with the additional invitation |
| 273 | + behavior. See the documentation above for the general creation behavior. |
| 274 | +
|
| 275 | + IMPORTANT: Since the invitation is sent by email / phone, make sure either |
| 276 | + the email / phone is explicitly set, or the login_id itself is an email address / phone number. |
| 277 | + You must configure the invitation URL in the Descope console prior to |
| 278 | + calling the method. |
| 279 | + """ |
| 280 | + |
| 281 | + response = self._auth.do_post( |
| 282 | + MgmtV1.user_create_batch_path, |
| 283 | + User._compose_create_batch_body( |
| 284 | + users, |
| 285 | + invite_url, |
| 286 | + send_mail, |
| 287 | + send_sms, |
| 288 | + ), |
| 289 | + pswd=self._auth.management_key, |
| 290 | + ) |
| 291 | + return response.json() |
| 292 | + |
224 | 293 | def update(
|
225 | 294 | self,
|
226 | 295 | login_id: str,
|
@@ -1193,6 +1262,45 @@ def _compose_create_body(
|
1193 | 1262 | body["sendSMS"] = send_sms
|
1194 | 1263 | return body
|
1195 | 1264 |
|
| 1265 | + @staticmethod |
| 1266 | + def _compose_create_batch_body( |
| 1267 | + users: List[UserObj], |
| 1268 | + invite_url: Optional[str], |
| 1269 | + send_mail: Optional[bool], |
| 1270 | + send_sms: Optional[bool], |
| 1271 | + ) -> dict: |
| 1272 | + usersBody = [] |
| 1273 | + for user in users: |
| 1274 | + role_names = [] if user.role_names is None else user.role_names |
| 1275 | + user_tenants = [] if user.user_tenants is None else user.user_tenants |
| 1276 | + uBody = User._compose_update_body( |
| 1277 | + login_id=user.login_id, |
| 1278 | + email=user.email, |
| 1279 | + phone=user.phone, |
| 1280 | + display_name=user.display_name, |
| 1281 | + given_name=user.given_name, |
| 1282 | + middle_name=user.middle_name, |
| 1283 | + family_name=user.family_name, |
| 1284 | + role_names=role_names, |
| 1285 | + user_tenants=user_tenants, |
| 1286 | + picture=user.picture, |
| 1287 | + custom_attributes=user.custom_attributes, |
| 1288 | + additional_login_ids=user.additional_login_ids, |
| 1289 | + verified_email=user.verified_email, |
| 1290 | + verified_phone=user.verified_phone, |
| 1291 | + test=False, |
| 1292 | + ) |
| 1293 | + usersBody.append(uBody) |
| 1294 | + |
| 1295 | + body = {"users": usersBody, "invite": True} |
| 1296 | + if invite_url is not None: |
| 1297 | + body["inviteUrl"] = invite_url |
| 1298 | + if send_mail is not None: |
| 1299 | + body["sendMail"] = send_mail |
| 1300 | + if send_sms is not None: |
| 1301 | + body["sendSMS"] = send_sms |
| 1302 | + return body |
| 1303 | + |
1196 | 1304 | @staticmethod
|
1197 | 1305 | def _compose_update_body(
|
1198 | 1306 | login_id: str,
|
|
0 commit comments