// (c) Copyright Ascensio System SIA 2010-2022 // // This program is a free software product. // You can redistribute it and/or modify it under the terms // of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software // Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended // to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of // any third-party rights. // // This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see // the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html // // You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021. // // The interactive user interfaces in modified source and object code versions of the Program must // display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3. // // Pursuant to Section 7(b) of the License you must retain the original Product logo when // distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under // trademark law for use of our trademarks. // // All the Product's GUI elements, including illustrations and icon sets, as well as technical writing // content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0 // International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode namespace ASC.Web.Api.Controllers.Settings; public class LdapController : BaseSettingsController { private Tenant Tenant { get { return ApiContext.Tenant; } } private readonly SettingsManager _settingsManager; private readonly TenantManager _tenantManager; private readonly LdapNotifyHelper _ldapNotifyHelper; private readonly LdapSaveSyncOperation _ldapSaveSyncOperation; private readonly AuthContext _authContext; private readonly PermissionContext _permissionContext; private readonly CoreBaseSettings _coreBaseSettings; private readonly TenantExtra _tenantExtra; public LdapController( ApiContext apiContext, WebItemManager webItemManager, IMemoryCache memoryCache, SettingsManager settingsManager, TenantManager tenantManager, LdapNotifyHelper ldapNotifyHelper, LdapSaveSyncOperation ldapSaveSyncOperation, AuthContext authContext, PermissionContext permissionContext, CoreBaseSettings coreBaseSettings, TenantExtra tenantExtra, IHttpContextAccessor httpContextAccessor) : base(apiContext, memoryCache, webItemManager, httpContextAccessor) { _settingsManager = settingsManager; _tenantManager = tenantManager; _ldapNotifyHelper = ldapNotifyHelper; _ldapSaveSyncOperation = ldapSaveSyncOperation; _authContext = authContext; _permissionContext = permissionContext; _coreBaseSettings = coreBaseSettings; _tenantExtra = tenantExtra; } /// /// Returns the current portal LDAP settings. /// /// /// Get the LDAP settings /// /// LDAP /// LDAP settings [Read("ldap")] public LdapSettings GetLdapSettings() { CheckLdapPermissions(); var settings = _settingsManager.Load(); settings = settings.Clone() as LdapSettings; // clone LdapSettings object for clear password (potencial AscCache.Memory issue) if (settings == null) { return new LdapSettings().GetDefault(); } settings.Password = null; settings.PasswordBytes = null; if (settings.IsDefault) return settings; var defaultSettings = settings.GetDefault(); if (settings.Equals(defaultSettings)) settings.IsDefault = true; return settings; } /// /// Returns the LDAP autosynchronous cron expression of the current portal if it exists. /// /// /// Get the LDAP cron expression /// /// LDAP /// Cron expression or null [Read("ldap/cron")] public object GetLdapCronSettings() { CheckLdapPermissions(); var settings = _settingsManager.Load(); if (settings == null) settings = new LdapCronSettings().GetDefault(); if (string.IsNullOrEmpty(settings.Cron)) return null; return settings.Cron; } /// /// Sets the LDAP autosynchronous cron expression of the current portal. /// /// /// Set the LDAP cron expression /// /// LDAP /// Cron expression /// [Create("ldap/cron")] public void SetLdapCronSettingsFromBody([FromBody] LdapCronSettings ldapCronSettings) { SetLdapCronSettings(ldapCronSettings); } private void SetLdapCronSettings(LdapCronSettings ldapCronSettings) { CheckLdapPermissions(); var cron = ldapCronSettings.Cron; if (!string.IsNullOrEmpty(cron)) { new CronExpression(cron); // validate if (!_settingsManager.Load().EnableLdapAuthentication) { throw new Exception(Resource.LdapSettingsErrorCantSaveLdapSettings); } } var settings = _settingsManager.Load(); if (settings == null) settings = new LdapCronSettings(); settings.Cron = cron; _settingsManager.Save(settings); var t = _tenantManager.GetCurrentTenant(); if (!string.IsNullOrEmpty(cron)) { _ldapNotifyHelper.UnregisterAutoSync(t); _ldapNotifyHelper.RegisterAutoSync(t, cron); } else { _ldapNotifyHelper.UnregisterAutoSync(t); } } /// /// Starts synchronizing users and groups by LDAP. /// /// /// Synchronize by LDAP /// /// LDAP /// Operation status [Read("ldap/sync")] public LdapOperationStatus SyncLdap() { CheckLdapPermissions(); var ldapSettings = _settingsManager.Load(); var userId = _authContext.CurrentAccount.ID.ToString(); return _ldapSaveSyncOperation.SyncLdap(ldapSettings, Tenant, userId); } /// /// Starts the process of collecting preliminary changes on the portal during the synchronization process according to the selected LDAP settings. /// /// /// Test the LDAP synchronization /// /// LDAP /// Operation status [Read("ldap/sync/test")] public LdapOperationStatus TestLdapSync() { CheckLdapPermissions(); var ldapSettings = _settingsManager.Load(); return _ldapSaveSyncOperation.TestLdapSync(ldapSettings, Tenant); } /// /// Saves the LDAP settings specified in the request and starts importing/synchronizing users and groups by LDAP. /// /// /// Save the LDAP settings /// /// LDAP /// LDAP settings in the serialized string format /// Specifies if the errors of checking certificates are allowed (true) or not (false) /// Operation status [Create("ldap")] public LdapOperationStatus SaveLdapSettings([FromBody] LdapSettings ldapSettings) { CheckLdapPermissions(); if (!ldapSettings.EnableLdapAuthentication) { SetLdapCronSettings(null); } var userId = _authContext.CurrentAccount.ID.ToString(); return _ldapSaveSyncOperation.SaveLdapSettings(ldapSettings, Tenant, userId); } /// /// Starts the process of collecting preliminary changes on the portal during the saving process according to the LDAP settings. /// /// /// Test the LDAP saving process /// /// LDAP /// LDAP settings in the serialized string format /// Specifies if the errors of checking certificates are allowed (true) or not (false) /// Operation status [Create("ldap/save/test")] public LdapOperationStatus TestLdapSave([FromBody] LdapSettings ldapSettings) { CheckLdapPermissions(); var userId = _authContext.CurrentAccount.ID.ToString(); return _ldapSaveSyncOperation.TestLdapSave(ldapSettings, Tenant, userId); } /// /// Returns the LDAP synchronization process status. /// /// /// Get the LDAP synchronization process status /// /// LDAP /// Operation status [Read("ldap/status")] public LdapOperationStatus GetLdapOperationStatus() { CheckLdapPermissions(); return _ldapSaveSyncOperation.ToLdapOperationStatus(Tenant.Id); } /// /// Returns the LDAP default settings. /// /// /// Get the LDAP default settings /// /// LDAP /// LDAP default settings [Read("ldap/default")] public LdapSettings GetDefaultLdapSettings() { CheckLdapPermissions(); return new LdapSettings().GetDefault(); } private void CheckLdapPermissions() { //_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings); //if (!_coreBaseSettings.Standalone // && (!SetupInfo.IsVisibleSettings(ManagementType.LdapSettings.ToString()) // || !_tenantExtra.GetTenantQuota().Ldap)) //{ // throw new BillingException(Resource.ErrorNotAllowedOption, "Ldap"); //} } }