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

275 lines
11 KiB
C#
Raw Normal View History

2019-05-15 14:56:09 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL 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 more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
namespace ASC.Core
2021-05-17 18:35:23 +00:00
{
[Scope]
2020-02-20 14:57:48 +00:00
class ConfigureHostedSolution : IConfigureNamedOptions<HostedSolution>
2020-02-20 08:05:10 +00:00
{
2020-08-12 09:58:08 +00:00
private UserFormatter UserFormatter { get; }
private IOptionsSnapshot<CachedTenantService> TenantService { get; }
private IOptionsSnapshot<CachedUserService> UserService { get; }
private IOptionsSnapshot<CachedQuotaService> QuotaService { get; }
private IOptionsSnapshot<TariffService> TariffService { get; }
private IOptionsSnapshot<TenantManager> TenantManager { get; }
private IOptionsSnapshot<TenantUtil> TenantUtil { get; }
private IOptionsSnapshot<DbSettingsManager> DbSettingsManager { get; }
private IOptionsSnapshot<CoreSettings> CoreSettings { get; }
2020-02-20 08:05:10 +00:00
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;
2019-05-15 14:56:09 +00:00
}
2020-02-20 08:05:10 +00:00
public void Configure(HostedSolution hostedSolution)
{
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;
}
public void Configure(string name, HostedSolution hostedSolution)
{
Configure(hostedSolution);
hostedSolution.Region = name;
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);
}
}
2020-10-20 20:10:17 +00:00
[Scope(typeof(ConfigureHostedSolution))]
2020-02-20 08:05:10 +00:00
public class HostedSolution
{
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; }
public string Region { get; set; }
public HostedSolution()
{
2019-05-15 14:56:09 +00:00
}
public List<Tenant> GetTenants(DateTime from)
{
2020-02-20 08:05:10 +00:00
return TenantService.GetTenants(from).Select(AddRegion).ToList();
2019-05-15 14:56:09 +00:00
}
public List<Tenant> FindTenants(string login)
{
return FindTenants(login, null);
}
public List<Tenant> FindTenants(string login, string passwordHash)
{
if (!string.IsNullOrEmpty(passwordHash) && UserService.GetUserByPasswordHash(Tenant.DEFAULT_TENANT, login, passwordHash) == null)
2019-05-15 14:56:09 +00:00
{
throw new SecurityException("Invalid login or password.");
}
return TenantService.GetTenants(login, passwordHash).Select(AddRegion).ToList();
2019-05-15 14:56:09 +00:00
}
2019-08-15 13:16:39 +00:00
public Tenant GetTenant(string domain)
2019-05-15 14:56:09 +00:00
{
2020-02-20 08:05:10 +00:00
return AddRegion(TenantService.GetTenant(domain));
2019-05-15 14:56:09 +00:00
}
public Tenant GetTenant(int id)
{
2020-02-20 08:05:10 +00:00
return AddRegion(TenantService.GetTenant(id));
2019-05-15 14:56:09 +00:00
}
public void CheckTenantAddress(string address)
{
2020-02-20 08:05:10 +00:00
TenantService.ValidateDomain(address);
2019-05-15 14:56:09 +00:00
}
public void RegisterTenant(TenantRegistrationInfo ri, out Tenant tenant)
2019-11-06 15:03:09 +00:00
{
2019-05-15 14:56:09 +00:00
if (ri == null) throw new ArgumentNullException("registrationInfo");
if (string.IsNullOrEmpty(ri.Address)) throw new Exception("Address can not be empty");
if (string.IsNullOrEmpty(ri.Email)) throw new Exception("Account email can not be empty");
if (ri.FirstName == null) throw new Exception("Account firstname can not be empty");
if (ri.LastName == null) throw new Exception("Account lastname can not be empty");
if (!UserFormatter.IsValidUserName(ri.FirstName, ri.LastName)) throw new Exception("Incorrect firstname or lastname");
if (string.IsNullOrEmpty(ri.PasswordHash)) ri.PasswordHash = Guid.NewGuid().ToString();
2019-05-15 14:56:09 +00:00
// create tenant
tenant = new Tenant(ri.Address.ToLowerInvariant())
{
Name = ri.Name,
Language = ri.Culture.Name,
2019-12-03 15:20:21 +00:00
TimeZone = ri.TimeZoneInfo.Id,
2019-05-15 14:56:09 +00:00
HostedRegion = ri.HostedRegion,
PartnerId = ri.PartnerId,
2020-01-17 13:58:26 +00:00
AffiliateId = ri.AffiliateId,
Campaign = ri.Campaign,
2019-05-15 14:56:09 +00:00
Industry = ri.Industry,
Spam = ri.Spam,
Calls = ri.Calls
};
2020-02-20 08:05:10 +00:00
tenant = TenantService.SaveTenant(CoreSettings, tenant);
2019-05-15 14:56:09 +00:00
// create user
var user = new UserInfo
{
UserName = ri.Email.Substring(0, ri.Email.IndexOf('@')),
LastName = ri.LastName,
FirstName = ri.FirstName,
Email = ri.Email,
MobilePhone = ri.MobilePhone,
WorkFromDate = TenantUtil.DateTimeNow(tenant.TimeZone),
ActivationStatus = ri.ActivationStatus
};
2020-02-20 08:05:10 +00:00
user = UserService.SaveUser(tenant.TenantId, user);
UserService.SetUserPasswordHash(tenant.TenantId, user.ID, ri.PasswordHash);
2020-10-12 19:39:23 +00:00
UserService.SaveUserGroupRef(tenant.TenantId, new UserGroupRef(user.ID, Constants.GroupAdmin.ID, UserGroupRefType.Contains));
2019-05-15 14:56:09 +00:00
// save tenant owner
tenant.OwnerId = user.ID;
2020-02-20 08:05:10 +00:00
tenant = TenantService.SaveTenant(CoreSettings, tenant);
2020-12-28 13:22:08 +00:00
2020-10-12 19:39:23 +00:00
SettingsManager.SaveSettings(new TenantControlPanelSettings { LimitedAccess = ri.LimitedControlPanel }, tenant.TenantId);
2019-05-15 14:56:09 +00:00
}
public Tenant SaveTenant(Tenant tenant)
{
2020-02-20 08:05:10 +00:00
return TenantService.SaveTenant(CoreSettings, tenant);
2019-05-15 14:56:09 +00:00
}
public void RemoveTenant(Tenant tenant)
{
2020-02-20 08:05:10 +00:00
TenantService.RemoveTenant(tenant.TenantId);
2019-05-15 14:56:09 +00:00
}
2019-10-18 08:48:27 +00:00
public string CreateAuthenticationCookie(CookieStorage cookieStorage, int tenantId, Guid userId)
2019-05-15 14:56:09 +00:00
{
var u = UserService.GetUser(tenantId, userId);
return CreateAuthenticationCookie(cookieStorage, tenantId, u);
2019-05-15 14:56:09 +00:00
}
private string CreateAuthenticationCookie(CookieStorage cookieStorage, int tenantId, UserInfo user)
{
if (user == null) return null;
2020-02-20 08:05:10 +00:00
var tenantSettings = SettingsManager.LoadSettingsFor<TenantCookieSettings>(tenantId, Guid.Empty);
2019-05-15 14:56:09 +00:00
var expires = tenantSettings.IsDefault() ? DateTime.UtcNow.AddYears(1) : DateTime.UtcNow.AddMinutes(tenantSettings.LifeTime);
var userSettings = SettingsManager.LoadSettingsFor<TenantCookieSettings>(tenantId, user.ID);
return cookieStorage.EncryptCookie(tenantId, user.ID, tenantSettings.Index, expires, userSettings.Index);
2019-05-15 14:56:09 +00:00
}
public Tariff GetTariff(int tenant, bool withRequestToPaymentSystem = true)
{
2020-02-20 08:05:10 +00:00
return TariffService.GetTariff(tenant, withRequestToPaymentSystem);
2019-05-15 14:56:09 +00:00
}
public TenantQuota GetTenantQuota(int tenant)
{
2020-02-20 08:05:10 +00:00
return ClientTenantManager.GetTenantQuota(tenant);
2019-05-15 14:56:09 +00:00
}
public IEnumerable<TenantQuota> GetTenantQuotas()
{
2020-02-20 08:05:10 +00:00
return ClientTenantManager.GetTenantQuotas();
2019-05-15 14:56:09 +00:00
}
public TenantQuota SaveTenantQuota(TenantQuota quota)
{
2020-02-20 08:05:10 +00:00
return ClientTenantManager.SaveTenantQuota(quota);
2019-05-15 14:56:09 +00:00
}
public void SetTariff(int tenant, bool paid)
{
2020-02-20 08:05:10 +00:00
var quota = QuotaService.GetTenantQuotas().FirstOrDefault(q => paid ? q.NonProfit : q.Trial);
2019-05-15 14:56:09 +00:00
if (quota != null)
{
2020-02-20 08:05:10 +00:00
TariffService.SetTariff(tenant, new Tariff { QuotaId = quota.Id, DueDate = DateTime.MaxValue, });
2019-05-15 14:56:09 +00:00
}
}
public void SetTariff(int tenant, Tariff tariff)
{
2020-02-20 08:05:10 +00:00
TariffService.SetTariff(tenant, tariff);
2019-05-15 14:56:09 +00:00
}
public void SaveButton(int tariffId, string partnerId, string buttonUrl)
{
2020-02-20 08:05:10 +00:00
TariffService.SaveButton(tariffId, partnerId, buttonUrl);
2021-11-16 17:40:15 +00:00
}
public IEnumerable<UserInfo> FindUsers(IEnumerable<Guid> userIds)
{
return UserService.GetUsersAllTenants(userIds);
2019-05-15 14:56:09 +00:00
}
private Tenant AddRegion(Tenant tenant)
{
if (tenant != null)
{
tenant.HostedRegion = Region;
}
return tenant;
}
}
}