1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| import win32security as w32
import ntsecuritycon as con
import getpass, os
def lock_permission(path,user=None):
sd = w32.GetFileSecurity(path, w32.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()
ordis = ["ordi1","ordi2","..."]
if user == None:
for ordi in ordis:
user, _, _ = w32.LookupAccountName("", ordi)
dacl.AddAccessDeniedAce(w32.ACL_REVISION, con.FILE_ALL_ACCESS, user)
w_user, _, _ = w32.LookupAccountName("", getpass.getuser())
lock_permission(path,w_user)
else:
dacl.DeleteAce(0)
dacl.AddAccessAllowedAce(w32.ACL_REVISION, con.GENERIC_ALL, user)
sd.SetSecurityDescriptorDacl(1, dacl, 0)
w32.SetFileSecurity(path, w32.DACL_SECURITY_INFORMATION, sd)
def open_permission(path):
sd = w32.GetFileSecurity(path, w32.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()
user, _, _ = w32.LookupAccountName("", "Tout le monde")
dacl.DeleteAce(0)
dacl.AddAccessAllowedAce(w32.ACL_REVISION, con.GENERIC_ALL, user)
sd.SetSecurityDescriptorDacl(1, dacl, 0)
w32.SetFileSecurity(path, w32.DACL_SECURITY_INFORMATION, sd)
filename = "texte.txt"
lock_permission(filename)
file = open(filename ,"r")
liste = file.readlines()
file.close()
tache = liste[0]
file = open(filename,"w")
file.writelines(liste[1:])
file.close()
open_permission(filename) |
Partager