DocSpace-client/web/ASC.Web.Api/Controllers/PortalController.cs
pavelbannov 79ea471098 Merge branch 'develop' into refactoring/scope
# Conflicts:
#	common/ASC.Api.Core/Core/BaseStartup.cs
#	common/ASC.Core.Common/BaseCommonLinkUtility.cs
#	common/services/ASC.ApiSystem/Startup.cs
#	common/services/ASC.Data.Backup/BackupServiceLauncher.cs
#	common/services/ASC.Data.Backup/Startup.cs
#	common/services/ASC.Data.Storage.Migration/Program.cs
#	common/services/ASC.Notify/NotifyServiceLauncher.cs
#	common/services/ASC.Notify/Program.cs
#	common/services/ASC.Socket.IO.Svc/Program.cs
#	common/services/ASC.Studio.Notify/Program.cs
#	common/services/ASC.Thumbnails.Svc/Program.cs
#	common/services/ASC.UrlShortener.Svc/Program.cs
#	products/ASC.Files/Core/Configuration/ProductEntryPoint.cs
#	products/ASC.Files/Server/Startup.cs
#	products/ASC.Files/Service/Program.cs
#	products/ASC.People/Server/Startup.cs
#	web/ASC.Web.Api/Controllers/PortalController.cs
#	web/ASC.Web.Api/Controllers/SettingsController.cs
#	web/ASC.Web.Api/Startup.cs
#	web/ASC.Web.Studio/Startup.cs
2020-11-04 15:22:43 +03:00

237 lines
7.9 KiB
C#

using System;
using System.Linq;
using System.Net;
using System.Security;
using ASC.Api.Core;
using ASC.Common;
using ASC.Common.Logging;
using ASC.Core;
using ASC.Core.Billing;
using ASC.Core.Common.Notify.Push;
using ASC.Core.Common.Settings;
using ASC.Core.Tenants;
using ASC.Core.Users;
using ASC.Web.Api.Models;
using ASC.Web.Api.Routing;
using ASC.Web.Core;
using ASC.Web.Core.Mobile;
using ASC.Web.Core.Utility;
using ASC.Web.Studio.Core;
using ASC.Web.Studio.UserControls.Management;
using ASC.Web.Studio.UserControls.Statistics;
using ASC.Web.Studio.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using SecurityContext = ASC.Core.SecurityContext;
namespace ASC.Web.Api.Controllers
{
[Scope]
[DefaultRoute]
[ApiController]
public class PortalController : ControllerBase
{
private Tenant Tenant { get { return ApiContext.Tenant; } }
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; }
private SettingsManager SettingsManager { get; }
private IMobileAppInstallRegistrator MobileAppInstallRegistrator { get; }
private IConfiguration Configuration { get; set; }
private TenantExtra TenantExtra { get; set; }
public ILog Log { get; }
public PortalController(
IOptionsMonitor<ILog> options,
ApiContext apiContext,
UserManager userManager,
TenantManager tenantManager,
PaymentManager paymentManager,
CommonLinkUtility commonLinkUtility,
UrlShortener urlShortener,
AuthContext authContext,
WebItemSecurity webItemSecurity,
SecurityContext securityContext,
SettingsManager settingsManager,
IMobileAppInstallRegistrator mobileAppInstallRegistrator,
TenantExtra tenantExtra,
IConfiguration configuration
)
{
Log = options.CurrentValue;
ApiContext = apiContext;
UserManager = userManager;
TenantManager = tenantManager;
PaymentManager = paymentManager;
CommonLinkUtility = commonLinkUtility;
UrlShortener = urlShortener;
AuthContext = authContext;
WebItemSecurity = webItemSecurity;
SecurityContext = securityContext;
SettingsManager = settingsManager;
MobileAppInstallRegistrator = mobileAppInstallRegistrator;
Configuration = configuration;
TenantExtra = tenantExtra;
}
[Read("")]
public Tenant Get()
{
return Tenant;
}
[Read("users/{userID}")]
public UserInfo GetUser(Guid userID)
{
return UserManager.GetUsers(userID);
}
[Read("users/invite/{employeeType}")]
public object GeInviteLink(EmployeeType employeeType)
{
if (!WebItemSecurity.IsProductAdministrator(WebItemManager.PeopleProductID, AuthContext.CurrentAccount.ID))
{
throw new SecurityException("Method not available");
}
return CommonLinkUtility.GetConfirmationUrl(string.Empty, ConfirmType.LinkInvite, (int)employeeType)
+ $"&emplType={employeeType:d}";
}
[Update("getshortenlink")]
public object GetShortenLink(string link)
{
try
{
return UrlShortener.Instance.GetShortenLink(link);
}
catch (Exception ex)
{
Log.Error("getshortenlink", ex);
return link;
}
}
[Read("tenantextra")]
public object GetTenantExtra()
{
return new
{
opensource = TenantExtra.Opensource,
enterprise = TenantExtra.Enterprise,
tariff = TenantExtra.GetCurrentTariff(),
quota = TenantExtra.GetTenantQuota(),
notPaid = TenantExtra.IsNotPaid(),
licenseAccept = SettingsManager.LoadForCurrentUser<TariffSettings>().LicenseAcceptSetting
};
}
[Read("usedspace")]
public double GetUsedSpace()
{
return Math.Round(
TenantManager.FindTenantQuotaRows(new TenantQuotaRowQuery(Tenant.TenantId))
.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 UserManager.GetUserNames(EmployeeStatus.Active).Count();
}
[Read("tariff")]
public Tariff GetTariff()
{
return PaymentManager.GetTariff(Tenant.TenantId);
}
[Read("quota")]
public TenantQuota GetQuota()
{
return TenantManager.GetTenantQuota(Tenant.TenantId);
}
[Read("quota/right")]
public TenantQuota GetRightQuota()
{
var usedSpace = GetUsedSpace();
var needUsersCount = GetUsersCount();
return TenantManager.GetTenantQuotas().OrderBy(r => r.Price)
.FirstOrDefault(quota =>
quota.ActiveUsers > needUsersCount
&& quota.MaxTotalSize > usedSpace
&& !quota.Year);
}
[Read("path")]
public object GetFullAbsolutePath(string virtualPath)
{
return CommonLinkUtility.GetFullAbsolutePath(virtualPath);
}
[Read("thumb")]
public FileResult GetThumb(string url)
{
if (!SecurityContext.IsAuthenticated || !(Configuration["bookmarking:thumbnail-url"] != null))
{
return null;
}
url = url.Replace("&amp;", "&");
url = WebUtility.UrlEncode(url);
using var wc = new WebClient();
var bytes = wc.DownloadData(string.Format(Configuration["bookmarking:thumbnail-url"], url));
var type = wc.ResponseHeaders["Content-Type"] ?? "image/png";
return File(bytes, type);
}
[Create("present/mark")]
public void MarkPresentAsReaded()
{
try
{
var settings = SettingsManager.LoadForCurrentUser<OpensourcePresentSettings>();
settings.Readed = true;
SettingsManager.SaveForCurrentUser(settings);
}
catch (Exception ex)
{
Log.Error("MarkPresentAsReaded", ex);
}
}
[Create("mobile/registration")]
public void RegisterMobileAppInstall(MobileAppModel model)
{
var currentUser = UserManager.GetUsers(SecurityContext.CurrentAccount.ID);
MobileAppInstallRegistrator.RegisterInstall(currentUser.Email, model.Type);
}
[Create("mobile/registration")]
public void RegisterMobileAppInstall(MobileAppType type)
{
var currentUser = UserManager.GetUsers(SecurityContext.CurrentAccount.ID);
MobileAppInstallRegistrator.RegisterInstall(currentUser.Email, type);
}
}
}