Provide HA username via auth WS (#43283)

This commit is contained in:
Philip Allgaier 2020-11-20 15:42:19 +01:00 committed by GitHub
parent 43ba053030
commit 82b7cc8ac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -123,8 +123,19 @@ async def websocket_update(hass, connection, msg):
def _user_info(user): def _user_info(user):
"""Format a user.""" """Format a user."""
ha_username = next(
(
cred.data.get("username")
for cred in user.credentials
if cred.auth_provider_type == "homeassistant"
),
None,
)
return { return {
"id": user.id, "id": user.id,
"username": ha_username,
"name": user.name, "name": user.name,
"is_owner": user.is_owner, "is_owner": user.is_owner,
"is_active": user.is_active, "is_active": user.is_active,

View File

@ -34,7 +34,9 @@ async def test_list(hass, hass_ws_client, hass_admin_user):
owner.credentials.append( owner.credentials.append(
auth_models.Credentials( auth_models.Credentials(
auth_provider_type="homeassistant", auth_provider_id=None, data={} auth_provider_type="homeassistant",
auth_provider_id=None,
data={"username": "test-owner"},
) )
) )
@ -58,6 +60,7 @@ async def test_list(hass, hass_ws_client, hass_admin_user):
assert len(data) == 4 assert len(data) == 4
assert data[0] == { assert data[0] == {
"id": hass_admin_user.id, "id": hass_admin_user.id,
"username": None,
"name": "Mock User", "name": "Mock User",
"is_owner": False, "is_owner": False,
"is_active": True, "is_active": True,
@ -67,6 +70,7 @@ async def test_list(hass, hass_ws_client, hass_admin_user):
} }
assert data[1] == { assert data[1] == {
"id": owner.id, "id": owner.id,
"username": "test-owner",
"name": "Test Owner", "name": "Test Owner",
"is_owner": True, "is_owner": True,
"is_active": True, "is_active": True,
@ -76,6 +80,7 @@ async def test_list(hass, hass_ws_client, hass_admin_user):
} }
assert data[2] == { assert data[2] == {
"id": system.id, "id": system.id,
"username": None,
"name": "Test Hass.io", "name": "Test Hass.io",
"is_owner": False, "is_owner": False,
"is_active": True, "is_active": True,
@ -85,6 +90,7 @@ async def test_list(hass, hass_ws_client, hass_admin_user):
} }
assert data[3] == { assert data[3] == {
"id": inactive.id, "id": inactive.id,
"username": None,
"name": "Inactive User", "name": "Inactive User",
"is_owner": False, "is_owner": False,
"is_active": False, "is_active": False,