filter code by userid

meevans1
meevans1 Registered Posts: 5

Hi all,

I'm making a webapp with Dash.

I'd like to make only parts of a dataframe visible to certain users, depending on permissions.

I've followed this to get the current user https://doc.dataiku.com/dss/latest/webapps/security.html#dash-webapp

and defined a dictionary like

user_permissions = {
    'user.name':['School'],
    ....
}

I'd then like to do

if user in user_permissions:
    df = df[df['_group'].str.contains(('|'.join(user_permissions[user])))]

but I'm not sure how to use get `user` from the function `update_output_div`. Could anyone help please? Thanks in advance!


Operating system used: Mac OSX

Tagged:

Best Answer

  • Zach
    Zach Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 153 Dataiker
    edited July 17 Answer ✓

    Hi @meevans1
    ,

    You can get the username from the "authIdentifier" key in the "auth_info_brower" dict.

    Here's a complete example:

    import dataiku
    from dash import dcc, html, Input, Output
    from flask import request
    
    user_permissions = {
        'user1': ['School'],
        'user2': ['Foo', 'Bar']
    }
    
    app.layout = html.Div([
        html.H6('Change the value in the text box to see callbacks in action!'),
        html.Div([
            'Input: ',
            dcc.Input(id='my-input', value='initial value', type='text')
        ]),
        html.Br(),
        html.Div(id='my-output')
    ])
    
    @app.callback(
        Output(component_id='my-output', component_property='children'),
        Input(component_id='my-input', component_property='value')
    )
    def update_output_div(input_value):
        request_headers = dict(request.headers)
        auth_info_brower = dataiku.api_client().get_auth_info_from_browser_headers(request_headers)
        
        user = auth_info_brower['authIdentifier']
        if user in user_permissions:
            return '|'.join(user_permissions[user])
        else:
            return 'User not authorized'

    Thanks,

    Zach

Answers

Setup Info
    Tags
      Help me…