DocSpace-client/common/ASC.Core.Common/HostedSolution.cs

301 lines
12 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2022-01-31 13:56:30 +00:00
using Constants = ASC.Core.Users.Constants;
2022-02-15 11:52:43 +00:00
namespace ASC.Core;
[Scope]
class ConfigureHostedSolution : IConfigureNamedOptions<HostedSolution>
{
private readonly UserFormatter _userFormatter;
private readonly IOptionsSnapshot<CachedTenantService> _tenantService;
private readonly IOptionsSnapshot<CachedUserService> _userService;
private readonly IOptionsSnapshot<CachedQuotaService> _quotaService;
private readonly IOptionsSnapshot<TariffService> _tariffService;
private readonly IOptionsSnapshot<TenantManager> _tenantManager;
private readonly IOptionsSnapshot<TenantUtil> _tenantUtil;
private readonly IOptionsSnapshot<DbSettingsManager> _dbSettingsManager;
private readonly IOptionsSnapshot<CoreSettings> _coreSettings;
public ConfigureHostedSolution(
UserFormatter userFormatter,
IOptionsSnapshot<CachedTenantService> tenantService,
IOptionsSnapshot<CachedUserService> userService,
IOptionsSnapshot<CachedQuotaService> quotaService,
IOptionsSnapshot<TariffService> tariffService,
IOptionsSnapshot<TenantManager> tenantManager,
IOptionsSnapshot<TenantUtil> tenantUtil,
IOptionsSnapshot<DbSettingsManager> dbSettingsManager,
IOptionsSnapshot<CoreSettings> coreSettings
)
{
_userFormatter = userFormatter;
_tenantService = tenantService;
_userService = userService;
_quotaService = quotaService;
_tariffService = tariffService;
_tenantManager = tenantManager;
_tenantUtil = tenantUtil;
_dbSettingsManager = dbSettingsManager;
_coreSettings = coreSettings;
}
public void Configure(HostedSolution hostedSolution)
{
2022-03-25 16:26:06 +00:00
hostedSolution.UserFormatter = _userFormatter;
hostedSolution.TenantService = _tenantService.Value;
hostedSolution.UserService = _userService.Value;
hostedSolution.QuotaService = _quotaService.Value;
hostedSolution.TariffService = _tariffService.Value;
hostedSolution.ClientTenantManager = _tenantManager.Value;
hostedSolution.TenantUtil = _tenantUtil.Value;
hostedSolution.SettingsManager = _dbSettingsManager.Value;
hostedSolution.CoreSettings = _coreSettings.Value;
2022-02-15 11:52:43 +00:00
}
public void Configure(string name, HostedSolution hostedSolution)
{
Configure(hostedSolution);
hostedSolution.Region = name;
2022-03-25 16:26:06 +00:00
hostedSolution.TenantService = _tenantService.Get(name);
hostedSolution.UserService = _userService.Get(name);
hostedSolution.QuotaService = _quotaService.Get(name);
hostedSolution.TariffService = _tariffService.Get(name);
hostedSolution.ClientTenantManager = _tenantManager.Get(name);
hostedSolution.TenantUtil = _tenantUtil.Get(name);
hostedSolution.SettingsManager = _dbSettingsManager.Get(name);
hostedSolution.CoreSettings = _coreSettings.Get(name);
2022-02-15 11:52:43 +00:00
}
}
[Scope(typeof(ConfigureHostedSolution))]
public class HostedSolution
2021-05-17 18:35:23 +00:00
{
2022-03-25 16:26:06 +00:00
internal ITenantService TenantService { get; set; }
internal IUserService UserService { get; set; }
internal IQuotaService QuotaService { get; set; }
internal ITariffService TariffService { get; set; }
internal UserFormatter UserFormatter { get; set; }
internal TenantManager ClientTenantManager { get; set; }
internal TenantUtil TenantUtil { get; set; }
internal DbSettingsManager SettingsManager { get; set; }
internal CoreSettings CoreSettings { get; set; }
2022-02-15 11:52:43 +00:00
public string Region { get; set; }
public HostedSolution() { }
public List<Tenant> GetTenants(DateTime from)
2020-02-20 08:05:10 +00:00
{
2022-03-25 16:26:06 +00:00
return TenantService.GetTenants(from).Select(AddRegion).ToList();
2022-02-15 11:52:43 +00:00
}
public List<Tenant> FindTenants(string login)
{
return FindTenants(login, null);
}
public List<Tenant> FindTenants(string login, string passwordHash)
{
2022-03-25 16:26:06 +00:00
if (!string.IsNullOrEmpty(passwordHash) && UserService.GetUserByPasswordHash(Tenant.DefaultTenant, login, passwordHash) == null)
2020-02-20 08:05:10 +00:00
{
2022-02-15 11:52:43 +00:00
throw new SecurityException("Invalid login or password.");
}
2022-03-25 16:26:06 +00:00
return TenantService.GetTenants(login, passwordHash).Select(AddRegion).ToList();
2022-02-15 11:52:43 +00:00
}
public Tenant GetTenant(string domain)
{
2022-03-25 16:26:06 +00:00
return AddRegion(TenantService.GetTenant(domain));
2022-02-15 11:52:43 +00:00
}
public Tenant GetTenant(int id)
{
2022-03-25 16:26:06 +00:00
return AddRegion(TenantService.GetTenant(id));
2022-02-15 11:52:43 +00:00
}
public void CheckTenantAddress(string address)
{
2022-03-25 16:26:06 +00:00
TenantService.ValidateDomain(address);
2022-02-15 11:52:43 +00:00
}
2022-03-09 17:15:51 +00:00
public void RegisterTenant(TenantRegistrationInfo registrationInfo, out Tenant tenant)
2022-02-15 11:52:43 +00:00
{
2022-03-09 17:15:51 +00:00
ArgumentNullException.ThrowIfNull(registrationInfo);
if (string.IsNullOrEmpty(registrationInfo.Address))
2022-02-15 11:52:43 +00:00
{
throw new Exception("Address can not be empty");
}
2022-03-09 17:15:51 +00:00
if (string.IsNullOrEmpty(registrationInfo.Email))
2022-02-15 11:52:43 +00:00
{
throw new Exception("Account email can not be empty");
}
2022-03-09 17:15:51 +00:00
if (registrationInfo.FirstName == null)
2022-02-15 11:52:43 +00:00
{
throw new Exception("Account firstname can not be empty");
}
2022-03-09 17:15:51 +00:00
if (registrationInfo.LastName == null)
2022-02-15 11:52:43 +00:00
{
throw new Exception("Account lastname can not be empty");
}
2022-03-25 16:26:06 +00:00
if (!UserFormatter.IsValidUserName(registrationInfo.FirstName, registrationInfo.LastName))
2020-02-20 08:05:10 +00:00
{
2022-02-15 11:52:43 +00:00
throw new Exception("Incorrect firstname or lastname");
2020-02-20 08:05:10 +00:00
}
2022-03-09 17:15:51 +00:00
if (string.IsNullOrEmpty(registrationInfo.PasswordHash))
2020-02-20 08:05:10 +00:00
{
2022-03-09 17:15:51 +00:00
registrationInfo.PasswordHash = Guid.NewGuid().ToString();
2020-02-20 08:05:10 +00:00
}
2022-02-15 11:52:43 +00:00
// create tenant
2022-03-09 17:15:51 +00:00
tenant = new Tenant(registrationInfo.Address.ToLowerInvariant())
2022-02-15 11:52:43 +00:00
{
2022-03-09 17:15:51 +00:00
Name = registrationInfo.Name,
Language = registrationInfo.Culture.Name,
TimeZone = registrationInfo.TimeZoneInfo.Id,
HostedRegion = registrationInfo.HostedRegion,
PartnerId = registrationInfo.PartnerId,
AffiliateId = registrationInfo.AffiliateId,
Campaign = registrationInfo.Campaign,
Industry = registrationInfo.Industry,
Spam = registrationInfo.Spam,
Calls = registrationInfo.Calls
2022-02-15 11:52:43 +00:00
};
2022-03-25 16:26:06 +00:00
tenant = TenantService.SaveTenant(CoreSettings, tenant);
2022-02-15 11:52:43 +00:00
// create user
var user = new UserInfo
{
2022-03-09 17:15:51 +00:00
UserName = registrationInfo.Email.Substring(0, registrationInfo.Email.IndexOf('@')),
LastName = registrationInfo.LastName,
FirstName = registrationInfo.FirstName,
Email = registrationInfo.Email,
MobilePhone = registrationInfo.MobilePhone,
2022-03-25 16:26:06 +00:00
WorkFromDate = TenantUtil.DateTimeNow(tenant.TimeZone),
2022-03-09 17:15:51 +00:00
ActivationStatus = registrationInfo.ActivationStatus
2022-02-15 11:52:43 +00:00
};
2022-03-25 16:26:06 +00:00
user = UserService.SaveUser(tenant.Id, user);
UserService.SetUserPasswordHash(tenant.Id, user.Id, registrationInfo.PasswordHash);
UserService.SaveUserGroupRef(tenant.Id, new UserGroupRef(user.Id, Constants.GroupAdmin.ID, UserGroupRefType.Contains));
2022-02-15 11:52:43 +00:00
// save tenant owner
tenant.OwnerId = user.Id;
2022-03-25 16:26:06 +00:00
tenant = TenantService.SaveTenant(CoreSettings, tenant);
2022-02-15 11:52:43 +00:00
2022-03-25 16:26:06 +00:00
SettingsManager.SaveSettings(new TenantControlPanelSettings { LimitedAccess = registrationInfo.LimitedControlPanel }, tenant.Id);
2020-02-20 08:05:10 +00:00
}
2020-10-20 20:10:17 +00:00
2022-02-15 11:52:43 +00:00
public Tenant SaveTenant(Tenant tenant)
{
2022-03-25 16:26:06 +00:00
return TenantService.SaveTenant(CoreSettings, tenant);
2022-02-15 11:52:43 +00:00
}
public void RemoveTenant(Tenant tenant)
{
2022-03-25 16:26:06 +00:00
TenantService.RemoveTenant(tenant.Id);
2022-02-15 11:52:43 +00:00
}
public string CreateAuthenticationCookie(CookieStorage cookieStorage, int tenantId, Guid userId)
{
2022-03-25 16:26:06 +00:00
var u = UserService.GetUser(tenantId, userId);
2022-02-15 11:52:43 +00:00
return CreateAuthenticationCookie(cookieStorage, tenantId, u);
}
private string CreateAuthenticationCookie(CookieStorage cookieStorage, int tenantId, UserInfo user)
{
if (user == null)
{
2022-02-15 11:52:43 +00:00
return null;
}
2020-12-28 13:22:08 +00:00
2022-03-25 16:26:06 +00:00
var tenantSettings = SettingsManager.LoadSettingsFor<TenantCookieSettings>(tenantId, Guid.Empty);
2022-02-15 11:52:43 +00:00
var expires = tenantSettings.IsDefault() ? DateTime.UtcNow.AddYears(1) : DateTime.UtcNow.AddMinutes(tenantSettings.LifeTime);
2022-03-25 16:26:06 +00:00
var userSettings = SettingsManager.LoadSettingsFor<TenantCookieSettings>(tenantId, user.Id);
2022-02-15 11:52:43 +00:00
return cookieStorage.EncryptCookie(tenantId, user.Id, tenantSettings.Index, expires, userSettings.Index);
}
public Tariff GetTariff(int tenant, bool withRequestToPaymentSystem = true)
{
2022-03-25 16:26:06 +00:00
return TariffService.GetTariff(tenant, withRequestToPaymentSystem);
2022-02-15 11:52:43 +00:00
}
public TenantQuota GetTenantQuota(int tenant)
{
2022-03-25 16:26:06 +00:00
return ClientTenantManager.GetTenantQuota(tenant);
2022-02-15 11:52:43 +00:00
}
public IEnumerable<TenantQuota> GetTenantQuotas()
{
2022-03-25 16:26:06 +00:00
return ClientTenantManager.GetTenantQuotas();
2022-02-15 11:52:43 +00:00
}
public TenantQuota SaveTenantQuota(TenantQuota quota)
{
2022-03-25 16:26:06 +00:00
return ClientTenantManager.SaveTenantQuota(quota);
2022-02-15 11:52:43 +00:00
}
public void SetTariff(int tenant, bool paid)
{
2022-03-25 16:26:06 +00:00
var quota = QuotaService.GetTenantQuotas().FirstOrDefault(q => paid ? q.NonProfit : q.Trial);
2022-02-15 11:52:43 +00:00
if (quota != null)
{
2022-03-25 16:26:06 +00:00
TariffService.SetTariff(tenant, new Tariff { QuotaId = quota.Tenant, DueDate = DateTime.MaxValue, });
2021-11-16 17:40:15 +00:00
}
2022-02-15 11:52:43 +00:00
}
public void SetTariff(int tenant, Tariff tariff)
{
2022-03-25 16:26:06 +00:00
TariffService.SetTariff(tenant, tariff);
2022-02-15 11:52:43 +00:00
}
public void SaveButton(int tariffId, string partnerId, string buttonUrl)
{
2022-03-25 16:26:06 +00:00
TariffService.SaveButton(tariffId, partnerId, buttonUrl);
2022-02-15 11:52:43 +00:00
}
public IEnumerable<UserInfo> FindUsers(IEnumerable<Guid> userIds)
{
2022-03-25 16:26:06 +00:00
return UserService.GetUsersAllTenants(userIds);
2022-02-15 11:52:43 +00:00
}
private Tenant AddRegion(Tenant tenant)
{
if (tenant != null)
2021-11-16 17:40:15 +00:00
{
2022-02-15 11:52:43 +00:00
tenant.HostedRegion = Region;
}
return tenant;
}
}