From a6ed6845c9cab3b314ce6434b851cc507a71ee62 Mon Sep 17 00:00:00 2001 From: chandrn7 Date: Wed, 9 Mar 2022 06:07:35 -0500 Subject: Allow login through OpenID Connect (#16221) * added OpenID Connect as an SSO option * minor fixes * added comments, removed an option that shouldn't be set * fixed Gemfile.lock * added newline to end of Gemfile.lock * removed tab from Gemfile.lock * remove chomp * codeclimate changes and small name change to make function's purpose clearer * codeclimate fix * added SSO buttons to /about page * minor refactor * minor style change * removed spurious change * removed unecessary conditional from ensure_valid_username and added support for auth.info.name in user_params_from_auth * minor changes --- app/models/concerns/omniauthable.rb | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'app/models/concerns') diff --git a/app/models/concerns/omniauthable.rb b/app/models/concerns/omniauthable.rb index 791a94911..a90d5d888 100644 --- a/app/models/concerns/omniauthable.rb +++ b/app/models/concerns/omniauthable.rb @@ -13,7 +13,7 @@ module Omniauthable Devise.omniauth_configs.keys end - def email_verified? + def email_present? email && email !~ TEMP_EMAIL_REGEX end end @@ -40,16 +40,14 @@ module Omniauthable end def create_for_oauth(auth) - # Check if the user exists with provided email if the provider gives us a - # verified email. If no verified email was provided or the user already - # exists, we assign a temporary email and ask the user to verify it on + # Check if the user exists with provided email. If no email was provided, + # we assign a temporary email and ask the user to verify it on # the next step via Auth::SetupController.show strategy = Devise.omniauth_configs[auth.provider.to_sym].strategy assume_verified = strategy&.security&.assume_email_is_verified - email_is_verified = auth.info.verified || auth.info.verified_email || assume_verified + email_is_verified = auth.info.verified || auth.info.verified_email || auth.info.email_verified || assume_verified email = auth.info.verified_email || auth.info.email - email = nil unless email_is_verified user = User.find_by(email: email) if email_is_verified @@ -58,7 +56,7 @@ module Omniauthable user = User.new(user_params_from_auth(email, auth)) user.account.avatar_remote_url = auth.info.image if /\A#{URI::DEFAULT_PARSER.make_regexp(%w(http https))}\z/.match?(auth.info.image) - user.skip_confirmation! + user.skip_confirmation! if email_is_verified user.save! user end @@ -71,8 +69,8 @@ module Omniauthable agreement: true, external: true, account_attributes: { - username: ensure_unique_username(auth.uid), - display_name: auth.info.full_name || [auth.info.first_name, auth.info.last_name].join(' '), + username: ensure_unique_username(ensure_valid_username(auth.uid)), + display_name: auth.info.full_name || auth.info.name || [auth.info.first_name, auth.info.last_name].join(' '), }, } end @@ -88,5 +86,12 @@ module Omniauthable username end + + def ensure_valid_username(starting_username) + starting_username = starting_username.split('@')[0] + temp_username = starting_username.gsub(/[^a-z0-9_]+/i, '') + validated_username = temp_username.truncate(30, omission: '') + validated_username + end end end -- cgit From 642528f45513acb823a7291f32fb07377bd2b621 Mon Sep 17 00:00:00 2001 From: Claire Date: Sat, 12 Mar 2022 08:33:11 +0100 Subject: Update fix-duplicates maintenance task (#17731) * Update fix-duplicates task to 2022_02_10_153119 Also add support for Appeal to AccountMerging#merge_with! * Update fix-duplicates task to 2022_03_07_094650 * Update fix-duplicates task to 2022_03_09_213005 * Update fix-duplicates task to 2022_03_07_083603 * Update fix-duplicates task to 2022_03_10_060626 * Update fix-duplicates script to 2022_03_07_083603 * Update fix-duplicates task to 2022_03_10_060706 * Update fix-duplicates task to 2022_03_10_060959 * Silence CodeClimate --- .rubocop.yml | 3 +++ app/models/concerns/account_merging.rb | 7 +++++- lib/mastodon/maintenance_cli.rb | 41 +++++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 9 deletions(-) (limited to 'app/models/concerns') diff --git a/.rubocop.yml b/.rubocop.yml index 68634e9e3..4948aea5a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -29,6 +29,9 @@ Layout/EmptyLineAfterMagicComment: Layout/EmptyLineAfterGuardClause: Enabled: false +Layout/EmptyLineBetweenDefs: + AllowAdjacentOneLineDefs: true + Layout/EmptyLinesAroundAttributeAccessor: Enabled: true diff --git a/app/models/concerns/account_merging.rb b/app/models/concerns/account_merging.rb index 119773e6b..8161761fb 100644 --- a/app/models/concerns/account_merging.rb +++ b/app/models/concerns/account_merging.rb @@ -15,7 +15,8 @@ module AccountMerging Status, StatusPin, MediaAttachment, Poll, Report, Tombstone, Favourite, Follow, FollowRequest, Block, Mute, AccountModerationNote, AccountPin, AccountStat, ListAccount, - PollVote, Mention, AccountDeletionRequest, AccountNote, FollowRecommendationSuppression + PollVote, Mention, AccountDeletionRequest, AccountNote, FollowRecommendationSuppression, + Appeal ] owned_classes.each do |klass| @@ -47,6 +48,10 @@ module AccountMerging record.update_attribute(:reference_account_id, id) end + Appeal.where(account_warning_id: other_account.id).find_each do |record| + record.update_attribute(:account_warning_id, id) + end + # Some follow relationships have moved, so the cache is stale Rails.cache.delete_matched("followers_hash:#{id}:*") Rails.cache.delete_matched("relationships:#{id}:*") diff --git a/lib/mastodon/maintenance_cli.rb b/lib/mastodon/maintenance_cli.rb index 1e047d96c..33beec236 100644 --- a/lib/mastodon/maintenance_cli.rb +++ b/lib/mastodon/maintenance_cli.rb @@ -13,8 +13,8 @@ module Mastodon true end - MIN_SUPPORTED_VERSION = 2019_10_01_213028 - MAX_SUPPORTED_VERSION = 2022_01_18_183123 + MIN_SUPPORTED_VERSION = 2019_10_01_213028 # rubocop:disable Style/NumericLiterals + MAX_SUPPORTED_VERSION = 2022_03_10_060959 # rubocop:disable Style/NumericLiterals # Stubs to enjoy ActiveRecord queries while not depending on a particular # version of the code/database @@ -44,6 +44,7 @@ module Mastodon class WebauthnCredential < ApplicationRecord; end class FollowRecommendationSuppression < ApplicationRecord; end class CanonicalEmailBlock < ApplicationRecord; end + class Appeal < ApplicationRecord; end class PreviewCard < ApplicationRecord self.inheritance_column = false @@ -92,6 +93,7 @@ module Mastodon owned_classes << AccountNote if ActiveRecord::Base.connection.table_exists?(:account_notes) owned_classes << FollowRecommendationSuppression if ActiveRecord::Base.connection.table_exists?(:follow_recommendation_suppressions) owned_classes << AccountIdentityProof if ActiveRecord::Base.connection.table_exists?(:account_identity_proofs) + owned_classes << Appeal if ActiveRecord::Base.connection.table_exists?(:appeals) owned_classes.each do |klass| klass.where(account_id: other_account.id).find_each do |record| @@ -121,6 +123,12 @@ module Mastodon record.update_attribute(:reference_account_id, id) end end + + if ActiveRecord::Base.connection.table_exists?(:appeals) + Appeal.where(account_warning_id: other_account.id).find_each do |record| + record.update_attribute(:account_warning_id, id) + end + end end end @@ -199,7 +207,7 @@ module Mastodon end @prompt.say 'Restoring index_accounts_on_username_and_domain_lower…' - if ActiveRecord::Migrator.current_version < 20200620164023 + if ActiveRecord::Migrator.current_version < 20200620164023 # rubocop:disable Style/NumericLiterals ActiveRecord::Base.connection.add_index :accounts, 'lower (username), lower(domain)', name: 'index_accounts_on_username_and_domain_lower', unique: true else ActiveRecord::Base.connection.add_index :accounts, "lower (username), COALESCE(lower(domain), '')", name: 'index_accounts_on_username_and_domain_lower', unique: true @@ -242,7 +250,7 @@ module Mastodon end end - if ActiveRecord::Migrator.current_version < 20220118183010 + if ActiveRecord::Migrator.current_version < 20220118183010 # rubocop:disable Style/NumericLiterals ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM users WHERE remember_token IS NOT NULL GROUP BY remember_token HAVING count(*) > 1").each do |row| users = User.where(id: row['ids'].split(',')).sort_by(&:updated_at).reverse.drop(1) @prompt.warn "Unsetting remember token for those accounts: #{users.map(&:account).map(&:acct).join(', ')}" @@ -266,7 +274,12 @@ module Mastodon ActiveRecord::Base.connection.add_index :users, ['confirmation_token'], name: 'index_users_on_confirmation_token', unique: true ActiveRecord::Base.connection.add_index :users, ['email'], name: 'index_users_on_email', unique: true ActiveRecord::Base.connection.add_index :users, ['remember_token'], name: 'index_users_on_remember_token', unique: true if ActiveRecord::Migrator.current_version < 20220118183010 - ActiveRecord::Base.connection.add_index :users, ['reset_password_token'], name: 'index_users_on_reset_password_token', unique: true + + if ActiveRecord::Migrator.current_version < 20220310060641 # rubocop:disable Style/NumericLiterals + ActiveRecord::Base.connection.add_index :users, ['reset_password_token'], name: 'index_users_on_reset_password_token', unique: true + else + ActiveRecord::Base.connection.add_index :users, ['reset_password_token'], name: 'index_users_on_reset_password_token', unique: true, where: 'reset_password_token IS NOT NULL', opclass: :text_pattern_ops + end end def deduplicate_account_domain_blocks! @@ -325,7 +338,11 @@ module Mastodon end @prompt.say 'Restoring conversations indexes…' - ActiveRecord::Base.connection.add_index :conversations, ['uri'], name: 'index_conversations_on_uri', unique: true + if ActiveRecord::Migrator.current_version < 20220307083603 # rubocop:disable Style/NumericLiterals + ActiveRecord::Base.connection.add_index :conversations, ['uri'], name: 'index_conversations_on_uri', unique: true + else + ActiveRecord::Base.connection.add_index :conversations, ['uri'], name: 'index_conversations_on_uri', unique: true, where: 'uri IS NOT NULL', opclass: :text_pattern_ops + end end def deduplicate_custom_emojis! @@ -438,7 +455,11 @@ module Mastodon end @prompt.say 'Restoring media_attachments indexes…' - ActiveRecord::Base.connection.add_index :media_attachments, ['shortcode'], name: 'index_media_attachments_on_shortcode', unique: true + if ActiveRecord::Migrator.current_version < 20220310060626 # rubocop:disable Style/NumericLiterals + ActiveRecord::Base.connection.add_index :media_attachments, ['shortcode'], name: 'index_media_attachments_on_shortcode', unique: true + else + ActiveRecord::Base.connection.add_index :media_attachments, ['shortcode'], name: 'index_media_attachments_on_shortcode', unique: true, where: 'shortcode IS NOT NULL', opclass: :text_pattern_ops + end end def deduplicate_preview_cards! @@ -467,7 +488,11 @@ module Mastodon end @prompt.say 'Restoring statuses indexes…' - ActiveRecord::Base.connection.add_index :statuses, ['uri'], name: 'index_statuses_on_uri', unique: true + if ActiveRecord::Migrator.current_version < 20220310060706 # rubocop:disable Style/NumericLiterals + ActiveRecord::Base.connection.add_index :statuses, ['uri'], name: 'index_statuses_on_uri', unique: true + else + ActiveRecord::Base.connection.add_index :statuses, ['uri'], name: 'index_statuses_on_uri', unique: true, where: 'uri IS NOT NULL', opclass: :text_pattern_ops + end end def deduplicate_tags! -- cgit