|
3 | 3 | from collections import deque |
4 | 4 | from collections.abc import Callable |
5 | 5 | from collections.abc import Generator |
| 6 | +from collections.abc import Iterable |
6 | 7 | from typing import Any |
7 | 8 | from urllib.parse import urlparse |
8 | 9 |
|
|
12 | 13 | from office365.onedrive.driveitems.driveItem import DriveItem |
13 | 14 | from office365.runtime.client_request import ClientRequestException |
14 | 15 | from office365.sharepoint.client_context import ClientContext |
| 16 | +from office365.sharepoint.permissions.roles.definitions.definition import RoleDefinition |
15 | 17 | from office365.sharepoint.permissions.securable_object import RoleAssignmentCollection |
16 | 18 | from office365.sharepoint.principal.users.collection import UserCollection |
17 | 19 | from pydantic import BaseModel |
|
38 | 40 | SHAREPOINT_GROUP_PRINCIPAL_TYPE = 8 # SharePoint site groups (local to the site) |
39 | 41 | MICROSOFT_DOMAIN = ".onmicrosoft" |
40 | 42 | SHAREPOINT_GROUP_SCOPE_SEPARATOR = "::" |
41 | | -# Limited Access role type, limited access is a travel through permission not a actual permission |
42 | | -LIMITED_ACCESS_ROLE_TYPES = [1, 9] |
43 | | -LIMITED_ACCESS_ROLE_NAMES = ["Limited Access", "Web-Only Limited Access"] |
| 43 | +# PnP RoleType defines Guest=1 and RestrictedGuest=9: |
| 44 | +# https://github.com/pnp/pnpcore/blob/4e4f58fcac797f2957bfcd14fedcecd690dfe7ee/src/sdk/PnP.Core/Model/SharePoint/Core/Public/Enums/RoleType.cs |
| 45 | +LIMITED_ACCESS_ROLE_TYPES = frozenset({1, 9}) |
44 | 46 |
|
45 | 47 |
|
46 | 48 | AD_GROUP_ENUMERATION_THRESHOLD = 100_000 |
|
56 | 58 | ROLE_ASSIGNMENTS_PAGE_SIZE = 100 |
57 | 59 |
|
58 | 60 |
|
| 61 | +def _has_only_limited_access( |
| 62 | + role_definition_bindings: Iterable[RoleDefinition], |
| 63 | +) -> bool: |
| 64 | + return all( |
| 65 | + binding.role_type_kind in LIMITED_ACCESS_ROLE_TYPES |
| 66 | + for binding in role_definition_bindings |
| 67 | + ) |
| 68 | + |
| 69 | + |
59 | 70 | def _graph_api_get( |
60 | 71 | url: str, |
61 | 72 | get_access_token: Callable[[], str], |
@@ -527,23 +538,11 @@ def add_user_and_group_to_sets( |
527 | 538 | # callback and recurses until Python hits its max recursion depth. |
528 | 539 | for assignment in role_assignments.current_page: |
529 | 540 | logger.debug("Assignment: %s", assignment.to_json()) |
530 | | - if assignment.role_definition_bindings: |
531 | | - is_limited_access = True |
532 | | - for role_definition_binding in assignment.role_definition_bindings: |
533 | | - if ( |
534 | | - role_definition_binding.role_type_kind |
535 | | - not in LIMITED_ACCESS_ROLE_TYPES |
536 | | - or role_definition_binding.name not in LIMITED_ACCESS_ROLE_NAMES |
537 | | - ): |
538 | | - is_limited_access = False |
539 | | - break |
540 | | - |
541 | | - # Skip if the role is only Limited Access, because this is not a actual permission its a travel through permission |
542 | | - if is_limited_access: |
543 | | - logger.info( |
544 | | - "Skipping assignment because it has only Limited Access role" |
545 | | - ) |
546 | | - continue |
| 541 | + if assignment.role_definition_bindings and _has_only_limited_access( |
| 542 | + assignment.role_definition_bindings |
| 543 | + ): |
| 544 | + logger.info("Skipping Limited Access-only assignment") |
| 545 | + continue |
547 | 546 | if assignment.member: |
548 | 547 | member = assignment.member |
549 | 548 | if member.principal_type == USER_PRINCIPAL_TYPE and hasattr( |
@@ -723,24 +722,11 @@ def add_group_to_sets(role_assignments: RoleAssignmentCollection) -> None: |
723 | 722 | # via `_get_next().execute_query()`, which re-fires this `page_loaded` |
724 | 723 | # callback and recurses until Python hits its max recursion depth. |
725 | 724 | for assignment in role_assignments.current_page: |
726 | | - if assignment.role_definition_bindings: |
727 | | - is_limited_access = True |
728 | | - for role_definition_binding in assignment.role_definition_bindings: |
729 | | - if ( |
730 | | - role_definition_binding.role_type_kind |
731 | | - not in LIMITED_ACCESS_ROLE_TYPES |
732 | | - or role_definition_binding.name not in LIMITED_ACCESS_ROLE_NAMES |
733 | | - ): |
734 | | - is_limited_access = False |
735 | | - break |
736 | | - |
737 | | - # Skip if the role assignment is only Limited Access, because this is not a actual permission its |
738 | | - # a travel through permission |
739 | | - if is_limited_access: |
740 | | - logger.info( |
741 | | - "Skipping assignment because it has only Limited Access role" |
742 | | - ) |
743 | | - continue |
| 725 | + if assignment.role_definition_bindings and _has_only_limited_access( |
| 726 | + assignment.role_definition_bindings |
| 727 | + ): |
| 728 | + logger.info("Skipping Limited Access-only assignment") |
| 729 | + continue |
744 | 730 | if assignment.member: |
745 | 731 | member = assignment.member |
746 | 732 | if member.principal_type in [ |
|
0 commit comments