【问题标题】:OpenWrt: LuCI: how to implement limited user accessOpenWrt:LuCI:如何实现受限用户访问
【发布时间】:2013-08-24 23:43:32
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: authentication lua openwrt


    【解决方案1】:

    我最终创建了一个 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

    【讨论】:

    • 感谢您分享您的解决方案。我有一个简单的问题:我不知道 remove_idx 写入哪个文件?在 dispatch() 函数之后或 dispatch() 'modifi dispatch()' 中添加“用户检查”。我在哪里使用“删除不需要的页面”代码?我糊涂了。对不起我的英语不好。
    • 我添加了remove_idx 功能和用户检查,但仅删除菜单首页渲染中的项目,如果重新加载或转到其他页面,ledspackages 项目返回并在菜单中可用.可能是我用错了检查用户代码,你能帮我吗?
    • remove_idx() 和身份验证检查都必须在libs/web/luasrc/dispatcher.lua 文件中,因为一旦您打开路由器 Web 界面,就会构建整个索引树。用户身份验证检查必须在dispatch() 例程中,就在我在答案中显示的代码之后。这是将填充索引树的例程。
    • 我在if c and c.index... 之后添加了if ctx.authuser...,如果我点击系统或网络菜单,ledspackages 在菜单列表中不可用,但如果我点击dhcpstartup。 ..、ledspackages 在菜单列表中可用。
    • 我通过在remove_idx()调用中将c替换为ctx.tree解决了重新出现子标签的问题。但是仍然可以通过操作 URL 直接访问。
    【解决方案2】:

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

    1. 创建local user account
    2. Add the user to the RCP configuration 并定义访问级别

    请参阅下面 /etc/config/rpcd config 的示例摘录:

    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 获取身份验证令牌,这也有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      相关资源
      最近更新 更多