Flask-security add role to user (MongoEngine)

2.2k Views Asked by At

I've built a small api that should be able to add the role to a user:

user = user_datastore.find_user(email=flask.request.args.get('email'))
role = user_datastore.find_role(flask.request.args.get('role'))
result = user_datastore.add_role_to_user(user, role)

(role & email are sent through the api). It finds correct user and role objects, the result gives True but nothing is added to the user's role list which stays [] without any roles.

My setup is similar to/directly based upon the MongoEngine example

It must be something really simple, but I cannot seem to find it :/

BR Carst

2

There are 2 best solutions below

1
On

May be I am missing something but you need to commit the changes, right ?

user = user_datastore.find_user(email=flask.request.args.get('email'))
role = user_datastore.find_role(flask.request.args.get('role'))
result = user_datastore.add_role_to_user(user, role)

# to save in database, you must commit (assuming 'db' is the object)
db.session.commit()
0
On

As stated by Matt W, it seems to be a bug. I've logged an issue in github at: https://github.com/mattupstate/flask-security/issues/170