Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always allow admins to edit any interface in PortAdmin #2786

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions python/nav/web/portadmin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#
"""Util functions for the PortAdmin"""
from __future__ import unicode_literals
from typing import List, Sequence, Dict, Any
from typing import List, Sequence, Dict, Any, Optional
import re
import logging
from operator import attrgetter
Expand All @@ -24,6 +24,7 @@

from nav.models import manage, profiles
from nav.django.utils import is_admin
from nav.models.profiles import Account
from nav.portadmin.config import CONFIG
from nav.portadmin.management import ManagementFactory
from nav.portadmin.handlers import ManagementHandler
Expand Down Expand Up @@ -76,7 +77,7 @@
):
"""Finds allowed vlans and indicate which interfaces can be edited"""
allowed_vlans = find_allowed_vlans_for_user_on_netbox(account, netbox, handler)
set_editable_flag_on_interfaces(interfaces, allowed_vlans)
set_editable_flag_on_interfaces(interfaces, allowed_vlans, account)

Check warning on line 80 in python/nav/web/portadmin/utils.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/portadmin/utils.py#L80

Added line #L80 was not covered by tests
return allowed_vlans


Expand Down Expand Up @@ -126,7 +127,9 @@


def set_editable_flag_on_interfaces(
interfaces: Sequence[manage.Interface], vlans: Sequence[FantasyVlan]
interfaces: Sequence[manage.Interface],
vlans: Sequence[FantasyVlan],
user: Optional[Account] = None,
):
"""Sets the pseudo-attribute `iseditable` on each interface in the interfaces
list, indicating whether the PortAdmin UI should allow edits to it or not.
Expand All @@ -136,8 +139,13 @@
an uplink, depending on how portadmin is configured.
"""
vlan_tags = {vlan.vlan for vlan in vlans}
allow_everything = not should_check_access_rights(account=user) if user else False

for interface in interfaces:
if allow_everything:
interface.iseditable = True
continue

vlan_is_acceptable = interface.vlan in vlan_tags
is_link = bool(interface.to_netbox)
refuse_link_edit = not CONFIG.get_link_edit() and is_link
Expand Down
Empty file.
39 changes: 39 additions & 0 deletions tests/unittests/web/portadmin/utils_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from unittest.mock import patch, Mock

from nav.web.portadmin.utils import set_editable_flag_on_interfaces


class TestSetEditableFlagOnInterfaces:
def test_when_user_is_admin_it_should_set_all_interfaces_to_editable(self):
with patch(
"nav.web.portadmin.utils.should_check_access_rights", return_value=False
):
mock_admin = Mock()
mock_interfaces = [Mock(iseditable=False)] * 3
set_editable_flag_on_interfaces(mock_interfaces, [], mock_admin)

assert all(ifc.iseditable for ifc in mock_interfaces)

def test_when_user_is_not_admin_it_should_set_only_matching_interfaces_to_editable(
self,
):
with patch(
"nav.web.portadmin.utils.should_check_access_rights", return_value=True
):
mock_user = Mock()
mock_vlans = [Mock(vlan=42), Mock(vlan=69), Mock(vlan=666)]
editable_interface = Mock(vlan=666, iseditable=False)
mock_interfaces = [
Mock(vlan=99, iseditable=False),
editable_interface,
Mock(vlan=27, iseditable=False),
]

set_editable_flag_on_interfaces(mock_interfaces, mock_vlans, mock_user)

assert editable_interface.iseditable
assert all(
not ifc.iseditable
for ifc in mock_interfaces
if ifc is not editable_interface
)
Loading