jbox-web/redmine_git_hosting

View on GitHub
lib/redmine_git_hosting/patches/users_controller_patch.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

require_dependency 'users_controller'

module RedmineGitHosting
  module Patches
    module UsersControllerPatch
      include RedmineGitHosting::GitoliteAccessor::Methods

      def self.prepended(base)
        base.class_eval do
          helper :gitolite_public_keys
          helper :git_hosting
          helper :git_hosting_users
        end
      end

      def edit
        # Set public key values for view
        set_public_key_values
        super
      end

      def update
        # Set public key values for view (in case of invalid form)
        set_public_key_values
        super

        # Update projects if needed
        update_projects if @user.status_has_changed?
      end

      def destroy
        # Build SSH keys list before user destruction.
        ssh_keys_list = ssh_keys_to_destroy

        # Destroy user
        super

        # Destroy SSH keys
        destroy_ssh_keys ssh_keys_list
      end

      private

      # Add in values for viewing public keys:
      def set_public_key_values
        @gitolite_user_keys   = @user.gitolite_public_keys.user_key.sorted
        @gitolite_deploy_keys = @user.gitolite_public_keys.deploy_key.sorted
      end

      def update_projects
        gitolite_accessor.update_projects projects_to_update,
                                          message: "Status of '#{@user.login}' has changed, update projects"
      end

      def projects_to_update
        @user.gitolite_projects.map(&:id)
      end

      def ssh_keys_to_destroy
        @user.gitolite_public_keys.map(&:data_for_destruction)
      end

      def destroy_ssh_keys(ssh_keys_list)
        RedmineGitHosting.logger.info "User '#{@user.login}' has been deleted from Redmine, delete membership and SSH keys !"
        ssh_keys_list.each do |ssh_key|
          gitolite_accessor.destroy_ssh_key ssh_key
        end
      end
    end
  end
end

unless UsersController.included_modules.include? RedmineGitHosting::Patches::UsersControllerPatch
  UsersController.prepend RedmineGitHosting::Patches::UsersControllerPatch
end