Commit 5225e5d44e9b6d7866ddd47845d81236b301e696
Exists in
staging
and in
42 other branches
Merge branch 'exclude_identifier_pattern' into 'next'
Add a config option that define a exclusion pattern for profile identifiers See merge request !548
Showing
3 changed files
with
25 additions
and
5 deletions
Show diff stats
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
test/unit/profile_test.rb
... | ... | @@ -1613,6 +1613,16 @@ class ProfileTest < ActiveSupport::TestCase |
1613 | 1613 | assert_equal false, Profile.is_available?('identifier-test', Environment.default) |
1614 | 1614 | end |
1615 | 1615 | |
1616 | + should 'not be available if identifier match with custom exclusion pattern' do | |
1617 | + NOOSFERO_CONF.stubs(:[]).with('exclude_profile_identifier_pattern').returns('identifier.*') | |
1618 | + assert_equal false, Profile.is_available?('identifier-test', Environment.default) | |
1619 | + end | |
1620 | + | |
1621 | + should 'be available if identifier do not match with custom exclusion pattern' do | |
1622 | + NOOSFERO_CONF.stubs(:[]).with('exclude_profile_identifier_pattern').returns('identifier.*') | |
1623 | + assert_equal false, Profile.is_available?('test-identifier', Environment.default) | |
1624 | + end | |
1625 | + | |
1616 | 1626 | should 'not have long descriptions' do |
1617 | 1627 | long_description = 'a' * 600 |
1618 | 1628 | profile = Profile.new | ... | ... |