From 727c8ca6fc5fb6db229f3c9f258aad0ea86ec34d Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 13 Aug 2019 13:14:11 +0100 Subject: [PATCH] Remove tags when deleting an account --- epicyon.py | 2 +- person.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/epicyon.py b/epicyon.py index 98dc42061..04d3fac00 100644 --- a/epicyon.py +++ b/epicyon.py @@ -844,7 +844,7 @@ if args.rmaccount: if not args.domain or not getConfigParam(baseDir,'domain'): print('Use the --domain option to set the domain name') sys.exit() - if removeAccount(baseDir,nickname,domain): + if removeAccount(baseDir,nickname,domain,port): print('Account for '+handle+' was removed') sys.exit() diff --git a/person.py b/person.py index a0bf25379..dab3a300e 100644 --- a/person.py +++ b/person.py @@ -535,7 +535,34 @@ def suspendAccount(baseDir: str,nickname: str,salts: {}) -> None: suspendedFile.close() salts[nickname]=createPassword(32) -def removeAccount(baseDir: str,nickname: str,domain: str) -> bool: +def removeTagsForNickname(baseDir: str,nickname: str,domain: str,port: int) -> None: + """Removes tags for a nickname + """ + if not os.path.isdir(baseDir+'/tags'): + return + domainFull=domain + if port: + if port!=80 and port!=443: + domainFull=domain+':'+str(port) + matchStr=domainFull+'/users/'+nickname+'/' + directory = os.fsencode(baseDir+'/tags/') + for f in os.listdir(directory): + filename = os.fsdecode(f) + if not filename.endswith(".txt"): + continue + tagFilename=os.path.join(baseDir+'/accounts/',filename) + if matchStr not in open(tagFilename).read(): + continue + with open(tagFilename, "r") as f: + lines = f.readlines() + tagFile=open(tagFilename,"w+") + if tagFile: + for tagline in lines: + if matchStr not in tagline: + tagFile.write(tagline) + tagFile.close() + +def removeAccount(baseDir: str,nickname: str,domain: str,port: int) -> bool: """Removes an account """ # Don't remove the admin @@ -555,6 +582,7 @@ def removeAccount(baseDir: str,nickname: str,domain: str) -> bool: unsuspendAccount(baseDir,nickname) handle=nickname+'@'+domain removePassword(baseDir,nickname) + removeTagsForNickname(baseDir,nickname,domain,port) if os.path.isdir(baseDir+'/accounts/'+handle): shutil.rmtree(baseDir+'/accounts/'+handle) if os.path.isfile(baseDir+'/accounts/'+handle+'.json'):