From 9cc5efbdbf1140d0331eb2d2cd2206ed692f7a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominic=20K=C3=B6nig?= Date: Wed, 13 Jul 2022 23:39:33 +0200 Subject: [PATCH] Upgrade script to remove obsolete role assignments --- VERSION | 2 +- .../templates/RLPPTM/upgrade/1.18.0-1.18.1.py | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 modules/templates/RLPPTM/upgrade/1.18.0-1.18.1.py diff --git a/VERSION b/VERSION index 06cd5d97f..d352b3e4e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -RLPPTM-1.18.1 (2022-07-13 23:07:42) +RLPPTM-1.18.1 (2022-07-13 23:07:33) diff --git a/modules/templates/RLPPTM/upgrade/1.18.0-1.18.1.py b/modules/templates/RLPPTM/upgrade/1.18.0-1.18.1.py new file mode 100644 index 000000000..4e086f852 --- /dev/null +++ b/modules/templates/RLPPTM/upgrade/1.18.0-1.18.1.py @@ -0,0 +1,69 @@ +# Database upgrade script +# +# RLPPTM Template Version 1.18.0 => 1.18.1 +# +# Execute in web2py folder after code upgrade like: +# python web2py.py -S eden -M -R applications/eden/modules/templates/RLPPTM/upgrade/1.18.0-1.18.1.py +# +import sys + +#from core import S3Duplicate + +# Override auth (disables all permission checks) +auth.override = True + +# Failed-flag +failed = False + +# Info +def info(msg): + sys.stderr.write("%s" % msg) +def infoln(msg): + sys.stderr.write("%s\n" % msg) + +# Load models for tables +#otable = s3db.org_organisation + +IMPORT_XSLT_FOLDER = os.path.join(request.folder, "static", "formats", "s3csv") +TEMPLATE_FOLDER = os.path.join(request.folder, "modules", "templates", "RLPPTM") + +# ----------------------------------------------------------------------------- +# Remove role assignments for deleted entities +# +if not failed: + infoln("Remove obsolete role assignments...") + + mtable = auth.settings.table_membership + etable = s3db.pr_pentity + + left = etable.on(etable.pe_id == mtable.pe_id) + query = (mtable.pe_id != None) & \ + (mtable.pe_id != 0) & \ + (mtable.deleted == False) & \ + ((etable.pe_id == None) | (etable.deleted == True)) + rows = db(query).select(mtable.id, + mtable.user_id, + mtable.group_id, + mtable.pe_id, + left = left, + ) + info("...found %s obsolete assignments..." % len(rows)) + removed = 0 + for row in rows: + auth.s3_remove_role(row.user_id, + row.group_id, + for_pe = row.pe_id, + ) + info(".") + removed += 1 + infoln("...done (%s removed)." % removed) + +# ----------------------------------------------------------------------------- +# Finishing up +# +if failed: + db.rollback() + infoln("UPGRADE FAILED - Action rolled back.") +else: + db.commit() + infoln("UPGRADE SUCCESSFUL.")