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



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/protector/adapters/sequel/model.rb', line 74

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



88
89
90
# File 'lib/protector/adapters/sequel/model.rb', line 88

def _associated_dataset(*args)
  super.restrict!(@protector_subject)
end

#_dataset(*args) ⇒ Object

This is used whenever we call counters and existance checkers



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

def _dataset(*args)
  super.restrict!(@protector_subject)
end

#before_destroyObject

Destroy availability check



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

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

#creatable?Boolean

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

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/protector/adapters/sequel/model.rb', line 41

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)


53
54
55
# File 'lib/protector/adapters/sequel/model.rb', line 53

def destroyable?
  protector_meta.destroyable?
end

#protector_metaObject

Storage for DSL::Meta::Box



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

def protector_meta
  @protector_meta ||= self.class.protector_meta.evaluate(
    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)


47
48
49
50
# File 'lib/protector/adapters/sequel/model.rb', line 47

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

#validateObject

Basic security validations



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

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)


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

def visible?
  protector_meta.relation.where(pk_hash).any?
end