Function for removing an account

master
Bob Mottram 2019-08-13 12:59:38 +01:00
parent d79a94af4d
commit 2cb5366ee9
3 changed files with 71 additions and 21 deletions

View File

@ -27,6 +27,8 @@ from person import personLookup
from person import personBoxJson from person import personBoxJson
from person import createSharedInbox from person import createSharedInbox
from person import isSuspended from person import isSuspended
from person import suspendAccount
from person import unsuspendAccount
from posts import outboxMessageCreateWrap from posts import outboxMessageCreateWrap
from posts import savePostToBox from posts import savePostToBox
from posts import sendToFollowers from posts import sendToFollowers
@ -2098,11 +2100,40 @@ class PubServer(BaseHTTPRequestHandler):
# moderator action buttons # moderator action buttons
if authorized and '/users/' in self.path and \ if authorized and '/users/' in self.path and \
self.path.endswith('/moderatoraction'): self.path.endswith('/moderationaction'):
actorStr=self.path.replace('/moderatoraction','') actorStr=self.path.replace('/moderationaction','')
length = int(self.headers['Content-length']) length = int(self.headers['Content-length'])
moderationParams=self.rfile.read(length).decode('utf-8') moderationParams=self.rfile.read(length).decode('utf-8')
print('moderationParams: '+moderationParams) print('moderationParams: '+moderationParams)
if '&' in moderationParams:
moderationText=None
moderationButton=None
for moderationStr in moderationParams.split('&'):
if moderationStr=='moderationAction':
if '=' in moderationStr:
moderationText=moderationStr.split('=')[1].strip()
moderationText=moderationText.replace('+',' ').replace('%40','@').replace('%3A',':').replace('%23','#').strip()
elif moderationStr.startswith('submitBlock'):
moderationButton='block'
elif moderationStr.startswith('submitUnblock'):
moderationButton='unblock'
elif moderationStr.startswith('submitSuspend'):
moderationButton='suspend'
elif moderationStr.startswith('submitUnsuspend'):
moderationButton='unsuspend'
elif moderationStr.startswith('submitRemove'):
moderationButton='remove'
if moderationButton and moderationText:
nickname=moderationText
if nickname.startswith('http') or \
nickname.startswith('dat'):
nickname=getNicknameFromActor(nickname)
if '@' in nickname:
nickname=nickname.split('@')[0]
if moderationButton=='suspend':
suspendAccount(self.server.baseDir,nickname,self.server.salts)
if moderationButton=='unsuspend':
unsuspendAccount(self.server.baseDir,nickname)
self._redirect_headers(actorStr+'/moderation',cookie) self._redirect_headers(actorStr+'/moderation',cookie)
self.server.POSTbusy=False self.server.POSTbusy=False
return return

View File

@ -12,6 +12,7 @@ from person import createCapabilitiesInbox
from person import setPreferredNickname from person import setPreferredNickname
from person import setBio from person import setBio
from person import setProfileImage from person import setProfileImage
from person import removeAccount
from skills import setSkillLevel from skills import setSkillLevel
from roles import setRole from roles import setRole
from person import setOrganizationScheme from person import setOrganizationScheme
@ -843,25 +844,7 @@ if args.rmaccount:
if not args.domain or not getConfigParam(baseDir,'domain'): if not args.domain or not getConfigParam(baseDir,'domain'):
print('Use the --domain option to set the domain name') print('Use the --domain option to set the domain name')
sys.exit() sys.exit()
handle=nickname+'@'+domain if removeAccount(baseDir,nickname,domain):
accountRemoved=False
removePassword(baseDir,nickname)
if os.path.isdir(baseDir+'/accounts/'+handle):
shutil.rmtree(baseDir+'/accounts/'+handle)
accountRemoved=True
if os.path.isfile(baseDir+'/accounts/'+handle+'.json'):
os.remove(baseDir+'/accounts/'+handle+'.json')
accountRemoved=True
if os.path.isfile(baseDir+'/wfendpoints/'+handle+'.json'):
os.remove(baseDir+'/wfendpoints/'+handle+'.json')
accountRemoved=True
if os.path.isfile(baseDir+'/keys/private/'+handle+'.key'):
os.remove(baseDir+'/keys/private/'+handle+'.key')
accountRemoved=True
if os.path.isfile(baseDir+'/keys/public/'+handle+'.pem'):
os.remove(baseDir+'/keys/public/'+handle+'.pem')
accountRemoved=True
if accountRemoved:
print('Account for '+handle+' was removed') print('Account for '+handle+' was removed')
sys.exit() sys.exit()

View File

@ -11,6 +11,7 @@ import commentjson
import os import os
import fileinput import fileinput
import subprocess import subprocess
import shutil
from pprint import pprint from pprint import pprint
from pathlib import Path from pathlib import Path
from Crypto.PublicKey import RSA from Crypto.PublicKey import RSA
@ -21,6 +22,7 @@ from posts import createInbox
from posts import createOutbox from posts import createOutbox
from posts import createModeration from posts import createModeration
from auth import storeBasicCredentials from auth import storeBasicCredentials
from auth import removePassword
from roles import setRole from roles import setRole
from media import removeMetaData from media import removeMetaData
from utils import validNickname from utils import validNickname
@ -532,3 +534,37 @@ def suspendAccount(baseDir: str,nickname: str,salts: {}) -> None:
suspendedFile.write(nickname+'\n') suspendedFile.write(nickname+'\n')
suspendedFile.close() suspendedFile.close()
salts[nickname]=createPassword(32) salts[nickname]=createPassword(32)
def removeAccount(baseDir: str,nickname: str,domain: str) -> bool:
"""Removes an account
"""
# Don't suspend the admin
adminNickname=getConfigParam(baseDir,'admin')
if nickname==adminNickname:
return False
# Don't suspend moderators
moderatorsFile=baseDir+'/accounts/moderators.txt'
if os.path.isfile(moderatorsFile):
with open(moderatorsFile, "r") as f:
lines = f.readlines()
for moderator in lines:
if moderator.strip('\n')==nickname:
return False
unsuspendAccount(baseDir,nickname)
handle=nickname+'@'+domain
removePassword(baseDir,nickname)
if os.path.isdir(baseDir+'/accounts/'+handle):
shutil.rmtree(baseDir+'/accounts/'+handle)
if os.path.isfile(baseDir+'/accounts/'+handle+'.json'):
os.remove(baseDir+'/accounts/'+handle+'.json')
if os.path.isfile(baseDir+'/wfendpoints/'+handle+'.json'):
os.remove(baseDir+'/wfendpoints/'+handle+'.json')
if os.path.isfile(baseDir+'/keys/private/'+handle+'.key'):
os.remove(baseDir+'/keys/private/'+handle+'.key')
if os.path.isfile(baseDir+'/keys/public/'+handle+'.pem'):
os.remove(baseDir+'/keys/public/'+handle+'.pem')
if os.path.isdir(baseDir+'/sharefiles/'+nickname):
shutil.rmtree(baseDir+'/sharefiles/'+nickname)
return True