Commit 64fa378d8831b994be7342d4a6fdfa139d370859

Authored by Leandro Santos
2 parents 27221c3d 5225e5d4

Merge branch 'next' into serpro-context

app/models/profile.rb
... ... @@ -317,16 +317,25 @@ class Profile < ActiveRecord::Base
317 317 @top_level_articles ||= Article.top_level_for(self)
318 318 end
319 319  
320   - def self.is_available?(identifier, environment)
321   - !(identifier =~ IDENTIFIER_FORMAT).nil? && !RESERVED_IDENTIFIERS.include?(identifier) && Profile.find(:first, :conditions => ['environment_id = ? and identifier = ?', environment.id, identifier]).nil?
  320 + def self.is_available?(identifier, environment, profile_id=nil)
  321 + return false unless identifier =~ IDENTIFIER_FORMAT &&
  322 + !RESERVED_IDENTIFIERS.include?(identifier) &&
  323 + (NOOSFERO_CONF['exclude_profile_identifier_pattern'].blank? || identifier !~ /#{NOOSFERO_CONF['exclude_profile_identifier_pattern']}/)
  324 + return true if environment.nil?
  325 +
  326 + profiles = environment.profiles.where(:identifier => identifier)
  327 + profiles = profiles.where(['id != ?', profile_id]) if profile_id.present?
  328 + !profiles.exists?
322 329 end
323 330  
324 331 validates_presence_of :identifier, :name
325   - validates_format_of :identifier, :with => IDENTIFIER_FORMAT, :if => lambda { |profile| !profile.identifier.blank? }
326   - validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS
327   - validates_uniqueness_of :identifier, :scope => :environment_id
328 332 validates_length_of :nickname, :maximum => 16, :allow_nil => true
329 333 validate :valid_template
  334 + validate :valid_identifier
  335 +
  336 + def valid_identifier
  337 + errors.add(:identifier, _('is not available.')) unless Profile.is_available?(identifier, environment, id)
  338 + end
330 339  
331 340 def valid_template
332 341 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
... ...