Skip to content

Commit

Permalink
Pull all permissions methods into Permissions class
Browse files Browse the repository at this point in the history
Simplify all permissions logic and remove duplication
  • Loading branch information
RogerSelwyn committed Sep 10, 2023
1 parent ec88cf4 commit 882eddf
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 232 deletions.
10 changes: 4 additions & 6 deletions custom_components/o365/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from O365 import Account, FileSystemTokenBackend
from oauthlib.oauth2.rfc6749.errors import InvalidClientError

from .classes.permissions import Permissions
from .const import (
CONF_ACCOUNT,
CONF_ACCOUNT_CONF,
Expand Down Expand Up @@ -46,7 +47,6 @@
build_token_filename,
check_file_location,
)
from .utils.permissions import build_minimum_permissions, validate_permissions

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -154,15 +154,13 @@ async def _async_setup_account(hass, account_conf, conf_type):
)
)
is_authenticated = account.is_authenticated
minimum_permissions = build_minimum_permissions(hass, account_conf, conf_type)
permissions, failed_permissions = validate_permissions(
hass, minimum_permissions, filename=token_file
)
perms = Permissions(hass, account_conf)
permissions, failed_permissions = perms.validate_permissions()
check_token = None
if is_authenticated and permissions and permissions != TOKEN_FILE_MISSING:
check_token = await _async_check_token(hass, account, account_name)
if check_token:
do_setup(hass, account_conf, account, account_name, conf_type)
do_setup(hass, account_conf, account, account_name, conf_type, perms)
else:
await _async_authorization_repair(
hass,
Expand Down
21 changes: 7 additions & 14 deletions custom_components/o365/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
CONF_HOURS_BACKWARD_TO_GET,
CONF_HOURS_FORWARD_TO_GET,
CONF_MAX_RESULTS,
CONF_PERMISSIONS,
CONF_SEARCH,
CONF_TRACK,
CONF_TRACK_NEW_CALENDAR,
Expand Down Expand Up @@ -73,13 +74,11 @@
)
from .utils.filemgmt import (
build_config_file_path,
build_token_filename,
build_yaml_filename,
check_file_location,
load_yaml_file,
update_calendar_file,
)
from .utils.permissions import get_permissions, validate_minimum_permission
from .utils.utils import (
add_call_data_to_event,
clean_html,
Expand All @@ -105,13 +104,11 @@ async def async_setup_platform(
if not account.is_authenticated:
return False

permissions = get_permissions(
hass,
filename=build_token_filename(conf, conf.get(CONF_CONFIG_TYPE)),
)
update_supported = bool(
conf[CONF_ENABLE_UPDATE]
and validate_minimum_permission(PERM_MINIMUM_CALENDAR_WRITE, permissions)
and conf[CONF_PERMISSIONS].validate_minimum_permission(
PERM_MINIMUM_CALENDAR_WRITE
)
)
cal_ids = _setup_add_entities(hass, account, add_entities, conf, update_supported)
hass.data[DOMAIN][account_name][CONF_CAL_IDS] = cal_ids
Expand Down Expand Up @@ -433,13 +430,9 @@ def _get_event_from_calendar(self, event_id):
return calendar.get_event(event_id)

def _validate_permissions(self, error_message):
permissions = get_permissions(
self.hass,
filename=build_token_filename(
self._config, self._config.get(CONF_CONFIG_TYPE)
),
)
if not validate_minimum_permission(PERM_MINIMUM_CALENDAR_WRITE, permissions):
if not self._config[CONF_PERMISSIONS].validate_minimum_permission(
PERM_MINIMUM_CALENDAR_WRITE
):
raise vol.Invalid(
f"Not authorisied to {PERM_CALENDARS_READWRITE} calendar event "
+ f"- requires permission: {error_message}"
Expand Down
Loading

0 comments on commit 882eddf

Please sign in to comment.