Merge branch 'main' of ssh://code.freedombone.net:2222/bashrc/epicyon into main

main
Bob Mottram 2021-03-08 22:12:25 +00:00
commit 233666ba90
5 changed files with 75 additions and 56 deletions

View File

@ -1463,7 +1463,9 @@ class PubServer(BaseHTTPRequestHandler):
return return
loginNickname, loginPassword, register = \ loginNickname, loginPassword, register = \
htmlGetLoginCredentials(loginParams, self.server.lastLoginTime) htmlGetLoginCredentials(loginParams,
self.server.lastLoginTime,
self.server.domain)
if loginNickname: if loginNickname:
if isSystemAccount(loginNickname): if isSystemAccount(loginNickname):
print('Invalid username login: ' + loginNickname + print('Invalid username login: ' + loginNickname +

View File

@ -1703,6 +1703,10 @@ 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()
if args.domain:
domain = args.domain
else:
domain = getConfigParam(baseDir, 'domain')
configuredDomain = getConfigParam(baseDir, 'domain') configuredDomain = getConfigParam(baseDir, 'domain')
if configuredDomain: if configuredDomain:

112
roles.py
View File

@ -18,30 +18,10 @@ from utils import loadJson
from utils import saveJson from utils import saveJson
def clearModeratorStatus(baseDir: str) -> None: def _clearRoleStatus(baseDir: str, role: str) -> None:
"""Removes moderator status from all accounts """Removes role status from all accounts
This could be slow if there are many users, but only happens This could be slow if there are many users, but only happens
rarely when moderators are appointed or removed rarely when roles are appointed or removed
"""
directory = os.fsencode(baseDir + '/accounts/')
for f in os.scandir(directory):
f = f.name
filename = os.fsdecode(f)
if filename.endswith(".json") and '@' in filename:
filename = os.path.join(baseDir + '/accounts/', filename)
if '"moderator"' in open(filename).read():
actorJson = loadJson(filename)
if actorJson:
if actorJson['roles'].get('instance'):
if 'moderator' in actorJson['roles']['instance']:
actorJson['roles']['instance'].remove('moderator')
saveJson(actorJson, filename)
def clearEditorStatus(baseDir: str) -> None:
"""Removes editor status from all accounts
This could be slow if there are many users, but only happens
rarely when editors are appointed or removed
""" """
directory = os.fsencode(baseDir + '/accounts/') directory = os.fsencode(baseDir + '/accounts/')
for f in os.scandir(directory): for f in os.scandir(directory):
@ -52,59 +32,76 @@ def clearEditorStatus(baseDir: str) -> None:
if not filename.endswith(".json"): if not filename.endswith(".json"):
continue continue
filename = os.path.join(baseDir + '/accounts/', filename) filename = os.path.join(baseDir + '/accounts/', filename)
if '"editor"' not in open(filename).read(): if '"' + role + '"' not in open(filename).read():
continue continue
actorJson = loadJson(filename) actorJson = loadJson(filename)
if not actorJson: if not actorJson:
continue continue
if actorJson['roles'].get('instance'): if actorJson['roles'].get('instance'):
if 'editor' in actorJson['roles']['instance']: if role in actorJson['roles']['instance']:
actorJson['roles']['instance'].remove('editor') actorJson['roles']['instance'].remove(role)
saveJson(actorJson, filename) saveJson(actorJson, filename)
def _addModerator(baseDir: str, nickname: str, domain: str) -> None: def clearEditorStatus(baseDir: str) -> None:
"""Adds a moderator nickname to the file """Removes editor status from all accounts
This could be slow if there are many users, but only happens
rarely when editors are appointed or removed
"""
_clearRoleStatus(baseDir, 'editor')
def clearModeratorStatus(baseDir: str) -> None:
"""Removes moderator status from all accounts
This could be slow if there are many users, but only happens
rarely when moderators are appointed or removed
"""
_clearRoleStatus(baseDir, 'moderator')
def _addRole(baseDir: str, nickname: str, domain: str,
roleFilename: str) -> None:
"""Adds a role nickname to the file
""" """
if ':' in domain: if ':' in domain:
domain = domain.split(':')[0] domain = domain.split(':')[0]
moderatorsFile = baseDir + '/accounts/moderators.txt' roleFile = baseDir + '/accounts/' + roleFilename
if os.path.isfile(moderatorsFile): if os.path.isfile(roleFile):
# is this nickname already in the file? # is this nickname already in the file?
with open(moderatorsFile, "r") as f: with open(roleFile, "r") as f:
lines = f.readlines() lines = f.readlines()
for moderator in lines: for roleNickname in lines:
moderator = moderator.strip('\n').strip('\r') roleNickname = roleNickname.strip('\n').strip('\r')
if moderator == nickname: if roleNickname == nickname:
return return
lines.append(nickname) lines.append(nickname)
with open(moderatorsFile, 'w+') as f: with open(roleFile, 'w+') as f:
for moderator in lines: for roleNickname in lines:
moderator = moderator.strip('\n').strip('\r') roleNickname = roleNickname.strip('\n').strip('\r')
if len(moderator) > 1: if len(roleNickname) > 1:
if os.path.isdir(baseDir + '/accounts/' + if os.path.isdir(baseDir + '/accounts/' +
moderator + '@' + domain): roleNickname + '@' + domain):
f.write(moderator + '\n') f.write(roleNickname + '\n')
else: else:
with open(moderatorsFile, "w+") as f: with open(roleFile, "w+") as f:
if os.path.isdir(baseDir + '/accounts/' + if os.path.isdir(baseDir + '/accounts/' +
nickname + '@' + domain): nickname + '@' + domain):
f.write(nickname + '\n') f.write(nickname + '\n')
def _removeModerator(baseDir: str, nickname: str): def _removeRole(baseDir: str, nickname: str, roleFilename: str) -> None:
"""Removes a moderator nickname from the file """Removes a role nickname from the file
""" """
moderatorsFile = baseDir + '/accounts/moderators.txt' roleFile = baseDir + '/accounts/' + roleFilename
if not os.path.isfile(moderatorsFile): if not os.path.isfile(roleFile):
return return
with open(moderatorsFile, "r") as f: with open(roleFile, "r") as f:
lines = f.readlines() lines = f.readlines()
with open(moderatorsFile, 'w+') as f: with open(roleFile, 'w+') as f:
for moderator in lines: for roleNickname in lines:
moderator = moderator.strip('\n').strip('\r') roleNickname = roleNickname.strip('\n').strip('\r')
if len(moderator) > 1 and moderator != nickname: if len(roleNickname) > 1 and roleNickname != nickname:
f.write(moderator + '\n') f.write(roleNickname + '\n')
def setRole(baseDir: str, nickname: str, domain: str, def setRole(baseDir: str, nickname: str, domain: str,
@ -120,12 +117,18 @@ def setRole(baseDir: str, nickname: str, domain: str,
if not os.path.isfile(actorFilename): if not os.path.isfile(actorFilename):
return False return False
roleFiles = {
"moderator": "moderators.txt",
"editor": "editors.txt"
}
actorJson = loadJson(actorFilename) actorJson = loadJson(actorFilename)
if actorJson: if actorJson:
if role: if role:
# add the role # add the role
if project == 'instance' and 'role' == 'moderator': if project == 'instance':
_addModerator(baseDir, nickname, domain) if roleFiles.get(role):
_addRole(baseDir, nickname, domain, roleFiles[role])
if actorJson['roles'].get(project): if actorJson['roles'].get(project):
if role not in actorJson['roles'][project]: if role not in actorJson['roles'][project]:
actorJson['roles'][project].append(role) actorJson['roles'][project].append(role)
@ -134,7 +137,8 @@ def setRole(baseDir: str, nickname: str, domain: str,
else: else:
# remove the role # remove the role
if project == 'instance': if project == 'instance':
_removeModerator(baseDir, nickname) if roleFiles.get(role):
_removeRole(baseDir, nickname, roleFiles[role])
if actorJson['roles'].get(project): if actorJson['roles'].get(project):
actorJson['roles'][project].remove(role) actorJson['roles'][project].remove(role)
# if the project contains no roles then remove it # if the project contains no roles then remove it

View File

@ -81,6 +81,7 @@ def _speakerPronounce(baseDir: str, sayText: str, translate: {}) -> str:
"XMPP": "X-M-P-P", "XMPP": "X-M-P-P",
"xmpp": "X-M-P-P", "xmpp": "X-M-P-P",
"sql": "S-Q-L", "sql": "S-Q-L",
".js": " dot J-S",
"PSQL": "Postgres S-Q-L", "PSQL": "Postgres S-Q-L",
"SQL": "S-Q-L", "SQL": "S-Q-L",
"coop": "co-op", "coop": "co-op",

View File

@ -18,7 +18,8 @@ from theme import getTextModeLogo
def htmlGetLoginCredentials(loginParams: str, def htmlGetLoginCredentials(loginParams: str,
lastLoginTime: int) -> (str, str, bool): lastLoginTime: int,
domain: str) -> (str, str, bool):
"""Receives login credentials via HTTPServer POST """Receives login credentials via HTTPServer POST
""" """
if not loginParams.startswith('username='): if not loginParams.startswith('username='):
@ -37,6 +38,13 @@ def htmlGetLoginCredentials(loginParams: str,
if '=' in arg: if '=' in arg:
if arg.split('=', 1)[0] == 'username': if arg.split('=', 1)[0] == 'username':
nickname = arg.split('=', 1)[1] nickname = arg.split('=', 1)[1]
if nickname.startswith('@'):
nickname = nickname[1:]
if '@' in nickname:
# the full nickname@domain has been entered
handleDomain = nickname.split('@')[1].strip()
if handleDomain == domain:
nickname = nickname.split('@')[0]
elif arg.split('=', 1)[0] == 'password': elif arg.split('=', 1)[0] == 'password':
password = arg.split('=', 1)[1] password = arg.split('=', 1)[1]
elif arg.split('=', 1)[0] == 'register': elif arg.split('=', 1)[0] == 'register':