Customizing User model to add more details
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?
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=""
):
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?
How can i get function get_data_scope() from variable g of flask ? thank all
For anyone who runs into a similar issue, I resolved this by overriding the
Userclass fromflask_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_zoneto the schema forab_user. After this, override theadd_userfunction from classSupersetSecurityManager. 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
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?
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'
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.