Discover all of the brand-new features and improvements to existing capabilities in the Dataiku 11.3 updateLET'S GO

filter code by userid

Solved!
meevans1
Level 2
filter code by userid

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

0 Kudos
1 Solution
ZachM
Dataiker

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

View solution in original post

2 Replies
ZachM
Dataiker

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

meevans1
Level 2
Author

thank you very much Zach! I've got it now

0 Kudos