mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 09:47:52 +00:00
Improve add user error messages (#120909)
This commit is contained in:
parent
001ee0cc0b
commit
a0b604f98c
@ -55,13 +55,6 @@ class InvalidUser(HomeAssistantError):
|
|||||||
Will not be raised when validating authentication.
|
Will not be raised when validating authentication.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class InvalidUsername(InvalidUser):
|
|
||||||
"""Raised when invalid username is specified.
|
|
||||||
|
|
||||||
Will not be raised when validating authentication.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
*args: object,
|
*args: object,
|
||||||
@ -77,6 +70,13 @@ class InvalidUsername(InvalidUser):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidUsername(InvalidUser):
|
||||||
|
"""Raised when invalid username is specified.
|
||||||
|
|
||||||
|
Will not be raised when validating authentication.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Data:
|
class Data:
|
||||||
"""Hold the user data."""
|
"""Hold the user data."""
|
||||||
|
|
||||||
@ -216,7 +216,7 @@ class Data:
|
|||||||
break
|
break
|
||||||
|
|
||||||
if index is None:
|
if index is None:
|
||||||
raise InvalidUser
|
raise InvalidUser(translation_key="user_not_found")
|
||||||
|
|
||||||
self.users.pop(index)
|
self.users.pop(index)
|
||||||
|
|
||||||
@ -232,7 +232,7 @@ class Data:
|
|||||||
user["password"] = self.hash_password(new_password, True).decode()
|
user["password"] = self.hash_password(new_password, True).decode()
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise InvalidUser
|
raise InvalidUser(translation_key="user_not_found")
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _validate_new_username(self, new_username: str) -> None:
|
def _validate_new_username(self, new_username: str) -> None:
|
||||||
@ -275,7 +275,7 @@ class Data:
|
|||||||
self._async_check_for_not_normalized_usernames(self._data)
|
self._async_check_for_not_normalized_usernames(self._data)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise InvalidUser
|
raise InvalidUser(translation_key="user_not_found")
|
||||||
|
|
||||||
async def async_save(self) -> None:
|
async def async_save(self) -> None:
|
||||||
"""Save data."""
|
"""Save data."""
|
||||||
|
@ -37,7 +37,10 @@
|
|||||||
"message": "Username \"{username}\" already exists"
|
"message": "Username \"{username}\" already exists"
|
||||||
},
|
},
|
||||||
"username_not_normalized": {
|
"username_not_normalized": {
|
||||||
"message": "Username \"{new_username}\" is not normalized"
|
"message": "Username \"{new_username}\" is not normalized. Please make sure the username is lowercase and does not contain any whitespace."
|
||||||
|
},
|
||||||
|
"user_not_found": {
|
||||||
|
"message": "User not found"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"issues": {
|
"issues": {
|
||||||
|
@ -53,11 +53,7 @@ async def websocket_create(
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
await provider.async_add_auth(msg["username"], msg["password"])
|
||||||
await provider.async_add_auth(msg["username"], msg["password"])
|
|
||||||
except auth_ha.InvalidUser:
|
|
||||||
connection.send_error(msg["id"], "username_exists", "Username already exists")
|
|
||||||
return
|
|
||||||
|
|
||||||
credentials = await provider.async_get_or_create_credentials(
|
credentials = await provider.async_get_or_create_credentials(
|
||||||
{"username": msg["username"]}
|
{"username": msg["username"]}
|
||||||
@ -94,13 +90,7 @@ async def websocket_delete(
|
|||||||
connection.send_result(msg["id"])
|
connection.send_result(msg["id"])
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
await provider.async_remove_auth(msg["username"])
|
||||||
await provider.async_remove_auth(msg["username"])
|
|
||||||
except auth_ha.InvalidUser:
|
|
||||||
connection.send_error(
|
|
||||||
msg["id"], "auth_not_found", "Given username was not found."
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
connection.send_result(msg["id"])
|
connection.send_result(msg["id"])
|
||||||
|
|
||||||
@ -187,14 +177,8 @@ async def websocket_admin_change_password(
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
await provider.async_change_password(username, msg["password"])
|
||||||
await provider.async_change_password(username, msg["password"])
|
connection.send_result(msg["id"])
|
||||||
connection.send_result(msg["id"])
|
|
||||||
except auth_ha.InvalidUser:
|
|
||||||
connection.send_error(
|
|
||||||
msg["id"], "credentials_not_found", "Credentials not found"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
|
@ -183,7 +183,13 @@ async def test_create_auth_duplicate_username(
|
|||||||
|
|
||||||
result = await client.receive_json()
|
result = await client.receive_json()
|
||||||
assert not result["success"], result
|
assert not result["success"], result
|
||||||
assert result["error"]["code"] == "username_exists"
|
assert result["error"] == {
|
||||||
|
"code": "home_assistant_error",
|
||||||
|
"message": "username_already_exists",
|
||||||
|
"translation_key": "username_already_exists",
|
||||||
|
"translation_placeholders": {"username": "test-user"},
|
||||||
|
"translation_domain": "auth",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_delete_removes_just_auth(
|
async def test_delete_removes_just_auth(
|
||||||
@ -282,7 +288,13 @@ async def test_delete_unknown_auth(
|
|||||||
|
|
||||||
result = await client.receive_json()
|
result = await client.receive_json()
|
||||||
assert not result["success"], result
|
assert not result["success"], result
|
||||||
assert result["error"]["code"] == "auth_not_found"
|
assert result["error"] == {
|
||||||
|
"code": "home_assistant_error",
|
||||||
|
"message": "user_not_found",
|
||||||
|
"translation_key": "user_not_found",
|
||||||
|
"translation_placeholders": None,
|
||||||
|
"translation_domain": "auth",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_change_password(
|
async def test_change_password(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user