Repository: goauthentik/authentik
Pull request: added example for custom user attributes
<!--
👋 Hello there! Welcome.

Please check the [Contributing guidelines](https://github.com/goauthentik/authentik/blob/master/CONTRIBUTING.md#how-can-i-contribute).
-->

# Details
* **Does this resolve an issue?**
Resolves #2405 

## Changes
### New Features
* extend documentation

### Breaking Changes


## Additional



Problem statement:
Custom User attribute in Scope Mapping
**Is your feature request related to a problem? Please describe.**
We wanted to add a custom attribute to the access token. Unfortunately we searched for a long time because we could not find an example in the documentation.

We created a pull request for the documentation.

**Describe the solution you'd like**
Add  a simple example to the documentation.

**Describe alternatives you've considered**

**Additional context**
In the multi-tenant environment, it is desirable to include additional attributes such as the tenant ID in the access token.

Hints from issue discussion:

Commit message: added example for custom user attributes

Patch to review:
diff --git a/website/docs/expressions/_user.md b/website/docs/expressions/_user.md
new file mode 100644
index 000000000000..1b372a2ffa9a
--- /dev/null
+++ b/website/docs/expressions/_user.md
@@ -0,0 +1,10 @@
+- `user`: The current user. This may be `None` if there is no contextual user. See ([User](../user-group/user.md#object-attributes))
+
+Example:
+
+```python
+if not 'custom_attribute' in request.user.attributes:
+  return {'custom_attribute': 'default'}
+
+return {"custom_attribute": request.user.attributes['custom_attribute']}
+```
\ No newline at end of file
diff --git a/website/docs/property-mappings/expression.mdx b/website/docs/property-mappings/expression.mdx
index 0d69974b1753..c8c0e36bb1b8 100644
--- a/website/docs/property-mappings/expression.mdx
+++ b/website/docs/property-mappings/expression.mdx
@@ -17,6 +17,9 @@ import Objects from '../expressions/_objects.md'
 
 <Objects />
 
-- `user`: The current user. This may be `None` if there is no contextual user. See ([User](../user-group/user.md#object-attributes))
+import User from '../expressions/_user.md'
+
+<User />
+
 - `request`: The current request. This may be `None` if there is no contextual request. See ([Django documentation](https://docs.djangoproject.com/en/3.0/ref/request-response/#httprequest-objects))
 - Other arbitrary arguments given by the provider, this is documented on the Provider/Source.


Code review comments:
diff_hunk: @@ -0,0 +1,10 @@
+- `user`: The current user. This may be `None` if there is no contextual user. See ([User](../user-group/user.md#object-attributes))
+
+Example:
+
+```python
+if not 'custom_attribute' in request.user.attributes:
+  return {'custom_attribute': 'default'}
+
+return {"custom_attribute": request.user.attributes['custom_attribute']}
path: website/docs/expressions/_user.md
text: @user1:
it would probably be cleaner to use something like 

```python
return {
  "custom_attribute": request.user.attributes.get("custom_attribute", "default"),
}
```

I'd also add `user.group_attributes()`, which returns a dict of all the user's group's attributes, merged