GRN2-260: Added the ability to merge user accounts (#938)

* Added the ability to merge user accounts

* Styling fixes
This commit is contained in:
Ahmad Farhat
2020-02-14 10:24:06 -05:00
committed by GitHub
parent 31258272c2
commit 005c738e4d
13 changed files with 222 additions and 5 deletions

View File

@ -24,7 +24,7 @@ class AdminsController < ApplicationController
include Rolify
include Populator
manage_users = [:edit_user, :promote, :demote, :ban_user, :unban_user, :approve, :reset]
manage_users = [:edit_user, :promote, :demote, :ban_user, :unban_user, :approve, :reset, :merge_user]
manage_deleted_users = [:undelete]
authorize_resource class: false
before_action :find_user, only: manage_users
@ -41,6 +41,8 @@ class AdminsController < ApplicationController
@role = params[:role] ? Role.find_by(name: params[:role], provider: @user_domain) : nil
@tab = params[:tab] || "active"
@user_list = merge_user_list
@pagy, @users = pagy(manage_users_list)
end
@ -140,6 +142,45 @@ class AdminsController < ApplicationController
redirect_to redirect_path, flash: { success: I18n.t("administrator.flash.reset_password") }
end
# POST /admins/merge/:user_uid
def merge_user
begin
# Get uid of user that will be merged into the other account
uid_to_merge = params[:merge]
logger.info "#{current_user.uid} is attempting to merge #{uid_to_merge} into #{@user.uid}"
# Check to make sure the 2 users are unique
raise "Can not merge the user into themself" if uid_to_merge == @user.uid
# Find user to merge
user_to_merge = User.find_by(uid: uid_to_merge)
# Move over user's rooms
user_to_merge.rooms.each do |room|
room.owner = @user
room.name = "(#{I18n.t('merged')}) #{room.name}"
room.save!
end
# Reload user to update merge rooms
user_to_merge.reload
# Delete merged user
user_to_merge.destroy(true)
rescue => e
logger.info "Failed to merge #{uid_to_merge} into #{@user.uid}: #{e}"
flash[:alert] = I18n.t("administrator.flash.merge_fail")
else
logger.info "#{current_user.uid} successfully merged #{uid_to_merge} into #{@user.uid}"
flash[:success] = I18n.t("administrator.flash.merge_success")
end
redirect_to admins_path
end
# SITE SETTINGS
# POST /admins/update_settings

View File

@ -77,7 +77,18 @@ module Populator
roles_can_appear << role.name if role.get_permission("can_appear_in_share_list") && role.name != "super_admin"
end
initial_list = User.where.not(uid: current_user.uid).with_highest_priority_role(roles_can_appear)
initial_list = User.where.not(uid: current_user.uid)
.without_role(:pending)
.without_role(:denied)
.with_highest_priority_role(roles_can_appear)
return initial_list unless Rails.configuration.loadbalanced_configuration
initial_list.where(provider: @user_domain)
end
# Returns a list of users that can merged into another user
def merge_user_list
initial_list = User.where.not(uid: current_user.uid).without_role(:super_admin)
return initial_list unless Rails.configuration.loadbalanced_configuration
initial_list.where(provider: @user_domain)