Feature #35439 » 0001-Option-to-require-2FA-authentication-only-for-users-.patch
| app/models/user.rb | ||
|---|---|---|
|
def must_activate_twofa?
|
||
|
(
|
||
|
Setting.twofa_required? ||
|
||
|
(Setting.twofa_optional? && groups.any?(&:twofa_required?))
|
||
|
(
|
||
|
Setting.twofa_optional? && (
|
||
|
groups.any?(&:twofa_required?) ||
|
||
|
(Setting.twofa_required_for_administrators? && self.admin)
|
||
|
)
|
||
|
)
|
||
|
) && !twofa_active?
|
||
|
end
|
||
| app/views/settings/_authentication.html.erb | ||
|---|---|---|
|
<%= t 'twofa_hint_optional_html', label: t(:label_optional) -%><br/>
|
||
|
<%= t 'twofa_hint_required_html', label: t(:label_required_lower) -%>
|
||
|
</em>
|
||
|
<span id="twofa_optional" class="<%= "hidden" unless Setting.twofa == "1" %>">
|
||
|
<label class="block">
|
||
|
<%= setting_check_box :twofa_required_for_administrators, label: false %>
|
||
|
<%= l(:setting_twofa_required_for_administrators) %>
|
||
|
</label>
|
||
|
</span>
|
||
|
</p>
|
||
|
</div>
|
||
| ... | ... | |
|
<%= submit_tag l(:button_save) %>
|
||
|
<% end %>
|
||
|
<%= javascript_tag do %>
|
||
|
$('#settings_twofa').on('change', function(e){
|
||
|
const twofa = e.target.value;
|
||
|
const parent_block = document.getElementById("twofa_optional");
|
||
|
if (twofa == "1") {
|
||
|
parent_block.classList.remove('hidden');
|
||
|
} else {
|
||
|
parent_block.classList.add('hidden');
|
||
|
}
|
||
|
});
|
||
|
<% end %>
|
||
| config/locales/en.yml | ||
|---|---|---|
|
setting_show_status_changes_in_mail_subject: Show status changes in issue mail notifications subject
|
||
|
setting_project_list_defaults: Projects list defaults
|
||
|
setting_twofa: Two-factor authentication
|
||
|
setting_twofa_required_for_administrators: Require two-factor authentication for administrators
|
||
|
permission_add_project: Create project
|
||
|
permission_add_subprojects: Create subprojects
|
||
| config/settings.yml | ||
|---|---|---|
|
twofa:
|
||
|
default: 1
|
||
|
security_notifications: 1
|
||
|
twofa_required_for_administrators:
|
||
|
default: 0
|
||
|
security_notifications: 1
|
||
|
unsubscribe:
|
||
|
default: 1
|
||
|
password_required_char_classes:
|
||
| test/integration/twofa_test.rb | ||
|---|---|---|
|
end
|
||
|
end
|
||
|
test "should require twofa setup when required for administrators" do
|
||
|
user = User.find_by_login 'admin'
|
||
|
assert_not user.must_activate_twofa?
|
||
|
with_settings twofa: "0", twofa_required_for_administrators: "1" do
|
||
|
assert_not Setting.twofa_optional?
|
||
|
assert_not Setting.twofa_required?
|
||
|
assert_not user.must_activate_twofa?
|
||
|
end
|
||
|
with_settings twofa: "1", twofa_required_for_administrators: "1" do
|
||
|
assert Setting.twofa_optional?
|
||
|
assert_not Setting.twofa_required?
|
||
|
assert user.must_activate_twofa?
|
||
|
log_user('admin', 'admin')
|
||
|
follow_redirect!
|
||
|
assert_redirected_to "/my/twofa/totp/activate/confirm"
|
||
|
end
|
||
|
end
|
||
|
test "should require twofa setup when required by group" do
|
||
|
user = User.find_by_login 'jsmith'
|
||
|
assert_not user.must_activate_twofa?
|
||