From a56c4cadafcda273706203d2bc12145a8294409b Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Fri, 26 Apr 2013 13:38:29 +0300 Subject: object-specific actions --- server.lua | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'server.lua') diff --git a/server.lua b/server.lua index 6be07da..ea5d12c 100644 --- a/server.lua +++ b/server.lua @@ -120,7 +120,7 @@ return function(env) if stringy.startswith(path, '/config/') then path = string.sub(path, 8, -1) - local parent, name + local parent, name, res if path ~= '/' then parent = txn:search(acf.path.parent(path)) name = acf.path.name(path) @@ -128,7 +128,8 @@ return function(env) if method == 'GET' then local obj = txn:search(path) - local res + + if type(obj) == 'function' then return 404 end if isinstance(obj, mnode.TreeNode) then if not mnode.has_permission(obj, user, 'read') then @@ -161,16 +162,20 @@ return function(env) if method == 'POST' then local obj = txn:search(path) - if not acf.object.isinstance(obj, acf.model.set.Set) then - -- TODO invoke model-specific actions - return 405 - end - if not mnode.has_permission(obj, user, 'create') then - return 403 - end + if acf.object.isinstance(obj, acf.model.set.Set) then + if not mnode.has_permission(obj, user, 'create') then + return 403 + end + acf.model.set.add(obj, data) - acf.model.set.add(obj, data) + elseif type(obj) == 'function' then + if not mnode.has_permission(parent, user, name) then + return 403 + end + res = obj(data) + + else return 405 end else local obj = parent[name] @@ -200,7 +205,7 @@ return function(env) end txn:commit() - return 205 + return res == nil and 205 or 200, nil, res end if path == '/' then -- cgit v1.2.3