Coverage for netbox/authentication/misc.py: 10%
15 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
1# Copyright (c) 2009, Peter Sagerson
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6#
7# - Redistributions of source code must retain the above copyright notice, this
8# list of conditions and the following disclaimer.
9#
10# - Redistributions in binary form must reproduce the above copyright notice,
11# this list of conditions and the following disclaimer in the documentation
12# and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24from users.models import Group
27# Copied from django_auth_ldap.backend._LDAPUser and modified to support our
28# custom Group model.
29def _mirror_groups(self):
30 """
31 Mirrors the user's LDAP groups in the Django database and updates the
32 user's membership.
33 """
34 target_group_names = frozenset(
35 filter(None, self._get_groups().get_group_names())
36 )
37 current_group_names = frozenset(
38 self._user.groups.values_list("name", flat=True).iterator()
39 )
41 # These were normalized to sets above.
42 MIRROR_GROUPS_EXCEPT = self.settings.MIRROR_GROUPS_EXCEPT
43 MIRROR_GROUPS = self.settings.MIRROR_GROUPS
45 # If the settings are white- or black-listing groups, we'll update
46 # target_group_names such that we won't modify the membership of groups
47 # beyond our purview.
48 if isinstance(MIRROR_GROUPS_EXCEPT, (set, frozenset)):
49 target_group_names = (target_group_names - MIRROR_GROUPS_EXCEPT) | (
50 current_group_names & MIRROR_GROUPS_EXCEPT
51 )
52 elif isinstance(MIRROR_GROUPS, (set, frozenset)):
53 target_group_names = (target_group_names & MIRROR_GROUPS) | (
54 current_group_names - MIRROR_GROUPS
55 )
57 if target_group_names != current_group_names:
58 existing_groups = list(
59 Group.objects.filter(name__in=target_group_names).iterator()
60 )
61 existing_group_names = frozenset(group.name for group in existing_groups)
63 new_groups = [
64 Group.objects.get_or_create(name=name)[0]
65 for name in target_group_names
66 if name not in existing_group_names
67 ]
69 self._user.groups.set(existing_groups + new_groups)