group.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. local shell = util.shell
  2. local get_content = util.get_content
  3. local file="/etc/group"
  4. local groups={}
  5. local users={}
  6. for line in get_content(file):gmatch("[^\r\n]+") do
  7. --group=line:match("([%w]+):")
  8. local name,x,id,_users = line:match("(['%w%d-_']+):(['%w']+):(['%d']+):(.*)")
  9. --if _users ~= nil then
  10. --for user in _users:gmatch("[^,\n]+") do table.insert(users,user) end
  11. --end
  12. groups[name]={id=id,users=_users}
  13. --users={}
  14. end
  15. function run()
  16. ret.data={}
  17. if request and request.data then
  18. req=json.decode(request.data)
  19. if req.method == "get" then
  20. --users,input,disk,network,netdev,floppy,fuse,video,lp,tty,audio,cdrom,scanner,adm,vboxusers,wheel,pulse-access
  21. local default_groups={"tty","floppy","disk","lp","audio","video","cdrom","adm","network","input","wheel","users"}
  22. if req.query == nil then req.query = "all" end
  23. if req.query == "all" then
  24. ret.data=groups
  25. end
  26. if req.query == "default" then
  27. ret.data={default_groups=default_groups}
  28. end
  29. if req.query == "info" then
  30. if req.user ~= nil then
  31. ret.data[req.user]={groups={}}
  32. for nm,gr in pairs(groups) do
  33. if gr.users:find(req.user) or gr.users:find(","..req.user) or nm == req.user then
  34. table.insert(ret.data[req.user]["groups"],nm)
  35. end
  36. end
  37. elseif req.group ~= nil then
  38. ret.data[req.group]={}
  39. if tonumber(groups[req.group].id) > 999 then
  40. groups[req.group].users=req.group
  41. end
  42. ret.data[req.group]=groups[req.group]
  43. else
  44. ret.data={error="user or group parameter not exist"}
  45. end
  46. end
  47. if next(ret.data) == nil then ret={error="undefined get query"} end
  48. end
  49. if req.method == "post" then
  50. local group=""
  51. if req.oper == "del" then
  52. local force=" -f "
  53. local cmd="groupdel "
  54. if req.group ~= nil then
  55. group=req.group
  56. if req.force and req.force == false then
  57. force=""
  58. end
  59. if groups[group] ~= nil then
  60. cmd=cmd..force..group
  61. ret.data.cmd=cmd
  62. else
  63. ret.error=group.." group is not exist"
  64. end
  65. else
  66. ret.error="group field is mandatory"
  67. end
  68. end
  69. if req.oper == "add" then
  70. local id=""
  71. local cmd="groupadd "
  72. if req.group ~= nil then
  73. if req.id ~= nil then
  74. id=(" -g %s "):format(req.id)
  75. end
  76. group=req.group
  77. cmd=cmd..id..group
  78. --check if already exist
  79. if groups[group] == nil then
  80. ret.data.cmd=cmd
  81. else
  82. ret.error=group.." group is already exist"
  83. end
  84. --shell(cmd)
  85. else
  86. ret.error="group field is mandatory"
  87. end
  88. end
  89. end
  90. else
  91. ret.api={
  92. title="Grup Yönetimi",
  93. auth=false,
  94. method={
  95. get={
  96. query="bilgi_tipi:*all*/default/info",
  97. user="kullanıcının grupları:query=info için parametre",
  98. group="grup kullanıcıları:query=info için parametre",
  99. },
  100. post={
  101. oper={
  102. add={
  103. group="group name, mandatory",
  104. id="group id, optional",
  105. },
  106. del={
  107. group="group name, mandatory",
  108. force="force remove,default:true, optional",
  109. },
  110. },
  111. },
  112. },
  113. }
  114. end
  115. response=json.encode(ret)
  116. end