2019-08-12 10:53:12 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2018
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL 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 more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
namespace ASC.Api.Settings;
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
[DefaultRoute]
|
|
|
|
[ApiController]
|
|
|
|
public class SmtpSettingsController : ControllerBase
|
2022-02-28 15:31:03 +00:00
|
|
|
{
|
2022-03-01 10:58:02 +00:00
|
|
|
private readonly PermissionContext _permissionContext;
|
|
|
|
private readonly CoreConfiguration _coreConfiguration;
|
|
|
|
private readonly CoreBaseSettings _coreBaseSettings;
|
2022-03-15 11:11:19 +00:00
|
|
|
private readonly IMapper _mapper;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
public SmtpSettingsController(
|
|
|
|
PermissionContext permissionContext,
|
|
|
|
CoreConfiguration coreConfiguration,
|
2022-03-15 11:11:19 +00:00
|
|
|
CoreBaseSettings coreBaseSettings,
|
|
|
|
IMapper mapper)
|
2022-03-01 10:58:02 +00:00
|
|
|
{
|
|
|
|
_permissionContext = permissionContext;
|
|
|
|
_coreConfiguration = coreConfiguration;
|
|
|
|
_coreBaseSettings = coreBaseSettings;
|
2022-03-15 11:11:19 +00:00
|
|
|
_mapper = mapper;
|
2022-03-01 10:58:02 +00:00
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
[Read("smtp")]
|
2022-03-15 10:00:41 +00:00
|
|
|
public SmtpSettingsDto GetSmtpSettings()
|
2022-03-01 10:58:02 +00:00
|
|
|
{
|
|
|
|
CheckSmtpPermissions();
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-15 11:11:19 +00:00
|
|
|
var settings = _mapper.Map<SmtpSettings, SmtpSettingsDto>(_coreConfiguration.SmtpSettings);
|
|
|
|
settings.CredentialsUserPassword = "";
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
return settings;
|
|
|
|
}
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
[Create("smtp")]
|
2022-03-15 11:11:19 +00:00
|
|
|
public SmtpSettingsDto SaveSmtpSettingsFromBody([FromBody] SmtpSettingsDto inDto)
|
2022-03-01 10:58:02 +00:00
|
|
|
{
|
2022-03-15 10:56:22 +00:00
|
|
|
return SaveSmtpSettings(inDto);
|
2022-03-01 10:58:02 +00:00
|
|
|
}
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
[Create("smtp")]
|
|
|
|
[Consumes("application/x-www-form-urlencoded")]
|
2022-03-15 10:56:22 +00:00
|
|
|
public SmtpSettingsDto SaveSmtpSettingsFromForm([FromForm] SmtpSettingsDto inDto)
|
2022-03-01 10:58:02 +00:00
|
|
|
{
|
2022-03-15 10:56:22 +00:00
|
|
|
return SaveSmtpSettings(inDto);
|
2022-03-01 10:58:02 +00:00
|
|
|
}
|
2022-02-28 15:31:03 +00:00
|
|
|
|
2022-03-15 10:56:22 +00:00
|
|
|
private SmtpSettingsDto SaveSmtpSettings(SmtpSettingsDto inDto)
|
2022-03-01 10:58:02 +00:00
|
|
|
{
|
|
|
|
CheckSmtpPermissions();
|
2022-02-28 15:31:03 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
//TODO: Add validation check
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-15 11:12:29 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(inDto);
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-15 10:56:22 +00:00
|
|
|
var settingConfig = ToSmtpSettingsConfig(inDto);
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
_coreConfiguration.SmtpSettings = settingConfig;
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-15 11:11:19 +00:00
|
|
|
var settings = _mapper.Map<SmtpSettings, SmtpSettingsDto>(settingConfig);
|
|
|
|
settings.CredentialsUserPassword = "";
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
return settings;
|
|
|
|
}
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
[Delete("smtp")]
|
2022-03-15 10:00:41 +00:00
|
|
|
public SmtpSettingsDto ResetSmtpSettings()
|
2022-03-01 10:58:02 +00:00
|
|
|
{
|
|
|
|
CheckSmtpPermissions();
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
if (!_coreConfiguration.SmtpSettings.IsDefaultSettings)
|
2019-08-12 10:53:12 +00:00
|
|
|
{
|
2022-03-01 10:58:02 +00:00
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
|
|
|
_coreConfiguration.SmtpSettings = null;
|
|
|
|
}
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
var current = _coreBaseSettings.Standalone ? _coreConfiguration.SmtpSettings : SmtpSettings.Empty;
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-15 11:11:19 +00:00
|
|
|
var settings = _mapper.Map<SmtpSettings, SmtpSettingsDto>(current);
|
|
|
|
settings.CredentialsUserPassword = "";
|
|
|
|
|
|
|
|
return settings;
|
2022-03-01 10:58:02 +00:00
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
//[Read("smtp/test")]
|
|
|
|
//public SmtpOperationStatus TestSmtpSettings()
|
|
|
|
//{
|
|
|
|
// CheckSmtpPermissions();
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// var settings = ToSmtpSettings(CoreConfiguration.SmtpSettings);
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// //add resolve
|
|
|
|
// var smtpTestOp = new SmtpOperation(settings, Tenant.Id, SecurityContext.CurrentAccount.ID, UserManager, SecurityContext, TenantManager, Configuration);
|
2020-02-17 08:58:14 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// SMTPTasks.QueueTask(smtpTestOp.RunJob, smtpTestOp.GetDistributedTask());
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// return ToSmtpOperationStatus();
|
|
|
|
//}
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
//[Read("smtp/test/status")]
|
|
|
|
//public SmtpOperationStatus GetSmtpOperationStatus()
|
|
|
|
//{
|
|
|
|
// CheckSmtpPermissions();
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// return ToSmtpOperationStatus();
|
|
|
|
//}
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
//private SmtpOperationStatus ToSmtpOperationStatus()
|
|
|
|
//{
|
|
|
|
// var operations = SMTPTasks.GetTasks().ToList();
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// foreach (var o in operations)
|
|
|
|
// {
|
|
|
|
// if (!string.IsNullOrEmpty(o.InstanseId) &&
|
|
|
|
// Process.GetProcesses().Any(p => p.Id == int.Parse(o.InstanseId)))
|
|
|
|
// continue;
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// o.SetProperty(SmtpOperation.PROGRESS, 100);
|
|
|
|
// SMTPTasks.RemoveTask(o.Id);
|
|
|
|
// }
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// var operation =
|
|
|
|
// operations
|
|
|
|
// .FirstOrDefault(t => t.GetProperty<int>(SmtpOperation.OWNER) == Tenant.Id);
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// if (operation == null)
|
|
|
|
// {
|
|
|
|
// return null;
|
|
|
|
// }
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// if (DistributedTaskStatus.Running < operation.Status)
|
|
|
|
// {
|
|
|
|
// operation.SetProperty(SmtpOperation.PROGRESS, 100);
|
|
|
|
// SMTPTasks.RemoveTask(operation.Id);
|
|
|
|
// }
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// var result = new SmtpOperationStatus
|
|
|
|
// {
|
|
|
|
// Id = operation.Id,
|
|
|
|
// Completed = operation.GetProperty<bool>(SmtpOperation.FINISHED),
|
|
|
|
// Percents = operation.GetProperty<int>(SmtpOperation.PROGRESS),
|
|
|
|
// Status = operation.GetProperty<string>(SmtpOperation.RESULT),
|
|
|
|
// Error = operation.GetProperty<string>(SmtpOperation.ERROR),
|
|
|
|
// Source = operation.GetProperty<string>(SmtpOperation.SOURCE)
|
|
|
|
// };
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
// return result;
|
|
|
|
//}
|
2019-10-14 08:23:45 +00:00
|
|
|
|
2022-03-15 10:56:22 +00:00
|
|
|
public static SmtpSettings ToSmtpSettingsConfig(SmtpSettingsDto inDto)
|
2022-03-01 10:58:02 +00:00
|
|
|
{
|
|
|
|
var settingsConfig = new SmtpSettings(
|
2022-03-15 10:56:22 +00:00
|
|
|
inDto.Host,
|
|
|
|
inDto.Port ?? SmtpSettings.DefaultSmtpPort,
|
|
|
|
inDto.SenderAddress,
|
|
|
|
inDto.SenderDisplayName)
|
2022-03-01 10:58:02 +00:00
|
|
|
{
|
2022-03-15 10:56:22 +00:00
|
|
|
EnableSSL = inDto.EnableSSL,
|
|
|
|
EnableAuth = inDto.EnableAuth
|
2022-03-01 10:58:02 +00:00
|
|
|
};
|
2019-08-12 10:53:12 +00:00
|
|
|
|
2022-03-15 10:56:22 +00:00
|
|
|
if (inDto.EnableAuth)
|
2019-08-12 10:53:12 +00:00
|
|
|
{
|
2022-03-15 10:56:22 +00:00
|
|
|
settingsConfig.SetCredentials(inDto.CredentialsUserName, inDto.CredentialsUserPassword);
|
2019-08-12 10:53:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
return settingsConfig;
|
|
|
|
}
|
|
|
|
private static void CheckSmtpPermissions()
|
|
|
|
{
|
|
|
|
if (!SetupInfo.IsVisibleSettings(nameof(ManagementType.SmtpSettings)))
|
2019-08-12 10:53:12 +00:00
|
|
|
{
|
2022-03-01 10:58:02 +00:00
|
|
|
throw new BillingException(Resource.ErrorNotAllowedOption, "Smtp");
|
2019-08-12 10:53:12 +00:00
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
}
|
2022-03-01 10:58:02 +00:00
|
|
|
}
|