Skip to content

Commit

Permalink
Revert "FlaskPlugin: use get_id() instead of id attr"
Browse files Browse the repository at this point in the history
This reverts commit 7eda527.

This was actually a misinterpretation of the API:

The API expects get_id() to be a unique string, that may or may not be
an integer. This feature is only used to uniquely identify the user, but
its not the same as the user's primary key.
https://flask-login.readthedocs.io/en/latest/#your-user-class

Please take a note on how get_id() is expected to behave on this feature
of flask-login:
https://flask-login.readthedocs.io/en/latest/#alternative-tokens

This means it can return any arbitrary string to identify the user which
can be swapped to any other value to invalidate all sessions. This
explicitly states that it is not the same as the user's primary id which
is also used as foreign key on the table.

sqlalchemy-continuum's transaction tables reference the user with a
foreign key of the actual id. If a downstream application uses the
alternative-tokens feature as described by the flask-login
documentation, this breaks horribly as sqlalchemy-continuum will then
try to insert an arbitrary unique string of the user as the foreign key
to reference the user in the user table.

Fixes kvesteri#316
Caused by kvesteri#149
  • Loading branch information
anthraxx committed Jan 3, 2023
1 parent f5a3f80 commit 543b398
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sqlalchemy_continuum/plugins/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def fetch_current_user_id():
if _app_ctx_stack.top is None or _request_ctx_stack.top is None:
return
try:
return current_user.get_id()
return current_user.id
except AttributeError:
return

Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/test_flask.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from flask import Flask, url_for
from flask_login import LoginManager, UserMixin, login_user
from flask_login import LoginManager, login_user
from flask_sqlalchemy import SQLAlchemy, _SessionSignalEvents
from flexmock import flexmock

Expand Down Expand Up @@ -63,7 +63,7 @@ def teardown_method(self, method):
def create_models(self):
TestCase.create_models(self)

class User(self.Model, UserMixin):
class User(self.Model):
__tablename__ = 'user'
__versioned__ = {
'base_classes': (self.Model, )
Expand Down

0 comments on commit 543b398

Please sign in to comment.