From 712946983588ecb0f54a9ff4cbf647cf24db7047 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 29 Jul 2021 17:18:01 +0200 Subject: [PATCH] [roles] MySQL is caseinsensitive for strings so workaround it for renaming roles --- flaschengeist/controller/roleController.py | 6 +++--- flaschengeist/database.py | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/flaschengeist/controller/roleController.py b/flaschengeist/controller/roleController.py index a280388..1e4e6e1 100644 --- a/flaschengeist/controller/roleController.py +++ b/flaschengeist/controller/roleController.py @@ -2,7 +2,7 @@ from sqlalchemy.exc import IntegrityError from werkzeug.exceptions import BadRequest, NotFound from flaschengeist.models.user import Role, Permission -from flaschengeist.database import db +from flaschengeist.database import db, case_sensitive from flaschengeist import logger from flaschengeist.utils.hook import Hook @@ -36,8 +36,8 @@ def update_role(role, new_name): except IntegrityError: logger.debug("IntegrityError: Role might still be in use", exc_info=True) raise BadRequest("Role still in use") - elif role.name != new_name: - if db.session.query(db.exists().where(Role.name == new_name)).scalar(): + else: + if role.name == new_name or db.session.query(db.exists().where(Role.name == case_sensitive(new_name))).scalar(): raise BadRequest("Name already used") role.name = new_name db.session.commit() diff --git a/flaschengeist/database.py b/flaschengeist/database.py index f0b13d6..85fc3e1 100644 --- a/flaschengeist/database.py +++ b/flaschengeist/database.py @@ -1,3 +1,10 @@ from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() + + +def case_sensitive(s): + if db.session.bind.dialect.name == "mysql": + from sqlalchemy import func + return func.binary(s) + return s