DocSpace-buildtools/web/ASC.Web.Api/Api/SecurityController.cs

179 lines
7.5 KiB
C#
Raw Normal View History

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;
2022-03-17 16:57:02 +00:00
private readonly AuditReportCreator _auditReportCreator;
2022-03-01 10:58:02 +00:00
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;
2022-03-17 16:57:02 +00:00
this._auditReportCreator = auditReportCreator;
2022-03-01 10:58:02 +00:00
_settingsManager = settingsManager;
}
2020-10-06 07:06:05 +00:00
[HttpGet("audit/login/last")]
2022-06-07 18:00:08 +00:00
public IEnumerable<ApiModel.ResponseDto.LoginEventDto> 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-06-07 18:00:08 +00:00
return _loginEventsRepository.GetLast(_tenantManager.GetCurrentTenant().Id, 20).Select(x => new ApiModel.ResponseDto.LoginEventDto(x));
2022-03-01 10:58:02 +00:00
}
2020-10-06 07:06:05 +00:00
[HttpGet("audit/events/last")]
2022-06-07 18:00:08 +00:00
public IEnumerable<ApiModel.ResponseDto.AuditEventDto> 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-06-07 18:00:08 +00:00
return _auditEventsRepository.GetLast(_tenantManager.GetCurrentTenant().Id, 20).Select(x => new ApiModel.ResponseDto.AuditEventDto(x));
2022-03-01 10:58:02 +00:00
}
2020-10-06 07:06:05 +00:00
[HttpPost("audit/login/report")]
2022-03-01 10:58:02 +00:00
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)))
2022-03-17 15:01:39 +00:00
{
2022-03-01 10:58:02 +00:00
throw new BillingException(Resource.ErrorNotAllowedOption, "Audit");
2022-03-17 15:01:39 +00:00
}
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);
2022-03-17 16:57:02 +00:00
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
[HttpPost("audit/events/report")]
2022-03-01 10:58:02 +00:00
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)))
2022-03-17 15:01:39 +00:00
{
2022-03-01 10:58:02 +00:00
throw new BillingException(Resource.ErrorNotAllowedOption, "Audit");
2022-03-17 15:01:39 +00:00
}
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);
2022-03-17 16:57:02 +00:00
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
[HttpGet("audit/settings/lifetime")]
2022-03-01 10:58:02 +00:00
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
[HttpPost("audit/settings/lifetime")]
public TenantAuditSettings SetAuditSettings(TenantAuditSettingsWrapper wrapper)
2022-03-01 10:58:02 +00:00
{
if (!_tenantExtra.GetTenantQuota().Audit || !SetupInfo.IsVisibleSettings(nameof(ManagementType.LoginHistory)))
2022-03-17 15:01:39 +00:00
{
2022-03-01 10:58:02 +00:00
throw new BillingException(Resource.ErrorNotAllowedOption, "Audit");
2022-03-17 15:01:39 +00:00
}
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-04-14 19:23:57 +00:00
if (wrapper.Settings.LoginHistoryLifeTime <= 0 || wrapper.Settings.LoginHistoryLifeTime > TenantAuditSettings.MaxLifeTime)
2022-03-17 15:01:39 +00:00
{
2022-03-01 10:58:02 +00:00
throw new ArgumentException("LoginHistoryLifeTime");
2022-03-17 15:01:39 +00:00
}
2020-10-06 07:06:05 +00:00
2022-04-14 19:23:57 +00:00
if (wrapper.Settings.AuditTrailLifeTime <= 0 || wrapper.Settings.AuditTrailLifeTime > TenantAuditSettings.MaxLifeTime)
2022-03-17 15:01:39 +00:00
{
2022-03-01 10:58:02 +00:00
throw new ArgumentException("AuditTrailLifeTime");
2022-03-17 15:01:39 +00:00
}
2020-10-06 07:06:05 +00:00
2022-04-14 19:23:57 +00:00
_settingsManager.SaveForTenant(wrapper.Settings, _tenantManager.GetCurrentTenant().Id);
2022-03-01 10:58:02 +00:00
_messageService.Send(MessageAction.AuditSettingsUpdated);
2020-10-06 07:06:05 +00:00
2022-04-14 19:23:57 +00:00
return wrapper.Settings;
2020-10-06 07:06:05 +00:00
}
}