OpenWrt: LuCI: 如何实现有限用户访问

目标:两个用户 root 和 user。root 可以通过 web 页面访问所有内容,但用户只能看到菜单的某些部分。

一种选择是将“sysauth”选项传递给相关的每个模块。这不是很实用,因为用户会看到每个菜单条目,并会为他不允许的每个菜单获得登录页面。

我的想法是弄清楚谁登录了,然后在每个受限制的模块的 index() 函数中不做任何事情。到目前为止,我无法在 LuCI API(http://luci.subsignal.org/api/luci/)中找到返回当前已登录用户的函数。

我知道如何在 OpenWrt / LuCI 中添加其他用户(https://forum.openwrt.org/viewtopic.php?pid=163013#p163013)。但这只是解决方案的一部分。

有什么想法能够实现我的目标吗?

点赞
用户1113139
用户1113139

我最终创建了一个 Lua 函数来查找和删除表中不需要的键,如此处所述:http://lua-users.org/wiki/SaveTableToFile

function remove_idx(  tbl, index )

   -- 初始化保存过程的变量
   local tables,lookup = { tbl },{ [tbl] = 1 }

   for idx,t in ipairs( tables ) do
      local thandled = {}

      for i,v in ipairs( t ) do
     thandled[i] = true
     local stype = type( v )
     -- 只处理值
     if stype == "table" then
        if not lookup[v] then
           table.insert( tables, v )
           lookup[v] = #tables
        end
     else
        if i == index then
           t[i] = nil
           return
        end
     end
      end

      for i,v in pairs( t ) do
     -- 跳过已处理的值
     if (not thandled[i]) then

        local flag = 0
        local stype = type( i )
        -- 处理索引
        if stype == "table" then
           if not lookup[i] then
          table.insert( tables,i )
          lookup[i] = #tables
           end
        else
           flag = 1
           if i == index then
          t[i] = nil
          return
           end
        end

        if flag == 1 then
           stype = type( v )
           -- 处理值
           if stype == "table" then
          if not lookup[v] then
             table.insert( tables,v )
             lookup[v] = #tables
          end
           else
          if i == index then
             t[i] = nil
             return
          end
           end
        end

     end
      end
   end
end

然后在 libs/web/luasrc/dispatcher.lua 的 dispatch() 中插入了我的用户检查和页面删除:

if c and c.index then
    local tpl = require "luci.template"

    if util.copcall(tpl.render, "indexer", {}) then
        return true
    end
 end

这是根据登录用户删除不需要的页面的方法:

if ctx.authuser == "user" then
        remove_idx(ctx.tree, "packages")
        remove_idx(ctx.tree, "leds")
end

它有点快速和粗糙,但它有效。请注意,仍然可以通过操纵 URL 直接访问。

更新

LuCI2 将提供 ACL 支持和多用户环境:http://git.openwrt.org/?p=project/luci2/ui.git;a%3Dsummary

2013-09-25 13:23:20
用户14199280
用户14199280

如果您想创建多个具有不同访问权限的OpenWRT Luci用户,您可以按照以下步骤操作:

  1. 创建一个本地用户账户
  2. 将用户添加到RCP配置中,并定义访问级别

请参阅以下配置文件/ etc / config / rpcd的示例节选:

config login
        option username 'adminuser'
        option password '$p$adminuser'
        list read '*'
        list write '*'

config login
        option username 'readonlyuser'
        option password '$p$readonlyuser'
        list read '*'

如果您正在获取用于JSON-RPC调用Luci的身份验证令牌,这也是可行的。

2021-12-21 21:44:58