Class: HaveAPI::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/authorization.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Authorization

Returns a new instance of Authorization.



3
4
5
# File 'lib/haveapi/authorization.rb', line 3

def initialize(&block)
  @blocks = [block]
end

Instance Method Details

#allowObject



56
57
58
# File 'lib/haveapi/authorization.rb', line 56

def allow
  throw(:rule, true)
end

#authorized?(user, path_params) ⇒ Boolean

Returns true if user is authorized. Block must call allow to authorize user, default rule is deny.

Returns:



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/haveapi/authorization.rb', line 14

def authorized?(user, path_params)
  @restrict = []

  catch(:rule) do
    @blocks.each do |block|
      instance_exec(user, path_params, &block)
    end

    deny # will not be called if some block throws allow
  end
end

#denyObject



60
61
62
# File 'lib/haveapi/authorization.rb', line 60

def deny
  throw(:rule, false)
end

#filter_input(input, params) ⇒ Object



74
75
76
# File 'lib/haveapi/authorization.rb', line 74

def filter_input(input, params)
  filter_inner(input, @input, params, false)
end

#filter_output(output, params, format = false) ⇒ Object



78
79
80
# File 'lib/haveapi/authorization.rb', line 78

def filter_output(output, params, format = false)
  filter_inner(output, @output, params, format)
end

#initialize_clone(other) ⇒ Object



7
8
9
10
# File 'lib/haveapi/authorization.rb', line 7

def initialize_clone(other)
  super
  @blocks = other.instance_variable_get('@blocks').clone
end

#input(whitelist: nil, blacklist: nil) ⇒ Object

Restrict parameters client can set/change.

Parameters:

  • whitelist (Array<Symbol>) (defaults to: nil)

    allow only listed parameters

  • blacklist (Array<Symbol>) (defaults to: nil)

    allow all parameters except listed ones



39
40
41
42
43
44
# File 'lib/haveapi/authorization.rb', line 39

def input(whitelist: nil, blacklist: nil)
  @input = {
    whitelist:,
    blacklist:
  }
end

#output(whitelist: nil, blacklist: nil) ⇒ Object

Restrict parameters client can retrieve.

Parameters:

  • whitelist (Array<Symbol>) (defaults to: nil)

    allow only listed parameters

  • blacklist (Array<Symbol>) (defaults to: nil)

    allow all parameters except listed ones



49
50
51
52
53
54
# File 'lib/haveapi/authorization.rb', line 49

def output(whitelist: nil, blacklist: nil)
  @output = {
    whitelist:,
    blacklist:
  }
end

#prepend_block(block) ⇒ Object



26
27
28
# File 'lib/haveapi/authorization.rb', line 26

def prepend_block(block)
  @blocks.insert(0, block)
end

#restrict(**kwargs) ⇒ Object

Apply restrictions on query which selects objects from database. Most common usage is restrict user to access only objects he owns.



32
33
34
# File 'lib/haveapi/authorization.rb', line 32

def restrict(**kwargs)
  @restrict << kwargs
end

#restrictionsObject



64
65
66
67
68
69
70
71
72
# File 'lib/haveapi/authorization.rb', line 64

def restrictions
  ret = {}

  @restrict.each do |r|
    ret.update(r)
  end

  ret
end