GitHub user dosubot[bot] added a comment to the discussion: An error occurs 
when accessing /users/userinfo/ after deleting a user:AttributeError: 
'AnonymousUserMixin' object has no attribute 'id'

<!-- Answer -->
To override the Flask-AppBuilder userinfo view and avoid the AttributeError, 
you can subclass the original view and add a check for anonymous users before 
accessing user attributes. For example, create a custom view that extends the 
original, override the userinfo method, and check if g.user is anonymous (using 
g.user.is_anonymous or hasattr(g.user, "id")). If the user is anonymous, 
redirect to login or show an error; otherwise, proceed as normal.

Here's a minimal example:

```python
from flask import g, redirect, url_for
from flask_appbuilder.security.views import UserDBModelView

class CustomUserInfoView(UserDBModelView):
    @expose('/userinfo/')
    def userinfo(self):
        if not hasattr(g.user, "id") or getattr(g.user, "is_anonymous", False):
            return redirect(url_for('AuthDBView.login'))
        return super().userinfo()
```

Then, register this custom view with your appbuilder instance, replacing the 
default one. This pattern is similar to how custom authentication views are 
created in Superset and Flask-AppBuilder, as shown in [this 
example](https://github.com/apache/superset/discussions/32038).

This approach ensures that if the user is anonymous (for example, after 
deletion), the code won't try to access attributes that don't exist, preventing 
the AttributeError.


<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot).*

---
<sup>How did I do? 
[Good](https://app.dosu.dev/response-feedback/e5bdbc51-d551-4c24-906a-a60a6dbede7b?feedback_type=great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/e5bdbc51-d551-4c24-906a-a60a6dbede7b?feedback_type=irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/e5bdbc51-d551-4c24-906a-a60a6dbede7b?feedback_type=incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/e5bdbc51-d551-4c24-906a-a60a6dbede7b?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/e5bdbc51-d551-4c24-906a-a60a6dbede7b?feedback_type=hallucination)
 | [Report 
🐛](https://app.dosu.dev/response-feedback/e5bdbc51-d551-4c24-906a-a60a6dbede7b?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/e5bdbc51-d551-4c24-906a-a60a6dbede7b?feedback_type=other)</sup>&nbsp;&nbsp;[![Join
 
Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Sh
 are on 
X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/discussions/33825)

GitHub link: 
https://github.com/apache/superset/discussions/33825#discussioncomment-13519088

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: 
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to