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

Override routes? Want to make invite_user available only to admin role

Open GitMorin opened this issue 4 years ago • 2 comments

Is there a way I can overwrite the user_invite functionality so only users with admin role can access the route? I dont want anyone to be able to invite new users.

GitMorin avatar Oct 31 '19 13:10 GitMorin

Using a CustomUserManager, you can over ride the default invitation view which is here:

https://github.com/lingthio/Flask-User/blob/5c652e6479036c3d33aa1626524e4e65bd3b961e/flask_user/user_manager__views.py#L317-L320

and add the roles_required decorator

class CustomUserManager(UserManager):
    
    @login_required
    @roles_required('specialrole')
    def invite_user_view(self):
        """ Allows users to send invitations to register an account. """

        <rest of original code> 

carissableker avatar Dec 14 '21 15:12 carissableker

@carissableker Thanks 😊, I implemented your solution and it worked like magic. and here is how I did it

# Customize Flask-User
class CustomUserManager(UserManager):
  # Making user invitation limited to admin role only
  @roles_required('admin')
  def invite_user_view(self):
    return super().invite_user_view()

# Setup Flask-User and specify the User data-model
user_manager = CustomUserManager(app, db, User)

mgonline86 avatar Apr 09 '22 09:04 mgonline86