@@ -20,34 +20,8 @@ local User = class.create(function(user)
2020end )
2121
2222local sessionFilter = HttpFilter .session ()
23- local filter = HttpFilter .byPath (HttpFilter .multiple (sessionFilter , HttpFilter :new (function (_ , exchange )
24- local request = exchange :getRequest ()
25- local method = request :getMethod ()
26- if method == ' GET' or method == ' HEAD' then
27- return
28- end
29- local path = request :getTargetPath ()
30- local session = exchange :getSession ()
31- local permission = ' r'
32- if session .attributes .user then
33- permission = session .attributes .user .permission
34- end
35- if string.match (path , ' ^/things' ) then
36- if permission > ' r' then
37- return
38- end
39- elseif string.match (path , ' ^/engine/admin/' ) then
40- if permission > ' rwc' then
41- return
42- end
43- elseif permission > ' rw' or path == ' /login' or path == ' /logout' or string.match (path , ' ^/user' ) then
44- return
45- end
46- HttpExchange .forbidden (exchange )
47- return false
48- end ))):exclude (' ^/static' )
4923
50- local contexts , base64 , md , userMap
24+ local contexts , filter , base64 , md , userMap
5125
5226local function cleanup (server )
5327 if contexts then
@@ -56,7 +30,10 @@ local function cleanup(server)
5630 end
5731 end
5832 contexts = {}
59- server :removeFilter (filter )
33+ if filter then
34+ server :removeFilter (filter )
35+ filter = nil
36+ end
6037 userMap = {}
6138 base64 = Codec .getInstance (' base64' )
6239 md = MessageDigest .getInstance (' SHA-1' )
@@ -114,6 +91,38 @@ extension:subscribeEvent('startup', function()
11491 HttpExchange .badRequest (exchange )
11592 end
11693 end )
94+ local userFilter = HttpFilter :new (function (_ , exchange )
95+ local request = exchange :getRequest ()
96+ local method = request :getMethod ()
97+ if method == ' GET' or method == ' HEAD' then
98+ return
99+ end
100+ local path = request :getTargetPath ()
101+ local session = exchange :getSession ()
102+ local permission = configuration .defaultPermission or ' '
103+ if session .attributes .user then
104+ permission = session .attributes .user .permission
105+ end
106+ if string.match (path , ' ^/things' ) then
107+ if permission > ' r' then
108+ return
109+ end
110+ elseif string.match (path , ' ^/engine/admin/' ) then
111+ if permission > ' rwc' then
112+ return
113+ end
114+ elseif permission > ' rw' or path == ' /login' or path == ' /logout' or string.match (path , ' ^/user' ) then
115+ return
116+ end
117+ HttpExchange .forbidden (exchange )
118+ return false
119+ end )
120+ local filters = HttpFilter .multiple (sessionFilter , userFilter )
121+ if configuration .login then
122+ local redirect = extension :require (' users.login-redirect' , true )
123+ filters :addFilter (redirect )
124+ end
125+ filter = HttpFilter .byPath (filters ):exclude (' ^/static' )
117126 server :addFilter (filter )
118127 engine :onExtension (' web-base' , function (webBaseExtension )
119128 webBaseExtension :registerAddonExtension (extension , ' user.js' )
0 commit comments