Skip to content

Commit

Permalink
Allow access for project and system admins
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed May 22, 2024
1 parent 5f840c0 commit dc77aee
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions sipi/scripts/sipi.init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "file_specific_folder_util"
require "authentication"
require "log_util"
require "util"
local util = require "util"

-------------------------------------------------------------------------------
-- This function returns the segments from the identifier
Expand Down Expand Up @@ -103,8 +103,8 @@ function pre_flight(prefix, identifier, cookie)
end

local token, error = auth_get_jwt_decoded()
if error == nil and token ~= nil and token["sub"] == "http://www.knora.org/ontology/knora-admin#SystemUser" then
log("pre_flight - always allow access for system user", server.loglevel.LOG_DEBUG)
if error == nil and _is_system_or_project_admin(token, prefix) then
log("pre_flight - always allow access for system or project admin", server.loglevel.LOG_DEBUG)
return 'allow', filepath
end

Expand Down Expand Up @@ -151,6 +151,30 @@ function pre_flight(prefix, identifier, cookie)
end
end

--- Checks if the user is a system or project admin.
--- @param token table The decoded JWT token.
--- @param shortcode string The shortcode of the project.
--- @return boolean True if the user is a system or project admin, false otherwise.
function _is_system_or_project_admin(token, shortcode)
if shortcode == nil or token == nil or token["scope"] == nil then
return false
else
local write_prj_scope = "write:project:" .. shortcode
local scopes = str_splitString(token["scope"], " ")
log("pre_flight - scopes: " .. tableToString(scopes), server.loglevel.LOG_DEBUG)
return _table_contains(scopes, "admin") or _table_contains(scopes, write_prj_scope)
end
end

function _table_contains(table, what)
for _, value in pairs(table) do
if value == what then
return true
end
end
return false
end

function _file_not_found_response()
return "allow", "file_does_not_exist"
end
Expand Down

0 comments on commit dc77aee

Please sign in to comment.