Advertisement
Guest User

auth.lua

a guest
Dec 2nd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. local cmp = require("component")
  2. local gpu = cmp.gpu
  3. local event = require("event")
  4. local serialization = require("serialization")
  5. local unicode=require("unicode")
  6. local filesystem = require("filesystem")
  7. local shell = require("shell")
  8. local term = require("term")
  9. local uc=require('unicode')
  10. local computer = require("computer")
  11. local keyboard = require("keyboard")
  12. local len = uc.len
  13. local sub = uc.sub
  14.  
  15. auth = {}
  16.  
  17. local function saveTbl(tbl, fl) file = io.open(fl, 'w') file:write(serialization.serialize(tbl)) file:close() end
  18.  
  19. local function loadTbl(fl)
  20.   file = io.open(fl, 'r')
  21.   if file == nil then
  22.     file = io.open(fl, 'w')
  23.     file:write('{}')
  24.     file:close()
  25.     return {}
  26.   else
  27.     file = io.open(fl, 'r')
  28.     return serialization.unserialize(file:read('*a'))
  29.   end
  30. end
  31.  
  32. function auth.delInArr(arr,id)
  33.   nar ={}
  34.   for i=1,#arr do
  35.     if i ~= id then
  36.       nar[#nar+1] = arr[i]
  37.     end
  38.   end
  39.   return nar
  40. end
  41.  
  42. function auth.check(arr,str)
  43.   find={false,0}
  44.   for i=1,#arr do
  45.     if arr[i]==str then
  46.      find={true,i}
  47.      break
  48.     end
  49.   end
  50.   return find
  51. end
  52.  
  53. function auth.delUser(db,user)
  54.   arr = loadTbl(db)
  55.   find = auth.check(arr,user)
  56.   if find[1] == true then
  57.     arr = auth.delInArr(arr,find[2])
  58.     saveTbl(arr,db)
  59.   end
  60.   return find[1]
  61. end
  62.  
  63. function auth.addUser(db,user)
  64.   arr=loadTbl(db)
  65.   status = nil
  66.   find = auth.check(arr,user)
  67.   if find[1] ~= true then
  68.     arr[#arr+1]=user
  69.     saveTbl(arr,db)
  70.     return true
  71.   else
  72.     return false
  73.   end
  74. end
  75.  
  76. return auth
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement