Module: Protector::Adapters::Sequel::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/protector/adapters/sequel/model.rb

Overview

Patches Sequel::Model

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object

Security-checking attributes reader

Parameters:

  • name (Symbol)

    Name of attribute to read



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/protector/adapters/sequel/model.rb', line 80

def [](name)
  if (
    !protector_subject? || 
    name == self.class.primary_key ||
    (self.class.primary_key.is_a?(Array) && self.class.primary_key.include?(name)) ||
    protector_meta.readable?(name.to_s)
  )
    @values[name]
  else
    nil
  end
end

#_associated_dataset(*args) ⇒ Object

This is used whenever we fetch data



94
95
96
97
# File 'lib/protector/adapters/sequel/model.rb', line 94

def _associated_dataset(*args)
  return super unless protector_subject?
  super.restrict!(protector_subject)
end

#_dataset(*args) ⇒ Object

This is used whenever we call counters and existance checkers



100
101
102
103
# File 'lib/protector/adapters/sequel/model.rb', line 100

def _dataset(*args)
  return super unless protector_subject?
  super.restrict!(protector_subject)
end

#before_destroyObject

Destroy availability check



72
73
74
75
# File 'lib/protector/adapters/sequel/model.rb', line 72

def before_destroy
  return false if protector_subject? && !destroyable?
  super
end

#can?(action, field = false) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/protector/adapters/sequel/model.rb', line 59

def can?(action, field=false)
  protector_meta.can?(action, field)
end

#creatable?Boolean

Checks if current model can be created in the context of current subject

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/protector/adapters/sequel/model.rb', line 43

def creatable?
  fields = HashWithIndifferentAccess[keys.map{|x| [x.to_s, @values[x]]}]
  protector_meta.creatable?(fields)
end

#destroyable?Boolean

Checks if current model can be destroyed in the context of current subject

Returns:

  • (Boolean)


55
56
57
# File 'lib/protector/adapters/sequel/model.rb', line 55

def destroyable?
  protector_meta.destroyable?
end

#protector_metaObject

Storage for DSL::Meta::Box



26
27
28
29
30
31
32
33
34
# File 'lib/protector/adapters/sequel/model.rb', line 26

def protector_meta
  @protector_meta ||= self.class.protector_meta.evaluate(
    Protector::Adapters::Sequel,
    self.class,
    protector_subject,
    self.class.columns,
    self
  )
end

#updatable?Boolean

Checks if current model can be updated in the context of current subject

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/protector/adapters/sequel/model.rb', line 49

def updatable?
  fields = HashWithIndifferentAccess[changed_columns.map{|x| [x.to_s, @values[x]]}]
  protector_meta.updatable?(fields)
end

#validateObject

Basic security validations



64
65
66
67
68
69
# File 'lib/protector/adapters/sequel/model.rb', line 64

def validate
  super
  return unless protector_subject?
  method = new? ? :creatable? : :updatable?
  errors.add(:base, I18n.t('protector.invalid')) unless __send__(method)
end

#visible?Boolean

Checks if current model can be selected in the context of current subject

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/protector/adapters/sequel/model.rb', line 37

def visible?
  return true unless protector_meta.scoped?
  protector_meta.relation.where(pk_hash).any?
end