Skip to content

Instantly share code, notes, and snippets.

@d9k
Created January 13, 2022 07:44
Show Gist options
  • Save d9k/a638071ce7146ef01c27779a51d96f2b to your computer and use it in GitHub Desktop.
Save d9k/a638071ce7146ef01c27779a51d96f2b to your computer and use it in GitHub Desktop.
Superset welcome page redirect
import logging
import pprint
from flask import Flask, redirect
from flask_appbuilder import expose, IndexView
from superset.typing import FlaskResponse
logger = logging.getLogger()
logger.warn("Loading config override for Uniweb");
WELCOME_PAGE_REDIRECT_ADMIN="/superset/dashboard/1/"
WELCOME_PAGE_REDIRECT_DEFAULT="/dashboard/list/"
WELCOME_PAGE_REDIRECT_BY_ROLE={
'Test': '/superset/dashboard/2/',
}
# Change welcome page
# https://stackoverflow.com/a/69930056/1760643
class SupersetDashboardIndexView(IndexView):
@expose("/")
def index(self) -> FlaskResponse:
from superset.views.base import is_user_admin, get_user_roles
user_roles = get_user_roles()
logger.warn('__DEBUG__ user roles: ' + pprint.pformat(user_roles))
if is_user_admin():
return redirect(WELCOME_PAGE_REDIRECT_ADMIN)
else:
for role in user_roles:
role_name = role.name
if role_name in WELCOME_PAGE_REDIRECT_BY_ROLE:
return redirect(WELCOME_PAGE_REDIRECT_BY_ROLE[role_name])
return redirect(WELCOME_PAGE_REDIRECT_DEFAULT)
FAB_INDEX_VIEW = f"{SupersetDashboardIndexView.__module__}.{SupersetDashboardIndexView.__name__}"
@mdeshmu
Copy link

mdeshmu commented May 31, 2022

#get_user_roles has been moved to security manager
#This worked for me:

from superset import security_manager
user_roles = security_manager.get_user_roles()

@ChristopherKi
Copy link

In superset 2.0.1 I had to replace the import
from superset.typing import FlaskResponse
with
from superset.superset_typing import FlaskResponse

@EVNIKHIL
Copy link

EVNIKHIL commented Feb 2, 2023

@ChristopherKi Hi where to put this code. There is no file named superset_config_docker.py. Can you provide the path too and other procedure if any.

@mdeshmu
Copy link

mdeshmu commented Feb 2, 2023

you have to create it

@ChristopherKi
Copy link

@ChristopherKi Hi where to put this code. There is no file named superset_config_docker.py. Can you provide the path too and other procedure if any.

The code snipped can be included in your normal superset config file. The exact location will depend on your general superset setup. I suggest you to take a look at the documentation

@rscarborough1996
Copy link

This works for me, but when someone visits the login page, there is always an "Access is Denied" banner at the top of the screen. Is there any way to avoid this?

@darkitechtor
Copy link

darkitechtor commented Aug 21, 2023

This works for me, but when someone visits the login page, there is always an "Access is Denied" banner at the top of the screen. Is there any way to avoid this?

Hi, @rscarborough1996, I just solved the problem by adding the check-out of the username:

if get_username():
            return redirect("/dashboard/list/")
        else:
            return redirect("/login/")

@chaima317
Copy link

Hello, you have each version of superset ?

@Mavyre
Copy link

Mavyre commented Nov 3, 2023

Doesn't seem to work on 3.0.1. Has anyone tried yet? Can't find a way to make it work for now :(

@Luiggi370z
Copy link

Hi,

This works for me in superset v3:

from flask import redirect, g, request

from flask_appbuilder import expose, IndexView

from superset.extensions import (
    appbuilder,
)

from superset.utils.core import (
    get_user_id,
)

from superset.superset_typing import FlaskResponse

class SupersetIndexView(IndexView):
    @expose("/")
    def index(self) -> FlaskResponse:
        if not g.user or not get_user_id():
            # Do steps for anonymous user e.g.
            return redirect("/login")
        # Do steps for authenticated user e.g.
        return redirect("/dashboard/list")


FAB_INDEX_VIEW = f"{SupersetIndexView.__module__}.{SupersetIndexView.__name__}"

@visharavana
Copy link

Hi,

This works for me in superset v3:

from flask import redirect, g, request

from flask_appbuilder import expose, IndexView

from superset.extensions import (
    appbuilder,
)

from superset.utils.core import (
    get_user_id,
)

from superset.superset_typing import FlaskResponse

class SupersetIndexView(IndexView):
    @expose("/")
    def index(self) -> FlaskResponse:
        if not g.user or not get_user_id():
            # Do steps for anonymous user e.g.
            return redirect("/login")
        # Do steps for authenticated user e.g.
        return redirect("/dashboard/list")


FAB_INDEX_VIEW = f"{SupersetIndexView.__module__}.{SupersetIndexView.__name__}"

Where do I put this code? @Luiggi370z

@ameen7626
Copy link

Hi,
This works for me in superset v3:

from flask import redirect, g, request

from flask_appbuilder import expose, IndexView

from superset.extensions import (
    appbuilder,
)

from superset.utils.core import (
    get_user_id,
)

from superset.superset_typing import FlaskResponse

class SupersetIndexView(IndexView):
    @expose("/")
    def index(self) -> FlaskResponse:
        if not g.user or not get_user_id():
            # Do steps for anonymous user e.g.
            return redirect("/login")
        # Do steps for authenticated user e.g.
        return redirect("/dashboard/list")


FAB_INDEX_VIEW = f"{SupersetIndexView.__module__}.{SupersetIndexView.__name__}"

Where do I put this code? @Luiggi370z

Put this code in a new file superset_config.py and specify it in your docker file or there is another way you can go through this documentations

@visharavana
Copy link

Hi,
This works for me in superset v3:

from flask import redirect, g, request

from flask_appbuilder import expose, IndexView

from superset.extensions import (
    appbuilder,
)

from superset.utils.core import (
    get_user_id,
)

from superset.superset_typing import FlaskResponse

class SupersetIndexView(IndexView):
    @expose("/")
    def index(self) -> FlaskResponse:
        if not g.user or not get_user_id():
            # Do steps for anonymous user e.g.
            return redirect("/login")
        # Do steps for authenticated user e.g.
        return redirect("/dashboard/list")


FAB_INDEX_VIEW = f"{SupersetIndexView.__module__}.{SupersetIndexView.__name__}"

Where do I put this code? @Luiggi370z

Put this code in a new file superset_config.py and specify it in your docker file or there is another way you can go through this documentations

@ameen7626 Thank you for your response; it's working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment