Daily scripting task for removing inactive users

You can share your Lua Scripts with everybody here.
Post Reply
FTP
Site Admin
Posts: 1938
Joined: Tue Sep 29, 2009 6:09 am

Daily scripting task for removing inactive users

Post by FTP »

-- Description: Daily scripting task for removing inactive users that have not logged in within 15 days
-- Author: Luke
-- Date: 2009-10-12

Code: Select all

do 
	--Get user list from domain "demo"
	local strUserlist = c_GetUserList("demo") 
	local userlist = Split(strUserlist,"\n") 

	for _,username in pairs(userlist) do 
		local user = c_GetUser("demo",username)
		local logintime = user.last_logintime
		--get last login time, formatted as time_t
		local logintime_t = c_TranslateTime(logintime)
		if (os.time() - logintime_t) >= 3600*24*15 then 
			c_DeleteUser("demo",username)
		end 
	end 
end

You may add a hourly task and input the above lua script.
Image
thomas.mccall
Posts: 3
Joined: Tue Jul 12, 2016 9:19 am

Re: Daily scheduler task for removing inactive users

Post by thomas.mccall »

Hello,

Could you explain how to modify this example so that inactive accounts are disabled not deleted?

Thanks!
FTP
Site Admin
Posts: 1938
Joined: Tue Sep 29, 2009 6:09 am

Re: Daily scheduler task for removing inactive users

Post by FTP »

thomas.mccall wrote:Hello,

Could you explain how to modify this example so that inactive accounts are disabled not deleted?

Thanks!
You just need to replace the following line:

Code: Select all

c_DeleteUser("demo",username)
into:

Code: Select all

   for k,v in pairs(user) do
      if type(v) == "boolean" then
         if v == true then
            user[k] = 1
         else
            user[k] = 0
         end
      end
   end

   user.enable_account = 0
   user.oldpassword = user.password

   AddUser("demo", user)
thomas.mccall
Posts: 3
Joined: Tue Jul 12, 2016 9:19 am

Re: Daily scheduler task for removing inactive users

Post by thomas.mccall »

Probably I done something wrong with the code but when I try to enter it into the task editor it rejects it saying:

attempt to call local 'type' (a string value)!

Code: Select all

do 
 --Get user list from domain "CBUK"
 local strUserlist = c_GetUserList("CBUK") 
 local userlist = Split(strUserlist,"\n") 

 for _,username in pairs(userlist) do 
  local user = c_GetUser("CBUK",username)
  local logintime = user.last_logintime
  --get last login time, formatted as time_t
  local logintime_t = c_TranslateTime(logintime)
   if (os.time() - logintime_t) >= 3600*24*15 then 
    for k,v in pairs(user) do
	 if type(v) == "boolean" then
	  if v == true then
	   user[k] = 1
	  else
	   user[k] = 0
	  end
	 end
    end

    user.enable_account = 0
    user.oldpassword = user.password
		
    AddUser("CBUK", user)

end 


FTP
Site Admin
Posts: 1938
Joined: Tue Sep 29, 2009 6:09 am

Re: Daily scheduler task for removing inactive users

Post by FTP »

Already told you very clearly, just replace one line. OK, paste the full code here:

Code: Select all

do 
   --Get user list from domain "demo"
   local strUserlist = c_GetUserList("demo") 
   local userlist = Split(strUserlist,"\n") 

   for _,username in pairs(userlist) do 
      local user = c_GetUser("demo",username)
      local logintime = user.last_logintime
      --get last login time, formatted as time_t
      local logintime_t = c_TranslateTime(logintime)
      if (os.time() - logintime_t) >= 3600*24*15 then 
	   for k,v in pairs(user) do
	      if type(v) == "boolean" then
		 if v == true then
		    user[k] = 1
		 else
		    user[k] = 0
		 end
	      end
	   end

	   user.enable_account = 0
	   user.oldpassword = user.password

	   AddUser("demo", user)
      end 
   end 
end
Post Reply