Skip to content

Commit b3cae80

Browse files
Add users default permission
1 parent 26364f3 commit b3cae80

File tree

4 files changed

+70
-40
lines changed

4 files changed

+70
-40
lines changed

extensions/https/https.lua

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ local extension = ...
22

33
local logger = require('jls.lang.logger')
44
local HttpServer = require('jls.net.http.HttpServer')
5-
local HttpExchange = require('jls.net.http.HttpExchange')
6-
local HttpFilter = require('jls.net.http.HttpFilter')
75
local Date = require('jls.util.Date')
86
local secure = require('jls.net.secure')
7+
98
local utils = require('lha.utils')
109

1110
local function writeCertificateAndPrivateKey(certFile, pkeyFile, commonName)
1211
local cacert, pkey = secure.createCertificate({
13-
--duration = (3600 * 24 * (365 + 31)),
1412
commonName = commonName
1513
})
1614
local cacertPem = cacert:export('pem')
@@ -47,7 +45,7 @@ extension:subscribeEvent('startup', function()
4745
local pkeyFile = utils.getAbsoluteFile(configuration.key, engine:getWorkDirectory())
4846
if not certFile:exists() or not pkeyFile:exists() then
4947
writeCertificateAndPrivateKey(certFile, pkeyFile, configuration.commonName)
50-
logger:info('Generate certificate %s and associated private key %s', certFile:getPath(), pkeyFile:getPath())
48+
logger:info('Generated certificate %s and associated private key %s', certFile:getPath(), pkeyFile:getPath())
5149
else
5250
-- check and log certificate expiration
5351
local cert = readCertificate(certFile)
@@ -70,14 +68,8 @@ extension:subscribeEvent('startup', function()
7068
logger:warn('Cannot bind HTTP secure server to "%s" on port %s due to %s', configuration.address, configuration.port, err)
7169
end)
7270
if configuration.login then
73-
local location = '/login.html'
74-
httpSecureServer:addFilter(HttpFilter.byPath(HttpFilter:new(function(_, exchange)
75-
local session = exchange:getSession()
76-
if session and not session.attributes.user then
77-
HttpExchange.redirect(exchange, location)
78-
return false
79-
end
80-
end)):excludePath(location, '/login'))
71+
local redirect = extension:require('users.login-redirect', true)
72+
httpSecureServer:addFilter(redirect)
8173
end
8274
-- share contexts
8375
httpSecureServer:setParentContextHolder(httpServer)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local HttpExchange = require('jls.net.http.HttpExchange')
2+
local HttpFilter = require('jls.net.http.HttpFilter')
3+
4+
local location = '/login.html'
5+
6+
return HttpFilter.byPath(HttpFilter:new(function(_, exchange)
7+
local session = exchange:getSession()
8+
if session and not session.attributes.user then
9+
HttpExchange.redirect(exchange, location)
10+
return false
11+
end
12+
end)):excludePath(location, '/login')

extensions/users/manifest.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@
3636
}
3737
}
3838
}
39+
},
40+
"defaultPermission": {
41+
"title": "No user access rigths",
42+
"type": "string",
43+
"enumValues": [
44+
{"const": "-", "title": "No access"},
45+
{"const": "r", "title": "Can read"},
46+
{"const": "rw", "title": "Can read and write"},
47+
{"const": "rwc", "title": "Can r/w and configure"},
48+
{"const": "rwca", "title": "Can r/w, configure and administer"}
49+
],
50+
"default": "rw"
51+
},
52+
"login": {
53+
"title": "Restrict the access to logged users",
54+
"type": "boolean",
55+
"default": true
3956
}
4057
}
4158
}

extensions/users/users.lua

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,8 @@ local User = class.create(function(user)
2020
end)
2121

2222
local 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

5226
local 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

Comments
 (0)