2022-05-05 13:23:05 +00:00
|
|
|
|
// (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;
|
2022-07-01 11:49:59 +00:00
|
|
|
|
private readonly LdapNotifyService _ldapNotifyHelper;
|
2022-05-05 13:23:05 +00:00
|
|
|
|
private readonly LdapSaveSyncOperation _ldapSaveSyncOperation;
|
|
|
|
|
private readonly AuthContext _authContext;
|
|
|
|
|
private readonly PermissionContext _permissionContext;
|
|
|
|
|
private readonly CoreBaseSettings _coreBaseSettings;
|
|
|
|
|
private readonly TenantExtra _tenantExtra;
|
2022-05-26 14:39:02 +00:00
|
|
|
|
private readonly IMapper _mapper;
|
2022-05-05 13:23:05 +00:00
|
|
|
|
|
|
|
|
|
public LdapController(
|
|
|
|
|
ApiContext apiContext,
|
|
|
|
|
WebItemManager webItemManager,
|
|
|
|
|
IMemoryCache memoryCache,
|
|
|
|
|
SettingsManager settingsManager,
|
|
|
|
|
TenantManager tenantManager,
|
2022-07-01 11:49:59 +00:00
|
|
|
|
LdapNotifyService ldapNotifyHelper,
|
2022-05-05 13:23:05 +00:00
|
|
|
|
LdapSaveSyncOperation ldapSaveSyncOperation,
|
|
|
|
|
AuthContext authContext,
|
|
|
|
|
PermissionContext permissionContext,
|
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
|
|
|
|
TenantExtra tenantExtra,
|
2022-05-26 14:39:02 +00:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
|
|
|
|
IMapper mapper) : base(apiContext, memoryCache, webItemManager, httpContextAccessor)
|
2022-05-05 13:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
_settingsManager = settingsManager;
|
|
|
|
|
_tenantManager = tenantManager;
|
|
|
|
|
_ldapNotifyHelper = ldapNotifyHelper;
|
|
|
|
|
_ldapSaveSyncOperation = ldapSaveSyncOperation;
|
|
|
|
|
_authContext = authContext;
|
|
|
|
|
_permissionContext = permissionContext;
|
|
|
|
|
_coreBaseSettings = coreBaseSettings;
|
|
|
|
|
_tenantExtra = tenantExtra;
|
2022-05-26 14:39:02 +00:00
|
|
|
|
_mapper = mapper;
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the current portal LDAP settings.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Get the LDAP settings
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>LDAP</category>
|
|
|
|
|
/// <returns>LDAP settings</returns>
|
2022-06-08 09:42:49 +00:00
|
|
|
|
[HttpGet("ldap")]
|
2022-05-26 14:39:02 +00:00
|
|
|
|
public LdapSettingsDto GetLdapSettings()
|
2022-05-05 13:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
CheckLdapPermissions();
|
|
|
|
|
|
|
|
|
|
var settings = _settingsManager.Load<LdapSettings>();
|
|
|
|
|
|
|
|
|
|
settings = settings.Clone() as LdapSettings; // clone LdapSettings object for clear password (potencial AscCache.Memory issue)
|
|
|
|
|
|
|
|
|
|
if (settings == null)
|
|
|
|
|
{
|
2022-05-26 14:39:02 +00:00
|
|
|
|
settings = new LdapSettings().GetDefault();
|
|
|
|
|
return _mapper.Map<LdapSettings, LdapSettingsDto>(settings);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
settings.Password = null;
|
|
|
|
|
settings.PasswordBytes = null;
|
|
|
|
|
|
|
|
|
|
if (settings.IsDefault)
|
2022-05-26 14:39:02 +00:00
|
|
|
|
return _mapper.Map<LdapSettings, LdapSettingsDto>(settings); ;
|
2022-05-05 13:23:05 +00:00
|
|
|
|
|
|
|
|
|
var defaultSettings = settings.GetDefault();
|
|
|
|
|
|
|
|
|
|
if (settings.Equals(defaultSettings))
|
|
|
|
|
settings.IsDefault = true;
|
|
|
|
|
|
2022-05-26 14:39:02 +00:00
|
|
|
|
return _mapper.Map<LdapSettings, LdapSettingsDto>(settings);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the LDAP autosynchronous cron expression of the current portal if it exists.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Get the LDAP cron expression
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>LDAP</category>
|
|
|
|
|
/// <returns>Cron expression or null</returns>
|
2022-06-08 09:42:49 +00:00
|
|
|
|
[HttpGet("ldap/cron")]
|
2022-05-26 14:39:02 +00:00
|
|
|
|
public LdapCronSettingsDto GetLdapCronSettings()
|
2022-05-05 13:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
CheckLdapPermissions();
|
|
|
|
|
|
|
|
|
|
var settings = _settingsManager.Load<LdapCronSettings>();
|
|
|
|
|
|
|
|
|
|
if (settings == null)
|
|
|
|
|
settings = new LdapCronSettings().GetDefault();
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(settings.Cron))
|
|
|
|
|
return null;
|
|
|
|
|
|
2022-05-26 14:39:02 +00:00
|
|
|
|
return _mapper.Map<LdapCronSettings, LdapCronSettingsDto>(settings);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the LDAP autosynchronous cron expression of the current portal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Set the LDAP cron expression
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>LDAP</category>
|
|
|
|
|
/// <param name="cron">Cron expression</param>
|
2022-05-24 09:59:04 +00:00
|
|
|
|
///
|
2022-06-08 09:42:49 +00:00
|
|
|
|
[HttpPost("ldap/cron")]
|
|
|
|
|
public void SetLdapCronSettingsFromBody(LdapCronRequestDto ldapCronRequest)
|
2022-05-05 13:23:05 +00:00
|
|
|
|
{
|
2022-05-26 14:39:02 +00:00
|
|
|
|
SetLdapCronSettings(ldapCronRequest);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 09:42:49 +00:00
|
|
|
|
private void SetLdapCronSettings(LdapCronRequestDto ldapCronRequest)
|
2022-05-05 13:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
CheckLdapPermissions();
|
|
|
|
|
|
2022-05-26 14:39:02 +00:00
|
|
|
|
var cron = ldapCronRequest.Cron;
|
2022-05-05 13:23:05 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(cron))
|
|
|
|
|
{
|
|
|
|
|
new CronExpression(cron); // validate
|
|
|
|
|
|
|
|
|
|
if (!_settingsManager.Load<LdapSettings>().EnableLdapAuthentication)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(Resource.LdapSettingsErrorCantSaveLdapSettings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var settings = _settingsManager.Load<LdapCronSettings>();
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts synchronizing users and groups by LDAP.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Synchronize by LDAP
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>LDAP</category>
|
|
|
|
|
/// <returns>Operation status</returns>
|
2022-06-08 09:42:49 +00:00
|
|
|
|
[HttpGet("ldap/sync")]
|
2022-05-26 14:39:02 +00:00
|
|
|
|
public LdapStatusDto SyncLdap()
|
2022-05-05 13:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
CheckLdapPermissions();
|
|
|
|
|
|
|
|
|
|
var ldapSettings = _settingsManager.Load<LdapSettings>();
|
|
|
|
|
|
|
|
|
|
var userId = _authContext.CurrentAccount.ID.ToString();
|
|
|
|
|
|
2022-05-26 14:39:02 +00:00
|
|
|
|
var result = _ldapSaveSyncOperation.SyncLdap(ldapSettings, Tenant, userId);
|
|
|
|
|
|
|
|
|
|
return _mapper.Map<LdapOperationStatus, LdapStatusDto>(result);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the process of collecting preliminary changes on the portal during the synchronization process according to the selected LDAP settings.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Test the LDAP synchronization
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>LDAP</category>
|
|
|
|
|
/// <returns>Operation status</returns>
|
2022-06-08 09:42:49 +00:00
|
|
|
|
[HttpGet("ldap/sync/test")]
|
2022-05-26 14:39:02 +00:00
|
|
|
|
public LdapStatusDto TestLdapSync()
|
2022-05-05 13:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
CheckLdapPermissions();
|
|
|
|
|
|
|
|
|
|
var ldapSettings = _settingsManager.Load<LdapSettings>();
|
|
|
|
|
|
2022-05-26 14:39:02 +00:00
|
|
|
|
var result = _ldapSaveSyncOperation.TestLdapSync(ldapSettings, Tenant);
|
|
|
|
|
|
|
|
|
|
return _mapper.Map<LdapOperationStatus, LdapStatusDto>(result);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the LDAP settings specified in the request and starts importing/synchronizing users and groups by LDAP.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Save the LDAP settings
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>LDAP</category>
|
|
|
|
|
/// <param name="settings">LDAP settings in the serialized string format</param>
|
|
|
|
|
/// <param name="acceptCertificate">Specifies if the errors of checking certificates are allowed (true) or not (false)</param>
|
|
|
|
|
/// <returns>Operation status</returns>
|
2022-06-08 09:42:49 +00:00
|
|
|
|
[HttpPost("ldap")]
|
2022-05-26 14:39:02 +00:00
|
|
|
|
public LdapStatusDto SaveLdapSettings(LdapRequestsDto ldapRequestsDto)
|
2022-05-05 13:23:05 +00:00
|
|
|
|
{
|
2022-05-26 14:39:02 +00:00
|
|
|
|
var ldapSettings = _mapper.Map<LdapRequestsDto, LdapSettings>(ldapRequestsDto);
|
|
|
|
|
|
2022-05-05 13:23:05 +00:00
|
|
|
|
CheckLdapPermissions();
|
|
|
|
|
|
|
|
|
|
if (!ldapSettings.EnableLdapAuthentication)
|
|
|
|
|
{
|
|
|
|
|
SetLdapCronSettings(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var userId = _authContext.CurrentAccount.ID.ToString();
|
|
|
|
|
|
2022-07-01 11:49:59 +00:00
|
|
|
|
var result = _ldapSaveSyncOperation.SaveLdapSettings(ldapSettings, Tenant, userId);
|
2022-05-26 14:39:02 +00:00
|
|
|
|
|
|
|
|
|
return _mapper.Map<LdapOperationStatus, LdapStatusDto>(result);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the process of collecting preliminary changes on the portal during the saving process according to the LDAP settings.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Test the LDAP saving process
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>LDAP</category>
|
|
|
|
|
/// <param name="settings">LDAP settings in the serialized string format</param>
|
|
|
|
|
/// <param name="acceptCertificate">Specifies if the errors of checking certificates are allowed (true) or not (false)</param>
|
|
|
|
|
/// <returns>Operation status</returns>
|
2022-06-08 09:42:49 +00:00
|
|
|
|
[HttpPost("ldap/save/test")]
|
2022-05-26 14:39:02 +00:00
|
|
|
|
public LdapStatusDto TestLdapSave(LdapSettings ldapSettings)
|
2022-05-05 13:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
CheckLdapPermissions();
|
|
|
|
|
|
|
|
|
|
var userId = _authContext.CurrentAccount.ID.ToString();
|
|
|
|
|
|
2022-05-26 14:39:02 +00:00
|
|
|
|
var result = _ldapSaveSyncOperation.TestLdapSave(ldapSettings, Tenant, userId);
|
|
|
|
|
|
|
|
|
|
return _mapper.Map<LdapOperationStatus, LdapStatusDto>(result);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the LDAP synchronization process status.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Get the LDAP synchronization process status
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>LDAP</category>
|
|
|
|
|
/// <returns>Operation status</returns>
|
2022-06-08 09:42:49 +00:00
|
|
|
|
[HttpGet("ldap/status")]
|
2022-05-26 14:39:02 +00:00
|
|
|
|
public LdapStatusDto GetLdapOperationStatus()
|
2022-05-05 13:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
CheckLdapPermissions();
|
|
|
|
|
|
2022-05-26 14:39:02 +00:00
|
|
|
|
var result = _ldapSaveSyncOperation.ToLdapOperationStatus(Tenant.Id);
|
|
|
|
|
|
|
|
|
|
return _mapper.Map<LdapOperationStatus, LdapStatusDto>(result);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the LDAP default settings.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Get the LDAP default settings
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>LDAP</category>
|
|
|
|
|
/// <returns>LDAP default settings</returns>
|
2022-06-08 09:42:49 +00:00
|
|
|
|
[HttpGet("ldap/default")]
|
2022-05-26 14:39:02 +00:00
|
|
|
|
public LdapSettingsDto GetDefaultLdapSettings()
|
2022-05-05 13:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
CheckLdapPermissions();
|
|
|
|
|
|
2022-07-01 11:49:59 +00:00
|
|
|
|
var settings = new LdapSettings().GetDefault();
|
2022-05-26 14:39:02 +00:00
|
|
|
|
|
|
|
|
|
return _mapper.Map<LdapSettings, LdapSettingsDto>(settings);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckLdapPermissions()
|
|
|
|
|
{
|
2022-05-26 14:39:02 +00:00
|
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
|
|
|
|
|
|
|
|
|
if (!_coreBaseSettings.Standalone
|
|
|
|
|
&& (!SetupInfo.IsVisibleSettings(ManagementType.LdapSettings.ToString())
|
|
|
|
|
|| !_tenantExtra.GetTenantQuota().Ldap))
|
|
|
|
|
{
|
|
|
|
|
throw new BillingException(Resource.ErrorNotAllowedOption, "Ldap");
|
|
|
|
|
}
|
2022-05-05 13:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|