Commit 9ccdbb658b481ec9a5997eeedd99eaed3c1766f6
1 parent
c383bcdc
Exists in
federation-webfinger
Creates External Profile model for federated users
Showing
11 changed files
with
90 additions
and
35 deletions
Show diff stats
app/models/abuse_complaint.rb
1 | class AbuseComplaint < Task | 1 | class AbuseComplaint < Task |
2 | has_many :abuse_reports, :dependent => :destroy | 2 | has_many :abuse_reports, :dependent => :destroy |
3 | - belongs_to :reported, :class_name => "Profile", :foreign_key => "requestor_id" | 3 | + belongs_to :reported, :polymorphic => true, :foreign_key => "requestor_id" |
4 | 4 | ||
5 | validates_presence_of :reported | 5 | validates_presence_of :reported |
6 | alias :requestor :reported | 6 | alias :requestor :reported |
app/models/abuse_report.rb
@@ -2,7 +2,7 @@ class AbuseReport < ApplicationRecord | @@ -2,7 +2,7 @@ class AbuseReport < ApplicationRecord | ||
2 | 2 | ||
3 | attr_accessible :content, :reason | 3 | attr_accessible :content, :reason |
4 | 4 | ||
5 | - belongs_to :reporter, :class_name => 'Person' | 5 | + belongs_to :reporter, :polymorphic => true |
6 | belongs_to :abuse_complaint | 6 | belongs_to :abuse_complaint |
7 | has_many :reported_images, :dependent => :destroy | 7 | has_many :reported_images, :dependent => :destroy |
8 | 8 |
app/models/comment.rb
@@ -15,7 +15,7 @@ class Comment < ApplicationRecord | @@ -15,7 +15,7 @@ class Comment < ApplicationRecord | ||
15 | alias :article= :source= | 15 | alias :article= :source= |
16 | attr_accessor :follow_article | 16 | attr_accessor :follow_article |
17 | 17 | ||
18 | - belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id' | 18 | + belongs_to :author, :polymorphic => true, :foreign_key => 'author_id' |
19 | has_many :children, :class_name => 'Comment', :foreign_key => 'reply_of_id', :dependent => :destroy | 19 | has_many :children, :class_name => 'Comment', :foreign_key => 'reply_of_id', :dependent => :destroy |
20 | belongs_to :reply_of, :class_name => 'Comment', :foreign_key => 'reply_of_id' | 20 | belongs_to :reply_of, :class_name => 'Comment', :foreign_key => 'reply_of_id' |
21 | 21 |
app/models/concerns/human.rb
@@ -2,15 +2,15 @@ module Human | @@ -2,15 +2,15 @@ module Human | ||
2 | extend ActiveSupport::Concern | 2 | extend ActiveSupport::Concern |
3 | 3 | ||
4 | included do | 4 | included do |
5 | - has_many :comments, :foreign_key => :author_id | ||
6 | - has_many :abuse_reports, :foreign_key => 'reporter_id', :dependent => :destroy | 5 | + has_many :comments, :as => :author, :foreign_key => :author_id |
6 | + has_many :abuse_reports, :as => :reporter, :foreign_key => 'reporter_id', :dependent => :destroy | ||
7 | 7 | ||
8 | scope :abusers, -> { | 8 | scope :abusers, -> { |
9 | - joins(:abuse_complaints).where('tasks.status = 3').distinct.select('profiles.*') | 9 | + joins(:abuse_complaints).where('tasks.status = 3').distinct.select("#{self.table_name}.*") |
10 | } | 10 | } |
11 | scope :non_abusers, -> { | 11 | scope :non_abusers, -> { |
12 | - distinct.select("profiles.*"). | ||
13 | - joins("LEFT JOIN tasks ON profiles.id = tasks.requestor_id AND tasks.type='AbuseComplaint'"). | 12 | + distinct.select("#{self.table_name}.*"). |
13 | + joins("LEFT JOIN tasks ON #{self.table_name}.id = tasks.requestor_id AND tasks.type='AbuseComplaint'"). | ||
14 | where("tasks.status != 3 OR tasks.id is NULL") | 14 | where("tasks.status != 3 OR tasks.id is NULL") |
15 | } | 15 | } |
16 | end | 16 | end |
app/models/concerns/profile_entity.rb
@@ -2,14 +2,15 @@ module ProfileEntity | @@ -2,14 +2,15 @@ module ProfileEntity | ||
2 | extend ActiveSupport::Concern | 2 | extend ActiveSupport::Concern |
3 | 3 | ||
4 | included do | 4 | included do |
5 | - attr_accessible :name, :identifier, :environment, :redirection_after_login | 5 | + attr_accessible :name, :identifier, :environment |
6 | 6 | ||
7 | validates_presence_of :identifier, :name | 7 | validates_presence_of :identifier, :name |
8 | - validates_inclusion_of :redirection_after_login, :in => Environment.login_redirection_options.keys, :allow_nil => true | ||
9 | 8 | ||
10 | belongs_to :environment | 9 | belongs_to :environment |
11 | has_many :search_terms, :as => :context | 10 | has_many :search_terms, :as => :context |
12 | - has_many :abuse_complaints, :foreign_key => 'requestor_id', :dependent => :destroy | 11 | + has_many :abuse_complaints, :as => :reported, :foreign_key => 'requestor_id', :dependent => :destroy |
12 | + | ||
13 | + before_create :set_default_environment | ||
13 | 14 | ||
14 | end | 15 | end |
15 | 16 | ||
@@ -23,11 +24,15 @@ module ProfileEntity | @@ -23,11 +24,15 @@ module ProfileEntity | ||
23 | self.save | 24 | self.save |
24 | end | 25 | end |
25 | 26 | ||
26 | - def preferred_login_redirection | ||
27 | - redirection_after_login.blank? ? environment.redirection_after_login : redirection_after_login | ||
28 | - end | ||
29 | - | ||
30 | def opened_abuse_complaint | 27 | def opened_abuse_complaint |
31 | abuse_complaints.opened.first | 28 | abuse_complaints.opened.first |
32 | end | 29 | end |
30 | + | ||
31 | + def set_default_environment | ||
32 | + if self.environment.nil? | ||
33 | + self.environment = Environment.default | ||
34 | + end | ||
35 | + true | ||
36 | + end | ||
37 | + | ||
33 | end | 38 | end |
app/models/external_person.rb
1 | # A pseudo profile is a person from a remote network | 1 | # A pseudo profile is a person from a remote network |
2 | -class ExternalPerson | 2 | +class ExternalPerson < ActiveRecord::Base |
3 | 3 | ||
4 | include Human | 4 | include Human |
5 | include ProfileEntity | 5 | include ProfileEntity |
6 | 6 | ||
7 | + validates_uniqueness_of :identifier, scope: :source | ||
8 | + | ||
9 | + attr_accessible :source, :email_md5_hash | ||
10 | + | ||
11 | + def self.get_or_create(webfinger) | ||
12 | + user = ExternalPerson.find_by(identifier: webfinger.identifier, source: webfinger.domain) | ||
13 | + if user.nil? | ||
14 | + user = ExternalPerson.create!(identifier: webfinger.identifier, | ||
15 | + name: webfinger.name, | ||
16 | + source: webfinger.domain, | ||
17 | + email_md5_hash: webfinger.email_md5 | ||
18 | + ) | ||
19 | + end | ||
20 | + user | ||
21 | + end | ||
22 | + | ||
23 | + def url | ||
24 | + "http://#{self.source}/#{self.identifier}" | ||
25 | + end | ||
26 | + | ||
27 | + def avatar | ||
28 | + "http://#{self.source}/plugin/gravatar_provider/h/#{self.email_md5_hash}" | ||
29 | + end | ||
30 | + | ||
31 | + def preferred_login_redirection | ||
32 | + environment.redirection_after_login | ||
33 | + end | ||
34 | + | ||
7 | end | 35 | end |
app/models/profile.rb
@@ -6,7 +6,7 @@ class Profile < ApplicationRecord | @@ -6,7 +6,7 @@ class Profile < ApplicationRecord | ||
6 | include ProfileEntity | 6 | include ProfileEntity |
7 | 7 | ||
8 | 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, | 8 | 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, |
9 | - :custom_url_redirection, :email_suggestions, :allow_members_to_invite, :invite_friends_only, :secret, :profile_admin_mail_notification | 9 | + :custom_url_redirection, :email_suggestions, :allow_members_to_invite, :invite_friends_only, :secret, :profile_admin_mail_notification, :redirection_after_login |
10 | 10 | ||
11 | # use for internationalizable human type names in search facets | 11 | # use for internationalizable human type names in search facets |
12 | # reimplement on subclasses | 12 | # reimplement on subclasses |
@@ -411,14 +411,6 @@ class Profile < ApplicationRecord | @@ -411,14 +411,6 @@ class Profile < ApplicationRecord | ||
411 | end | 411 | end |
412 | end | 412 | end |
413 | 413 | ||
414 | - before_create :set_default_environment | ||
415 | - def set_default_environment | ||
416 | - if self.environment.nil? | ||
417 | - self.environment = Environment.default | ||
418 | - end | ||
419 | - true | ||
420 | - end | ||
421 | - | ||
422 | # registar callback for creating boxes after the object is created. | 414 | # registar callback for creating boxes after the object is created. |
423 | after_create :create_default_set_of_boxes | 415 | after_create :create_default_set_of_boxes |
424 | 416 | ||
@@ -1103,6 +1095,10 @@ private :generate_url, :url_options | @@ -1103,6 +1095,10 @@ private :generate_url, :url_options | ||
1103 | display_private_info_to?(current_person) || (public_fields.include?(field) && public?) | 1095 | display_private_info_to?(current_person) || (public_fields.include?(field) && public?) |
1104 | end | 1096 | end |
1105 | 1097 | ||
1098 | + validates_inclusion_of :redirection_after_login, :in => Environment.login_redirection_options.keys, :allow_nil => true | ||
1099 | + def preferred_login_redirection | ||
1100 | + redirection_after_login.blank? ? environment.redirection_after_login : redirection_after_login | ||
1101 | + end | ||
1106 | settings_items :custom_url_redirection, type: String, default: nil | 1102 | settings_items :custom_url_redirection, type: String, default: nil |
1107 | 1103 | ||
1108 | def remove_from_suggestion_list(person) | 1104 | def remove_from_suggestion_list(person) |
db/migrate/20160420125236_add_type_to_polymorphic_profile_associations.rb
0 → 100644
@@ -0,0 +1,17 @@ | @@ -0,0 +1,17 @@ | ||
1 | +class AddTypeToPolymorphicProfileAssociations < ActiveRecord::Migration | ||
2 | + def up | ||
3 | + add_column :tasks, :reported_type, :string | ||
4 | + add_column :abuse_reports, :reporter_type, :string | ||
5 | + add_column :comments, :author_type, :string | ||
6 | + | ||
7 | + update("UPDATE tasks SET reported_type='Profile'") | ||
8 | + update("UPDATE abuse_reports SET reporter_type='Person'") | ||
9 | + update("UPDATE comments SET author_type='Person'") | ||
10 | + end | ||
11 | + | ||
12 | + def down | ||
13 | + remove_column :abuse_complaints, :reported_type | ||
14 | + remove_column :abuse_reports, :reporter_type | ||
15 | + remove_column :comments, :author_type | ||
16 | + end | ||
17 | +end |
@@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
1 | +class CreateExternalPerson < ActiveRecord::Migration | ||
2 | + def change | ||
3 | + create_table :external_people do |t| | ||
4 | + t.string :name | ||
5 | + t.string :identifier | ||
6 | + t.string :source | ||
7 | + t.string :email_md5_hash | ||
8 | + t.integer :environment_id | ||
9 | + t.boolean :visible, default: true | ||
10 | + end | ||
11 | + end | ||
12 | +end |
test/unit/comment_test.rb
@@ -23,17 +23,14 @@ class CommentTest < ActiveSupport::TestCase | @@ -23,17 +23,14 @@ class CommentTest < ActiveSupport::TestCase | ||
23 | end | 23 | end |
24 | end | 24 | end |
25 | 25 | ||
26 | - should 'record authenticated author' do | 26 | + should 'record authenticated author polymorphically' do |
27 | c = Comment.new | 27 | c = Comment.new |
28 | - assert_raise ActiveRecord::AssociationTypeMismatch do | ||
29 | - c.author = 1 | ||
30 | - end | ||
31 | - assert_raise ActiveRecord::AssociationTypeMismatch do | ||
32 | - c.author = Profile | ||
33 | - end | ||
34 | assert_nothing_raised do | 28 | assert_nothing_raised do |
35 | c.author = Person.new | 29 | c.author = Person.new |
36 | end | 30 | end |
31 | + assert_nothing_raised do | ||
32 | + c.author = ExternalPerson.new | ||
33 | + end | ||
37 | end | 34 | end |
38 | 35 | ||
39 | should 'record unauthenticated author' do | 36 | should 'record unauthenticated author' do |
test/unit/person_test.rb
@@ -1831,9 +1831,9 @@ class PersonTest < ActiveSupport::TestCase | @@ -1831,9 +1831,9 @@ class PersonTest < ActiveSupport::TestCase | ||
1831 | p1 = fast_create(Person) | 1831 | p1 = fast_create(Person) |
1832 | p2 = fast_create(Person) | 1832 | p2 = fast_create(Person) |
1833 | article = fast_create(Article) | 1833 | article = fast_create(Article) |
1834 | - c1 = fast_create(Comment, :source_id => article.id, :author_id => p1.id) | ||
1835 | - c2 = fast_create(Comment, :source_id => article.id, :author_id => p2.id) | ||
1836 | - c3 = fast_create(Comment, :source_id => article.id, :author_id => p1.id) | 1834 | + c1 = fast_create(Comment, :source_id => article.id, :author_id => p1.id, :author_type => 'Profile') |
1835 | + c2 = fast_create(Comment, :source_id => article.id, :author_id => p2.id, :author_type => 'Profile') | ||
1836 | + c3 = fast_create(Comment, :source_id => article.id, :author_id => p1.id, :author_type => 'Profile') | ||
1837 | 1837 | ||
1838 | assert_equivalent [c1,c3], p1.comments | 1838 | assert_equivalent [c1,c3], p1.comments |
1839 | end | 1839 | end |