FEATURE: make full names a required field of user profiles with the full_name_required setting

This commit is contained in:
Neil Lalonde 2015-04-02 17:07:56 -04:00
parent b63e9450a6
commit 30b063c08b
10 changed files with 74 additions and 3 deletions

View file

@ -69,12 +69,20 @@ export default DiscourseController.extend(ModalFunctionality, {
return I18n.t('user.password.instructions', {count: Discourse.SiteSettings.min_password_length}); return I18n.t('user.password.instructions', {count: Discourse.SiteSettings.min_password_length});
}.property(), }.property(),
// Validate the name. It's not required. nameInstructions: function() {
return I18n.t(Discourse.SiteSettings.full_name_required ? 'user.name.instructions_required' : 'user.name.instructions');
}.property(),
// Validate the name.
nameValidation: function() { nameValidation: function() {
if (this.get('accountPasswordConfirm') === 0) { if (this.get('accountPasswordConfirm') === 0) {
this.fetchConfirmationValue(); this.fetchConfirmationValue();
} }
if (Discourse.SiteSettings.full_name_required && this.blank('accountName')) {
return Discourse.InputValidation.create({ failed: true });
}
return Discourse.InputValidation.create({ok: true}); return Discourse.InputValidation.create({ok: true});
}.property('accountName'), }.property('accountName'),

View file

@ -39,6 +39,10 @@ export default ObjectController.extend(CanCheckEmails, {
canEditName: Discourse.computed.setting('enable_names'), canEditName: Discourse.computed.setting('enable_names'),
nameInstructions: function() {
return I18n.t(Discourse.SiteSettings.full_name_required ? 'user.name.instructions_required' : 'user.name.instructions');
}.property(),
canSelectTitle: function() { canSelectTitle: function() {
return this.siteSettings.enable_badges && this.get('model.has_title_badges'); return this.siteSettings.enable_badges && this.get('model.has_title_badges');
}.property('model.badge_count'), }.property('model.badge_count'),

View file

@ -42,7 +42,7 @@
</tr> </tr>
<tr class="instructions"> <tr class="instructions">
<td></td> <td></td>
<td><label>{{i18n 'user.name.instructions'}}</label></td> <td><label>{{nameInstructions}}</label></td>
</tr> </tr>
{{#if passwordRequired}} {{#if passwordRequired}}

View file

@ -32,7 +32,7 @@
{{/if}} {{/if}}
</div> </div>
<div class='instructions'> <div class='instructions'>
{{i18n 'user.name.instructions'}} {{nameInstructions}}
</div> </div>
</div> </div>
{{/if}} {{/if}}

View file

@ -72,6 +72,7 @@ class User < ActiveRecord::Base
validates :email, presence: true, uniqueness: true validates :email, presence: true, uniqueness: true
validates :email, email: true, if: :email_changed? validates :email, email: true, if: :email_changed?
validate :password_validator validate :password_validator
validates :name, user_full_name: true, if: :name_changed?
validates :ip_address, allowed_ip_address: {on: :create, message: :signup_not_allowed} validates :ip_address, allowed_ip_address: {on: :create, message: :signup_not_allowed}
after_initialize :add_trust_level after_initialize :add_trust_level

View file

@ -441,6 +441,7 @@ en:
name: name:
title: "Name" title: "Name"
instructions: "Your full name (optional)" instructions: "Your full name (optional)"
instructions_required: "Your full name"
too_short: "Your name is too short" too_short: "Your name is too short"
ok: "Your name looks good" ok: "Your name looks good"
username: username:

View file

@ -1101,6 +1101,7 @@ en:
disable_edit_notifications: "Disables edit notifications by the system user when 'download_remote_images_to_local' is active." disable_edit_notifications: "Disables edit notifications by the system user when 'download_remote_images_to_local' is active."
full_name_required: "Full name is a required field of a user's profile."
enable_names: "Show the user's full name on their profile, user card, and emails. Disable to hide full name everywhere." enable_names: "Show the user's full name on their profile, user card, and emails. Disable to hide full name everywhere."
display_name_on_posts: "Show a user's full name on their posts in addition to their @username." display_name_on_posts: "Show a user's full name on their posts in addition to their @username."
invites_per_page: "Default invites shown on the user page." invites_per_page: "Default invites shown on the user page."

View file

@ -276,6 +276,9 @@ users:
logout_redirect: logout_redirect:
client: true client: true
default: '' default: ''
full_name_required:
client: true
default: false
enable_names: enable_names:
client: true client: true
default: true default: true

View file

@ -0,0 +1,8 @@
class UserFullNameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if SiteSetting.full_name_required && !record.name.present?
record.errors.add(attribute, :blank)
end
end
end

View file

@ -0,0 +1,45 @@
require "spec_helper"
describe UserFullNameValidator do
let(:validator) { described_class.new({attributes: :name}) }
subject(:validate) { validator.validate_each(record,:name,@name) }
let(:record) { Fabricate.build(:user, name: @name) }
context "name not required" do
before { SiteSetting.stubs(:full_name_required).returns(false) }
it "allows no name" do
@name = nil
validate
expect(record.errors[:name]).not_to be_present
end
it "allows name being set" do
@name = "Bigfoot"
validate
expect(record.errors[:name]).not_to be_present
end
end
context "name required" do
before { SiteSetting.stubs(:full_name_required).returns(true) }
it "adds error for nil name" do
@name = nil
validate
expect(record.errors[:name]).to be_present
end
it "adds error for empty string name" do
@name = ""
validate
expect(record.errors[:name]).to be_present
end
it "allows name being set" do
@name = "Bigfoot"
validate
expect(record.errors[:name]).not_to be_present
end
end
end