Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizing User model to add more details #9448

Closed
jay-finn opened this issue Apr 2, 2020 · 7 comments
Closed

Customizing User model to add more details #9448

jay-finn opened this issue Apr 2, 2020 · 7 comments

Comments

@jay-finn
Copy link

jay-finn commented Apr 2, 2020

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
Copy link
Author

jay-finn commented Apr 6, 2020

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 jay-finn closed this as completed Apr 6, 2020
@BigDataArtist
Copy link

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?

@CaptainHoangLe
Copy link

CaptainHoangLe commented May 7, 2021

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

@mikusher
Copy link

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

@DAAworld
Copy link

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
Copy link

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'

@IceColdBaby
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants