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

222 lines
7.1 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;
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-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.MessagingSystem;
2019-09-17 12:42:32 +00:00
using ASC.Security.Cryptography;
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;
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;
2019-09-03 15:13:25 +00:00
using ASC.Web.Studio.Core.Notify;
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
{
[DefaultRoute]
[ApiController]
public class PortalController : ControllerBase
{
2019-09-03 15:13:25 +00:00
public Tenant Tenant { get { return ApiContext.Tenant; } }
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; }
2020-08-12 09:58:08 +00:00
private IConfiguration Configuration { get; set; }
2019-10-17 15:55:35 +00:00
public ILog Log { 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-06-23 11:14:13 +00:00
IConfiguration configuration
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-06-23 11:14:13 +00:00
Configuration = configuration;
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")]
2020-10-09 10:05:48 +00:00
public object GetShortenLink(string link)
2019-09-03 15:13:25 +00:00
{
try
{
2019-12-20 11:17:01 +00:00
return UrlShortener.Instance.GetShortenLink(link);
2019-09-03 15:13:25 +00:00
}
catch (Exception ex)
{
2019-10-17 15:55:35 +00:00
Log.Error("getshortenlink", ex);
2019-09-03 15:13:25 +00:00
return link;
}
}
[Read("usedspace")]
public double GetUsedSpace()
{
return Math.Round(
2019-09-17 08:07:46 +00:00
TenantManager.FindTenantQuotaRows(new TenantQuotaRowQuery(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 UserManager.GetUserNames(EmployeeStatus.Active).Count();
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
{
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);
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);
2020-06-16 11:55:46 +00:00
}
2020-09-18 14:45:59 +00:00
[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);
}
}
2019-09-03 15:13:25 +00:00
}
2019-10-31 13:54:43 +00:00
public static class PortalControllerExtension
2019-10-31 13:54:43 +00:00
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddPortalController(this DIHelper services)
2019-10-31 13:54:43 +00:00
{
return services
.AddUrlShortener()
.AddMessageServiceService()
.AddStudioNotifyServiceService()
.AddApiContextService()
.AddUserManagerService()
.AddAuthContextService()
.AddAuthContextService()
.AddTenantManagerService()
.AddEmailValidationKeyProviderService()
.AddPaymentManagerService()
.AddCommonLinkUtilityService()
2020-01-22 07:53:16 +00:00
.AddAuthContextService()
2020-06-16 11:55:46 +00:00
.AddWebItemSecurity()
2020-06-23 11:14:13 +00:00
.AddSecurityContextService();
2019-10-31 13:54:43 +00:00
}
}
2019-09-03 15:13:25 +00:00
}