簡體   English   中英

如何刪除Windows上的文件權限?

[英]How to remove file permissions on windows?

我已使用以下答案授予對文件的訪問權限。 感謝@kindall https://stackoverflow.com/a/12168268/740899

> import win32security 
> import ntsecuritycon as con
> 
> FILENAME = "whatever"
> 
> userx, domain, type = win32security.LookupAccountName ("", "User X")
> 
> sd = win32security.GetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION) 
> dacl = sd.GetSecurityDescriptorDacl()   # instead of dacl = win32security.ACL()
> 
> dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_GENERIC_READ | con.FILE_GENERIC_WRITE, userx)
> 
> sd.SetSecurityDescriptorDacl(1, dacl, 0)   # may not be necessary
> win32security.SetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION, sd)

但是,訪問需要是臨時的。 所以我使用dacl.AddAccessDeniedAce代替dacl.AddAccessAllowedAce顯示的dacl.AddAccessAllowedAce 但是,這有不良行為,因為我的用戶將來需要再次臨時訪問。 運行AddAccessDeniedAce然后重新運行AddAccessAllowedAce ,被拒絕的控制仍然存在,我的用戶仍然無法訪問該文件。 當用戶不再需要訪問權限時,我想將他們完全從訪問權限中刪除。 這可以通過 Windows 資源管理器中的屬性菜單完成:

在此處輸入圖片說明

我一直無法找到支持此類任務的文檔。 有誰知道如何通過操作 dacl 來做到這一點? 還是我必須通過 Windows 界面手動執行此操作?

在這里找到了解決方案: http : //voices.canonical.com/tag/windows/

我不得不稍微調整一下,但它正在工作。 哇!

def remove_ace(path,usernames):
    """Remove the ace for the given users."""
    if not os.path.exists(path):
        raise WindowsError('Path %s could not be found.' % path)
    total = 0
    for x in usernames:
        userx, domain, utype = win32security.LookupAccountName("", x)
        sd = win32security.GetFileSecurity(path, win32security.DACL_SECURITY_INFORMATION)
        dacl = sd.GetSecurityDescriptorDacl()
        num_delete = 0
        for index in range(0, dacl.GetAceCount()):
            ace = dacl.GetAce(index - num_delete)
            if userx == ace[2]:
                dacl.DeleteAce(index - num_delete)
                num_delete += 1
                total += 1
        if num_delete > 0:
            sd.SetSecurityDescriptorDacl(1, dacl, 0)
            win32security.SetFileSecurity(path, win32security.DACL_SECURITY_INFORMATION, sd)
    if total > 0:
        return True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM