$79 GRAYBYTE WORDPRESS FILE MANAGER $69

SERVER : premium201.web-hosting.com #1 SMP Wed Mar 26 12:08:09 UTC 2025
SERVER IP : 172.67.217.254 | ADMIN IP 216.73.216.180
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : mail

/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/feature_management/

HOME
Current File : /opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/feature_management//hooks.py
"""
This module contains hook, which are called on feature management permission
changes. Note that hooks are not executed automatically, developer is
responsible to obtain specific hook using get_hook() function and call it.
To add hook, create function with name equal to feature name
"""
import functools
import logging
from typing import Any, Callable, Optional

from defence360agent.contracts.config import ConfigFile
from defence360agent.feature_management.constants import AV, FULL, PROACTIVE

logger = logging.getLogger(__name__)


def _hook_stub(*_):
    return True


def _result_warn(callback):
    @functools.wraps(callback)
    def wrap(user, value):
        result = callback(user, value)
        result or logger.warning(
            "Hook '%s(%s)' failed for user '%s'.",
            callback.__name__,
            value,
            user,
        )
        return result

    return wrap


@_result_warn
def antivirus(user: Optional[str], value: Any) -> bool:
    """Called when 'av' feature is changed"""
    if not user:
        return True

    config = ConfigFile()
    config_value = config.get("MALWARE_SCANNING", "default_action")

    user_config = ConfigFile(user)
    user_config_value = user_config.get("MALWARE_SCANNING", "default_action")

    if value == FULL:
        user_config_value = None
    elif (
        user_config_value
        and user_config_value.startswith("cleanup")
        and config_value
        and config_value.startswith("cleanup")
    ):
        user_config_value = "notify"

    try:
        user_config.set(
            "MALWARE_SCANNING", "default_action", user_config_value
        )
    except Exception:
        return False

    return True


@_result_warn
def proactive(user: Optional[str], value: Any) -> bool:
    """Called when 'proactive' feature is changed"""
    if not user:
        return True  # do nothing if no user specified

    config_value = None if value == FULL else "DISABLED"

    try:
        ConfigFile(user).set("PROACTIVE_DEFENCE", "mode", config_value)
    except Exception:
        return False

    return True


HOOKS = {
    AV: antivirus,
    PROACTIVE: proactive,
}


def get_hook(feature: str) -> Callable[[Optional[str], Any], bool]:
    """
    Get hook for specific feature. If no hook is implemented for this feature,
    return stub function
    :param feature: feature name
    :return: callable hook
    """
    return HOOKS.get(feature, _hook_stub)


Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
3 Mar 2026 8.45 AM
root / root
0755
__pycache__
--
3 Mar 2026 8.45 AM
root / root
0755
plugins
--
3 Mar 2026 8.45 AM
root / root
0755
rpc
--
3 Mar 2026 8.45 AM
root / root
0755
__init__.py
0 KB
23 Jan 2026 1.13 PM
root / root
0644
checkers.py
2.449 KB
23 Jan 2026 1.13 PM
root / root
0644
constants.py
0.793 KB
23 Jan 2026 1.13 PM
root / root
0644
control.py
2.496 KB
23 Jan 2026 1.13 PM
root / root
0644
exceptions.py
0.299 KB
23 Jan 2026 1.13 PM
root / root
0644
hooks.py
2.394 KB
23 Jan 2026 1.13 PM
root / root
0644
lookup.py
2.765 KB
23 Jan 2026 1.13 PM
root / root
0644
model.py
2.318 KB
23 Jan 2026 1.13 PM
root / root
0644
utils.py
3.477 KB
23 Jan 2026 1.13 PM
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2025 CONTACT ME
Static GIF