如何在 Windows 中设置文件夹权限? [英] How to set folder permissions in Windows?

查看:37
本文介绍了如何在 Windows 中设置文件夹权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建用户 AD 帐户时,我正在使用 Python 创建一个新的个人文件夹.正在创建文件夹,但权限不正确.Python 可以将用户添加到新创建的文件夹并更改其权限吗?我不知道从哪里开始编码.

I'm using Python to create a new personal folder when a users AD account is created. The folder is being created but the permissions are not correct. Can Python add the user to the newly created folder and change their permissions? I'm not sure where to begin coding this.

推荐答案

您需要 win32security 模块,它是 pywin32.这是执行此类操作的示例你想做.

You want the win32security module, which is a part of pywin32. Here's an example of doing the sort of thing you want to do.

该示例为文件创建了一个新的 DACL 并替换了旧的,但修改现有的很容易;您需要做的就是从安全描述符中获取现有的 DACL,而不是创建一个空的 DACL,如下所示:

That example creates a new DACL for the file and replaces the old one, but it's easy to modify the existing one; all you need to do is get the existing DACL from the security descriptor instead of creating an empty one, like so:

import win32security
import ntsecuritycon as con

FILENAME = "whatever"

userx, domain, type = win32security.LookupAccountName ("", "User X")
usery, domain, type = win32security.LookupAccountName ("", "User Y")

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)
dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_ALL_ACCESS, usery)

sd.SetSecurityDescriptorDacl(1, dacl, 0)   # may not be necessary
win32security.SetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION, sd)

这篇关于如何在 Windows 中设置文件夹权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆