首页 文章

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

提问于
浏览
5

目标:两个用户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 回答

  • 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直接访问 .

    Update

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

相关问题