<commit_before>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information

namespace Dnn.PersonaBar.Extensions.Components.Editors
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Dnn.PersonaBar.Extensions.Components.Dto;
    using Dnn.PersonaBar.Extensions.Components.Dto.Editors;
    using Dnn.PersonaBar.Library.Helper;
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Entities.Modules.Definitions;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Instrumentation;
    using DotNetNuke.Security.Permissions;
    using DotNetNuke.Services.Installer.Packages;
    using Newtonsoft.Json;

    public class ModulePackageEditor : IPackageEditor
    {
        private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(ModulePackageEditor));

        public PackageInfoDto GetPackageDetail(int portalId, PackageInfo package)
        {
            var desktopModule = DesktopModuleController.GetDesktopModuleByPackageID(package.PackageID);

            if (desktopModule == null)
            {
                return new PackageInfoDto(portalId, package);
            }

            var isHostUser = UserController.Instance.GetCurrentUserInfo().IsSuperUser;

            var detail = isHostUser ? new ModulePackageDetailDto(portalId, package, desktopModule)
                                        : new ModulePackagePermissionsDto(portalId, package);

            detail.DesktopModuleId = desktopModule.DesktopModuleID;
            detail.Permissions = this.GetPermissionsData(portalId, desktopModule.DesktopModuleID);

            return detail;
        }

        public bool SavePackageSettings(PackageSettingsDto packageSettings, out string errorMessage)
        {
            errorMessage = string.Empty;

            try
            {
                var desktopModule = DesktopModuleController.GetDesktopModuleByPackageID(packageSettings.PackageId);

                if (desktopModule == null)
                {
                    return false;
                }

                var isHostUser = UserController.Instance.GetCurrentUserInfo().IsSuperUser;

                this.UpdatePermissions(desktopModule, packageSettings);

                if (isHostUser)
                {
                    foreach (var settingName in packageSettings.EditorActions.Keys)
                    {
                        var settingValue = packageSettings.EditorActions[settingName];

                        switch (settingName.ToLowerInvariant())
                        {
                            case "foldername":
                                desktopModule.FolderName = settingValue;
                                break;
                            case "category":
                                desktopModule.Category = settingValue;
                                break;
                            case "businesscontroller":
                                desktopModule.BusinessControllerClass = settingValue;
                                break;
                            case "dependencies":
                                desktopModule.Dependencies = settingValue;
                                break;
                            case "hostpermissions":
                                desktopModule.Permissions = settingValue;
                                break;
                            case "premiummodule":
                                desktopModule.IsPremium = Convert.ToBoolean(settingValue);
                                break;
                            case "shareable":
                                desktopModule.Shareable = (ModuleSharing)Convert.ToInt32(settingValue);
                                break;
                            case "assignportal":
                                AssignPortals(desktopModule, JsonConvert.DeserializeObject<IList<ListItemDto>>(settingValue));
                                break;
                            case "unassignportal":
                                UnassignPortals(desktopModule, JsonConvert.DeserializeObject<IList<ListItemDto>>(settingValue));
                                break;
                            case "savedefinition":
                                var definition = JsonConvert.DeserializeObject<ModuleDefinitionDto>(settingValue);
                                SaveModuleDefinition(definition);
                                break;
                            case "deletedefinition":
                                DeleteModuleDefinition(Convert.ToInt32(settingValue));
                                break;
                            case "savemodulecontrol":
                                var moduleControl = JsonConvert.DeserializeObject<ModuleControlDto>(settingValue);
                                SaveModuleControl(moduleControl);
                                break;
                            case "deletemodulecontrol":
                                DeleteModuleControl(Convert.ToInt32(settingValue));
                                break;
                        }
                    }

                    DesktopModuleController.SaveDesktopModule(desktopModule, false, true);
                }
                return true;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                errorMessage = ex.Message;
                return false;
            }
        }

        private static void UnassignPortals(DesktopModuleInfo desktopModule, IList<ListItemDto> portals)
        {
            foreach (var portal in portals)
            {
                DesktopModuleController.RemoveDesktopModuleFromPortal(portal.Id, desktopModule.DesktopModuleID, true);
            }
        }

        private static void AssignPortals(DesktopModuleInfo desktopModule, IList<ListItemDto> portals)
        {
            foreach (var portal in portals)
            {
                DesktopModuleController.AddDesktopModuleToPortal(portal.Id, desktopModule.DesktopModuleID, true, true);
            }
        }

        private static void SaveModuleDefinition(ModuleDefinitionDto definitionDto)
        {
            var moduleDefinition = definitionDto.ToModuleDefinitionInfo();
            ModuleDefinitionController.SaveModuleDefinition(moduleDefinition, false, true);
        }

        private static void DeleteModuleDefinition(int defId)<commit_msg>module friendly name updated as part of module extension update<commit_after>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information

namespace Dnn.PersonaBar.Extensions.Components.Editors
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Dnn.PersonaBar.Extensions.Components.Dto;
    using Dnn.PersonaBar.Extensions.Components.Dto.Editors;
    using Dnn.PersonaBar.Library.Helper;
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Entities.Modules.Definitions;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Instrumentation;
    using DotNetNuke.Security.Permissions;
    using DotNetNuke.Services.Installer.Packages;
    using Newtonsoft.Json;

    public class ModulePackageEditor : IPackageEditor
    {
        private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(ModulePackageEditor));

        public PackageInfoDto GetPackageDetail(int portalId, PackageInfo package)
        {
            var desktopModule = DesktopModuleController.GetDesktopModuleByPackageID(package.PackageID);

            if (desktopModule == null)
            {
                return new PackageInfoDto(portalId, package);
            }

            var isHostUser = UserController.Instance.GetCurrentUserInfo().IsSuperUser;

            var detail = isHostUser ? new ModulePackageDetailDto(portalId, package, desktopModule)
                                        : new ModulePackagePermissionsDto(portalId, package);

            detail.DesktopModuleId = desktopModule.DesktopModuleID;
            detail.Permissions = this.GetPermissionsData(portalId, desktopModule.DesktopModuleID);

            return detail;
        }

        public bool SavePackageSettings(PackageSettingsDto packageSettings, out string errorMessage)
        {
            errorMessage = string.Empty;

            try
            {
                var desktopModule = DesktopModuleController.GetDesktopModuleByPackageID(packageSettings.PackageId);

                if (desktopModule == null)
                {
                    return false;
                }

                var isHostUser = UserController.Instance.GetCurrentUserInfo().IsSuperUser;

                this.UpdatePermissions(desktopModule, packageSettings);

                if (isHostUser)
                {
                    this.UpdateModuleProperties(desktopModule, packageSettings.Settings);
                    this.UpdateModuleProperties(desktopModule, packageSettings.EditorActions);

                    DesktopModuleController.SaveDesktopModule(desktopModule, false, true);
                }
                return true;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                errorMessage = ex.Message;
                return false;
            }
        }

        private void UpdateModuleProperties(DesktopModuleInfo desktopModule, IDictionary<string, string> settings)
        {
            foreach (var settingName in settings.Keys)
            {
                var settingValue = settings[settingName];

                switch (settingName.ToLowerInvariant())
                {
                    case "foldername":
                        desktopModule.FolderName = settingValue;
                        break;
                    case "category":
                        desktopModule.Category = settingValue;
                        break;
                    case "businesscontroller":
                        desktopModule.BusinessControllerClass = settingValue;
                        break;
                    case "dependencies":
                        desktopModule.Dependencies = settingValue;
                        break;
                    case "hostpermissions":
                        desktopModule.Permissions = settingValue;
                        break;
                    case "premiummodule":
                        desktopModule.IsPremium = Convert.ToBoolean(settingValue);
                        break;
                    case "shareable":
                        desktopModule.Shareable = (ModuleSharing)Convert.ToInt32(settingValue);
                        break;
                    case "assignportal":
                        AssignPortals(desktopModule, JsonConvert.DeserializeObject<IList<ListItemDto>>(settingValue));
                        break;
                    case "unassignportal":
                        UnassignPortals(desktopModule, JsonConvert.DeserializeObject<IList<ListItemDto>>(settingValue));
                        break;
                    case "savedefinition":
                        var definition = JsonConvert.DeserializeObject<ModuleDefinitionDto>(settingValue);
                        SaveModuleDefinition(definition);
                        break;
                    case "deletedefinition":
                        DeleteModuleDefinition(Convert.ToInt32(settingValue));
                        break;
                    case "savemodulecontrol":
                        var moduleControl = JsonConvert.DeserializeObject<ModuleControlDto>(settingValue);
                        SaveModuleControl(moduleControl);
                        break;
                    case "deletemodulecontrol":
                        DeleteModuleControl(Convert.ToInt32(settingValue));
                        break;
                    case "friendlyname":
                        desktopModule.FriendlyName = settingValue;
                        break;
                }
            }
        }

        private static void UnassignPortals(DesktopModuleInfo desktopModule, IList<ListItemDto> portals)
        {
            foreach (var portal in portals)
            {
                DesktopModuleController.RemoveDesktopModuleFromPortal(portal.Id, desktopModule.DesktopModuleID, true);
            }
        }

        private static void AssignPortals(DesktopModuleInfo desktopModule, IList<ListItemDto> portals)
        {
            foreach (var portal in portals)
            {
                DesktopModuleController.AddDesktopModuleToPortal(portal.Id, desktopModule.DesktopModuleID, true, true);
            }
        }

        private static void SaveModuleDefinition(ModuleDefinitionDto definitionDto)
        {
            var moduleDefinition = definitionDto.ToModuleDefinitionInfo();
            ModuleDefinitionController.SaveModuleDefinition(moduleDefinition, false, true);
        }

        private static void DeleteModuleDefinition(int defId)
{'fraction_non_alphanumeric': 0.061022744165303996, 'fraction_numerical': 0.0008919280511372082, 'mean_word_length': 1.5979918903263177, 'pattern_counts': {'":': 27, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '145630', 'n_tokens_mistral': 3025, 'n_tokens_neox': 2834, 'n_words': 576}