Sha256: fc6d896d3007a8005d88155f04eaf20ebfc39be295bba320b6e5d36b8c757e4a

Contents?: true

Size: 1.57 KB

Versions: 60

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module Motor
  module BuildSchema
    module ApplyPermissions
      module_function

      def call(schema, ability)
        schema.map do |model|
          klass = model[:class_name].constantize

          next unless ability.can?(:read, klass)

          model[:associations] = filter_associations(model[:associations], ability)
          model[:columns] = filter_columns(klass, model[:columns], ability)
          model[:actions] = filter_actions(klass, model[:actions], ability)

          model
        end.compact
      end

      def filter_associations(associations, ability)
        associations.select do |assoc|
          model_class = assoc[:model_name].classify.safe_constantize

          model_class && ability.can?(:read, model_class)
        end
      end

      def filter_columns(model, columns, ability)
        columns.map do |column|
          next unless ability.can?(:read, model, column[:name])

          reference_model_name = column.dig(:reference, :model_name)
          model_class = reference_model_name&.classify&.safe_constantize

          next if reference_model_name &&
                  (model_class.nil? || !ability.can?(:read, model_class))

          unless ability.can?(:update, model, column[:name])
            column = column.merge(access_type: BuildSchema::ColumnAccessTypes::READ_ONLY)
          end

          column
        end.compact
      end

      def filter_actions(model, actions, ability)
        actions.select do |action|
          ability.can?(action[:name].to_sym, model)
        end
      end
    end
  end
end

Version data entries

60 entries across 60 versions & 2 rubygems

Version Path
motor-admin-0.3.17 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.16 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.15 lib/motor/build_schema/apply_permissions.rb
mafia-admin-0.3.18 lib/motor/build_schema/apply_permissions.rb
mafia-admin-0.3.17 lib/motor/build_schema/apply_permissions.rb
mafia-admin-0.3.16 lib/motor/build_schema/apply_permissions.rb
mafia-admin-0.3.15 lib/motor/build_schema/apply_permissions.rb
mafia-admin-0.3.14 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.14 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.13 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.12 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.11 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.10 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.9 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.8 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.7 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.6 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.5 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.4 lib/motor/build_schema/apply_permissions.rb
motor-admin-0.3.3 lib/motor/build_schema/apply_permissions.rb