body_filter.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. local config = require("config")
  2. local util = require("util")
  3. local agent_pub = require("agent_pub")
  4. local logout_url = "/wolf/rbac/logout"
  5. local change_pwd_url = "/wolf/rbac/change_pwd.html"
  6. local def_topbar_style = [[
  7. <style type="text/css">
  8. <!--
  9. .rbac-topbar {
  10. top: 0px;
  11. height:30px;
  12. background-color: #E6E8E7;
  13. font-size: 12px;
  14. color: black;
  15. background-position: top;
  16. /* box-shadow: 0 1px 4px 0 rgb(30, 2, 2); */
  17. border-bottom: 1px solid rgba(0,0,0,0.12);
  18. padding: 5px;
  19. display: flex;
  20. }
  21. .rbac-sysname {
  22. width: 30%;
  23. text-align:left;
  24. padding-left:8px;
  25. padding-right:8px;
  26. display: flex;
  27. align-items: center;
  28. }
  29. .rbac-info {
  30. display: flex;
  31. width: 70%;
  32. justify-content: flex-end;
  33. align-items: center;
  34. }
  35. .rbac-info div {
  36. float: left;
  37. text-align:right;
  38. padding: 0px 8px;
  39. white-space:nowrap;
  40. }
  41. -->
  42. </style>
  43. ]]
  44. local topbar_tpl = [[
  45. <div id="rbac-topbar" class="rbac-topbar">
  46. <div id="rbac-sysname" class="rbac-sysname">%s</div>
  47. <div id="rbac-info" class="rbac-info">
  48. <div id="rbac-username" class="rbac-username">USER: %s(%s)</div>
  49. <div id="rbac-password" class="rbac-password"><a %s>Change Password</a></div>
  50. <div id="rbac-logout" class="rbac-logout"><a href="%s" target="_self">Logout</a></div>
  51. </div>
  52. </div>
  53. ]]
  54. local function get_style()
  55. return def_topbar_style
  56. end
  57. local function get_infobar()
  58. local username = "NONE"
  59. local nickname = "UNKNOW"
  60. local userInfo = ngx.ctx.userInfo
  61. if userInfo then
  62. username = userInfo.username
  63. nickname = userInfo.nickname or ''
  64. elseif ngx.var.arg_username then
  65. username = ngx.var.arg_username
  66. nickname = ngx.var.arg_nickname or ''
  67. end
  68. local href = string.format([[href="%s" target="_blank"]], change_pwd_url)
  69. if config.not_allow_change_pwd then
  70. href = string.format([[href="#" onclick="javascript:alert('Password change is not allowed');"]])
  71. end
  72. ngx.log(ngx.INFO, "user [", username, "](", nickname, ") request...")
  73. local replace = get_style() .. string.format(topbar_tpl, config.sysname or 'WOLF-RBAC', username, nickname, href, logout_url)
  74. return true, replace
  75. end
  76. ngx.log(ngx.DEBUG, "url:", ngx.var.uri)
  77. if agent_pub.need_replace() then
  78. local ok, infobar = get_infobar()
  79. if ok then
  80. local n = nil
  81. if ngx.var.uri == "/" or util.endswith(ngx.var.uri, "/") then
  82. ngx.arg[1], n = ngx.re.sub(ngx.arg[1], "\\<body[^\\>]*\\>", "$0 " .. infobar , "jom")
  83. else
  84. ngx.arg[1] = ngx.re.sub(ngx.arg[1], [[<div id="rbac" style="display:none"></div>]], infobar , "jom")
  85. end
  86. ngx.ctx.topbar_added = (n==1)
  87. ngx.log(ngx.INFO, "### add infobar. ### n:", tostring(ngx.ctx.topbar_added))
  88. end
  89. else
  90. ngx.log(ngx.INFO, "---- ignore url: ", ngx.var.uri);
  91. end