DocSpace-client/web/ASC.Web.Api/Controllers/PortalController.cs

372 lines
14 KiB
C#
Raw Normal View History

2019-09-03 15:13:25 +00:00
using System;
using System.Linq;
2020-06-16 11:55:46 +00:00
using System.Net;
2021-10-12 10:14:33 +00:00
using System.Net.Http;
2020-01-22 07:53:16 +00:00
using System.Security;
2020-06-15 15:33:53 +00:00
2019-09-03 15:13:25 +00:00
using ASC.Api.Core;
2020-02-17 08:58:14 +00:00
using ASC.Common;
2019-09-03 15:13:25 +00:00
using ASC.Common.Logging;
using ASC.Core;
using ASC.Core.Billing;
2020-11-04 11:15:14 +00:00
using ASC.Core.Common.Notify.Push;
2020-09-18 14:45:59 +00:00
using ASC.Core.Common.Settings;
2019-09-03 15:13:25 +00:00
using ASC.Core.Tenants;
using ASC.Core.Users;
using ASC.Web.Api.Models;
2019-09-03 15:13:25 +00:00
using ASC.Web.Api.Routing;
2020-01-22 07:53:16 +00:00
using ASC.Web.Core;
2021-11-16 17:40:15 +00:00
using ASC.Web.Core.Files;
2022-02-17 13:39:47 +00:00
using ASC.Web.Core.Helpers;
2020-11-04 11:15:14 +00:00
using ASC.Web.Core.Mobile;
2022-02-17 13:39:47 +00:00
using ASC.Web.Core.PublicResources;
2019-09-03 15:13:25 +00:00
using ASC.Web.Core.Utility;
2020-09-18 14:45:59 +00:00
using ASC.Web.Studio.Core;
2022-02-17 13:39:47 +00:00
using ASC.Web.Studio.Core.Notify;
using ASC.Web.Studio.UserControls.Management;
2019-09-03 15:13:25 +00:00
using ASC.Web.Studio.Utility;
2019-12-20 11:17:01 +00:00
2019-09-03 15:13:25 +00:00
using Microsoft.AspNetCore.Mvc;
2020-06-23 11:14:13 +00:00
using Microsoft.Extensions.Configuration;
2019-10-17 15:55:35 +00:00
using Microsoft.Extensions.Options;
2019-09-03 15:13:25 +00:00
2020-06-16 11:55:46 +00:00
using SecurityContext = ASC.Core.SecurityContext;
2019-09-03 15:13:25 +00:00
namespace ASC.Web.Api.Controllers
{
2020-10-19 15:53:15 +00:00
[Scope]
2019-09-03 15:13:25 +00:00
[DefaultRoute]
[ApiController]
public class PortalController : ControllerBase
{
private Tenant Tenant { get { return ApiContext.Tenant; } }
2019-09-03 15:13:25 +00:00
2020-08-12 09:58:08 +00:00
private ApiContext ApiContext { get; }
private UserManager UserManager { get; }
private TenantManager TenantManager { get; }
private PaymentManager PaymentManager { get; }
private CommonLinkUtility CommonLinkUtility { get; }
private UrlShortener UrlShortener { get; }
private AuthContext AuthContext { get; }
private WebItemSecurity WebItemSecurity { get; }
private SecurityContext SecurityContext { get; }
2020-09-18 14:45:59 +00:00
private SettingsManager SettingsManager { get; }
private IMobileAppInstallRegistrator MobileAppInstallRegistrator { get; }
2020-08-12 09:58:08 +00:00
private IConfiguration Configuration { get; set; }
2021-11-16 17:40:15 +00:00
private CoreBaseSettings CoreBaseSettings { get; }
private LicenseReader LicenseReader { get; }
private SetupInfo SetupInfo { get; }
private DocumentServiceLicense DocumentServiceLicense { get; }
2022-02-17 13:39:47 +00:00
private CoreSettings CoreSettings { get; }
private PermissionContext PermissionContext { get; }
private ApiSystemHelper ApiSystemHelper { get; }
private StudioNotifyService StudioNotifyService { get; }
private TenantExtra TenantExtra { get; set; }
2019-10-17 15:55:35 +00:00
public ILog Log { get; }
2022-01-13 11:19:39 +00:00
public IHttpClientFactory ClientFactory { get; }
2019-09-03 15:13:25 +00:00
2019-10-17 15:55:35 +00:00
public PortalController(
2019-11-06 15:03:09 +00:00
IOptionsMonitor<ILog> options,
2019-09-09 12:56:33 +00:00
ApiContext apiContext,
UserManager userManager,
2019-09-17 12:42:32 +00:00
TenantManager tenantManager,
2019-09-19 15:55:44 +00:00
PaymentManager paymentManager,
2019-09-24 10:32:12 +00:00
CommonLinkUtility commonLinkUtility,
UrlShortener urlShortener,
2020-01-22 07:53:16 +00:00
AuthContext authContext,
2020-06-16 11:55:46 +00:00
WebItemSecurity webItemSecurity,
2020-06-17 11:00:28 +00:00
SecurityContext securityContext,
2020-09-18 14:45:59 +00:00
SettingsManager settingsManager,
2020-11-04 11:15:14 +00:00
IMobileAppInstallRegistrator mobileAppInstallRegistrator,
TenantExtra tenantExtra,
2020-12-28 13:22:08 +00:00
IConfiguration configuration,
CoreBaseSettings coreBaseSettings,
LicenseReader licenseReader,
2022-01-13 11:19:39 +00:00
SetupInfo setupInfo,
DocumentServiceLicense documentServiceLicense,
2022-02-17 13:39:47 +00:00
CoreSettings coreSettings,
PermissionContext permissionContext,
ApiSystemHelper apiSystemHelper,
StudioNotifyService studioNotifyService,
2022-01-13 11:19:39 +00:00
IHttpClientFactory clientFactory
2019-09-09 12:56:33 +00:00
)
2019-09-03 15:13:25 +00:00
{
2019-11-06 15:03:09 +00:00
Log = options.CurrentValue;
2019-09-03 15:13:25 +00:00
ApiContext = apiContext;
2019-09-09 12:56:33 +00:00
UserManager = userManager;
2019-09-17 08:07:46 +00:00
TenantManager = tenantManager;
PaymentManager = paymentManager;
2019-09-19 15:55:44 +00:00
CommonLinkUtility = commonLinkUtility;
2019-09-24 10:32:12 +00:00
UrlShortener = urlShortener;
2020-01-22 07:53:16 +00:00
AuthContext = authContext;
WebItemSecurity = webItemSecurity;
2020-06-16 11:55:46 +00:00
SecurityContext = securityContext;
2020-09-18 14:45:59 +00:00
SettingsManager = settingsManager;
2020-11-04 11:15:14 +00:00
MobileAppInstallRegistrator = mobileAppInstallRegistrator;
2020-06-23 11:14:13 +00:00
Configuration = configuration;
2020-12-28 13:22:08 +00:00
CoreBaseSettings = coreBaseSettings;
LicenseReader = licenseReader;
SetupInfo = setupInfo;
2021-11-16 17:40:15 +00:00
DocumentServiceLicense = documentServiceLicense;
2022-02-17 13:39:47 +00:00
CoreSettings = coreSettings;
PermissionContext = permissionContext;
ApiSystemHelper = apiSystemHelper;
StudioNotifyService = studioNotifyService;
TenantExtra = tenantExtra;
2022-01-13 11:19:39 +00:00
ClientFactory = clientFactory;
2019-09-03 15:13:25 +00:00
}
[Read("")]
public Tenant Get()
{
return Tenant;
}
[Read("users/{userID}")]
public UserInfo GetUser(Guid userID)
{
return UserManager.GetUsers(userID);
2019-09-03 15:13:25 +00:00
}
[Read("users/invite/{employeeType}")]
2020-10-09 10:05:48 +00:00
public object GeInviteLink(EmployeeType employeeType)
2019-09-03 15:13:25 +00:00
{
2020-02-03 10:48:06 +00:00
if (!WebItemSecurity.IsProductAdministrator(WebItemManager.PeopleProductID, AuthContext.CurrentAccount.ID))
2020-01-22 07:53:16 +00:00
{
throw new SecurityException("Method not available");
}
return CommonLinkUtility.GetConfirmationUrl(string.Empty, ConfirmType.LinkInvite, (int)employeeType)
2019-09-03 15:13:25 +00:00
+ $"&emplType={employeeType:d}";
}
[Update("getshortenlink")]
2021-03-21 15:29:35 +00:00
public object GetShortenLink(ShortenLinkModel model)
2019-09-03 15:13:25 +00:00
{
try
{
2021-03-21 15:29:35 +00:00
return UrlShortener.Instance.GetShortenLink(model.Link);
2019-09-03 15:13:25 +00:00
}
catch (Exception ex)
{
2019-10-17 15:55:35 +00:00
Log.Error("getshortenlink", ex);
2021-03-21 15:29:35 +00:00
return model.Link;
2019-09-03 15:13:25 +00:00
}
}
[Read("tenantextra")]
public object GetTenantExtra()
{
return new
{
2020-12-28 13:22:08 +00:00
customMode = CoreBaseSettings.CustomMode,
opensource = TenantExtra.Opensource,
enterprise = TenantExtra.Enterprise,
tariff = TenantExtra.GetCurrentTariff(),
quota = TenantExtra.GetTenantQuota(),
notPaid = TenantExtra.IsNotPaid(),
2020-12-28 13:22:08 +00:00
licenseAccept = SettingsManager.LoadForCurrentUser<TariffSettings>().LicenseAcceptSetting,
enableTariffPage = //TenantExtra.EnableTarrifSettings - think about hide-settings for opensource
(!CoreBaseSettings.Standalone || !string.IsNullOrEmpty(LicenseReader.LicensePath))
&& string.IsNullOrEmpty(SetupInfo.AmiMetaUrl)
2021-11-16 17:40:15 +00:00
&& !CoreBaseSettings.CustomMode,
DocServerUserQuota = DocumentServiceLicense.GetLicenseQuota(),
DocServerLicense = DocumentServiceLicense.GetLicense()
};
}
2019-09-03 15:13:25 +00:00
[Read("usedspace")]
public double GetUsedSpace()
{
return Math.Round(
2020-12-28 13:22:08 +00:00
TenantManager.FindTenantQuotaRows(Tenant.TenantId)
2019-09-03 15:13:25 +00:00
.Where(q => !string.IsNullOrEmpty(q.Tag) && new Guid(q.Tag) != Guid.Empty)
.Sum(q => q.Counter) / 1024f / 1024f / 1024f, 2);
}
[Read("userscount")]
public long GetUsersCount()
{
return CoreBaseSettings.Personal ? 1 : UserManager.GetUserNames(EmployeeStatus.Active).Length;
2019-09-03 15:13:25 +00:00
}
[Read("tariff")]
public Tariff GetTariff()
{
return PaymentManager.GetTariff(Tenant.TenantId);
2019-09-03 15:13:25 +00:00
}
[Read("quota")]
public TenantQuota GetQuota()
{
2019-09-17 08:07:46 +00:00
return TenantManager.GetTenantQuota(Tenant.TenantId);
2019-09-03 15:13:25 +00:00
}
[Read("quota/right")]
public TenantQuota GetRightQuota()
{
var usedSpace = GetUsedSpace();
var needUsersCount = GetUsersCount();
2019-09-17 08:07:46 +00:00
return TenantManager.GetTenantQuotas().OrderBy(r => r.Price)
2019-09-03 15:13:25 +00:00
.FirstOrDefault(quota =>
quota.ActiveUsers > needUsersCount
&& quota.MaxTotalSize > usedSpace
&& !quota.Year);
}
[Read("path")]
2020-10-09 10:05:48 +00:00
public object GetFullAbsolutePath(string virtualPath)
2019-09-03 15:13:25 +00:00
{
2019-09-19 15:55:44 +00:00
return CommonLinkUtility.GetFullAbsolutePath(virtualPath);
2019-09-03 15:13:25 +00:00
}
2020-06-16 11:55:46 +00:00
[Read("thumb")]
public FileResult GetThumb(string url)
2020-06-25 12:17:22 +00:00
{
2022-01-21 13:27:09 +00:00
if (!SecurityContext.IsAuthenticated || Configuration["bookmarking:thumbnail-url"] == null)
2020-06-16 11:55:46 +00:00
{
return null;
}
2020-06-25 12:17:22 +00:00
2020-06-16 11:55:46 +00:00
url = url.Replace("&amp;", "&");
2020-06-25 12:17:22 +00:00
url = WebUtility.UrlEncode(url);
2021-10-12 10:14:33 +00:00
var request = new HttpRequestMessage();
request.RequestUri = new Uri(string.Format(Configuration["bookmarking:thumbnail-url"], url));
2022-01-13 11:19:39 +00:00
var httpClient = ClientFactory.CreateClient();
2021-11-24 19:34:39 +00:00
using var response = httpClient.Send(request);
2021-10-12 10:14:33 +00:00
using var stream = response.Content.ReadAsStream();
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
string type;
if (response.Headers.TryGetValues("Content-Type", out var values))
{
type = values.First();
}
else
{
type = "image/png";
}
2020-06-25 12:17:22 +00:00
return File(bytes, type);
2020-06-16 11:55:46 +00:00
}
2020-09-18 14:45:59 +00:00
[Create("present/mark")]
public void MarkPresentAsReaded()
{
try
{
2020-12-28 13:22:08 +00:00
var settings = SettingsManager.LoadForCurrentUser<OpensourceGiftSettings>();
2020-09-18 14:45:59 +00:00
settings.Readed = true;
SettingsManager.SaveForCurrentUser(settings);
}
catch (Exception ex)
{
Log.Error("MarkPresentAsReaded", ex);
}
}
2020-11-04 11:15:14 +00:00
[Create("mobile/registration")]
2021-11-24 19:34:39 +00:00
public void RegisterMobileAppInstallFromBody([FromBody] MobileAppModel model)
{
var currentUser = UserManager.GetUsers(SecurityContext.CurrentAccount.ID);
MobileAppInstallRegistrator.RegisterInstall(currentUser.Email, model.Type);
2020-11-10 10:41:32 +00:00
}
[Create("mobile/registration")]
[Consumes("application/x-www-form-urlencoded")]
2021-11-24 19:34:39 +00:00
public void RegisterMobileAppInstallFromForm([FromForm] MobileAppModel model)
{
var currentUser = UserManager.GetUsers(SecurityContext.CurrentAccount.ID);
MobileAppInstallRegistrator.RegisterInstall(currentUser.Email, model.Type);
}
2019-10-31 13:54:43 +00:00
2020-11-04 11:15:14 +00:00
[Create("mobile/registration")]
public void RegisterMobileAppInstall(MobileAppType type)
{
var currentUser = UserManager.GetUsers(SecurityContext.CurrentAccount.ID);
MobileAppInstallRegistrator.RegisterInstall(currentUser.Email, type);
}
2022-02-17 13:39:47 +00:00
[Update("portalrename")]
public object UpdatePortalName(PortalRenameModel model)
{
var enabled = SetupInfo.IsVisibleSettings("PortalRename");
if (!enabled)
throw new SecurityException(Resource.PortalAccessSettingsTariffException);
if (CoreBaseSettings.Personal)
throw new Exception(Resource.ErrorAccessDenied);
PermissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
var alias = model.Alias;
if (string.IsNullOrEmpty(alias)) throw new ArgumentException();
var tenant = TenantManager.GetCurrentTenant();
var user = UserManager.GetUsers(SecurityContext.CurrentAccount.ID);
var localhost = CoreSettings.BaseDomain == "localhost" || tenant.TenantAlias == "localhost";
var newAlias = alias.ToLowerInvariant();
var oldAlias = tenant.TenantAlias;
var oldVirtualRootPath = CommonLinkUtility.GetFullAbsolutePath("~").TrimEnd('/');
if (!string.Equals(newAlias, oldAlias, StringComparison.InvariantCultureIgnoreCase))
{
if (!string.IsNullOrEmpty(ApiSystemHelper.ApiSystemUrl))
{
ApiSystemHelper.ValidatePortalName(newAlias, user.ID);
}
else
{
TenantManager.CheckTenantAddress(newAlias.Trim());
}
if (!string.IsNullOrEmpty(ApiSystemHelper.ApiCacheUrl))
{
ApiSystemHelper.AddTenantToCache(newAlias, user.ID);
}
tenant.TenantAlias = alias;
tenant = TenantManager.SaveTenant(tenant);
if (!string.IsNullOrEmpty(ApiSystemHelper.ApiCacheUrl))
{
ApiSystemHelper.RemoveTenantFromCache(oldAlias, user.ID);
}
if (!localhost || string.IsNullOrEmpty(tenant.MappedDomain))
{
StudioNotifyService.PortalRenameNotify(tenant, oldVirtualRootPath);
}
}
else
{
return null;
}
var reference = string.Format("{0}{1}{2}/{3}",
ApiContext.HttpContextAccessor.HttpContext.Request?.Scheme ?? Uri.UriSchemeHttp,
Uri.SchemeDelimiter,
tenant.GetTenantDomain(CoreSettings),
CommonLinkUtility.GetConfirmationUrlRelative(tenant.TenantId, user.Email, ConfirmType.Auth));
return new
{
message = Resource.SuccessfullyPortalRenameMessage,
reference = reference
};
}
2019-09-03 15:13:25 +00:00
}
}