Skip to content

Commit 35e6238

Browse files
committed
Changes to Allowed Domains
- improved error displays on front end when a user with a not allowed domain signs up for both local and external - added corresponding tests
1 parent 5c3fb0a commit 35e6238

File tree

6 files changed

+19
-3
lines changed

6 files changed

+19
-3
lines changed

app/controllers/api/v1/users_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def create
6262
create_user_params[:language] = current_user&.language || I18n.default_locale if create_user_params[:language].blank?
6363

6464
# renders an error if the user is signing up with an invalid domain based off site settings
65-
return render_error errors: Rails.configuration.custom_error_msgs[:unauthorized], status: :forbidden unless valid_domain?
65+
return render_error errors: Rails.configuration.custom_error_msgs[:banned_user], status: :forbidden unless valid_domain?
6666

6767
user = UserCreator.new(user_params: create_user_params.except(:invite_token), provider: current_provider, role: default_role).call
6868

app/controllers/external_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ def create_user
4848
return redirect_to root_path(error: Rails.configuration.custom_error_msgs[:invite_token_invalid])
4949
end
5050

51-
return render_error status: :forbidden unless valid_domain?(user_info[:email])
51+
# Redirect to root if the user doesn't exist and has an invalid domain
52+
return redirect_to root_path(error: Rails.configuration.custom_error_msgs[:banned_user]) if new_user && !valid_domain?(user_info[:email])
5253

53-
# Create the user if they dont exist
54+
# Create the user if they don't exist
5455
if new_user
5556
user = UserCreator.new(user_params: user_info, provider: current_provider, role: default_role).call
5657
user.save!

app/javascript/components/admin/site_settings/registration/Registration.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export default function Registration() {
108108
<input
109109
className="form-control"
110110
placeholder={t('admin.site_settings.registration.enter_allowed_domains_rule')}
111+
defaultValue={siteSettings?.AllowedDomains}
111112
/>
112113
<Button
113114
variant="brand"

app/javascript/components/home/HomePage.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ export default function HomePage() {
5959
case 'SignupError':
6060
toast.error(t('toast.error.users.signup_error'));
6161
break;
62+
case 'BannedUser':
63+
toast.error(t('toast.error.users.banned'));
64+
break;
6265
default:
6366
}
6467
if (error) { setSearchParams(searchParams.delete('error')); }

app/javascript/hooks/mutations/users/useCreateUser.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export default function useCreateUser() {
5151
toast.error(t('toast.error.users.invalid_invite'));
5252
} else if (err.response.data.errors === 'EmailAlreadyExists') {
5353
toast.error(t('toast.error.users.email_exists'));
54+
} else if (err.response.data.errors === 'BannedUser') {
55+
toast.error(t('toast.error.users.banned'));
5456
} else {
5557
toast.error(t('toast.error.problem_completing_action'));
5658
}

spec/controllers/external_controller_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,15 @@
371371

372372
expect { get :create_user, params: { provider: 'openid_connect' } }.not_to change(User, :count)
373373
end
374+
375+
it 'does not affect existing users with different domains' do
376+
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:openid_connect]
377+
378+
create(:user, external_id: OmniAuth.config.mock_auth[:openid_connect][:uid])
379+
380+
get :create_user, params: { provider: 'openid_connect' }
381+
expect(response).not_to redirect_to(root_path(error: Rails.configuration.custom_error_msgs[:banned_user]))
382+
end
374383
end
375384

376385
context 'restricted domain set to multiple domain' do

0 commit comments

Comments
 (0)