superset icon indicating copy to clipboard operation
superset copied to clipboard

Customizing User model to add more details

Open jay-finn opened this issue 6 years ago • 7 comments

The user model in Superset only contains the following fields:

username
firstName
lastName
userId
isActive
email

If I want to add more (let's say country, location, etc.) how would I go about it? I looked at the UserAttribute class, but not quite sure how to use it.

Would I need to customize the schema for ab_user table? And if so, how can I do that?

jay-finn avatar Apr 02 '20 07:04 jay-finn

For anyone who runs into a similar issue, I resolved this by overriding the User class from flask_appbuilder.security.sqla.models.

from flask_appbuilder.security.sqla.models import User
from sqlalchemy import (
    Column,
    String
)
class CustomUser(User):
    __tablename__ = "ab_user"
    time_zone = Column(String(64))

This adds time_zone to the schema for ab_user. After this, override the add_user function from class SupersetSecurityManager. Note that if you're implementing auth through an external OAuth provider, you'd already be doing this.

from superset.security import SupersetSecurityManager
class CustomSsoSecurityManager(SupersetSecurityManager):
  def add_user(
        self,
        username,
        first_name,
        last_name,
        email,
        role,
        password="",
        hashed_password="",
        time_zone=""
    ):

jay-finn avatar Apr 06 '20 01:04 jay-finn

Hi, can you please share the full code? we want to add a few more fields and we cannot do them into our DB. Is it possible to share the full code?

BigDataArtist avatar Dec 14 '20 17:12 BigDataArtist

image How can i get function get_data_scope() from variable g of flask ? thank all

CaptainHoangLe avatar May 07 '21 08:05 CaptainHoangLe

For anyone who runs into a similar issue, I resolved this by overriding the User class from flask_appbuilder.security.sqla.models.

from flask_appbuilder.security.sqla.models import User
from sqlalchemy import (
    Column,
    String
)
class CustomUser(User):
    __tablename__ = "ab_user"
    time_zone = Column(String(64))

This adds time_zone to the schema for ab_user. After this, override the add_user function from class SupersetSecurityManager. Note that if you're implementing auth through an external OAuth provider, you'd already be doing this.

from superset.security import SupersetSecurityManager
class CustomSsoSecurityManager(SupersetSecurityManager):
  def add_user(
        self,
        username,
        first_name,
        last_name,
        email,
        role,
        password="",
        hashed_password="",
        time_zone=""
    ):

@jay-finn I'm trying the solution you indicated but I'm having a lot of problems, do I have to upgrade or migrate? if you have the source would appreciate it, to see the configuration you had to do to add more columns in the user table. thanks

mikusher avatar Aug 19 '21 17:08 mikusher

image How can i get function get_data_scope() from variable g of flask ? thank all

I wrote the same as you did,but it return an error. Are you sure you didn't get an error?

DAAworld avatar Mar 10 '22 10:03 DAAworld

image How can i get function get_data_scope() from variable g of flask ? thank all `from flask_appbuilder.security.sqla.models import User as AbUser

class User(AbUser): tablename = "ab_user" ids = db.Column(db.Integer)`

`from superset.models.user_relation import User

class SupersetSecurityManager( # pylint: disable=too-many-public-methods SecurityManager ): user_model = User authdbview = AuthDBView`

888 None Traceback (most recent call last): File "/home/lida/insight-Backend/superset/app.py", line 23, in <module> from superset.initialization import SupersetAppInitializer File "/home/lida/insight-Backend/superset/__init__.py", line 21, in <module> from superset.app import create_app File "/home/lida/insight-Backend/superset/app.py", line 23, in <module> from superset.initialization import SupersetAppInitializer File "/home/lida/insight-Backend/superset/initialization/__init__.py", line 50, in <module> from superset.security import SupersetSecurityManager File "/home/lida/insight-Backend/superset/security/__init__.py", line 17, in <module> from superset.security.manager import SupersetSecurityManager # noqa: F401 File "/home/lida/insight-Backend/superset/security/manager.py", line 65, in <module> from superset.models.user_relation import User File "/home/lida/insight-Backend/superset/models/__init__.py", line 17, in <module> from . import ( File "/home/lida/insight-Backend/superset/models/alerts.py", line 40, in <module> print(999, security_manager.user_model) File "/home/lida/infodator-vir/lib/python3.8/site-packages/werkzeug/local.py", line 347, in __getattr__ return getattr(self._get_current_object(), name) AttributeError: 'NoneType' object has no attribute 'user_model'

DAAworld avatar Mar 10 '22 10:03 DAAworld

Can someone help me adjust this to my superset install. I have tried what @jay-finn suggested but i only see the column in my database, buut nothing is saved. I would appreciate some guidance.

IceColdBaby avatar Mar 22 '24 11:03 IceColdBaby