\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/imunify360/venv/lib64/python3.11/site-packages/defence360agent/contracts/

Viewing File: permissions.py

import logging
from asyncio.coroutines import iscoroutinefunction
from pathlib import Path
from typing import Optional

from defence360agent.contracts.config import (
    MyImunifyConfig,
    PermissionsConfig,
    Wordpress,
)
from defence360agent.contracts.license import LicenseCLN
from defence360agent.feature_management.constants import AV_REPORT, FULL
from defence360agent.feature_management.model import FeatureManagementPerms
from defence360agent.myimunify.model import MyImunify
from defence360agent.subsys.panels.hosting_panel import HostingPanel
from defence360agent.subsys.panels.plesk import Plesk
from defence360agent.utils import importer

try:
    from imav.malwarelib.api.imunify_patch_subscription import (
        ImunifyPatchSubscriptionAPI,
    )
except ImportError:
    ImunifyPatchSubscriptionAPI = None

logger = logging.getLogger(__name__)

PERMISSIONS = (
    MS_VIEW,
    MS_CLEAN,
    MS_CLEAN_REQUIRES_MYIMUNIFY_PROTECTION,
    MS_ON_DEMAND_SCAN,
    MS_ON_DEMAND_SCAN_WITHOUT_RATE_LIMIT,
    MS_IGNORE_LIST_EDIT,
    MS_CONFIG_DEFAULT_ACTION_EDIT,
    MS_IMUNIFY_PATCH_ENABLED,
    MS_IMUNIFY_PATCH_ELIGIBLE_TO_PURCHASE,
    PD_VIEW,
    PD_CONFIG_MODE_EDIT,
    WP_WAF_EDIT,
    WP_WAF_RULES_EDIT,
) = (
    "malware_scanner.view",
    "malware_scanner.clean",
    "malware_scanner.clean_requires_myimunify_protection",
    "malware_scanner.on_demand.scan",
    "malware_scanner.on_demand.scan_without_rate_limit",
    "malware_scanner.ignore_list.edit",
    "malware_scanner.config.default_action.edit",
    "malware_scanner.imunify_patch.enabled",
    "malware_scanner.imunify_patch.eligible_to_purchase",
    "proactive_defense.view",
    "proactive_defense.config.mode.edit",
    "wordpress.waf.edit",
    "wordpress.waf.rules.edit",
)

GLOBAL_CONFDIR = Path("/etc/sysconfig/imunify360")


def is_plesk_service_plan_enabled() -> bool:
    return (
        HostingPanel().NAME == Plesk.NAME
        and PermissionsConfig.USE_PLESK_SERVICE_PLAN
    )


def myimunify_protection_enabled(user: Optional[str] = None) -> bool:
    return MyImunify.get_protection(user)


def ms_view(user: Optional[str] = None) -> bool:
    if user is None:
        return True

    return FeatureManagementPerms.get_perm(user).av in (
        AV_REPORT,
        FULL,
    )


def ms_clean(user: Optional[str] = None) -> bool:
    if LicenseCLN.is_free() or not LicenseCLN.is_valid():
        return False

    if user is None:
        return True

    if is_plesk_service_plan_enabled():
        #  should be handled by Plesk extension
        return True

    return FeatureManagementPerms.get_perm(user).av == FULL


def ms_clean_requires_myimunify_protection(user: Optional[str] = None):
    if MyImunifyConfig.ENABLED:
        return myimunify_protection_enabled(user)
    return ms_clean(user)


def ms_on_demand_scan(user: Optional[str] = None) -> bool:
    if user is None:
        return True

    if MyImunifyConfig.ENABLED:
        #  on-demand scan is available for both Basic and Pro subscriptions
        return True

    if is_plesk_service_plan_enabled():
        #  should be handled by Plesk extension
        return True

    return PermissionsConfig.ALLOW_MALWARE_SCAN


def ms_on_demand_scan_without_rate_limit(
    user: Optional[str] = None,
) -> bool:
    if MyImunifyConfig.ENABLED:
        return myimunify_protection_enabled(user)

    return PermissionsConfig.ALLOW_MALWARE_SCAN


def ms_ignore_list_edit(user: Optional[str] = None):
    if user is None:
        return True

    if MyImunifyConfig.ENABLED:
        # so far, MyImunify doesn't allow to the user editing ignore list
        return False

    return PermissionsConfig.USER_IGNORE_LIST


def ms_config_default_action_edit(user: Optional[str] = None):
    if user is None:
        return True

    if MyImunifyConfig.ENABLED:
        # so far, MyImunify doesn't allow to the user
        # editing default malware action
        return False

    return PermissionsConfig.USER_OVERRIDE_MALWARE_ACTIONS


ms_imunify_patch_enabled = importer.get(
    module="imav.contracts.permissions",
    name="is_imunify_patch_enabled",
    default=lambda _: False,
)


has_imunify_patch_subscriptions = importer.get(
    module="imav.malwarelib.api.imunify_patch_subscription",
    name="has_imunify_patch_subscriptions",
    default=lambda _: False,
)


async def ms_imunify_patch_eligible_to_purchase(
    user: str | None = None,
) -> bool:
    if ImunifyPatchSubscriptionAPI is None:
        return (
            LicenseCLN.is_eligible_for_imunify_patch()
            or has_imunify_patch_subscriptions(user)
        )
    return (
        LicenseCLN.is_eligible_for_imunify_patch()
        or has_imunify_patch_subscriptions(user)
        or (
            await ImunifyPatchSubscriptionAPI.get_purchase_eligibility()
        ).eligible
    )


def pd_view(user: Optional[str] = None):
    if user is None:
        return True

    return FeatureManagementPerms.get_perm(user).proactive == FULL


def pd_config_mode_edit(user: Optional[str] = None):
    if user is None:
        return True

    if MyImunifyConfig.ENABLED:
        return False

    return PermissionsConfig.USER_OVERRIDE_PROACTIVE_DEFENSE


def wp_waf_edit(user: Optional[str] = None):
    if user is None:
        return True
    if not Wordpress.SECURITY_PLUGIN_ENABLED:
        return False
    try:
        return bool(Wordpress.WAF_ENABLED)
    except KeyError:
        return True


def wp_waf_rules_edit(user: Optional[str] = None):
    if user is None:
        return True
    try:
        return bool(PermissionsConfig.ALLOW_WP_WAF_RULES_MANAGEMENT)
    except KeyError:
        return True


HAS_PERMISSION = {
    MS_VIEW: ms_view,
    MS_CLEAN: ms_clean,
    MS_CLEAN_REQUIRES_MYIMUNIFY_PROTECTION: (
        ms_clean_requires_myimunify_protection
    ),
    MS_ON_DEMAND_SCAN: ms_on_demand_scan,
    MS_ON_DEMAND_SCAN_WITHOUT_RATE_LIMIT: ms_on_demand_scan_without_rate_limit,
    MS_IGNORE_LIST_EDIT: ms_ignore_list_edit,
    MS_CONFIG_DEFAULT_ACTION_EDIT: ms_config_default_action_edit,
    MS_IMUNIFY_PATCH_ENABLED: ms_imunify_patch_enabled,
    MS_IMUNIFY_PATCH_ELIGIBLE_TO_PURCHASE: ms_imunify_patch_eligible_to_purchase,
    PD_VIEW: pd_view,
    PD_CONFIG_MODE_EDIT: pd_config_mode_edit,
    WP_WAF_EDIT: wp_waf_edit,
    WP_WAF_RULES_EDIT: wp_waf_rules_edit,
}


async def has_permission(permission, user) -> bool:
    func = HAS_PERMISSION.get(permission)
    if func is None:
        return False
    if iscoroutinefunction(func):
        return await func(user)
    return func(user)


async def check_permission(permission, user) -> None:
    func = HAS_PERMISSION.get(permission)
    if func is None:
        raise PermissionError("notifications.generalPermissionError")
    if iscoroutinefunction(func):
        if not await func(user):
            raise PermissionError("notifications.generalPermissionError")
    else:
        if not func(user):
            raise PermissionError("notifications.generalPermissionError")


async def permissions_list(user) -> list[str]:
    return [
        permission
        for permission in PERMISSIONS
        if await has_permission(permission, user)
    ]