Only create cloud user if cloud in use (#29150)
* Only create cloud user if cloud in use * Pass context to alexa * Update requirements * Fix handing & design pattern for 0.30 * fix tests * Fix lint & tests * rename internal user
This commit is contained in:
1 parent
5d5d053bce
commit
b847d55077
15 files changed
+221
-145
No files matched your search
@@ -1,7 +1,10 @@
|
||||
"""Preference management for cloud."""
|
||||
from ipaddress import ip_address
|
||||
from typing import Optional
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.auth.models import User
|
||||
from homeassistant.auth.const import GROUP_ID_ADMIN
|
||||
from homeassistant.util.logging import async_create_catching_coro
|
||||
|
||||
from .const import (
|
||||
@@ -19,6 +22,7 @@ from .const import (
|
||||
PREF_SHOULD_EXPOSE,
|
||||
PREF_ALEXA_ENTITY_CONFIGS,
|
||||
PREF_ALEXA_REPORT_STATE,
|
||||
PREF_USERNAME,
|
||||
DEFAULT_ALEXA_REPORT_STATE,
|
||||
PREF_GOOGLE_REPORT_STATE,
|
||||
PREF_GOOGLE_LOCAL_WEBHOOK_ID,
|
||||
@@ -47,16 +51,7 @@ class CloudPreferences:
|
||||
prefs = await self._store.async_load()
|
||||
|
||||
if prefs is None:
|
||||
prefs = {
|
||||
PREF_ENABLE_ALEXA: True,
|
||||
PREF_ENABLE_GOOGLE: True,
|
||||
PREF_ENABLE_REMOTE: False,
|
||||
PREF_GOOGLE_SECURE_DEVICES_PIN: None,
|
||||
PREF_GOOGLE_ENTITY_CONFIGS: {},
|
||||
PREF_ALEXA_ENTITY_CONFIGS: {},
|
||||
PREF_CLOUDHOOKS: {},
|
||||
PREF_CLOUD_USER: None,
|
||||
}
|
||||
prefs = self._empty_config("")
|
||||
|
||||
self._prefs = prefs
|
||||
|
||||
@@ -166,6 +161,27 @@ class CloudPreferences:
|
||||
updated_entities = {**entities, entity_id: updated_entity}
|
||||
await self.async_update(alexa_entity_configs=updated_entities)
|
||||
|
||||
async def async_set_username(self, username):
|
||||
"""Set the username that is logged in."""
|
||||
# Logging out.
|
||||
if username is None:
|
||||
user = await self._load_cloud_user()
|
||||
|
||||
if user is not None:
|
||||
await self._hass.auth.async_remove_user(user)
|
||||
await self._save_prefs({**self._prefs, PREF_CLOUD_USER: None})
|
||||
return
|
||||
|
||||
cur_username = self._prefs.get(PREF_USERNAME)
|
||||
|
||||
if cur_username == username:
|
||||
return
|
||||
|
||||
if cur_username is None:
|
||||
await self._save_prefs({**self._prefs, PREF_USERNAME: username})
|
||||
else:
|
||||
await self._save_prefs(self._empty_config(username))
|
||||
|
||||
def as_dict(self):
|
||||
"""Return dictionary version."""
|
||||
return {
|
||||
@@ -178,7 +194,6 @@ class CloudPreferences:
|
||||
PREF_ALEXA_REPORT_STATE: self.alexa_report_state,
|
||||
PREF_GOOGLE_REPORT_STATE: self.google_report_state,
|
||||
PREF_CLOUDHOOKS: self.cloudhooks,
|
||||
PREF_CLOUD_USER: self.cloud_user,
|
||||
}
|
||||
|
||||
@property
|
||||
@@ -239,10 +254,29 @@ class CloudPreferences:
|
||||
"""Return the published cloud webhooks."""
|
||||
return self._prefs.get(PREF_CLOUDHOOKS, {})
|
||||
|
||||
@property
|
||||
def cloud_user(self) -> str:
|
||||
async def get_cloud_user(self) -> str:
|
||||
"""Return ID from Home Assistant Cloud system user."""
|
||||
return self._prefs.get(PREF_CLOUD_USER)
|
||||
user = await self._load_cloud_user()
|
||||
|
||||
if user:
|
||||
return user.id
|
||||
|
||||
user = await self._hass.auth.async_create_system_user(
|
||||
"Home Assistant Cloud", [GROUP_ID_ADMIN]
|
||||
)
|
||||
await self.async_update(cloud_user=user.id)
|
||||
return user.id
|
||||
|
||||
async def _load_cloud_user(self) -> Optional[User]:
|
||||
"""Load cloud user if available."""
|
||||
user_id = self._prefs.get(PREF_CLOUD_USER)
|
||||
|
||||
if user_id is None:
|
||||
return None
|
||||
|
||||
# Fetch the user. It can happen that the user no longer exists if
|
||||
# an image was restored without restoring the cloud prefs.
|
||||
return await self._hass.auth.async_get_user(user_id)
|
||||
|
||||
@property
|
||||
def _has_local_trusted_network(self) -> bool:
|
||||
@@ -283,3 +317,19 @@ class CloudPreferences:
|
||||
|
||||
for listener in self._listeners:
|
||||
self._hass.async_create_task(async_create_catching_coro(listener(self)))
|
||||
|
||||
@callback
|
||||
def _empty_config(self, username):
|
||||
"""Return an empty config."""
|
||||
return {
|
||||
PREF_ENABLE_ALEXA: True,
|
||||
PREF_ENABLE_GOOGLE: True,
|
||||
PREF_ENABLE_REMOTE: False,
|
||||
PREF_GOOGLE_SECURE_DEVICES_PIN: None,
|
||||
PREF_GOOGLE_ENTITY_CONFIGS: {},
|
||||
PREF_ALEXA_ENTITY_CONFIGS: {},
|
||||
PREF_CLOUDHOOKS: {},
|
||||
PREF_CLOUD_USER: None,
|
||||
PREF_USERNAME: username,
|
||||
PREF_GOOGLE_LOCAL_WEBHOOK_ID: self._hass.components.webhook.async_generate_id(),
|
||||
}
|
||||
Reference in new issue
Block a user