Sha256: 9107fa332c459a23a82174aff829d1192a1d20a7c8540c01ab4104a4a04eb44a

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module Lita
  module Handlers
    class Authorization < Handler
      route(/^auth\s+add/, to: :add, command: true)
      route(/^auth\s+remove/, to: :remove, command: true)

      def self.help
        robot_name = Lita.config.robot.name

        {
          "#{robot_name}: auth add USER GROUP" => "Add USER to authorization group GROUP. Requires admin privileges.",
          "#{robot_name}: auth remove USER GROUP" => "Remove USER from authorization group GROUP. Requires admin privileges."
        }
      end

      def add(matches)
        return unless valid_message?

        if Lita::Authorization.add_user_to_group(@user, @group)
          reply "#{@user.name} was added to #{@group}."
        else
          reply "#{@user.name} was already in #{@group}."
        end
      end

      def remove(matches)
        return unless valid_message?

        if Lita::Authorization.remove_user_from_group(@user, @group)
          reply "#{@user.name} was removed from #{@group}."
        else
          reply "#{@user.name} was not in #{@group}."
        end
      end

      private

      def valid_message?
        command, identifier, @group = args

        unless identifier && @group
          reply "Format: #{robot.name} auth add USER GROUP"
          return
        end

        @user = User.find_by_id(identifier)
        @user = User.find_by_name(identifier) unless @user

        unless @user
          reply %{No user was found with the identifier "#{identifier}".}
          return
        end

        true
      end
    end

    Lita.register_handler(Authorization)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lita-1.1.0 lib/lita/handlers/authorization.rb
lita-1.0.0 lib/lita/handlers/authorization.rb