diff --git a/app/controllers/internal/users_controller.rb b/app/controllers/internal/users_controller.rb index f292b6bba..55cd27542 100644 --- a/app/controllers/internal/users_controller.rb +++ b/app/controllers/internal/users_controller.rb @@ -75,6 +75,32 @@ class Internal::UsersController < Internal::ApplicationController redirect_to "/internal/users/#{@user.id}/edit" end + def remove_identity + identity = Identity.find(user_params[:identity_id]) + @user = identity.user + begin + BackupData.backup!(identity) + identity.delete + @user.update("#{identity.provider}_username" => nil) + flash[:success] = "The #{identity.provider.capitalize} identity was successfully deleted and backed up." + rescue StandardError => e + flash[:error] = e.message + end + redirect_to "/internal/users/#{@user.id}/edit" + end + + def recover_identity + backup = BackupData.find(user_params[:backup_data_id]) + @user = backup.instance_user + begin + identity = backup.recover! + flash[:success] = "The #{identity.provider} identity was successfully recovered, and the backup was removed." + rescue StandardError => e + flash[:error] = e.message + end + redirect_to "/internal/users/#{@user.id}/edit" + end + private def manage_credits @@ -121,7 +147,7 @@ class Internal::UsersController < Internal::ApplicationController new_note note_for_current_role user_status pro merge_user_id add_credits remove_credits add_org_credits remove_org_credits ghostify - organization_id + organization_id identity_id backup_data_id ] params.require(:user).permit(allowed_params) end diff --git a/app/models/backup_data.rb b/app/models/backup_data.rb new file mode 100644 index 000000000..3665bbf5a --- /dev/null +++ b/app/models/backup_data.rb @@ -0,0 +1,15 @@ +class BackupData < ApplicationRecord + belongs_to :instance, polymorphic: true + belongs_to :instance_user, class_name: "User" + validates :instance_id, :instance_type, :json_data, presence: true + + def self.backup!(instance) + BackupData.create!(instance_type: instance.class.name, instance_id: instance.id, instance_user_id: instance.user_id, json_data: instance.attributes) + end + + def recover! + instance = instance_type.constantize.create!(json_data.to_h) + destroy! + instance + end +end diff --git a/app/models/identity.rb b/app/models/identity.rb index ed5a2b082..02746769f 100644 --- a/app/models/identity.rb +++ b/app/models/identity.rb @@ -1,5 +1,6 @@ class Identity < ApplicationRecord belongs_to :user + has_many :backup_data, as: :instance, class_name: "BackupData", dependent: :destroy validates :uid, :provider, presence: true validates :uid, uniqueness: { scope: :provider }, if: proc { |i| i.uid_changed? || i.provider_changed? } validates :user_id, uniqueness: { scope: :provider }, if: proc { |i| i.user_id_changed? || i.provider_changed? } diff --git a/app/models/user.rb b/app/models/user.rb index 2072e5ec6..ff7535669 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -44,6 +44,7 @@ class User < ApplicationRecord has_many :classified_listings has_many :poll_votes has_many :poll_skips + has_many :backup_data, foreign_key: "instance_user_id", inverse_of: :instance_user, class_name: "BackupData" mount_uploader :profile_image, ProfileImageUploader diff --git a/app/views/internal/users/edit.html.erb b/app/views/internal/users/edit.html.erb index ea4f0380c..e56eda72a 100644 --- a/app/views/internal/users/edit.html.erb +++ b/app/views/internal/users/edit.html.erb @@ -39,6 +39,54 @@ <% end %> <%= render "notes" %> +
Removing a social account identity can solve certain sign in issues, for example:
+<%= identity.provider.capitalize %> UID: <%= identity.uid %> - Username: <%= identity.auth_data_dump["info"]["nickname"] %>
+ <%= f.submit "Delete #{identity.provider.capitalize} Identity" %> + <% end %> + <% end %> +To merge a duplicate account, make sure you are currently on the page of the user you want to KEEP. Below, add the user id of the account to merge information from, and delete.
diff --git a/app/views/users/_account.html.erb b/app/views/users/_account.html.erb index bc9193e27..b5a9e1816 100644 --- a/app/views/users/_account.html.erb +++ b/app/views/users/_account.html.erb @@ -1,4 +1,4 @@ -<% unless @user.identities.exists?(provider: 'github') %> +<% unless @user.identities.exists?(provider: "github") %>