1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

Frontend indicate require admin (#22272)

* Allow panels to indicate they are meant for admins

* Panels to indicate when they require admin access

* Do not return admin-only panels to non-admin users

* Fix flake8
This commit is contained in:
Paulus Schoutsen
2019-03-25 10:04:35 -07:00
committed by GitHub
parent b57d809dad
commit f1a0ad9e4a
9 changed files with 90 additions and 14 deletions

View File

@@ -123,14 +123,18 @@ class Panel:
# Config to pass to the webcomponent
config = None
# If the panel should only be visible to admins
require_admin = False
def __init__(self, component_name, sidebar_title, sidebar_icon,
frontend_url_path, config):
frontend_url_path, config, require_admin):
"""Initialize a built-in panel."""
self.component_name = component_name
self.sidebar_title = sidebar_title
self.sidebar_icon = sidebar_icon
self.frontend_url_path = frontend_url_path or component_name
self.config = config
self.require_admin = require_admin
@callback
def async_register_index_routes(self, router, index_view):
@@ -150,16 +154,18 @@ class Panel:
'title': self.sidebar_title,
'config': self.config,
'url_path': self.frontend_url_path,
'require_admin': self.require_admin,
}
@bind_hass
async def async_register_built_in_panel(hass, component_name,
sidebar_title=None, sidebar_icon=None,
frontend_url_path=None, config=None):
frontend_url_path=None, config=None,
require_admin=False):
"""Register a built-in panel."""
panel = Panel(component_name, sidebar_title, sidebar_icon,
frontend_url_path, config)
frontend_url_path, config, require_admin)
panels = hass.data.get(DATA_PANELS)
if panels is None:
@@ -247,9 +253,11 @@ async def async_setup(hass, config):
await asyncio.wait(
[async_register_built_in_panel(hass, panel) for panel in (
'dev-event', 'dev-info', 'dev-service', 'dev-state',
'dev-template', 'dev-mqtt', 'kiosk', 'states', 'profile')],
loop=hass.loop)
'kiosk', 'states', 'profile')], loop=hass.loop)
await asyncio.wait(
[async_register_built_in_panel(hass, panel, require_admin=True)
for panel in ('dev-event', 'dev-info', 'dev-service', 'dev-state',
'dev-template', 'dev-mqtt')], loop=hass.loop)
hass.data[DATA_FINALIZE_PANEL] = async_finalize_panel
@@ -478,9 +486,11 @@ def websocket_get_panels(hass, connection, msg):
Async friendly.
"""
user_is_admin = connection.user.is_admin
panels = {
panel: connection.hass.data[DATA_PANELS][panel].to_response()
for panel in connection.hass.data[DATA_PANELS]}
panel_key: panel.to_response()
for panel_key, panel in connection.hass.data[DATA_PANELS].items()
if user_is_admin or not panel.require_admin}
connection.send_message(websocket_api.result_message(
msg['id'], panels))