Flask-User icon indicating copy to clipboard operation
Flask-User copied to clipboard

Feature request: Support for MongoDB

Open lingthio opened this issue 11 years ago • 12 comments

This feature requires:

  • a MongoDbAdapter class with a .save(object) method.
  • a TokenManager that accepts string IDs

@sebastiansdrawings has a fork with MongoDB support: See https://github.com/sebastiansdrawings/Flask-User.

lingthio avatar Oct 13 '14 17:10 lingthio

Unfortunately, the user sebastiansdrawings is no longer on GitHub. Do you have any tips on writing a DbAdapter for MongoDB?

billzingler avatar Oct 25 '15 21:10 billzingler

Hi, maybe you have refused this feature in the past or you are working on this request, but for a my personal project I have made some modify to Flask_User 0.6 and I have made a DBAdapter for support MongoDB (with MongoAlchemy) as backend and now this work.

This feature can ben interesting for the project ? If you are interested can be send my code :)

Best Regards Massimiliano :)

massimiliano-dalcero avatar Mar 14 '17 08:03 massimiliano-dalcero

Thanks Massimiliano, If you send me your code, I will try to integrate it into Flask-User at some point (as time permits). You can mail it to: ling DOT thio AT gmail DOT com. Thanks! Ling

On Tue, Mar 14, 2017 at 1:45 AM, Massimiliano Dal Cero < [email protected]> wrote:

Hi, maybe you have refused this feature in the past or you are working on this request, but for a my personal project I have made some modify to Flask_User 0.6 and I have made a DBAdapter for support MongoDB (with MongoAlchemy) as backend and now this work.

This feature can ben interesting for the project ? If you are interested can be send my code :)

Best Regards Massimiliano :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lingthio/Flask-User/issues/37#issuecomment-286356882, or mute the thread https://github.com/notifications/unsubscribe-auth/AEacJG9zGaxiZ6pr6WJARg3a27CK2tZVks5rllPCgaJpZM4CuHjY .

lingthio avatar Mar 14 '17 18:03 lingthio

I send to you a .zip file with the only files that I touched :) (Note: MongoAlchemyAdapter is in init_app.py )

in the code, you can find "#YATTA" for a fast check of code that I modifed ;)

massimiliano-dalcero avatar Mar 15 '17 11:03 massimiliano-dalcero

Because I read today that mongoalchemy is no longer supported (https://github.com/jeffjenkins/MongoAlchemy) I migrated to mongoengine that is alive :)

massimiliano-dalcero avatar Mar 15 '17 21:03 massimiliano-dalcero

I also need a Flask-user with mongo. When feature will be implemented? Thank you!

andreyhammer avatar Mar 31 '17 04:03 andreyhammer

I looked at yattamax's zip file and I can not accept this contribution. There are too many places where one of the following is used:

  • if hasattr(self.db_adapter, 'id_as_str') and self.db_adapter.id_as_str
  • if db_adapter.class.name == "MongoAlchemyAdapter"

This defeats the purpose of making the code agnostic to the underlying DB (and in this case to the underlying DbAdapter).

The approach I'd like to see:

  1. Add a db_adapter.save_object() method that saves an object in MongoAdapter, and does nothing in SQLAlchemyAdapter. Add calls to db_adapter.save_object() in the FlaskUser code without a test.

  2. Change FlaskUser so that record IDs are always treated as a string (this will be a lot of work). Then, in SQLAlchemyAdapter, convert string IDs to integer IDs.

lingthio avatar Aug 25 '17 04:08 lingthio

See #166

lingthio avatar Aug 25 '17 04:08 lingthio

OK. Good news and Bad news:

Good news: Using your example code for MongoAlchemy, I managed to get a MongoAlchemyDbAdapter up and running for most functionalities. Mongo ObjectIDs can be retrieved as a hex string, which can be converted to a Python Integer. There were a lot of places where Flask-User just changes an object attribute, which needed to be replaced with a call to db_adapter.update_object(): user.confirmed_at=now() --> db_adapter.update_object(user, confirmed=now()).

Bad news: These object Id integers have more than 16 digits, which the current token manager can not handle (it assumed integers were less than 16 digits). In v0.9 of Flask-User I was able to make the token manager handle unlimited length integers, but this breaks backward compatibility for v0.6.

Another issue that needs to be resolved is that Flask-User currently relies on User.roles for role-based authentication, implemented as a UserRoles pivot table. This needs to be reworked so that we can use a ListField in MongoAlchemy.

I will continue to work on this in the v0.9 branch of the code, and will update this issue if progress is made.

Ling

lingthio avatar Aug 27 '17 04:08 lingthio

Flask-User v0.9 (alpha) and v1.0 (stable) will have MongoDB support via Flask-MongoEngine. The TokenManager has been rewritten to allow (a list of) integers and strings. I'm not sure when I will be releasing v0.9.

lingthio avatar Sep 04 '17 18:09 lingthio

As far as I can see current version of Flask-User supports Mongo. Can we close this issue? Is it still relevant?

and-semakin avatar Nov 22 '19 18:11 and-semakin