DocSpace-client/web/ASC.Web.Api/Api/Settings/OwnerController.cs

121 lines
4.4 KiB
C#
Raw Normal View History

2022-03-01 10:58:02 +00:00
using Constants = ASC.Core.Users.Constants;
namespace ASC.Web.Api.Controllers.Settings;
2022-03-02 08:08:59 +00:00
public class OwnerController : BaseSettingsController
2022-03-01 10:58:02 +00:00
{
private readonly MessageService _messageService;
private readonly StudioNotifyService _studioNotifyService;
private readonly UserManager _userManager;
private readonly TenantManager _tenantManager;
private readonly AuthContext _authContext;
private readonly PermissionContext _permissionContext;
private readonly CommonLinkUtility _commonLinkUtility;
private readonly DisplayUserSettingsHelper _displayUserSettingsHelper;
private readonly MessageTarget _messageTarget;
public OwnerController(
2022-03-02 08:08:59 +00:00
MessageService messageService,
CommonLinkUtility commonLinkUtility,
2022-03-01 10:58:02 +00:00
StudioNotifyService studioNotifyService,
ApiContext apiContext,
UserManager userManager,
TenantManager tenantManager,
AuthContext authContext,
PermissionContext permissionContext,
WebItemManager webItemManager,
2022-03-02 08:08:59 +00:00
DisplayUserSettingsHelper displayUserSettingsHelper,
MessageTarget messageTarget,
2022-03-01 10:58:02 +00:00
IMemoryCache memoryCache) : base(apiContext, memoryCache, webItemManager)
{
2022-03-02 08:08:59 +00:00
_messageService = messageService;
_commonLinkUtility = commonLinkUtility;
2022-03-01 10:58:02 +00:00
_studioNotifyService = studioNotifyService;
_userManager = userManager;
_tenantManager = tenantManager;
_authContext = authContext;
_permissionContext = permissionContext;
2022-03-02 08:08:59 +00:00
_displayUserSettingsHelper = displayUserSettingsHelper;
_messageTarget = messageTarget;
2022-03-01 10:58:02 +00:00
}
[Create("owner")]
public object SendOwnerChangeInstructionsFromBody([FromBody] SettingsDto model)
{
return SendOwnerChangeInstructions(model);
}
[Create("owner")]
[Consumes("application/x-www-form-urlencoded")]
public object SendOwnerChangeInstructionsFromForm([FromForm] SettingsDto model)
{
return SendOwnerChangeInstructions(model);
}
private object SendOwnerChangeInstructions(SettingsDto model)
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
var curTenant = _tenantManager.GetCurrentTenant();
var owner = _userManager.GetUsers(curTenant.OwnerId);
var newOwner = _userManager.GetUsers(model.OwnerId);
if (newOwner.IsVisitor(_userManager)) throw new System.Security.SecurityException("Collaborator can not be an owner");
if (!owner.Id.Equals(_authContext.CurrentAccount.ID) || Guid.Empty.Equals(newOwner.Id))
{
return new { Status = 0, Message = Resource.ErrorAccessDenied };
}
var confirmLink = _commonLinkUtility.GetConfirmationUrl(owner.Email, ConfirmType.PortalOwnerChange, newOwner.Id, newOwner.Id);
_studioNotifyService.SendMsgConfirmChangeOwner(owner, newOwner, confirmLink);
_messageService.Send(MessageAction.OwnerSentChangeOwnerInstructions, _messageTarget.Create(owner.Id), owner.DisplayUserName(false, _displayUserSettingsHelper));
var emailLink = $"<a href=\"mailto:{owner.Email}\">{owner.Email}</a>";
return new { Status = 1, Message = Resource.ChangePortalOwnerMsg.Replace(":email", emailLink) };
}
[Update("owner")]
[Authorize(AuthenticationSchemes = "confirm", Roles = "PortalOwnerChange")]
public void OwnerFromBody([FromBody] SettingsDto model)
{
Owner(model);
}
[Update("owner")]
[Authorize(AuthenticationSchemes = "confirm", Roles = "PortalOwnerChange")]
[Consumes("application/x-www-form-urlencoded")]
public void OwnerFromForm([FromForm] SettingsDto model)
{
Owner(model);
}
private void Owner(SettingsDto model)
{
var newOwner = Constants.LostUser;
try
{
newOwner = _userManager.GetUsers(model.OwnerId);
}
catch
{
}
if (Constants.LostUser.Equals(newOwner))
{
throw new Exception(Resource.ErrorUserNotFound);
}
if (_userManager.IsUserInGroup(newOwner.Id, Constants.GroupVisitor.ID))
{
throw new Exception(Resource.ErrorUserNotFound);
}
var curTenant = _tenantManager.GetCurrentTenant();
curTenant.OwnerId = newOwner.Id;
_tenantManager.SaveTenant(curTenant);
_messageService.Send(MessageAction.OwnerUpdated, newOwner.DisplayUserName(false, _displayUserSettingsHelper));
}
}