web/website/scripts/install-native-account-controls.py
2026-11-21 20:22:37 +01:00

39 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Install the existing MyBB account adapter and disable duplicate human account tools."""
import hashlib,json,os,subprocess
from pathlib import Path
import pymysql
def main():
if os.geteuid()!=0:raise RuntimeError('Run as the authorised deployment operator')
source=Path(__file__).resolve().parents[1]/'integrations'/'mybb'
target=Path('/home/dunerealms/apps/integrations/mybb')
files=['central-account-controls.php','inc/plugins/dune_accounts.php']
for name in files:
subprocess.run(['php','-l',str(source/name)],check=True,capture_output=True)
if not (target/'inc/plugins/dune_accounts.php').is_file():raise RuntimeError('Expected existing account adapter is missing')
installed=[]
for name in files:
destination=target/name
metadata=destination.stat() if destination.exists() else (target/'inc/plugins/dune_accounts.php').stat()
temporary=destination.with_name('.'+destination.name+'.central-next')
if temporary.exists():raise RuntimeError('Stale integration staging file')
temporary.write_bytes((source/name).read_bytes());os.chown(temporary,metadata.st_uid,metadata.st_gid);os.chmod(temporary,metadata.st_mode&0o777);temporary.replace(destination)
installed.append({'file':name,'sha256':hashlib.sha256(destination.read_bytes()).hexdigest()})
config=json.loads(Path('/home/dunerealms/.config/integrations.json').read_text())
db=pymysql.connect(host=config['host'],user=config['username'],password=config['password'],database=config['database'])
permissions=['canusepms','cansendpms','cantrackpms','canchangename','canuploadavatars','caneditprofiles','cansendemail']
try:
with db.cursor() as cursor:
# Native administrator group is reserved for the exempt Dev service.
cursor.execute('UPDATE drforum_usergroups SET '+','.join(name+'=0' for name in permissions)+' WHERE gid<>4')
changed=cursor.rowcount
db.commit()
except BaseException:
db.rollback();raise
finally:db.close()
php="define('IN_MYBB',1);$_SERVER['SCRIPT_NAME']='/index.php';require './inc/init.php';$cache->update_usergroups();echo 'Native group cache refreshed';"
subprocess.run(['php','-r',php],cwd=target,check=True,capture_output=True)
print(json.dumps({'installed':installed,'humanGroupsUpdated':changed,'nativeDevAdministratorPreserved':True,'privateMessagesDeleted':False,'forumCoreChanged':False}))
if __name__=='__main__':main()