Flask-Middleware / flask-security

Quick and simple security for Flask applications
MIT License
622 stars 154 forks source link

enable custom role table name when using `fsqla_v3.FsUserMixin` #976

Closed NicholasZXT closed 1 month ago

NicholasZXT commented 1 month ago

When using FsUserMixin class from flask_security/models/fsqla_v3.py,the role model class must be named with Role,otherwise it's not working. The reason is the source code snippet as follow:

class FsUserMixin(FsUserMixinV2):
    """User information"""
   ...

    @declared_attr
    def roles(cls):
        return FsModels.db.relationship(
            "Role",   # here is the problem, it references role model class with name 'Role'
            secondary=FsModels.roles_users,
            backref=FsModels.db.backref(
                "users", lazy="dynamic", cascade_backrefs=False
            ),
        )

Due to the code, role model likes class SecurityRole(db.Model, fsqla.FsRoleMixin) will failed to run. It's a little problem, but I do struggle with it for a while. Maybe it's not bad to add some custom for users like me in the future release.

jwag956 commented 1 month ago

There are 2 different things here - the DB table name and the class name.

Having your own DB tablename is supported and works as documented here: https://flask-security-too.readthedocs.io/en/stable/api.html#flask_security.models.fsqla.FsModels

The way to specify your tablename using sqlalchemy is: tablename = "myrole"

You are correct that changing the class isn't currently supported.