-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for invite codes in the registration API (#27805)
- Loading branch information
1 parent
5bca5c4
commit 07a4059
Showing
9 changed files
with
156 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V1::InvitesController < Api::BaseController | ||
include RegistrationHelper | ||
|
||
skip_before_action :require_authenticated_user! | ||
skip_around_action :set_locale | ||
|
||
before_action :set_invite | ||
before_action :check_enabled_registrations! | ||
|
||
# Override `current_user` to avoid reading session cookies | ||
def current_user; end | ||
|
||
def show | ||
render json: { invite_code: params[:invite_code], instance_api_url: api_v2_instance_url }, status: 200 | ||
end | ||
|
||
private | ||
|
||
def set_invite | ||
@invite = Invite.find_by!(code: params[:invite_code]) | ||
end | ||
|
||
def check_enabled_registrations! | ||
return render json: { error: I18n.t('invites.invalid') }, status: 401 unless @invite.valid_for_use? | ||
|
||
raise Mastodon::NotPermittedError unless allowed_registration?(request.remote_ip, @invite) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module RegistrationHelper | ||
extend ActiveSupport::Concern | ||
|
||
def allowed_registration?(remote_ip, invite) | ||
!Rails.configuration.x.single_user_mode && !omniauth_only? && (registrations_open? || invite&.valid_for_use?) && !ip_blocked?(remote_ip) | ||
end | ||
|
||
def registrations_open? | ||
Setting.registrations_mode != 'none' | ||
end | ||
|
||
def omniauth_only? | ||
ENV['OMNIAUTH_ONLY'] == 'true' | ||
end | ||
|
||
def ip_blocked?(remote_ip) | ||
IpBlock.where(severity: :sign_up_block).exists?(['ip >>= ?', remote_ip.to_s]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe 'invites' do | ||
let(:invite) { Fabricate(:invite) } | ||
|
||
context 'when requesting a JSON document' do | ||
it 'returns a JSON document with expected attributes' do | ||
get "/invite/#{invite.code}", headers: { 'Accept' => 'application/activity+json' } | ||
|
||
expect(response).to have_http_status(200) | ||
expect(response.media_type).to eq 'application/json' | ||
|
||
expect(body_as_json[:invite_code]).to eq invite.code | ||
end | ||
end | ||
|
||
context 'when not requesting a JSON document' do | ||
it 'returns an HTML page' do | ||
get "/invite/#{invite.code}" | ||
|
||
expect(response).to have_http_status(200) | ||
expect(response.media_type).to eq 'text/html' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters