2022-03-15 18:00:53 +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;
|
2022-03-01 10:58:02 +00:00
|
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
|
[DefaultRoute]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class SecurityController : ControllerBase
|
2022-02-28 15:31:03 +00:00
|
|
|
|
{
|
2022-03-01 10:58:02 +00:00
|
|
|
|
private readonly PermissionContext _permissionContext;
|
|
|
|
|
private readonly TenantExtra _tenantExtra;
|
|
|
|
|
private readonly TenantManager _tenantManager;
|
|
|
|
|
private readonly MessageService _messageService;
|
|
|
|
|
private readonly LoginEventsRepository _loginEventsRepository;
|
|
|
|
|
private readonly AuditEventsRepository _auditEventsRepository;
|
|
|
|
|
private readonly AuditReportCreator auditReportCreator;
|
|
|
|
|
private readonly SettingsManager _settingsManager;
|
|
|
|
|
|
|
|
|
|
public SecurityController(
|
|
|
|
|
PermissionContext permissionContext,
|
|
|
|
|
TenantExtra tenantExtra,
|
|
|
|
|
TenantManager tenantManager,
|
|
|
|
|
MessageService messageService,
|
|
|
|
|
LoginEventsRepository loginEventsRepository,
|
|
|
|
|
AuditEventsRepository auditEventsRepository,
|
|
|
|
|
AuditReportCreator auditReportCreator,
|
|
|
|
|
SettingsManager settingsManager)
|
2020-10-06 07:06:05 +00:00
|
|
|
|
{
|
2022-03-01 10:58:02 +00:00
|
|
|
|
_permissionContext = permissionContext;
|
|
|
|
|
_tenantExtra = tenantExtra;
|
|
|
|
|
_tenantManager = tenantManager;
|
|
|
|
|
_messageService = messageService;
|
|
|
|
|
_loginEventsRepository = loginEventsRepository;
|
|
|
|
|
_auditEventsRepository = auditEventsRepository;
|
|
|
|
|
this.auditReportCreator = auditReportCreator;
|
|
|
|
|
_settingsManager = settingsManager;
|
|
|
|
|
}
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
[Read("audit/login/last")]
|
2022-03-15 10:00:41 +00:00
|
|
|
|
public IEnumerable<EventDto> GetLastLoginEvents()
|
2022-03-01 10:58:02 +00:00
|
|
|
|
{
|
|
|
|
|
if (!SetupInfo.IsVisibleSettings(nameof(ManagementType.LoginHistory)))
|
2022-02-28 15:31:03 +00:00
|
|
|
|
{
|
2022-03-01 10:58:02 +00:00
|
|
|
|
throw new BillingException(Resource.ErrorNotAllowedOption, "Audit");
|
|
|
|
|
}
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-15 10:00:41 +00:00
|
|
|
|
return _loginEventsRepository.GetLast(_tenantManager.GetCurrentTenant().Id, 20).Select(x => new EventDto(x));
|
2022-03-01 10:58:02 +00:00
|
|
|
|
}
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
[Read("audit/events/last")]
|
2022-03-15 10:00:41 +00:00
|
|
|
|
public IEnumerable<EventDto> GetLastAuditEvents()
|
2022-03-01 10:58:02 +00:00
|
|
|
|
{
|
|
|
|
|
if (!SetupInfo.IsVisibleSettings(nameof(ManagementType.AuditTrail)))
|
2022-02-28 15:31:03 +00:00
|
|
|
|
{
|
2022-03-01 10:58:02 +00:00
|
|
|
|
throw new BillingException(Resource.ErrorNotAllowedOption, "Audit");
|
|
|
|
|
}
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-15 10:00:41 +00:00
|
|
|
|
return _auditEventsRepository.GetLast(_tenantManager.GetCurrentTenant().Id, 20).Select(x => new EventDto(x));
|
2022-03-01 10:58:02 +00:00
|
|
|
|
}
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
[Create("audit/login/report")]
|
|
|
|
|
public object CreateLoginHistoryReport()
|
|
|
|
|
{
|
|
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
var tenantId = _tenantManager.GetCurrentTenant().Id;
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
if (!_tenantExtra.GetTenantQuota().Audit || !SetupInfo.IsVisibleSettings(nameof(ManagementType.LoginHistory)))
|
|
|
|
|
throw new BillingException(Resource.ErrorNotAllowedOption, "Audit");
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
var settings = _settingsManager.LoadForTenant<TenantAuditSettings>(_tenantManager.GetCurrentTenant().Id);
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
var to = DateTime.UtcNow;
|
|
|
|
|
var from = to.Subtract(TimeSpan.FromDays(settings.LoginHistoryLifeTime));
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
var reportName = string.Format(AuditReportResource.LoginHistoryReportName + ".csv", from.ToShortDateString(), to.ToShortDateString());
|
|
|
|
|
var events = _loginEventsRepository.Get(tenantId, from, to);
|
|
|
|
|
var result = auditReportCreator.CreateCsvReport(events, reportName);
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
_messageService.Send(MessageAction.LoginHistoryReportDownloaded);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
[Create("audit/events/report")]
|
|
|
|
|
public object CreateAuditTrailReport()
|
|
|
|
|
{
|
|
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
var tenantId = _tenantManager.GetCurrentTenant().Id;
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
if (!_tenantExtra.GetTenantQuota().Audit || !SetupInfo.IsVisibleSettings(nameof(ManagementType.AuditTrail)))
|
|
|
|
|
throw new BillingException(Resource.ErrorNotAllowedOption, "Audit");
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
var settings = _settingsManager.LoadForTenant<TenantAuditSettings>(_tenantManager.GetCurrentTenant().Id);
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
var to = DateTime.UtcNow;
|
|
|
|
|
var from = to.Subtract(TimeSpan.FromDays(settings.AuditTrailLifeTime));
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
var reportName = string.Format(AuditReportResource.AuditTrailReportName + ".csv", from.ToString("MM.dd.yyyy"), to.ToString("MM.dd.yyyy"));
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
var events = _auditEventsRepository.Get(tenantId, from, to);
|
|
|
|
|
var result = auditReportCreator.CreateCsvReport(events, reportName);
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
_messageService.Send(MessageAction.AuditTrailReportDownloaded);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
[Read("audit/settings/lifetime")]
|
|
|
|
|
public TenantAuditSettings GetAuditSettings()
|
|
|
|
|
{
|
|
|
|
|
if (!SetupInfo.IsVisibleSettings(nameof(ManagementType.LoginHistory)))
|
2022-02-28 15:31:03 +00:00
|
|
|
|
{
|
2022-03-01 10:58:02 +00:00
|
|
|
|
throw new BillingException(Resource.ErrorNotAllowedOption, "Audit");
|
|
|
|
|
}
|
2021-05-23 16:11:25 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
return _settingsManager.LoadForTenant<TenantAuditSettings>(_tenantManager.GetCurrentTenant().Id);
|
|
|
|
|
}
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
[Create("audit/settings/lifetime")]
|
|
|
|
|
public TenantAuditSettings SetAuditSettingsFromBody([FromBody] TenantAuditSettingsWrapper wrapper)
|
|
|
|
|
{
|
|
|
|
|
return SetAuditSettings(wrapper);
|
|
|
|
|
}
|
2022-02-28 15:31:03 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
[Create("audit/settings/lifetime")]
|
|
|
|
|
[Consumes("application/x-www-form-urlencoded")]
|
|
|
|
|
public TenantAuditSettings SetAuditSettingsFromForm([FromForm] TenantAuditSettingsWrapper wrapper)
|
|
|
|
|
{
|
|
|
|
|
return SetAuditSettings(wrapper);
|
|
|
|
|
}
|
2022-02-28 15:31:03 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
private TenantAuditSettings SetAuditSettings(TenantAuditSettingsWrapper wrapper)
|
|
|
|
|
{
|
|
|
|
|
if (!_tenantExtra.GetTenantQuota().Audit || !SetupInfo.IsVisibleSettings(nameof(ManagementType.LoginHistory)))
|
|
|
|
|
throw new BillingException(Resource.ErrorNotAllowedOption, "Audit");
|
2021-05-23 16:11:25 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
if (wrapper.settings.LoginHistoryLifeTime <= 0 || wrapper.settings.LoginHistoryLifeTime > TenantAuditSettings.MaxLifeTime)
|
|
|
|
|
throw new ArgumentException("LoginHistoryLifeTime");
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
if (wrapper.settings.AuditTrailLifeTime <= 0 || wrapper.settings.AuditTrailLifeTime > TenantAuditSettings.MaxLifeTime)
|
|
|
|
|
throw new ArgumentException("AuditTrailLifeTime");
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
_settingsManager.SaveForTenant(wrapper.settings, _tenantManager.GetCurrentTenant().Id);
|
|
|
|
|
_messageService.Send(MessageAction.AuditSettingsUpdated);
|
2020-10-06 07:06:05 +00:00
|
|
|
|
|
2022-03-01 10:58:02 +00:00
|
|
|
|
return wrapper.settings;
|
2020-10-06 07:06:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|