Skip to content

Commit 143147b

Browse files
ClearlyClaireGargron
authored andcommitted
Check that an invite link is valid before bypassing approval mode (mastodon#10657)
* Check that an invite link is valid before bypassing approval mode Fixes mastodon#10656 * Add tests * Only consider valid invite links in registration controller * fixup
1 parent 12e26de commit 143147b

3 files changed

Lines changed: 86 additions & 2 deletions

File tree

app/controllers/auth/registrations_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def set_body_classes
9191
end
9292

9393
def set_invite
94-
@invite = invite_code.present? ? Invite.find_by(code: invite_code) : nil
94+
invite = invite_code.present? ? Invite.find_by(code: invite_code) : nil
95+
@invite = invite&.valid_for_use? ? invite : nil
9596
end
9697

9798
def determine_layout

app/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def confirmed?
114114
end
115115

116116
def invited?
117-
invite_id.present?
117+
invite_id.present? && invite.valid_for_use?
118118
end
119119

120120
def disable!

spec/controllers/auth/registrations_controller_spec.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,89 @@
107107
end
108108
end
109109

110+
context 'approval-based registrations without invite' do
111+
around do |example|
112+
registrations_mode = Setting.registrations_mode
113+
example.run
114+
Setting.registrations_mode = registrations_mode
115+
end
116+
117+
subject do
118+
Setting.registrations_mode = 'approved'
119+
request.headers["Accept-Language"] = accept_language
120+
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678' } }
121+
end
122+
123+
it 'redirects to login page' do
124+
subject
125+
expect(response).to redirect_to new_user_session_path
126+
end
127+
128+
it 'creates user' do
129+
subject
130+
user = User.find_by(email: 'test@example.com')
131+
expect(user).to_not be_nil
132+
expect(user.locale).to eq(accept_language)
133+
expect(user.approved).to eq(false)
134+
end
135+
end
136+
137+
context 'approval-based registrations with expired invite' do
138+
around do |example|
139+
registrations_mode = Setting.registrations_mode
140+
example.run
141+
Setting.registrations_mode = registrations_mode
142+
end
143+
144+
subject do
145+
Setting.registrations_mode = 'approved'
146+
request.headers["Accept-Language"] = accept_language
147+
invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.ago)
148+
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', 'invite_code': invite.code } }
149+
end
150+
151+
it 'redirects to login page' do
152+
subject
153+
expect(response).to redirect_to new_user_session_path
154+
end
155+
156+
it 'creates user' do
157+
subject
158+
user = User.find_by(email: 'test@example.com')
159+
expect(user).to_not be_nil
160+
expect(user.locale).to eq(accept_language)
161+
expect(user.approved).to eq(false)
162+
end
163+
end
164+
165+
context 'approval-based registrations with valid invite' do
166+
around do |example|
167+
registrations_mode = Setting.registrations_mode
168+
example.run
169+
Setting.registrations_mode = registrations_mode
170+
end
171+
172+
subject do
173+
Setting.registrations_mode = 'approved'
174+
request.headers["Accept-Language"] = accept_language
175+
invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.from_now)
176+
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', 'invite_code': invite.code } }
177+
end
178+
179+
it 'redirects to login page' do
180+
subject
181+
expect(response).to redirect_to new_user_session_path
182+
end
183+
184+
it 'creates user' do
185+
subject
186+
user = User.find_by(email: 'test@example.com')
187+
expect(user).to_not be_nil
188+
expect(user.locale).to eq(accept_language)
189+
expect(user.approved).to eq(true)
190+
end
191+
end
192+
110193
it 'does nothing if user already exists' do
111194
Fabricate(:user, account: Fabricate(:account, username: 'test'))
112195
subject

0 commit comments

Comments
 (0)