Commit 0fea1551c437b918408310245fb996cc9bbca6c0

Authored by Leandro Santos
2 parents 2568571e 5225e5d4

Merge branch 'next' into stable

app/models/profile.rb
... ... @@ -355,16 +355,25 @@ class Profile < ActiveRecord::Base
355 355 @top_level_articles ||= Article.top_level_for(self)
356 356 end
357 357  
358   - def self.is_available?(identifier, environment)
359   - !(identifier =~ IDENTIFIER_FORMAT).nil? && !RESERVED_IDENTIFIERS.include?(identifier) && Profile.find(:first, :conditions => ['environment_id = ? and identifier = ?', environment.id, identifier]).nil?
  358 + def self.is_available?(identifier, environment, profile_id=nil)
  359 + return false unless identifier =~ IDENTIFIER_FORMAT &&
  360 + !RESERVED_IDENTIFIERS.include?(identifier) &&
  361 + (NOOSFERO_CONF['exclude_profile_identifier_pattern'].blank? || identifier !~ /#{NOOSFERO_CONF['exclude_profile_identifier_pattern']}/)
  362 + return true if environment.nil?
  363 +
  364 + profiles = environment.profiles.where(:identifier => identifier)
  365 + profiles = profiles.where(['id != ?', profile_id]) if profile_id.present?
  366 + !profiles.exists?
360 367 end
361 368  
362 369 validates_presence_of :identifier, :name
363   - validates_format_of :identifier, :with => IDENTIFIER_FORMAT, :if => lambda { |profile| !profile.identifier.blank? }
364   - validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS
365   - validates_uniqueness_of :identifier, :scope => :environment_id
366 370 validates_length_of :nickname, :maximum => 16, :allow_nil => true
367 371 validate :valid_template
  372 + validate :valid_identifier
  373 +
  374 + def valid_identifier
  375 + errors.add(:identifier, _('is not available.')) unless Profile.is_available?(identifier, environment, id)
  376 + end
368 377  
369 378 def valid_template
370 379 if template_id.present? && template && !template.is_template
... ...
config/noosfero.yml.dist
... ... @@ -10,6 +10,7 @@ development:
10 10 exception_recipients: [admin@example.com]
11 11 max_upload_size: 5MB
12 12 hours_until_user_activation_check: 72
  13 + exclude_profile_identifier_pattern: index(\..*)?|home(\..*)?
13 14  
14 15 test:
15 16  
... ...
test/unit/profile_test.rb
... ... @@ -1623,6 +1623,16 @@ class ProfileTest < ActiveSupport::TestCase
1623 1623 assert_equal false, Profile.is_available?('identifier-test', Environment.default)
1624 1624 end
1625 1625  
  1626 + should 'not be available if identifier match with custom exclusion pattern' do
  1627 + NOOSFERO_CONF.stubs(:[]).with('exclude_profile_identifier_pattern').returns('identifier.*')
  1628 + assert_equal false, Profile.is_available?('identifier-test', Environment.default)
  1629 + end
  1630 +
  1631 + should 'be available if identifier do not match with custom exclusion pattern' do
  1632 + NOOSFERO_CONF.stubs(:[]).with('exclude_profile_identifier_pattern').returns('identifier.*')
  1633 + assert_equal false, Profile.is_available?('test-identifier', Environment.default)
  1634 + end
  1635 +
1626 1636 should 'not have long descriptions' do
1627 1637 long_description = 'a' * 600
1628 1638 profile = Profile.new
... ...