Flask-Admin Role Based Access - Modify access based on role
<p>I took the Flask-Admin auth example from <a href="https://github.com/flask-admin/flask-admin/tree/master/examples/auth" rel="nofollow noreferrer">here</a> and changed it slightly.</p>

<p>I added the following block to the view below, but it doesn't show the export button. I was expecting it to add the export option to the admin views. It does print <code>---superuser</code> to the console.</p>

<pre><code>        if current_user.has_role('superuser'):
            can_export = True
            print ' ---- superuser '
</code></pre>

<p>I have used the export feature many times before. It will work if I put the statement <code>can_export = True</code> just below <code>class MyModelView(sqla.ModelView):</code> I am using this as an example of controlling access to creating/editing/etc based on the user role. For example, I will want to have a readonly role where can_create=False, can_edit=False, etc.</p>

<p>Can someone help? Can someone tell me what I am doing wrong?</p>

<p>==</p>

<p>This is the entire view.</p>

<pre><code># Create customized model view class
class MyModelView(sqla.ModelView):

    def is_accessible(self):
        if not current_user.is_active or not current_user.is_authenticated:
            return False

        if current_user.has_role('superuser'):
            return True

        return False

    def _handle_view(self, name, **kwargs):
        """
        Override builtin _handle_view in order to redirect users when a view is not accessible.
        """
        if current_user.has_role('superuser'):
            can_export = True
            print ' ---- superuser '

        if not self.is_accessible():
            if current_user.is_authenticated:
                # permission denied
                abort(403)
            else:
                # login
                return redirect(url_for('security.login', next=request.url))
</code></pre>

<p>==</p>

<p>For reference: I put the all the code <a href="https://github.com/dgleba/flaskplayground/blob/master/201roles/auth/app.py" rel="nofollow noreferrer">here</a>.</p>