Commit ba0a61bdbae6169ac546e078fbdcebfb3ebdb4e0

Authored by Leandro Santos
2 parents 258d1c2c 5225e5d4

Merge branch 'next' into api

app/models/profile.rb
@@ -327,16 +327,25 @@ class Profile < ActiveRecord::Base @@ -327,16 +327,25 @@ class Profile < ActiveRecord::Base
327 @top_level_articles ||= Article.top_level_for(self) 327 @top_level_articles ||= Article.top_level_for(self)
328 end 328 end
329 329
330 - def self.is_available?(identifier, environment)  
331 - !(identifier =~ IDENTIFIER_FORMAT).nil? && !RESERVED_IDENTIFIERS.include?(identifier) && Profile.find(:first, :conditions => ['environment_id = ? and identifier = ?', environment.id, identifier]).nil? 330 + def self.is_available?(identifier, environment, profile_id=nil)
  331 + return false unless identifier =~ IDENTIFIER_FORMAT &&
  332 + !RESERVED_IDENTIFIERS.include?(identifier) &&
  333 + (NOOSFERO_CONF['exclude_profile_identifier_pattern'].blank? || identifier !~ /#{NOOSFERO_CONF['exclude_profile_identifier_pattern']}/)
  334 + return true if environment.nil?
  335 +
  336 + profiles = environment.profiles.where(:identifier => identifier)
  337 + profiles = profiles.where(['id != ?', profile_id]) if profile_id.present?
  338 + !profiles.exists?
332 end 339 end
333 340
334 validates_presence_of :identifier, :name 341 validates_presence_of :identifier, :name
335 - validates_format_of :identifier, :with => IDENTIFIER_FORMAT, :if => lambda { |profile| !profile.identifier.blank? }  
336 - validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS  
337 - validates_uniqueness_of :identifier, :scope => :environment_id  
338 validates_length_of :nickname, :maximum => 16, :allow_nil => true 342 validates_length_of :nickname, :maximum => 16, :allow_nil => true
339 validate :valid_template 343 validate :valid_template
  344 + validate :valid_identifier
  345 +
  346 + def valid_identifier
  347 + errors.add(:identifier, _('is not available.')) unless Profile.is_available?(identifier, environment, id)
  348 + end
340 349
341 def valid_template 350 def valid_template
342 if template_id.present? && template && !template.is_template 351 if template_id.present? && template && !template.is_template
config/noosfero.yml.dist
@@ -10,6 +10,7 @@ development: @@ -10,6 +10,7 @@ development:
10 exception_recipients: [admin@example.com] 10 exception_recipients: [admin@example.com]
11 max_upload_size: 5MB 11 max_upload_size: 5MB
12 hours_until_user_activation_check: 72 12 hours_until_user_activation_check: 72
  13 + exclude_profile_identifier_pattern: index(\..*)?|home(\..*)?
13 14
14 test: 15 test:
15 16
test/unit/profile_test.rb
@@ -1623,6 +1623,16 @@ class ProfileTest < ActiveSupport::TestCase @@ -1623,6 +1623,16 @@ class ProfileTest < ActiveSupport::TestCase
1623 assert_equal false, Profile.is_available?('identifier-test', Environment.default) 1623 assert_equal false, Profile.is_available?('identifier-test', Environment.default)
1624 end 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 should 'not have long descriptions' do 1636 should 'not have long descriptions' do
1627 long_description = 'a' * 600 1637 long_description = 'a' * 600
1628 profile = Profile.new 1638 profile = Profile.new