2022-03-03 17:33:11 +00:00
|
|
|
namespace ASC.Api.Documents;
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
[DefaultRoute]
|
|
|
|
[ApiController]
|
|
|
|
public class PrivacyRoomController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly AuthContext _authContext;
|
2022-03-04 10:25:12 +00:00
|
|
|
private readonly EncryptionKeyPairDtoHelper _encryptionKeyPairHelper;
|
2022-03-03 17:33:11 +00:00
|
|
|
private readonly FileStorageService<string> _fileStorageService;
|
|
|
|
private readonly FileStorageService<int> _fileStorageServiceInt;
|
|
|
|
private readonly ILog _logger;
|
|
|
|
private readonly MessageService _messageService;
|
|
|
|
private readonly PermissionContext _permissionContext;
|
|
|
|
private readonly SettingsManager _settingsManager;
|
|
|
|
private readonly TenantManager _tenantManager;
|
|
|
|
|
|
|
|
public PrivacyRoomController(
|
|
|
|
AuthContext authContext,
|
|
|
|
PermissionContext permissionContext,
|
|
|
|
SettingsManager settingsManager,
|
|
|
|
TenantManager tenantManager,
|
2022-03-04 10:25:12 +00:00
|
|
|
EncryptionKeyPairDtoHelper encryptionKeyPairHelper,
|
2022-03-03 17:33:11 +00:00
|
|
|
FileStorageService<int> fileStorageServiceInt,
|
|
|
|
FileStorageService<string> fileStorageService,
|
|
|
|
MessageService messageService,
|
|
|
|
IOptionsMonitor<ILog> option)
|
|
|
|
{
|
|
|
|
_authContext = authContext;
|
|
|
|
_permissionContext = permissionContext;
|
|
|
|
_settingsManager = settingsManager;
|
|
|
|
_tenantManager = tenantManager;
|
|
|
|
_encryptionKeyPairHelper = encryptionKeyPairHelper;
|
|
|
|
_fileStorageServiceInt = fileStorageServiceInt;
|
|
|
|
_fileStorageService = fileStorageService;
|
|
|
|
_messageService = messageService;
|
|
|
|
_logger = option.Get("ASC.Api.Documents");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <visible>false</visible>
|
|
|
|
[Read("keys")]
|
2022-03-04 10:25:12 +00:00
|
|
|
public EncryptionKeyPairDto GetKeys()
|
2022-03-03 17:33:11 +00:00
|
|
|
{
|
|
|
|
_permissionContext.DemandPermissions(new UserSecurityProvider(_authContext.CurrentAccount.ID), Constants.Action_EditUser);
|
|
|
|
|
2022-03-05 14:41:47 +00:00
|
|
|
if (!PrivacyRoomSettings.GetEnabled(_settingsManager))
|
|
|
|
{
|
|
|
|
throw new System.Security.SecurityException();
|
|
|
|
}
|
2022-03-03 17:33:11 +00:00
|
|
|
|
|
|
|
return _encryptionKeyPairHelper.GetKeyPair();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <visible>false</visible>
|
|
|
|
[Read("access/{fileId}")]
|
2022-03-04 10:25:12 +00:00
|
|
|
public Task<IEnumerable<EncryptionKeyPairDto>> GetPublicKeysWithAccess(string fileId)
|
2022-03-03 17:33:11 +00:00
|
|
|
{
|
2022-03-05 14:41:47 +00:00
|
|
|
if (!PrivacyRoomSettings.GetEnabled(_settingsManager))
|
|
|
|
{
|
|
|
|
throw new System.Security.SecurityException();
|
|
|
|
}
|
2022-03-03 17:33:11 +00:00
|
|
|
|
|
|
|
return _encryptionKeyPairHelper.GetKeyPairAsync(fileId, _fileStorageService);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Read("access/{fileId:int}")]
|
2022-03-04 10:25:12 +00:00
|
|
|
public Task<IEnumerable<EncryptionKeyPairDto>> GetPublicKeysWithAccess(int fileId)
|
2022-03-03 17:33:11 +00:00
|
|
|
{
|
2022-03-08 17:37:23 +00:00
|
|
|
if (!PrivacyRoomSettings.GetEnabled(_settingsManager))
|
|
|
|
{
|
|
|
|
throw new System.Security.SecurityException();
|
|
|
|
}
|
2022-03-03 17:33:11 +00:00
|
|
|
|
|
|
|
return _encryptionKeyPairHelper.GetKeyPairAsync(fileId, _fileStorageServiceInt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <visible>false</visible>
|
|
|
|
[Read("")]
|
|
|
|
public bool PrivacyRoom()
|
|
|
|
{
|
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
|
|
|
|
|
|
|
return PrivacyRoomSettings.GetEnabled(_settingsManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <visible>false</visible>
|
|
|
|
[Update("keys")]
|
2022-03-05 14:41:47 +00:00
|
|
|
public object SetKeysFromBody([FromBody] PrivacyRoomRequestDto inDto)
|
2022-03-03 17:33:11 +00:00
|
|
|
{
|
2022-03-05 14:41:47 +00:00
|
|
|
return SetKeys(inDto);
|
2022-03-03 17:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Update("keys")]
|
|
|
|
[Consumes("application/x-www-form-urlencoded")]
|
2022-03-05 14:41:47 +00:00
|
|
|
public object SetKeysFromForm([FromForm] PrivacyRoomRequestDto inDto)
|
2022-03-03 17:33:11 +00:00
|
|
|
{
|
2022-03-05 14:41:47 +00:00
|
|
|
return SetKeys(inDto);
|
2022-03-03 17:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="enable"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <visible>false</visible>
|
|
|
|
[Update("")]
|
2022-03-05 14:41:47 +00:00
|
|
|
public bool SetPrivacyRoomFromBody([FromBody] PrivacyRoomRequestDto inDto)
|
2022-03-03 17:33:11 +00:00
|
|
|
{
|
2022-03-05 14:41:47 +00:00
|
|
|
return SetPrivacyRoom(inDto);
|
2022-03-03 17:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Update("")]
|
|
|
|
[Consumes("application/x-www-form-urlencoded")]
|
2022-03-05 14:41:47 +00:00
|
|
|
public bool SetPrivacyRoomFromForm([FromForm] PrivacyRoomRequestDto inDto)
|
2022-03-03 17:33:11 +00:00
|
|
|
{
|
2022-03-05 14:41:47 +00:00
|
|
|
return SetPrivacyRoom(inDto);
|
2022-03-03 17:33:11 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 14:41:47 +00:00
|
|
|
private object SetKeys(PrivacyRoomRequestDto inDto)
|
2022-03-03 17:33:11 +00:00
|
|
|
{
|
|
|
|
_permissionContext.DemandPermissions(new UserSecurityProvider(_authContext.CurrentAccount.ID), Constants.Action_EditUser);
|
|
|
|
|
2022-03-05 14:41:47 +00:00
|
|
|
if (!PrivacyRoomSettings.GetEnabled(_settingsManager))
|
|
|
|
{
|
|
|
|
throw new System.Security.SecurityException();
|
|
|
|
}
|
2022-03-03 17:33:11 +00:00
|
|
|
|
|
|
|
var keyPair = _encryptionKeyPairHelper.GetKeyPair();
|
|
|
|
if (keyPair != null)
|
|
|
|
{
|
2022-03-05 14:41:47 +00:00
|
|
|
if (!string.IsNullOrEmpty(keyPair.PublicKey) && !inDto.Update)
|
2022-03-03 17:33:11 +00:00
|
|
|
{
|
|
|
|
return new { isset = true };
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.InfoFormat("User {0} updates address", _authContext.CurrentAccount.ID);
|
|
|
|
}
|
|
|
|
|
2022-03-05 14:41:47 +00:00
|
|
|
_encryptionKeyPairHelper.SetKeyPair(inDto.PublicKey, inDto.PrivateKeyEnc);
|
2022-03-03 17:33:11 +00:00
|
|
|
|
|
|
|
return new
|
|
|
|
{
|
|
|
|
isset = true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-05 14:41:47 +00:00
|
|
|
private bool SetPrivacyRoom(PrivacyRoomRequestDto inDto)
|
2022-03-03 17:33:11 +00:00
|
|
|
{
|
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
|
|
|
|
2022-03-05 14:41:47 +00:00
|
|
|
if (inDto.Enable)
|
2022-03-03 17:33:11 +00:00
|
|
|
{
|
|
|
|
if (!PrivacyRoomSettings.IsAvailable(_tenantManager))
|
|
|
|
{
|
|
|
|
throw new BillingException(Resource.ErrorNotAllowedOption, "PrivacyRoom");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 14:41:47 +00:00
|
|
|
PrivacyRoomSettings.SetEnabled(_tenantManager, _settingsManager, inDto.Enable);
|
2022-03-03 17:33:11 +00:00
|
|
|
|
2022-03-05 14:41:47 +00:00
|
|
|
_messageService.Send(inDto.Enable ? MessageAction.PrivacyRoomEnable : MessageAction.PrivacyRoomDisable);
|
2022-03-03 17:33:11 +00:00
|
|
|
|
2022-03-05 14:41:47 +00:00
|
|
|
return inDto.Enable;
|
2022-03-03 17:33:11 +00:00
|
|
|
}
|
|
|
|
}
|