how to remove Subfolderpermission from Group via Script

You can share your Lua Scripts with everybody here.
Post Reply
ABSFTP
Posts: 2
Joined: Wed Nov 06, 2024 7:19 am

how to remove Subfolderpermission from Group via Script

Post by ABSFTP »

Hi
Now in our scenario we have Folder "Client" that contains Clientfolders like "Client_A" "Client_B" and so on.
All Folders in the Folder "Client" have Users named the same as their Folder (Client_A, Client_B) where the Directory is set to the according Folder

We have another Group "Support"
All members of this group should have Access to the Subfolders from "Client" but should not be able to modify or create other Folders in the Root of the Folder Client.
I have therefor set Subfolder Permissions in the Support Group.

This works as intended.

Now every Clientuser has a expiredate and the script checks if the user is expired and deletes the User and the corresponding Folder.
So if Client_A is expired the script deletes the user and the Folder "Client_A"

This all works realy good

What i cannot get to work is that the script also removes the Subfolderpermission in the Group "Support"

Here is the Script that works currently:

Code: Select all

local mydomain = "Domainname"
local rootdirtmp = "D:/ftp/Client/"
local exclude_folders = { "Exclude1", "Exclude2" }

-- Hilfsfunktion, um zu überprüfen, ob ein Element in einer Tabelle vorhanden ist
local function table_contains(table, element)
    for _, value in pairs(table) do
        if value == element then
            return true
        end
    end
    return false
end

-- Funktion, um die Liste der gültigen Benutzer zu erhalten
local function get_valid_user_folders()
    local valid_user_folders = {}
    local strUserlist = c_GetUserList(mydomain)
    local userlist = Split(strUserlist, "\n")

    for _, username in pairs(userlist) do
        local user = c_GetUser(mydomain, username)
        if user then
            table.insert(valid_user_folders, username)
        end
    end

    return valid_user_folders
end

-- Funktion, um alle Ordner im Verzeichnis zu erhalten (mit PowerShell, UTF-8 sichergestellt)
local function get_all_folders_in_directory(directory)
    local folders = {}
    local p = io.popen('powershell.exe -Command "[Console]::OutputEncoding = [Text.Encoding]::UTF8; Get-ChildItem -Path \'' .. directory .. '\' -Directory | ForEach-Object { $_.Name }"')
    for folder in p:lines() do
        table.insert(folders, folder)
    end
    p:close()
    return folders
end

-- Hauptfunktion, um Ordner zu löschen, die nicht für gültige Benutzer sind und nicht in der Ausschlussliste stehen
local function delete_invalid_folders()
    local valid_user_folders = get_valid_user_folders()

    -- Hole die Liste aller Ordner im rootdirtmp-Verzeichnis
    local all_folders = get_all_folders_in_directory(rootdirtmp)

    for _, folder_name in pairs(all_folders) do
        -- Überprüfe, ob der Ordner weder auf der Ausschlussliste steht noch zu einem gültigen Benutzer gehört
        if not table_contains(exclude_folders, folder_name) then
            if not table_contains(valid_user_folders, folder_name) then
                -- Verwende PowerShell-Befehl über os.execute(), um Ordner zu löschen, auch bei Sonderzeichen
                local delete_command = 'powershell.exe -Command "[Console]::OutputEncoding = [Text.Encoding]::UTF8; Remove-Item -Recurse -Force \'' .. rootdirtmp .. folder_name .. '\'"'
                os.execute(delete_command)  -- Lösche den Ordner rekursiv und ohne Bestätigung
            end
        end
    end
end

-- Starte das Löschen
delete_invalid_folders()
Is there a way to remove Subfolderpermissions from Groups via Script?
FTP
Site Admin
Posts: 2102
Joined: Tue Sep 29, 2009 6:09 am

Re: how to remove Subfolderpermission from Group via Script

Post by FTP »

OK, maybe you can add the following script after your code "os.execute(delete_command)":

Code: Select all

	
		local group = c_GetGroup(mydomain, "Support")
		for i,subdir in pairs(group.subdir_perm) do
			if subdir.dir == rootdirtmp .. folder_name then
				table.remove(group.subdir_perm, i)
			end
		end
		AddGroup(mydomain,group)
BTW, you need to run the recent version of WingFTP, otherwise, the above code might be incorrect.
ABSFTP
Posts: 2
Joined: Wed Nov 06, 2024 7:19 am

Re: how to remove Subfolderpermission from Group via Script

Post by ABSFTP »

Works like a Charm, Thank you
I have set it as a separate script to run it daily

Code: Select all

-- Define the domain and group to check
local mydomain = "Domainname"
local group_name = "Support"
local rootdirtmp = "D:/ftp/Client/"

-- Function to check if a folder exists
local function folder_exists(folder_path)
    local command = 'powershell.exe -Command "[Console]::OutputEncoding = [Text.Encoding]::UTF8; Test-Path \'' .. folder_path .. '\'"'
    local handle = io.popen(command)
    local result = handle:read("*a")
    handle:close()
    return result:match("True") ~= nil
end

-- Function to clean up invalid subfolder permissions
local function cleanup_invalid_subfolder_permissions()
    -- Retrieve the group information
    local group = c_GetGroup(mydomain, group_name)
    if not group then
        print("Group '" .. group_name .. "' not found in domain '" .. mydomain .. "'!")
        return
    end

    -- Iterate through subdirectory permissions and remove invalid entries
    local removed_permissions = {}
    for i = #group.subdir_perm, 1, -1 do
        local subdir = group.subdir_perm[i]
        if not folder_exists(subdir.dir) then
            table.remove(group.subdir_perm, i)
            table.insert(removed_permissions, subdir.dir)
        end
    end

    -- Update the group with the cleaned-up permissions
    AddGroup(mydomain, group)

    -- Print log of removed permissions
    if #removed_permissions > 0 then
        print("Removed invalid subfolder permissions:")
        for _, dir in ipairs(removed_permissions) do
            print("  - " .. dir)
        end
    else
        print("No invalid subfolder permissions found.")
    end
end

-- Run the cleanup
cleanup_invalid_subfolder_permissions()
Post Reply