diff --git a/app/models/abuse_complaint.rb b/app/models/abuse_complaint.rb index 8c8c3ab..2894880 100644 --- a/app/models/abuse_complaint.rb +++ b/app/models/abuse_complaint.rb @@ -1,6 +1,6 @@ class AbuseComplaint < Task has_many :abuse_reports, :dependent => :destroy - belongs_to :reported, :class_name => "Profile", :foreign_key => "requestor_id" + belongs_to :reported, :polymorphic => true, :foreign_key => "requestor_id" validates_presence_of :reported alias :requestor :reported diff --git a/app/models/abuse_report.rb b/app/models/abuse_report.rb index 8fbec90..0fa161a 100644 --- a/app/models/abuse_report.rb +++ b/app/models/abuse_report.rb @@ -2,7 +2,7 @@ class AbuseReport < ApplicationRecord attr_accessible :content, :reason - belongs_to :reporter, :class_name => 'Person' + belongs_to :reporter, :polymorphic => true belongs_to :abuse_complaint has_many :reported_images, :dependent => :destroy diff --git a/app/models/comment.rb b/app/models/comment.rb index b79be4d..5f41b4b 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -15,7 +15,7 @@ class Comment < ApplicationRecord alias :article= :source= attr_accessor :follow_article - belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id' + belongs_to :author, :polymorphic => true, :foreign_key => 'author_id' has_many :children, :class_name => 'Comment', :foreign_key => 'reply_of_id', :dependent => :destroy belongs_to :reply_of, :class_name => 'Comment', :foreign_key => 'reply_of_id' diff --git a/app/models/concerns/human.rb b/app/models/concerns/human.rb index 3f5f59c..e9e7f90 100644 --- a/app/models/concerns/human.rb +++ b/app/models/concerns/human.rb @@ -2,15 +2,15 @@ module Human extend ActiveSupport::Concern included do - has_many :comments, :foreign_key => :author_id - has_many :abuse_reports, :foreign_key => 'reporter_id', :dependent => :destroy + has_many :comments, :as => :author, :foreign_key => :author_id + has_many :abuse_reports, :as => :reporter, :foreign_key => 'reporter_id', :dependent => :destroy scope :abusers, -> { - joins(:abuse_complaints).where('tasks.status = 3').distinct.select('profiles.*') + joins(:abuse_complaints).where('tasks.status = 3').distinct.select("#{self.table_name}.*") } scope :non_abusers, -> { - distinct.select("profiles.*"). - joins("LEFT JOIN tasks ON profiles.id = tasks.requestor_id AND tasks.type='AbuseComplaint'"). + distinct.select("#{self.table_name}.*"). + joins("LEFT JOIN tasks ON #{self.table_name}.id = tasks.requestor_id AND tasks.type='AbuseComplaint'"). where("tasks.status != 3 OR tasks.id is NULL") } end diff --git a/app/models/concerns/profile_entity.rb b/app/models/concerns/profile_entity.rb index e39aab7..47807e4 100644 --- a/app/models/concerns/profile_entity.rb +++ b/app/models/concerns/profile_entity.rb @@ -2,14 +2,15 @@ module ProfileEntity extend ActiveSupport::Concern included do - attr_accessible :name, :identifier, :environment, :redirection_after_login + attr_accessible :name, :identifier, :environment validates_presence_of :identifier, :name - validates_inclusion_of :redirection_after_login, :in => Environment.login_redirection_options.keys, :allow_nil => true belongs_to :environment has_many :search_terms, :as => :context - has_many :abuse_complaints, :foreign_key => 'requestor_id', :dependent => :destroy + has_many :abuse_complaints, :as => :reported, :foreign_key => 'requestor_id', :dependent => :destroy + + before_create :set_default_environment end @@ -23,11 +24,15 @@ module ProfileEntity self.save end - def preferred_login_redirection - redirection_after_login.blank? ? environment.redirection_after_login : redirection_after_login - end - def opened_abuse_complaint abuse_complaints.opened.first end + + def set_default_environment + if self.environment.nil? + self.environment = Environment.default + end + true + end + end diff --git a/app/models/external_person.rb b/app/models/external_person.rb index 43f86b9..6eee16e 100644 --- a/app/models/external_person.rb +++ b/app/models/external_person.rb @@ -1,7 +1,35 @@ # A pseudo profile is a person from a remote network -class ExternalPerson +class ExternalPerson < ActiveRecord::Base include Human include ProfileEntity + validates_uniqueness_of :identifier, scope: :source + + attr_accessible :source, :email_md5_hash + + def self.get_or_create(webfinger) + user = ExternalPerson.find_by(identifier: webfinger.identifier, source: webfinger.domain) + if user.nil? + user = ExternalPerson.create!(identifier: webfinger.identifier, + name: webfinger.name, + source: webfinger.domain, + email_md5_hash: webfinger.email_md5 + ) + end + user + end + + def url + "http://#{self.source}/#{self.identifier}" + end + + def avatar + "http://#{self.source}/plugin/gravatar_provider/h/#{self.email_md5_hash}" + end + + def preferred_login_redirection + environment.redirection_after_login + end + end diff --git a/app/models/profile.rb b/app/models/profile.rb index f5c7e20..0c998f5 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -6,7 +6,7 @@ class Profile < ApplicationRecord include ProfileEntity attr_accessible :public_profile, :nickname, :custom_footer, :custom_header, :address, :zip_code, :contact_phone, :image_builder, :description, :closed, :template_id, :lat, :lng, :is_template, :fields_privacy, :preferred_domain_id, :category_ids, :country, :city, :state, :national_region_code, :email, :contact_email, :redirect_l10n, :notification_time, - :custom_url_redirection, :email_suggestions, :allow_members_to_invite, :invite_friends_only, :secret, :profile_admin_mail_notification + :custom_url_redirection, :email_suggestions, :allow_members_to_invite, :invite_friends_only, :secret, :profile_admin_mail_notification, :redirection_after_login # use for internationalizable human type names in search facets # reimplement on subclasses @@ -411,14 +411,6 @@ class Profile < ApplicationRecord end end - before_create :set_default_environment - def set_default_environment - if self.environment.nil? - self.environment = Environment.default - end - true - end - # registar callback for creating boxes after the object is created. after_create :create_default_set_of_boxes @@ -1103,6 +1095,10 @@ private :generate_url, :url_options display_private_info_to?(current_person) || (public_fields.include?(field) && public?) end + validates_inclusion_of :redirection_after_login, :in => Environment.login_redirection_options.keys, :allow_nil => true + def preferred_login_redirection + redirection_after_login.blank? ? environment.redirection_after_login : redirection_after_login + end settings_items :custom_url_redirection, type: String, default: nil def remove_from_suggestion_list(person) diff --git a/db/migrate/20160420125236_add_type_to_polymorphic_profile_associations.rb b/db/migrate/20160420125236_add_type_to_polymorphic_profile_associations.rb new file mode 100644 index 0000000..26cf03d --- /dev/null +++ b/db/migrate/20160420125236_add_type_to_polymorphic_profile_associations.rb @@ -0,0 +1,17 @@ +class AddTypeToPolymorphicProfileAssociations < ActiveRecord::Migration + def up + add_column :tasks, :reported_type, :string + add_column :abuse_reports, :reporter_type, :string + add_column :comments, :author_type, :string + + update("UPDATE tasks SET reported_type='Profile'") + update("UPDATE abuse_reports SET reporter_type='Person'") + update("UPDATE comments SET author_type='Person'") + end + + def down + remove_column :abuse_complaints, :reported_type + remove_column :abuse_reports, :reporter_type + remove_column :comments, :author_type + end +end diff --git a/db/migrate/20160420140141_create_external_person.rb b/db/migrate/20160420140141_create_external_person.rb new file mode 100644 index 0000000..2beaec5 --- /dev/null +++ b/db/migrate/20160420140141_create_external_person.rb @@ -0,0 +1,12 @@ +class CreateExternalPerson < ActiveRecord::Migration + def change + create_table :external_people do |t| + t.string :name + t.string :identifier + t.string :source + t.string :email_md5_hash + t.integer :environment_id + t.boolean :visible, default: true + end + end +end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index eaaff46..2bd4da6 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -23,17 +23,14 @@ class CommentTest < ActiveSupport::TestCase end end - should 'record authenticated author' do + should 'record authenticated author polymorphically' do c = Comment.new - assert_raise ActiveRecord::AssociationTypeMismatch do - c.author = 1 - end - assert_raise ActiveRecord::AssociationTypeMismatch do - c.author = Profile - end assert_nothing_raised do c.author = Person.new end + assert_nothing_raised do + c.author = ExternalPerson.new + end end should 'record unauthenticated author' do diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 440a8f6..f605fd2 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -1831,9 +1831,9 @@ class PersonTest < ActiveSupport::TestCase p1 = fast_create(Person) p2 = fast_create(Person) article = fast_create(Article) - c1 = fast_create(Comment, :source_id => article.id, :author_id => p1.id) - c2 = fast_create(Comment, :source_id => article.id, :author_id => p2.id) - c3 = fast_create(Comment, :source_id => article.id, :author_id => p1.id) + c1 = fast_create(Comment, :source_id => article.id, :author_id => p1.id, :author_type => 'Profile') + c2 = fast_create(Comment, :source_id => article.id, :author_id => p2.id, :author_type => 'Profile') + c3 = fast_create(Comment, :source_id => article.id, :author_id => p1.id, :author_type => 'Profile') assert_equivalent [c1,c3], p1.comments end -- libgit2 0.21.2