【问题标题】:How to remove file permissions on windows?windows如何取消文件权限?
【发布时间】:2014-04-03 16:17:08
【问题描述】:

我已使用以下答案授予对文件的访问权限。由@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。但是,这具有不良行为,因为我的用户将来需要再次临时访问。在运行AddAccessDeniedAce 然后重新运行AddAccessAllowedAce 之后,被拒绝的控件仍然存在,我的用户仍然无法访问该文件。当用户不再需要访问权限时,我想将他们完全从访问权限中删除。这可以通过 Windows 资源管理器中的属性菜单完成:

我无法找到支持此类任务的文档。有谁知道如何通过操纵 dacl 来做到这一点?还是我必须通过 Windows 界面手动执行此操作?

【问题讨论】:

  • 我目前正在查看this post,看看我是否可以这样做。
  • 所以我发现这些方法中的每一个都将单独的 ACE 添加到文件中。我相信我可能需要删除 ACE。正在努力。

标签: python windows permissions


【解决方案1】:

在这里找到解决方案: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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 2015-12-23
    • 1970-01-01
    • 2013-12-30
    • 2016-10-11
    • 2012-10-23
    • 2014-09-10
    • 2017-07-22
    相关资源
    最近更新 更多