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
loginNickname, loginPassword, register = \
htmlGetLoginCredentials(loginParams, self.server.lastLoginTime)
htmlGetLoginCredentials(loginParams,
self.server.lastLoginTime,
self.server.domain)
if loginNickname:
if isSystemAccount(loginNickname):
print('Invalid username login: ' + loginNickname +

View File

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

112
roles.py
View File

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

View File

@ -18,7 +18,8 @@ from theme import getTextModeLogo
def htmlGetLoginCredentials(loginParams: str,
lastLoginTime: int) -> (str, str, bool):
lastLoginTime: int,
domain: str) -> (str, str, bool):
"""Receives login credentials via HTTPServer POST
"""
if not loginParams.startswith('username='):
@ -37,6 +38,13 @@ def htmlGetLoginCredentials(loginParams: str,
if '=' in arg:
if arg.split('=', 1)[0] == 'username':
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':
password = arg.split('=', 1)[1]
elif arg.split('=', 1)[0] == 'register':