2020-04-04 10:28:58 +00:00
|
|
|
__filename__ = "roles.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2021-01-26 10:07:42 +00:00
|
|
|
__version__ = "1.2.0"
|
2020-04-04 10:28:58 +00:00
|
|
|
__maintainer__ = "Bob Mottram"
|
|
|
|
__email__ = "bob@freedombone.net"
|
|
|
|
__status__ = "Production"
|
|
|
|
|
2019-07-18 15:09:23 +00:00
|
|
|
import os
|
2019-10-22 11:55:06 +00:00
|
|
|
from utils import loadJson
|
|
|
|
from utils import saveJson
|
2019-07-18 15:09:23 +00:00
|
|
|
|
2020-04-04 10:28:58 +00:00
|
|
|
|
2021-03-08 21:07:28 +00:00
|
|
|
def _clearRoleStatus(baseDir: str, role: str) -> None:
|
|
|
|
"""Removes role status from all accounts
|
2020-10-11 19:42:21 +00:00
|
|
|
This could be slow if there are many users, but only happens
|
2021-03-08 21:07:28 +00:00
|
|
|
rarely when roles are appointed or removed
|
2020-10-11 19:42:21 +00:00
|
|
|
"""
|
|
|
|
directory = os.fsencode(baseDir + '/accounts/')
|
|
|
|
for f in os.scandir(directory):
|
|
|
|
f = f.name
|
|
|
|
filename = os.fsdecode(f)
|
2020-11-24 14:06:42 +00:00
|
|
|
if '@' not in filename:
|
|
|
|
continue
|
|
|
|
if not filename.endswith(".json"):
|
|
|
|
continue
|
|
|
|
filename = os.path.join(baseDir + '/accounts/', filename)
|
2021-03-08 21:07:28 +00:00
|
|
|
if '"' + role + '"' not in open(filename).read():
|
2020-11-24 14:06:42 +00:00
|
|
|
continue
|
|
|
|
actorJson = loadJson(filename)
|
|
|
|
if not actorJson:
|
|
|
|
continue
|
2021-05-13 19:58:16 +00:00
|
|
|
if not actorJson.get('affiliation'):
|
|
|
|
continue
|
|
|
|
rolesList = \
|
2021-05-14 20:07:44 +00:00
|
|
|
getRolesFromList(actorJson['affiliation']['roleName'])
|
2021-05-13 19:58:16 +00:00
|
|
|
if role in rolesList:
|
|
|
|
rolesList.remove(role)
|
|
|
|
saveJson(actorJson, filename)
|
2020-10-11 19:42:21 +00:00
|
|
|
|
|
|
|
|
2021-03-08 21:07:28 +00:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
2021-03-08 23:03:02 +00:00
|
|
|
def clearCounselorStatus(baseDir: str) -> None:
|
|
|
|
"""Removes counselor status from all accounts
|
|
|
|
This could be slow if there are many users, but only happens
|
|
|
|
rarely when counselors are appointed or removed
|
|
|
|
"""
|
|
|
|
_clearRoleStatus(baseDir, 'editor')
|
|
|
|
|
|
|
|
|
2021-03-08 21:07:28 +00:00
|
|
|
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
|
2019-08-11 11:25:27 +00:00
|
|
|
"""
|
2019-08-12 21:20:47 +00:00
|
|
|
if ':' in domain:
|
2020-04-04 10:28:58 +00:00
|
|
|
domain = domain.split(':')[0]
|
2021-03-08 21:07:28 +00:00
|
|
|
roleFile = baseDir + '/accounts/' + roleFilename
|
|
|
|
if os.path.isfile(roleFile):
|
2019-08-11 11:25:27 +00:00
|
|
|
# is this nickname already in the file?
|
2021-03-08 21:07:28 +00:00
|
|
|
with open(roleFile, "r") as f:
|
2020-04-04 10:28:58 +00:00
|
|
|
lines = f.readlines()
|
2021-03-08 21:07:28 +00:00
|
|
|
for roleNickname in lines:
|
|
|
|
roleNickname = roleNickname.strip('\n').strip('\r')
|
|
|
|
if roleNickname == nickname:
|
2019-08-11 11:25:27 +00:00
|
|
|
return
|
|
|
|
lines.append(nickname)
|
2021-03-08 21:07:28 +00:00
|
|
|
with open(roleFile, 'w+') as f:
|
|
|
|
for roleNickname in lines:
|
|
|
|
roleNickname = roleNickname.strip('\n').strip('\r')
|
|
|
|
if len(roleNickname) > 1:
|
2020-04-04 10:28:58 +00:00
|
|
|
if os.path.isdir(baseDir + '/accounts/' +
|
2021-03-08 21:07:28 +00:00
|
|
|
roleNickname + '@' + domain):
|
|
|
|
f.write(roleNickname + '\n')
|
2019-08-11 11:27:29 +00:00
|
|
|
else:
|
2021-03-08 21:07:28 +00:00
|
|
|
with open(roleFile, "w+") as f:
|
2020-04-04 10:28:58 +00:00
|
|
|
if os.path.isdir(baseDir + '/accounts/' +
|
|
|
|
nickname + '@' + domain):
|
|
|
|
f.write(nickname + '\n')
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2020-04-04 10:28:58 +00:00
|
|
|
|
2021-03-08 21:07:28 +00:00
|
|
|
def _removeRole(baseDir: str, nickname: str, roleFilename: str) -> None:
|
|
|
|
"""Removes a role nickname from the file
|
2019-08-11 11:25:27 +00:00
|
|
|
"""
|
2021-03-08 21:07:28 +00:00
|
|
|
roleFile = baseDir + '/accounts/' + roleFilename
|
|
|
|
if not os.path.isfile(roleFile):
|
2019-08-11 11:25:27 +00:00
|
|
|
return
|
2021-03-08 21:07:28 +00:00
|
|
|
with open(roleFile, "r") as f:
|
2020-04-04 10:28:58 +00:00
|
|
|
lines = f.readlines()
|
2021-03-08 21:07:28 +00:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
2021-05-13 19:58:16 +00:00
|
|
|
def setRolesFromList(actorJson: {}, rolesList: []) -> None:
|
|
|
|
"""Sets roles from a list
|
|
|
|
"""
|
|
|
|
if actorJson.get('affiliation'):
|
2021-05-14 17:56:26 +00:00
|
|
|
actorJson['affiliation']['roleName'] = rolesList.copy()
|
2021-05-13 19:58:16 +00:00
|
|
|
|
|
|
|
|
2021-05-14 20:07:44 +00:00
|
|
|
def getRolesFromList(rolesList: []) -> []:
|
|
|
|
"""Returns a list of roles from a list
|
2021-05-13 19:58:16 +00:00
|
|
|
"""
|
2021-05-14 20:07:44 +00:00
|
|
|
if isinstance(rolesList, list):
|
|
|
|
rolesList2 = rolesList
|
2021-05-14 17:56:26 +00:00
|
|
|
else:
|
2021-05-14 20:07:44 +00:00
|
|
|
rolesList2 = rolesList.split(',')
|
2021-05-13 19:58:16 +00:00
|
|
|
rolesResult = []
|
2021-05-14 20:07:44 +00:00
|
|
|
for roleName in rolesList2:
|
2021-05-13 19:58:16 +00:00
|
|
|
rolesResult.append(roleName.strip().lower())
|
|
|
|
return rolesResult
|
|
|
|
|
|
|
|
|
2020-04-04 10:28:58 +00:00
|
|
|
def setRole(baseDir: str, nickname: str, domain: str,
|
2021-05-13 19:58:16 +00:00
|
|
|
role: str) -> bool:
|
|
|
|
"""Set a person's role
|
2019-07-18 15:09:23 +00:00
|
|
|
Setting the role to an empty string or None will remove it
|
|
|
|
"""
|
|
|
|
# avoid giant strings
|
2021-05-13 19:58:16 +00:00
|
|
|
if len(role) > 128:
|
2019-07-18 15:09:23 +00:00
|
|
|
return False
|
2020-04-04 10:28:58 +00:00
|
|
|
actorFilename = baseDir + '/accounts/' + \
|
|
|
|
nickname + '@' + domain + '.json'
|
2019-07-18 15:09:23 +00:00
|
|
|
if not os.path.isfile(actorFilename):
|
|
|
|
return False
|
2019-09-30 22:39:02 +00:00
|
|
|
|
2021-03-08 21:18:20 +00:00
|
|
|
roleFiles = {
|
|
|
|
"moderator": "moderators.txt",
|
2021-03-08 23:03:02 +00:00
|
|
|
"editor": "editors.txt",
|
|
|
|
"counselor": "counselors.txt"
|
2021-03-08 21:18:20 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 10:28:58 +00:00
|
|
|
actorJson = loadJson(actorFilename)
|
2020-03-22 21:16:02 +00:00
|
|
|
if actorJson:
|
2021-05-13 19:58:16 +00:00
|
|
|
if not actorJson.get('affiliation'):
|
|
|
|
return False
|
|
|
|
rolesList = \
|
2021-05-14 20:07:44 +00:00
|
|
|
getRolesFromList(actorJson['affiliation']['roleName'])
|
2021-05-13 19:58:16 +00:00
|
|
|
actorChanged = False
|
2019-07-18 15:09:23 +00:00
|
|
|
if role:
|
2019-08-11 11:25:27 +00:00
|
|
|
# add the role
|
2021-05-13 19:58:16 +00:00
|
|
|
if roleFiles.get(role):
|
|
|
|
_addRole(baseDir, nickname, domain, roleFiles[role])
|
|
|
|
if role not in rolesList:
|
|
|
|
rolesList.append(role)
|
|
|
|
rolesList.sort()
|
|
|
|
setRolesFromList(actorJson, rolesList)
|
|
|
|
actorChanged = True
|
2019-07-18 15:09:23 +00:00
|
|
|
else:
|
2019-08-11 11:25:27 +00:00
|
|
|
# remove the role
|
2021-05-13 19:58:16 +00:00
|
|
|
if roleFiles.get(role):
|
|
|
|
_removeRole(baseDir, nickname, roleFiles[role])
|
|
|
|
if role in rolesList:
|
|
|
|
rolesList.remove(role)
|
|
|
|
setRolesFromList(actorJson, rolesList)
|
|
|
|
actorChanged = True
|
|
|
|
if actorChanged:
|
|
|
|
saveJson(actorJson, actorFilename)
|
2019-07-18 16:21:26 +00:00
|
|
|
return True
|