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

时间:2013-08-22 09:53:47

标签: authentication lua openwrt

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

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

我的想法是找出谁登录,然后在每个受限模块的index()函数中什么都不做。到目前为止,我无法在LuCI API(http://luci.subsignal.org/api/luci/)中找到这样的函数,它会返回当前记录的用户。

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

任何想法,如何实现我的目标?

1 个答案:

答案 0 :(得分:3)

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

function remove_idx(  tbl, index )

   -- initiate variables for save procedure
   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 )
     -- only handle value
     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
     -- escape handled values
     if (not thandled[i]) then

        local flag = 0
        local stype = type( i )
        -- handle index
        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 )
           -- handle value
           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

相关问题