Forums

flask appbuilder check role of user

Hi i want to check which role a user has. A admin user should see all entries of an object. If the user is not an admin the user should see only its own entries. I got the stuff ready for the user who only can see its data. But i have a problem with the role of the user. When i call the function get_user_role() i get the error Working outside of application context. When i leave the brakets away it is not called. I can not use the base_filtering because i have not attribute role in the model. Maybe i have a understanding problem with the whole context thing. Does anyone has a clue how i could achieve the my goal. thanks in advance

def get_user():
return g.user.id


def get_user_role():
return g.user.roles

class MeasuringDevicePosView(ModelView):
datamodel = SQLAInterface(MeasuringDevicePos)

if(get_user_role != 'Admin'):
    base_filters = [['user_id', FilterEqualFunction, get_user]]

You need to call the function with () to get the return value.

when i call the function with () then i get the message:

This typically means that you attempted to use functionality that needed to interface with the current application object in some way. To solve this, set up an application context with app.app_context(). See the documentation for more information.

actually i found a way to get the user.roles but i can not solve my problem with it.

I want to show all data if the user has an admin role. If the user is not an admin the user should only see its own created data. That the user sees its own created data is possible with the following code

base_filters = [['user_id', FilterEqualFunction, get_user]]

no i want to make something like this:

base_filters = [['user_id', FilterEqualFunction, get_user],['user.roles', FilterNotEqual, 'Admin']]

Is there a possibilit to make a check something like this: if the user.roles is not admin then use the filer user_id == current_user.id

In what context are you running that code? Is it inside your website's code, or are you working on a command-line script? Or something else?

is is inside my website code. it is inside view.py

from flask import render_template
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_appbuilder import ModelView, ModelRestApi
from flask_appbuilder.models.sqla.filters import FilterEqualFunction, FilterContains, FilterNotEqual, FilterInFunction
from flask import g
import logging
from .models import MeasuringDevice, MeasuringDevicePos, MedicalProduct, MedicalProductPos, Producer
import app
from . import appbuilder, db
from flask_login import current_user

def get_user():
    #print(str(g.user.id))
    return g.user.id

class MeasuringDevicePosView(ModelView):
    datamodel = SQLAInterface(MeasuringDevicePos)

    base_filters = [['user_id', FilterEqualFunction, get_user]]

[edit by admin: formatting]

The error "Working outside of application context" normally means that some of your code is being called from outside a view function. Is it possible that your filtering code is running at application startup time rather than inside the view code itself?

It seems that the view class is not used whitin the application. Actualy i want to do a filtering in the MeasuringDevicePosView like something like g.user.id what that can also not be called in the class. I also get the error. One possibility is define a custom Filter. I right now don't understand this is working and why the basefilter is working but when i call g.user.id is not.

None of us here have used flask appbuilder, so we're just guessing about how you should use it. I would suggest that you find some forums that are dedicated to flask appbuilder to ask you questions there.