Class: HaveAPI::Authorization
- Inherits:
-
Object
- Object
- HaveAPI::Authorization
- Defined in:
- lib/haveapi/authorization.rb
Instance Method Summary collapse
- #allow ⇒ Object
-
#authorized?(user, path_params) ⇒ Boolean
Returns true if user is authorized.
- #deny ⇒ Object
- #filter_input(input, params) ⇒ Object
- #filter_output(output, params, format = false) ⇒ Object
-
#initialize(&block) ⇒ Authorization
constructor
A new instance of Authorization.
-
#input(whitelist: nil, blacklist: nil) ⇒ Object
Restrict parameters client can set/change.
-
#output(whitelist: nil, blacklist: nil) ⇒ Object
Restrict parameters client can retrieve.
- #prepend_block(block) ⇒ Object
-
#restrict(**kwargs) ⇒ Object
Apply restrictions on query which selects objects from database.
- #restrictions ⇒ Object
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
#allow ⇒ Object
51 52 53 |
# File 'lib/haveapi/authorization.rb', line 51 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.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/haveapi/authorization.rb', line 9 def (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 |
#deny ⇒ Object
55 56 57 |
# File 'lib/haveapi/authorization.rb', line 55 def deny throw(:rule, false) end |
#filter_input(input, params) ⇒ Object
69 70 71 |
# File 'lib/haveapi/authorization.rb', line 69 def filter_input(input, params) filter_inner(input, @input, params, false) end |
#filter_output(output, params, format = false) ⇒ Object
73 74 75 |
# File 'lib/haveapi/authorization.rb', line 73 def filter_output(output, params, format = false) filter_inner(output, @output, params, format) end |
#input(whitelist: nil, blacklist: nil) ⇒ Object
Restrict parameters client can set/change.
34 35 36 37 38 39 |
# File 'lib/haveapi/authorization.rb', line 34 def input(whitelist: nil, blacklist: nil) @input = { whitelist: whitelist, blacklist: blacklist, } end |
#output(whitelist: nil, blacklist: nil) ⇒ Object
Restrict parameters client can retrieve.
44 45 46 47 48 49 |
# File 'lib/haveapi/authorization.rb', line 44 def output(whitelist: nil, blacklist: nil) @output = { whitelist: whitelist, blacklist: blacklist, } end |
#prepend_block(block) ⇒ Object
21 22 23 |
# File 'lib/haveapi/authorization.rb', line 21 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.
27 28 29 |
# File 'lib/haveapi/authorization.rb', line 27 def restrict(**kwargs) @restrict << kwargs end |
#restrictions ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/haveapi/authorization.rb', line 59 def restrictions ret = {} @restrict.each do |r| ret.update(r) end ret end |