Sha256: fb19ba089c111c8e3b00676a5ffcef0ff07d4d841dc2f1b31d09f57642fd7efc

Contents?: true

Size: 1.98 KB

Versions: 9

Compression:

Stored size: 1.98 KB

Contents

require 'thor'
require 'eucalypt/errors'
require 'eucalypt/helpers'

module Eucalypt
  class SecurityPolicyRole < Thor
    include Thor::Actions
    include Eucalypt::Helpers
    using Colorize

    desc "generate [POLICY] [ROLE]", "Create a new Pundit policy role".colorize(:grey)
    def generate(name, role)
      directory = File.expand_path('.')
      if Eucalypt.app? directory
        # Check for authorization gems
        return unless Gemfile.check(%w[pundit], 'eucalypt security pundit setup', directory)

        # Check for user model
        unless File.exist? File.join(directory, 'app', 'models', 'user.rb')
          Eucalypt::Error.no_user_model
          return
        end

        # Check for role model
        unless File.exist? File.join(directory, 'app', 'models', 'role.rb')
          Eucalypt::Error.no_role_model
          return
        end

        policy = Inflect.new(:policy, name)

        # Check for policy file and policy role model
        policy_file = File.join(directory, 'app', 'policies', policy.file_name)
        policy_role_model = File.join(directory, 'app', 'models', "#{policy.resource}_role.rb")
        unless File.exist?(policy_file) && File.exist?(policy_role_model)
          Eucalypt::Error.no_policy(policy.resource)
          return
        end

        # Add role column to policy roles table
        Dir.chdir(directory) do
          args = %w[migration add column]
          args << "#{policy.resource}_roles"
          args << Inflect.resource(role)
          args << 'boolean'
          args << %w[-o default:false]
          args.flatten!
          Eucalypt::CLI.start(args)
        end
      else
        Eucalypt::Error.wrong_directory
      end
    end

    # def destroy()
    # end

    class << self
      require 'eucalypt/list'
      include Eucalypt::List
      def banner(task, namespace = false, subcommand = true)
        "#{basename} security policy #{task.formatted_usage(self, true, subcommand).split(':').join(' ')}"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
eucalypt-0.3.1 lib/eucalypt/security/namespaces/security-policy-role/cli/security-policy-role.rb
eucalypt-0.3.0 lib/eucalypt/security/namespaces/security-policy-role/cli/security-policy-role.rb
eucalypt-0.2.2 lib/eucalypt/eucalypt-security/namespaces/security-policy-role/cli/security-policy-role.rb
eucalypt-0.2.1 lib/eucalypt/eucalypt-security/namespaces/security-policy-role/cli/security-policy-role.rb
eucalypt-0.2.0 lib/eucalypt/eucalypt-security/namespaces/security-policy-role/cli/security-policy-role.rb
eucalypt-0.1.3 lib/eucalypt/eucalypt-security/namespaces/security-policy-role/cli/security-policy-role.rb
eucalypt-0.1.2 lib/eucalypt/eucalypt-security/namespaces/security-policy-role/cli/security-policy-role.rb
eucalypt-0.1.1 lib/eucalypt/eucalypt-security/namespaces/security-policy-role/cli/security-policy-role.rb
eucalypt-0.1.0 lib/eucalypt/eucalypt-security/namespaces/security-policy-role/cli/security-policy-role.rb