%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /opt/imunify360/venv/lib/python3.8/site-packages/defence360agent/contracts/
Upload File :
Create Path :
Current File : //opt/imunify360/venv/lib/python3.8/site-packages/defence360agent/contracts/permissions.py

"""
This program is free software: you can redistribute it and/or modify it under 
the terms of the GNU General Public License as published by 
the Free Software Foundation, either version 3 of the License, 
or (at your option) any later version.


This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
See the GNU General Public License for more details.


You should have received a copy of the GNU General Public License
 along with this program.  If not, see <https://www.gnu.org/licenses/>.

Copyright © 2019 Cloud Linux Software Inc.

This software is also available under ImunifyAV commercial license,
see <https://www.imunify360.com/legal/eula>
"""
import logging
from pathlib import Path
from typing import Optional

from defence360agent.contracts.config import (
    PermissionsConfig,
    SecureSiteConfig,
)
from defence360agent.contracts.license import LicenseCLN
from defence360agent.feature_management.constants import AV_REPORT, FULL
from defence360agent.feature_management.model import FeatureManagementPerms
from defence360agent.secure_site.constants import PRO
from defence360agent.secure_site.model import SecureSite

logger = logging.getLogger(__name__)

PERMISSIONS = (
    MS_VIEW,
    MS_CLEAN,
    MS_CLEAN_REQUIRES_SECURE_SITE_PRO,
    MS_ON_DEMAND_SCAN,
    MS_ON_DEMAND_SCAN_WITHOUT_RATE_LIMIT,
    MS_IGNORE_LIST_EDIT,
    MS_CONFIG_DEFAULT_ACTION_EDIT,
    PD_VIEW,
    PD_CONFIG_MODE_EDIT,
) = (
    "malware_scanner.view",
    "malware_scanner.clean",
    "malware_scanner.clean_requires_secure_site_pro",
    "malware_scanner.on_demand.scan",
    "malware_scanner.on_demand.scan_without_rate_limit",
    "malware_scanner.ignore_list.edit",
    "malware_scanner.config.default_action.edit",
    "proactive_defense.view",
    "proactive_defense.config.mode.edit",
)

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


def secure_site_pro_enabled(user: Optional[str] = None) -> bool:
    return SecureSite.get_subscription_type(user) == PRO


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

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


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

    if user is None:
        return True

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


def ms_clean_requires_secure_site_pro(user: Optional[str] = None):
    return secure_site_pro_enabled(user)


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

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

    return PermissionsConfig.ALLOW_MALWARE_SCAN


def ms_on_demand_scan_without_rate_limit(
    user: Optional[str] = None,
):
    if SecureSiteConfig.ENABLED:
        return secure_site_pro_enabled(user)

    return PermissionsConfig.ALLOW_MALWARE_SCAN


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

    if SecureSiteConfig.ENABLED:
        # so far, Secure Site 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 SecureSiteConfig.ENABLED:
        # so far, Secure Site doesn't allow to the user
        # editing default malware action
        return False

    return PermissionsConfig.USER_OVERRIDE_MALWARE_ACTIONS


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 SecureSiteConfig.ENABLED:
        return False

    return PermissionsConfig.USER_OVERRIDE_PROACTIVE_DEFENSE


HAS_PERMISSION = {
    MS_VIEW: ms_view,
    MS_CLEAN: ms_clean,
    MS_CLEAN_REQUIRES_SECURE_SITE_PRO: ms_clean_requires_secure_site_pro,
    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,
    PD_VIEW: pd_view,
    PD_CONFIG_MODE_EDIT: pd_config_mode_edit,
}


def has_permission(permission, user):
    return HAS_PERMISSION[permission](user)


def check_permission(permission, user) -> None:
    if not HAS_PERMISSION[permission](user):
        raise PermissionError("notifications.generalPermissionError")


def permissions_list(user):
    return [
        permission
        for permission in PERMISSIONS
        if has_permission(permission, user)
    ]

Zerion Mini Shell 1.0