If not, are there any projects that have added this feature to Flask-Login? Otherwise, it appears to be a bit daunting to migrate from Flask-Login to Flask-User. Otherwise, is there any sort of direction out there for migrating from Flask-Login to Flask-User?
Asked
Active
Viewed 4,111 times
6
-
https://github.com/raddevon/flask-permissions – Mad Wombat Sep 11 '18 at 22:41
-
Unfortunately, that project is no longer supported and after installing Flask-Permissions via pip, it does not recognize the Github page's suggested import. – user1995565 Sep 12 '18 at 02:02
-
1you might want to check Flask Security, which has support for roles – gittert Sep 12 '18 at 13:53
-
@gittert als Flask Security is no longer mantained.... you have to check Flask Security-too what a mess... – G M Feb 23 '22 at 14:18
1 Answers
7
Again, answering my own question here for anyone else curious how you can add the feature of handling multiple roles while still using Flask-Login. I created the below decorator which just checks the current_user.role to see if it is "Admin". You should also check the same thing when letting the user log in, depending on if they are logging in to the admin or the user panel.
from functools import wraps
def admin_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if current_user.role == "Admin":
return f(*args, **kwargs)
else:
flash("You need to be an admin to view this page.")
return redirect(url_for('index'))
return wrap
nbro
- 15,395
- 32
- 113
- 196
user1995565
- 165
- 1
- 10
-
how to add role to the User Object. i tried hardcoding role at the time User object creation to see if i can fetch it from current_user. but i get - AttributeError: 'User' object has no attribute 'role' – rakesh Aug 19 '20 at 03:59