def invite_users_backend( request: HttpRequest, user_profile: UserProfile, invitee_emails_raw: str = REQ("invitee_emails"), invite_expires_in_minutes: Optional[int] = REQ( json_validator=check_none_or(check_int), default=settings.INVITATION_LINK_VALIDITY_MINUTES ), invite_as: int = REQ(json_validator=check_int, default=PreregistrationUser.INVITE_AS["MEMBER"]), stream_ids: List[int] = REQ(json_validator=check_list(check_int)), ) -> HttpResponse: if not user_profile.can_invite_others_to_realm(): # Guest users case will not be handled here as it will # be handled by the decorator above. raise JsonableError(_("Insufficient permission")) if invite_as not in PreregistrationUser.INVITE_AS.values(): raise JsonableError(_("Must be invited as an valid type of user")) check_if_owner_required(invite_as, user_profile) if ( invite_as in [ PreregistrationUser.INVITE_AS["REALM_ADMIN"], PreregistrationUser.INVITE_AS["MODERATOR"], ] and not user_profile.is_realm_admin ): raise JsonableError(_("Must be an organization administrator")) if not invitee_emails_raw: raise JsonableError(_("You must specify at least one email address.")) if not stream_ids: raise JsonableError(_("You must specify at least one stream for invitees to join.")) invitee_emails = get_invitee_emails_set(invitee_emails_raw) streams: List[Stream] = [] for stream_id in stream_ids: try: (stream, sub) = access_stream_by_id(user_profile, stream_id) except JsonableError: raise JsonableError( _("Stream does not exist with id: {}. No invites were sent.").format(stream_id) ) streams.append(stream) do_invite_users( user_profile, invitee_emails, streams, invite_expires_in_minutes=invite_expires_in_minutes, invite_as=invite_as, ) return json_success(request)
def invite_users_backend( request: HttpRequest, user_profile: UserProfile, invitee_emails_raw: str = REQ("invitee_emails"), invite_as: int = REQ(validator=check_int, default=PreregistrationUser.INVITE_AS["MEMBER"]), stream_ids: List[int] = REQ(validator=check_list(check_int)), ) -> HttpResponse: if not user_profile.can_invite_others_to_realm(): if user_profile.realm.invite_to_realm_policy == Realm.POLICY_ADMINS_ONLY: return json_error( _("Only administrators can invite others to this organization." )) if user_profile.realm.invite_to_realm_policy == Realm.POLICY_MODERATORS_ONLY: return json_error( _("Only administrators and moderators can invite others to this organization." )) if user_profile.realm.invite_to_realm_policy == Realm.POLICY_FULL_MEMBERS_ONLY: return json_error( _("Your account is too new to invite others to this organization." )) # Guest case will be handled by require_member_or_admin decorator. raise AssertionError("Unexpected policy validation failure") if invite_as not in PreregistrationUser.INVITE_AS.values(): return json_error(_("Must be invited as an valid type of user")) check_if_owner_required(invite_as, user_profile) if (invite_as == PreregistrationUser.INVITE_AS["REALM_ADMIN"] and not user_profile.is_realm_admin): return json_error(_("Must be an organization administrator")) if not invitee_emails_raw: return json_error(_("You must specify at least one email address.")) if not stream_ids: return json_error( _("You must specify at least one stream for invitees to join.")) invitee_emails = get_invitee_emails_set(invitee_emails_raw) streams: List[Stream] = [] for stream_id in stream_ids: try: (stream, sub) = access_stream_by_id(user_profile, stream_id) except JsonableError: return json_error( _("Stream does not exist with id: {}. No invites were sent."). format(stream_id)) streams.append(stream) do_invite_users(user_profile, invitee_emails, streams, invite_as) return json_success()