Skip to content

Commit e5ca51b

Browse files
Directory namespace improvements (new types & methods)
1 parent 9067c02 commit e5ca51b

32 files changed

+309
-95
lines changed

examples/b2c/registerIdP.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44

55
client = GraphClient(acquire_token_client_credentials)
6-
idp_col = client.identityProviders.get().execute_query()
6+
idp_col = client.identity_providers.get().execute_query()
77
for idp in idp_col:
88
print(idp.id)

examples/directory/delete_groups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
print("Group deleted.")
1414
index += 1
1515

16-
deleted_groups = client.directory.deletedGroups.get().execute_query()
16+
deleted_groups = client.directory.deleted_groups.get().execute_query()
1717
groups_count = len(deleted_groups)
1818
index = 0
1919
while len(deleted_groups) > 0:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from office365.directory.directoryObject import DirectoryObject
2+
from office365.entity_collection import EntityCollection
3+
4+
5+
class AppRoleAssignment(DirectoryObject):
6+
pass
7+
8+
9+
class AppRoleAssignmentCollection(EntityCollection):
10+
11+
def __init__(self, context, resource_path=None):
12+
super(AppRoleAssignmentCollection, self).__init__(context, AppRoleAssignment, resource_path)

office365/directory/application.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from office365.directory.directoryObject import DirectoryObject
22
from office365.directory.keyCredential import KeyCredential
3+
from office365.directory.passwordCredential import PasswordCredential
34
from office365.runtime.client_value_collection import ClientValueCollection
5+
from office365.runtime.queries.service_operation_query import ServiceOperationQuery
46

57

68
class Application(DirectoryObject):
@@ -12,6 +14,33 @@ class Application(DirectoryObject):
1214
an Application in Azure AD
1315
"""
1416

17+
def add_password(self, display_name):
18+
"""Adds a strong password to an application.
19+
:param str display_name: App display name
20+
"""
21+
return_type = PasswordCredential(displayName=display_name)
22+
qry = ServiceOperationQuery(self, "addPassword", None, return_type, None, return_type)
23+
self.context.add_query(qry)
24+
return return_type
25+
26+
def remove_password(self, keyId):
27+
"""Remove a password from an application."""
28+
qry = ServiceOperationQuery(self, "removePassword", None, {"keyId": keyId})
29+
self.context.add_query(qry)
30+
return self
31+
32+
def delete_object(self, permanent_delete=False):
33+
"""
34+
:param permanent_delete: Permanently deletes the application from directory
35+
:type permanent_delete: bool
36+
37+
"""
38+
super(Application, self).delete_object()
39+
if permanent_delete:
40+
deleted_item = self.context.directory.deleted_applications[self.id]
41+
deleted_item.delete_object()
42+
return self
43+
1544
@property
1645
def key_credentials(self):
1746
"""The collection of key credentials associated with the application. Not nullable.

office365/directory/applicationCollection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ class ApplicationCollection(EntityCollection):
66

77
def __init__(self, context, resource_path=None):
88
super(ApplicationCollection, self).__init__(context, Application, resource_path)
9+
10+
def add(self, displayName):
11+
"""
12+
Create a new application object.
13+
:type displayName: str
14+
"""
15+
return self.add_from_json({"displayName": displayName})

office365/directory/directory.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,28 @@ class Directory(Entity):
88
"container". Deleted items will remain available to restore for up to 30 days. After 30 days, the items are
99
permanently deleted. """
1010

11-
def get_deleted_items(self, entity_type=None):
11+
def deleted_items(self, entity_type=None):
1212
"""Recently deleted items. Read-only. Nullable."""
13-
if self.is_property_available('deletedItems'):
14-
return self.properties['deletedItems']
13+
if entity_type:
14+
return DirectoryObjectCollection(self.context, ResourcePath(entity_type,
15+
ResourcePath("deletedItems",
16+
self.resource_path)))
1517
else:
16-
res_path = entity_type and \
17-
ResourcePath(entity_type, ResourcePath("deletedItems", self.resource_path)) or \
18-
ResourcePath("deletedItems", self.resource_path)
19-
return DirectoryObjectCollection(self.context, res_path)
18+
return self.properties.get('deletedItems',
19+
DirectoryObjectCollection(self.context,
20+
ResourcePath("deletedItems", self.resource_path)))
2021

2122
@property
22-
def deletedGroups(self):
23+
def deleted_groups(self):
2324
"""Recently deleted groups"""
24-
return self.get_deleted_items("microsoft.graph.group")
25+
return self.deleted_items("microsoft.graph.group")
2526

2627
@property
27-
def deletedUsers(self):
28+
def deleted_users(self):
2829
"""Recently deleted users"""
29-
return self.get_deleted_items("microsoft.graph.user")
30+
return self.deleted_items("microsoft.graph.user")
3031

3132
@property
32-
def deletedApplications(self):
33+
def deleted_applications(self):
3334
"""Recently deleted applications"""
34-
return self.get_deleted_items("microsoft.graph.application")
35+
return self.deleted_items("microsoft.graph.application")

office365/directory/directoryObject.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
from office365.entity import Entity
22
from office365.runtime.client_result import ClientResult
3-
from office365.runtime.queries.delete_entity_query import DeleteEntityQuery
43
from office365.runtime.queries.service_operation_query import ServiceOperationQuery
5-
from office365.runtime.queries.update_entity_query import UpdateEntityQuery
64

75

86
class DirectoryObject(Entity):
97
"""Represents an Azure Active Directory object. The directoryObject type is the base type for many other
108
directory entity types. """
119

10+
def get_member_objects(self, security_enabled_only=True):
11+
"""Returns all the groups and directory roles that a user, group, or directory object is a member of.
12+
This function is transitive.
13+
14+
:type security_enabled_only: bool"""
15+
result = ClientResult(None)
16+
payload = {
17+
"securityEnabledOnly": security_enabled_only
18+
}
19+
qry = ServiceOperationQuery(self, "getMemberObjects", None, payload, None, result)
20+
self.context.add_query(qry)
21+
return result
22+
1223
def get_member_groups(self, security_enabled_only=True):
1324
"""Return all the groups that the specified user, group, or directory object is a member of. This function is
1425
transitive.
@@ -23,11 +34,9 @@ def get_member_groups(self, security_enabled_only=True):
2334
return result
2435

2536
@property
26-
def deletedDateTime(self):
37+
def deleted_datetime(self):
2738
"""ETag for the item."""
28-
if self.is_property_available("deletedDateTime"):
29-
return self.properties['deletedDateTime']
30-
return None
39+
return self.properties.get('deletedDateTime', None)
3140

3241
def set_property(self, name, value, persist_changes=True):
3342
super(DirectoryObject, self).set_property(name, value, persist_changes)

office365/directory/directoryObjectCollection.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,10 @@ def get(self):
2020

2121
def __getitem__(self, key):
2222
"""
23-
24-
:param key: key is used to address a DirectoryObject resource by either an index in collection
25-
or by resource id
2623
:type key: int or str
2724
:rtype: DirectoryObject
2825
"""
29-
if type(key) == int:
30-
return super(DirectoryObjectCollection, self).__getitem__(key)
31-
return self._item_type(self.context,
32-
ResourcePath(key, self.resource_path))
26+
return super(DirectoryObjectCollection, self).__getitem__(key)
3327

3428
def getByIds(self, ids):
3529
"""Returns the directory objects specified in a list of IDs."""

office365/directory/group.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22

33
from office365.calendar.event_collection import EventCollection
4+
from office365.directory.appRoleAssignment import AppRoleAssignmentCollection
45
from office365.directory.directoryObject import DirectoryObject
56
from office365.directory.directoryObjectCollection import DirectoryObjectCollection
67
from office365.onedrive.driveCollection import DriveCollection
@@ -16,10 +17,18 @@ class Group(DirectoryObject):
1617
"""Represents an Azure Active Directory (Azure AD) group, which can be an Office 365 group, or a security group."""
1718

1819
def subscribe_by_mail(self):
19-
pass
20+
"""Calling this method will enable the current user to receive email notifications for this group,
21+
about new posts, events, and files in that group. Supported for Microsoft 365 groups only."""
22+
qry = ServiceOperationQuery(self, "subscribeByMail")
23+
self.context.add_query(qry)
24+
return self
2025

2126
def unsubscribe_by_mail(self):
22-
pass
27+
"""Calling this method will prevent the current user from receiving email notifications for this group
28+
about new posts, events, and files in that group. Supported for Microsoft 365 groups only."""
29+
qry = ServiceOperationQuery(self, "unsubscribeByMail")
30+
self.context.add_query(qry)
31+
return self
2332

2433
def check_member_groups(self, group_ids):
2534
"""Check for membership in the specified list of groups. Returns from the list those groups of which
@@ -49,6 +58,7 @@ def _construct_create_team_request(request):
4958
request.method = HttpMethod.Put
5059
request.set_header('Content-Type', "application/json")
5160
request.data = json.dumps(request.data)
61+
5262
self.context.before_execute(_construct_create_team_request, False)
5363
return team
5464

@@ -60,7 +70,7 @@ def delete_object(self, permanent_delete=False):
6070
"""
6171
super(Group, self).delete_object()
6272
if permanent_delete:
63-
deleted_item = self.context.directory.deletedGroups[self.id]
73+
deleted_item = self.context.directory.deleted_groups[self.id]
6474
deleted_item.delete_object()
6575
return self
6676

@@ -92,3 +102,10 @@ def sites(self):
92102
def events(self):
93103
"""Get an event collection or an event."""
94104
return self.properties.get('events', EventCollection(self.context, ResourcePath("events", self.resource_path)))
105+
106+
@property
107+
def appRoleAssignments(self):
108+
"""Get an event collection or an appRoleAssignments."""
109+
return self.properties.get('appRoleAssignments',
110+
AppRoleAssignmentCollection(self.context,
111+
ResourcePath("appRoleAssignments", self.resource_path)))
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
from office365.directory.directoryObjectCollection import DirectoryObjectCollection
21
from office365.directory.groupSettingTemplate import GroupSettingTemplate
2+
from office365.entity_collection import EntityCollection
33

44

5-
class GroupSettingTemplateCollection(DirectoryObjectCollection):
6-
"""User's collection"""
5+
class GroupSettingTemplateCollection(EntityCollection):
76

87
def __init__(self, context, resource_path=None):
9-
super(GroupSettingTemplateCollection, self).__init__(context, resource_path)
10-
self._item_type = GroupSettingTemplate
8+
super(GroupSettingTemplateCollection, self).__init__(context, GroupSettingTemplate, resource_path)

0 commit comments

Comments
 (0)