-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resources: implement resources using new Container model
The first cut will not find the next closes matching Resource for object class yet, it just falls back to Resource for now: RESOURCES.get(model.get_object_class(), Resource) The next version can fix this properly. It needs to go through the list of object classes until it finds one that matches.
- Loading branch information
Showing
8 changed files
with
83 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
from .computer import ComputerResource | ||
from .container import ContainerResource | ||
from .gmsa import GroupManagedServiceAccountResource | ||
from .group import GroupResource | ||
from .resource import Resource | ||
from .root import RootFactory | ||
from .user import UserResource | ||
|
||
__all__ = ("Resource", "RootFactory", "UserResource") | ||
__all__ = ( | ||
"ComputerResource", | ||
"ContainerResource", | ||
"GroupResource", | ||
"GroupManagedServiceAccountResource", | ||
"Resource", | ||
"RootFactory", | ||
"UserResource", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from samba.domain.models import Computer | ||
|
||
from .resource import Resource | ||
|
||
|
||
class ComputerResource(Resource): | ||
model = Computer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from ldb import SCOPE_ONELEVEL | ||
from samba.domain.models import Container | ||
|
||
from .resource import Resource | ||
|
||
|
||
class ContainerResource(Resource): | ||
model = Container | ||
|
||
def __init__(self, request, container): | ||
super().__init__(request, container) | ||
|
||
if request.samdb: | ||
queryset = self.model.query( | ||
request.samdb, | ||
base_dn=container.dn, | ||
scope=SCOPE_ONELEVEL, | ||
polymorphic=True, | ||
) | ||
|
||
for obj in queryset: | ||
if obj: | ||
resource_class = self.resource_for_model(obj) | ||
self[obj.name] = resource_class(request, obj) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from samba.domain.models import GroupManagedServiceAccount | ||
|
||
from .resource import Resource | ||
|
||
|
||
class GroupManagedServiceAccountResource(Resource): | ||
model = GroupManagedServiceAccount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from samba.domain.models import Group | ||
|
||
from .resource import Resource | ||
|
||
|
||
class GroupResource(Resource): | ||
model = Group |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
from samba.domain.models import Model | ||
|
||
# A lookup table object_class to Resource. | ||
RESOURCES = {} | ||
|
||
class Resource(dict): | ||
|
||
class ResourceMeta(type): | ||
def __new__(mcls, name, bases, namespace, **kwargs): | ||
cls = super().__new__(mcls, name, bases, namespace, **kwargs) | ||
RESOURCES[cls.model.get_object_class()] = cls | ||
return cls | ||
|
||
|
||
class Resource(dict, metaclass=ResourceMeta): | ||
model = Model | ||
|
||
def __init__(self, request, obj): | ||
if request.samdb: | ||
qs = self.model.query(request.samdb, base_dn=obj.dn, scope=SCOPE_ONELEVEL) | ||
data = {model.name: model.as_dict() for model in qs if model} | ||
else: | ||
data = {} | ||
super().__init__() | ||
self.update(obj.as_dict()) | ||
|
||
super().__init__(**data) | ||
@staticmethod | ||
def resource_for_model(model): | ||
return RESOURCES.get(model.get_object_class(), Resource) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters