Create temporary user and delete after expiration

You can share your Lua Scripts with everybody here.

Create temporary user and delete after expiration

Postby MountainVision » Thu Mar 04, 2010 2:40 pm

I want to let my internal users create a temporary account which is available for a desired amount of days. And after the expiration the account and the created directory will be deleted automatically.
It's intended for sharing files with an external comany only for a short time.


The following code is a simple PHP-Site with a dropdown-menu where you can chose the amount of days the account is available. After submitting, the script creates a user with the accordant homedir and with a prefix, the expiredate and a random number in the username (exp. tmp_YYYYMMDD1234)
(Sorry, but the comments in the script are in german)

Code: Select all
<html>
<head>
</head>

<body>
<?
   //Formular-Variablen abholen
   $i_days = isset($_POST["i_days"]) ? $_POST["i_days"] : 0;
   
   if ($i_days > 0) {
      //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
      // Variablen setzen
      $s_server    = 'yourserver:5466';         //Servername & Port des Adminzugangs
      $s_admin   = 'admin';                  //Account mit Administratorberechtigung
      $s_adminpwd = 'adminpassword';            //Passwort
      $s_domain    = 'yourdomain';               //Auf dem WFTPServer eingerichtete Domäne
      $s_rootdir   = '/data01/tmp_users/';         //Root Verzeichnis (darin wird jeweils ein Verzeichnis mit dem Usernamen erstellt)
      $s_group   = 'tmp_users';               //Gruppe des neuen Users (Leerlassen falls keine gewünscht)
      $s_prefix   = 'tmp_';                  //Prefix für den Usernamen
      //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      
      $b_read = 'true';
      $b_write = 'true';
      $b_delete = 'true';
      /* Uncomment this part to have the option to set permissions
      $b_read = isset($_POST["b_read"]) ? $_POST["b_read"] : 'false';
      $b_write = isset($_POST["b_write"]) ? $_POST["b_write"] : 'false';
      $b_delete = isset($_POST["b_delete"]) ? $_POST["b_delete"] : 'false';*/
      
      
      $expire_stamp = time() + ($i_days * 24 * 60 * 60);            //Ablaufzeitpunkt setzen
      $expire_date  = date('Y-m-d H:i:s', $expire_stamp);            //Ablaufzeitpunkt setzen
      $strUrl = "http://".$s_server."/admin_webservice.html";         //URL zusammen setzen
      $strUrlParam = "?admin=".$s_admin."&pass=".$s_adminpwd."&cmd=";   //URL zusammen setzen
      $username = $s_prefix.date('Ymd', $expire_stamp).mt_rand(0,9999);  //account name (prefix tmp_ / Ablaufdatum / 4-stellige Zufallszahl)
       $password = mt_rand();                                 //account password
       $homedir = $s_rootdir.$username;                           //home directory

      //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
      //LUA Script erstellen
      
      //Userverzeichnis erstellen
      $strLuaScript = "c_MkDir('".$homedir."')";
      //temporären User erstellen
      
      //                                                Parameternummern:       7           10                  15                 20                  25                  30                  35                           40                  45                        48
      $strLuaScript .= "c_AddUser('".$s_domain."','".$username."',md5('".$password."'),63,1,1, '', '', '', '', '', '', '', '', 0, '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', {{'".$s_group."'}}, '', '', '', '', '', '', '', '', '', 1,'".$expire_date."', '', '')";

      // User-Verzeichnis mit entsprechenden Berechtigungen setzen
      $strLuaScript .= "c_AddUserDirectory('".$s_domain."','".$username."','".$homedir."','/',true,".$b_read.",".$b_write.",".$b_write.",".$b_delete.",".$b_read.",".$b_write.",".$b_delete.",".$b_write.")";
      //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      
      
      //LUA Script ausführen
      $strResult = file_get_contents($strUrl.$strUrlParam.rawurlencode($strLuaScript));
      
      //echo $strLuaScript;
      echo 'Username: '.$username.'<br>'.
          'Password: '.$password.'<br>'.
          'Expires: '.$expire_date.'<br><br>';
      echo '<a href='.$PHP_SELF.'>Zurueck</a>';

   
   }
   else {
      ?>
      <h1>Neuen tempor&auml;ren User-Account generieren</h1>
      <form method="post" action="<?=$PHP_SELF; ?>">
         <p>G&uuml;ltigkeit <select name="i_days">
            <option value="1">1 Tag</option>
            <option value="2">2 Tage</option>
            <option value="5">5 Tage</option>
            <option value="10">10 Tage</option>
            <option value="20">20 Tage</option>
            <option value="30">30 Tage</option>
         </select>
         </p>
         <!-- Uncomment this part to have the option to set permissions
         <p>
         <input type="checkbox" name="b_read" value="true"> Leseberechtigung<br>
         <input type="checkbox" name="b_write" value="true"> Schreibberechtigung<br>
         <input type="checkbox" name="b_delete" value="true"> L&ouml;schberechtigung<br>
         </p> -->
         <input type="submit" value="Los geht's">
      </form>
   <?
   }
   ?>

</body>
</html>




To remove the expired accounts automatically put the following LUA-Script into the task sheduler
Code: Select all
local mydomain = "yourdomain"
local rootdirtmp = "/data01/tmp_users/"
local group = "tmp_users"
do
   local strUserlist = c_GetUserList(mydomain)
   local userlist = Split(strUserlist,"\n")

   for _,username in pairs(userlist) do
     local user = c_GetUser(mydomain,username)
     local expiretime_t = c_TranslateTime(user.expiretime)
     if (os.time() > expiretime_t) and (user.enable_expire == true) and (user.usergroups[1].groupname == group) then
      c_DeleteUser(mydomain,username)
      c_RemoveFileDir(rootdirtmp .. username)
     end
   end
end



This is still very basic, but it may helps you to create something similar...

//edit: I made a little change to the deleting script. It checks now if a user is in the tmp_users group...
Last edited by MountainVision on Fri Mar 05, 2010 9:11 am, edited 3 times in total.
MountainVision
 
posts 10
 
joined Wed Mar 03, 2010 3:09 pm
Location: Laax, Switzerland

Re: Create temporary user and delete after expiration

Postby FTP » Thu Mar 04, 2010 5:18 pm

Good scripts! I think it is very helpful for many people.
FTP
Site Admin
 
posts 1215
 
joined Tue Sep 29, 2009 6:09 am

Re: Create temporary user and delete after expiration

Postby david » Tue May 04, 2010 8:32 pm

Hello,

Thank you for posting this. However, it seems that the statement "user.usergroups[1].groupname" isn't working for me (at least that's what I narrowed it down to).

For example, if I use a statment such as ugroup = user.usergroups[1].groupname The value isn't returned even though the user has a group associated with it.

Any ideas?

Thanks,

Dave
david
 
posts 9
 
joined Fri Apr 16, 2010 6:35 pm

Re: Create temporary user and delete after expiration

Postby MountainVision » Wed May 05, 2010 7:11 am

did you set the variable "user" correctly? Don't forget to replace the domain and username with your values....

Code: Select all
local user = c_GetUser(mydomain,username)


you can also test in your console with a command like this:
Code: Select all
print(c_GetUser("mydomain","username").usergroups[1].groupname)
MountainVision
 
posts 10
 
joined Wed Mar 03, 2010 3:09 pm
Location: Laax, Switzerland

Re: Create temporary user and delete after expiration

Postby ppcinj » Fri Jan 10, 2014 10:42 am

I'm sorry for digging up this Thread again, but I have problems with the code:

I modified the PHP code variables to our Server details and tried to run it, but as result I only get an permission error. Maybe someone of you or an admin can help me.

Server Details:

Version: 4.2.6

The URL, which is called to create an User (without "rawurlencode" for readability reasons and some parts hidden):

Code: Select all
http://*.*.*.*:5466/admin_webservice.html?admin=acc_creator&pass=xxxx&cmd=c_MkDir('/data/tmp_users/tmp_201401114617')c_AddUser('xdomainx','tmp_201401114617',md5('2111147002'),63,1,1, '', '', '', '', '', '', '', '', 0, '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', {{'tmp_users'}}, '', '', '', '', '', '', '', '', '', 1,'2014-01-11 11:36:58', '', '')c_AddUserDirectory('xdomainx','tmp_201401114617','/data/tmp_users/tmp_201401114617','/',true,true,true,true,true,true,true,true,true)


The Result:

Code: Select all
[ERROR RESULT] no permission!


Are there any Permissions i need to set to the Creator-Account for this to succeed?
ppcinj
 
posts 1
 
joined Fri Jan 10, 2014 10:32 am

Re: Create temporary user and delete after expiration

Postby FTP » Fri Jan 10, 2014 4:28 pm

Unless you use a domain admin, or using a wrong password.
FTP
Site Admin
 
posts 1215
 
joined Tue Sep 29, 2009 6:09 am


Return to Lua scripts

Who is online

Users browsing this forum: No registered users and 2 guests

cron