2020-02-18 13:48:38 +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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
|
2020-02-18 13:48:38 +00:00
|
|
|
using ASC.ApiSystem.Classes;
|
|
|
|
using ASC.ApiSystem.Models;
|
2020-05-19 17:41:37 +00:00
|
|
|
using ASC.Common;
|
2020-05-19 13:22:47 +00:00
|
|
|
using ASC.Common.Logging;
|
|
|
|
using ASC.Common.Utils;
|
|
|
|
using ASC.Core;
|
2020-02-18 13:48:38 +00:00
|
|
|
using ASC.Core.Billing;
|
2020-05-19 13:22:47 +00:00
|
|
|
using ASC.Core.Common.Settings;
|
2020-02-18 13:48:38 +00:00
|
|
|
using ASC.Core.Tenants;
|
2020-05-19 13:22:47 +00:00
|
|
|
using ASC.Core.Users;
|
|
|
|
using ASC.Web.Core.Helpers;
|
|
|
|
using ASC.Web.Core.Users;
|
2020-02-18 13:48:38 +00:00
|
|
|
using ASC.Web.Core.Utility;
|
|
|
|
using ASC.Web.Core.Utility.Settings;
|
2020-05-19 13:22:47 +00:00
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-02-18 13:48:38 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-05-19 13:22:47 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
2020-02-18 13:48:38 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
namespace ASC.ApiSystem.Controllers
|
|
|
|
{
|
|
|
|
[Obsolete("Registration is deprecated, please use PortalController or TariffController instead.")]
|
|
|
|
[ApiController]
|
2020-02-25 08:45:16 +00:00
|
|
|
[Route("[controller]")]
|
|
|
|
public class RegistrationController : ControllerBase
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
private CommonMethods CommonMethods { get; }
|
|
|
|
private CommonConstants CommonConstants { get; }
|
|
|
|
private HostedSolution HostedSolution { get; }
|
|
|
|
private TimeZonesProvider TimeZonesProvider { get; }
|
|
|
|
private TimeZoneConverter TimeZoneConverter { get; }
|
|
|
|
private ApiSystemHelper ApiSystemHelper { get; }
|
|
|
|
private SecurityContext SecurityContext { get; }
|
|
|
|
private TenantManager TenantManager { get; }
|
|
|
|
private SettingsManager SettingsManager { get; }
|
|
|
|
private CoreSettings CoreSettings { get; }
|
|
|
|
private TenantDomainValidator TenantDomainValidator { get; }
|
|
|
|
private UserFormatter UserFormatter { get; }
|
|
|
|
private UserManagerWrapper UserManagerWrapper { get; }
|
|
|
|
private IConfiguration Configuration { get; }
|
|
|
|
private ILog Log { get; }
|
|
|
|
|
|
|
|
public RegistrationController(
|
|
|
|
CommonMethods commonMethods,
|
|
|
|
CommonConstants commonConstants,
|
2020-05-19 17:41:37 +00:00
|
|
|
IOptionsSnapshot<HostedSolution> hostedSolution,
|
2020-05-19 13:22:47 +00:00
|
|
|
TimeZonesProvider timeZonesProvider,
|
|
|
|
TimeZoneConverter timeZoneConverter,
|
|
|
|
ApiSystemHelper apiSystemHelper,
|
|
|
|
SecurityContext securityContext,
|
|
|
|
TenantManager tenantManager,
|
|
|
|
SettingsManager settingsManager,
|
|
|
|
CoreSettings coreSettings,
|
|
|
|
TenantDomainValidator tenantDomainValidator,
|
|
|
|
UserFormatter userFormatter,
|
|
|
|
UserManagerWrapper userManagerWrapper,
|
|
|
|
IConfiguration configuration,
|
|
|
|
IOptionsMonitor<ILog> option)
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
CommonMethods = commonMethods;
|
|
|
|
CommonConstants = commonConstants;
|
2020-05-19 17:41:37 +00:00
|
|
|
HostedSolution = hostedSolution.Value;
|
2020-05-19 13:22:47 +00:00
|
|
|
TimeZonesProvider = timeZonesProvider;
|
|
|
|
TimeZoneConverter = timeZoneConverter;
|
|
|
|
ApiSystemHelper = apiSystemHelper;
|
|
|
|
SecurityContext = securityContext;
|
|
|
|
TenantManager = tenantManager;
|
|
|
|
SettingsManager = settingsManager;
|
|
|
|
CoreSettings = coreSettings;
|
|
|
|
TenantDomainValidator = tenantDomainValidator;
|
|
|
|
UserFormatter = userFormatter;
|
|
|
|
UserManagerWrapper = userManagerWrapper;
|
|
|
|
Configuration = configuration;
|
|
|
|
Log = option.Get("ASC.ApiSystem");
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#region For TEST api
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
[HttpGet("test")]
|
|
|
|
public IActionResult Check()
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
value = "Registration api works"
|
2020-02-18 13:48:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region API methods
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
[HttpPost("registerportal")]
|
2020-02-18 13:48:38 +00:00
|
|
|
[AllowCrossSiteJson]
|
2020-02-25 11:43:01 +00:00
|
|
|
[Authorize(AuthenticationSchemes = "auth.allowskip.registerportal")]
|
2020-02-25 08:45:16 +00:00
|
|
|
public IActionResult Register(TenantModel model)
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
if (model == null)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "Tenant data is required."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
var errors = new JArray();
|
|
|
|
|
|
|
|
foreach (var k in ModelState.Keys)
|
|
|
|
{
|
|
|
|
errors.Add(ModelState[k].Errors.FirstOrDefault().ErrorMessage);
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
|
|
|
object error;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.Password))
|
|
|
|
{
|
|
|
|
if (!CheckPasswordPolicy(model.Password, out error))
|
|
|
|
{
|
|
|
|
sw.Stop();
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(error);
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CheckValidName(model.FirstName.Trim() + model.LastName.Trim(), out error))
|
|
|
|
{
|
|
|
|
sw.Stop();
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(error);
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var checkTenantBusyPesp = CheckExistingNamePortal(model.PortalName.Trim());
|
|
|
|
|
|
|
|
if (checkTenantBusyPesp != null)
|
|
|
|
{
|
|
|
|
sw.Stop();
|
|
|
|
|
|
|
|
return checkTenantBusyPesp;
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.DebugFormat("PortalName = {0}; Elapsed ms. CheckExistingNamePortal: {1}", model.PortalName, sw.ElapsedMilliseconds);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
var clientIP = CommonMethods.GetClientIp();
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.DebugFormat("clientIP = {0}", clientIP);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
if (CommonMethods.CheckMuchRegistration(model, clientIP, sw))
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = new[] { "tooMuchAttempts" }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
if (CommonConstants.RecaptchaRequired && !CommonMethods.IsTestEmail(model.Email))
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
/*** validate recaptcha ***/
|
2020-02-25 08:45:16 +00:00
|
|
|
if (!CommonMethods.ValidateRecaptcha(model.RecaptchaResponse, clientIP))
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.DebugFormat("PortalName = {0}; Elapsed ms. ValidateRecaptcha: {1}", model.PortalName, sw.ElapsedMilliseconds);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
sw.Stop();
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = new[] { "recaptchaInvalid" },
|
|
|
|
message = "Recaptcha is invalid"
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.DebugFormat("PortalName = {0}; Elapsed ms. ValidateRecaptcha: {1}", model.PortalName, sw.ElapsedMilliseconds);
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//check payment portal count
|
2020-05-19 13:22:47 +00:00
|
|
|
if (Configuration["core:base-domain"] == "localhost")
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
var tenants = HostedSolution.GetTenants(DateTime.MinValue);
|
2020-02-18 13:48:38 +00:00
|
|
|
var firstTenant = tenants.FirstOrDefault();
|
|
|
|
|
|
|
|
if (firstTenant != null)
|
|
|
|
{
|
|
|
|
var activePortals = tenants.Count(r => r.Status != TenantStatus.Suspended && r.Status != TenantStatus.RemovePending);
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var quota = HostedSolution.GetTenantQuota(firstTenant.TenantId);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
if (quota.CountPortals > 0 && quota.CountPortals <= activePortals)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = new[] { "portalsCountTooMuch" },
|
|
|
|
message = "Too much portals registered already",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var language = model.Language ?? string.Empty;
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var tz = TimeZonesProvider.GetCurrentTimeZoneInfo(language);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.DebugFormat("PortalName = {0}; Elapsed ms. TimeZonesProvider.GetCurrentTimeZoneInfo: {1}", model.PortalName, sw.ElapsedMilliseconds);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.TimeZoneName))
|
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
tz = TimeZoneConverter.GetTimeZone(model.TimeZoneName.Trim(), false) ?? tz;
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.DebugFormat("PortalName = {0}; Elapsed ms. TimeZonesProvider.OlsonTimeZoneToTimeZoneInfo: {1}", model.PortalName, sw.ElapsedMilliseconds);
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var lang = TimeZonesProvider.GetCurrentCulture(language);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.DebugFormat("PortalName = {0}; model.Language = {1}, resultLang.DisplayName = {2}", model.PortalName, language, lang.DisplayName);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
var info = new TenantRegistrationInfo
|
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
Name = Configuration["web:portal-name"] ?? "Cloud Office Applications",
|
2020-02-18 13:48:38 +00:00
|
|
|
Address = model.PortalName,
|
|
|
|
Culture = lang,
|
|
|
|
FirstName = model.FirstName.Trim(),
|
|
|
|
LastName = model.LastName.Trim(),
|
|
|
|
Password = string.IsNullOrEmpty(model.Password) ? null : model.Password,
|
|
|
|
Email = model.Email.Trim(),
|
|
|
|
TimeZoneInfo = tz,
|
|
|
|
MobilePhone = string.IsNullOrEmpty(model.Phone) ? null : model.Phone.Trim(),
|
|
|
|
Industry = (TenantIndustry)model.Industry,
|
|
|
|
Spam = model.Spam,
|
|
|
|
Calls = model.Calls,
|
|
|
|
Analytics = model.Analytics
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.PartnerId))
|
|
|
|
{
|
|
|
|
if (Guid.TryParse(model.PartnerId, out Guid guid))
|
|
|
|
{
|
|
|
|
// valid guid
|
|
|
|
info.PartnerId = model.PartnerId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.AffiliateId))
|
|
|
|
{
|
|
|
|
info.AffiliateId = model.AffiliateId;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tenant t;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
/****REGISTRATION!!!*****/
|
2020-05-19 13:22:47 +00:00
|
|
|
if (!string.IsNullOrEmpty(ApiSystemHelper.ApiCacheUrl))
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
ApiSystemHelper.AddTenantToCache(info.Address, SecurityContext.CurrentAccount.ID);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.DebugFormat("PortalName = {0}; Elapsed ms. CacheController.AddTenantToCache: {1}", model.PortalName, sw.ElapsedMilliseconds);
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
HostedSolution.RegisterTenant(info, out t);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
/*********/
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.DebugFormat("PortalName = {0}; Elapsed ms. HostedSolution.RegisterTenant: {1}", model.PortalName, sw.ElapsedMilliseconds);
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
sw.Stop();
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.Error(e);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
errors = new[] { "registerNewTenantError" },
|
2020-02-18 13:48:38 +00:00
|
|
|
message = e.Message,
|
|
|
|
stacktrace = e.StackTrace
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var isFirst = true;
|
|
|
|
|
|
|
|
string sendCongratulationsAddress = null;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.Password))
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
isFirst = !CommonMethods.SendCongratulations(Request.Scheme, t, model.SkipWelcome, out sendCongratulationsAddress);
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
2020-05-19 13:22:47 +00:00
|
|
|
else if (Configuration["core:base-domain"] == "localhost")
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
/* set wizard not completed*/
|
2020-05-19 13:22:47 +00:00
|
|
|
TenantManager.SetCurrentTenant(t);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var settings = SettingsManager.Load<WizardSettings>();
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
settings.Completed = false;
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
SettingsManager.Save(settings);
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.Error(e);
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var reference = CommonMethods.CreateReference(Request.Scheme, t.GetTenantDomain(CoreSettings), info.Email, isFirst, model.Module);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.DebugFormat("PortalName = {0}; Elapsed ms. CreateReferenceByCookie...: {1}", model.PortalName, sw.ElapsedMilliseconds);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
sw.Stop();
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "",
|
|
|
|
reference,
|
|
|
|
tenant = ToTenantWrapper(t),
|
|
|
|
referenceWelcome = sendCongratulationsAddress,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
[HttpDelete("removeportal")]
|
2020-02-18 13:48:38 +00:00
|
|
|
[AllowCrossSiteJson]
|
2020-02-25 11:43:01 +00:00
|
|
|
[Authorize(AuthenticationSchemes = "auth.allowskip")]
|
2020-02-25 08:45:16 +00:00
|
|
|
public IActionResult Remove(string portalName)
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(portalName))
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "portalName is required."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var tenant = HostedSolution.GetTenant(portalName.Trim());
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
if (tenant == null)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "Tenant not found."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
HostedSolution.RemoveTenant(tenant);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "",
|
|
|
|
tenant = ToTenantWrapper(tenant)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
[HttpPut("tariff")]
|
2020-02-18 13:48:38 +00:00
|
|
|
[AllowCrossSiteJson]
|
2020-02-25 11:43:01 +00:00
|
|
|
[Authorize(AuthenticationSchemes = "auth.allowskip")]
|
2020-02-25 08:45:16 +00:00
|
|
|
public IActionResult SetTariff(TariffModel model)
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
if (model == null)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "PortalName is required."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
var errors = new JArray();
|
|
|
|
|
|
|
|
foreach (var k in ModelState.Keys)
|
|
|
|
{
|
|
|
|
errors.Add(ModelState[k].Errors.FirstOrDefault().ErrorMessage);
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var tenant = HostedSolution.GetTenant((model.PortalName ?? "").Trim());
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
if (tenant == null)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "Tenant not found."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var quota = new TenantQuota(tenant.TenantId)
|
|
|
|
{
|
|
|
|
ActiveUsers = 10000,
|
|
|
|
Features = model.Features ?? "",
|
|
|
|
MaxFileSize = 1024 * 1024 * 1024,
|
|
|
|
MaxTotalSize = 1024L * 1024 * 1024 * 1024 - 1,
|
|
|
|
Name = "api",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (model.ActiveUsers != default)
|
|
|
|
{
|
|
|
|
quota.ActiveUsers = model.ActiveUsers;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model.MaxTotalSize != default)
|
|
|
|
{
|
|
|
|
quota.MaxTotalSize = model.MaxTotalSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model.MaxFileSize != default)
|
|
|
|
{
|
|
|
|
quota.MaxFileSize = model.MaxFileSize;
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
HostedSolution.SaveTenantQuota(quota);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
var tariff = new Tariff
|
|
|
|
{
|
|
|
|
QuotaId = quota.Id,
|
|
|
|
DueDate = model.DueDate != default ? model.DueDate : DateTime.MaxValue,
|
|
|
|
};
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
HostedSolution.SetTariff(tenant.TenantId, tariff);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
tariff = HostedSolution.GetTariff(tenant.TenantId, false);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
quota = HostedSolution.GetTenantQuota(tenant.TenantId);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "",
|
|
|
|
tenant = ToTenantWrapper(tenant),
|
|
|
|
tariff = ToTariffWrapper(tariff, quota)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
[HttpGet("tariff")]
|
2020-02-18 13:48:38 +00:00
|
|
|
[AllowCrossSiteJson]
|
2020-02-25 11:43:01 +00:00
|
|
|
[Authorize(AuthenticationSchemes = "auth.allowskip")]
|
2020-02-25 08:45:16 +00:00
|
|
|
public IActionResult GetTariff(string portalName)
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(portalName))
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "portalName is required."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var tenant = HostedSolution.GetTenant(portalName.Trim());
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
if (tenant == null)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "Tenant not found."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var tariff = HostedSolution.GetTariff(tenant.TenantId, false);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var quota = HostedSolution.GetTenantQuota(tenant.TenantId);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "",
|
|
|
|
tenant = ToTenantWrapper(tenant),
|
|
|
|
tariff = ToTariffWrapper(tariff, quota)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
[HttpPut("statusportal")]
|
2020-02-18 13:48:38 +00:00
|
|
|
[AllowCrossSiteJson]
|
2020-02-25 11:43:01 +00:00
|
|
|
[Authorize(AuthenticationSchemes = "auth.allowskip")]
|
2020-02-25 08:45:16 +00:00
|
|
|
public IActionResult ChangeStatus(TenantModel model, bool active)
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
if (model == null)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "PortalName is required."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var tenant = HostedSolution.GetTenant((model.PortalName ?? "").Trim());
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
if (tenant == null)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "Tenant not found."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tenant.SetStatus(active ? TenantStatus.Active : TenantStatus.Suspended);
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
HostedSolution.SaveTenant(tenant);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "",
|
|
|
|
tenant = ToTenantWrapper(tenant)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
[HttpPost("validateportalname")]
|
2020-02-18 13:48:38 +00:00
|
|
|
[AllowCrossSiteJson]
|
2020-02-25 08:45:16 +00:00
|
|
|
public IActionResult CheckExistingNamePortal(TenantModel model)
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
if (model == null)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "PortalName is required."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var response = CheckExistingNamePortal((model.PortalName ?? "").Trim());
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return response ?? Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "",
|
|
|
|
message = "portalNameReadyToRegister"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
[HttpGet("getportals")]
|
2020-02-18 13:48:38 +00:00
|
|
|
[AllowCrossSiteJson]
|
2020-02-25 11:43:01 +00:00
|
|
|
[Authorize(AuthenticationSchemes = "auth.allowskip")]
|
2020-02-25 08:45:16 +00:00
|
|
|
public IActionResult GetPortalsByEmail(string email)
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var tenants = string.IsNullOrEmpty(email)
|
2020-05-19 13:22:47 +00:00
|
|
|
? HostedSolution.GetTenants(DateTime.MinValue).OrderBy(t => t.TenantId).ToList()
|
|
|
|
: HostedSolution.FindTenants(email.Trim()).OrderBy(t => t.TenantId).ToList();
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
var refers = tenants.Where(t => t.Status == TenantStatus.Active).ToList()
|
|
|
|
.ConvertAll(t => string.Format("{0}{1}{2}",
|
|
|
|
Request.Scheme,
|
|
|
|
Uri.SchemeDelimiter,
|
2020-05-19 13:22:47 +00:00
|
|
|
t.GetTenantDomain(CoreSettings)));
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = "",
|
|
|
|
message = "",
|
|
|
|
portals = refers
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.Error(ex);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
errors = new[] { "error" },
|
2020-02-18 13:48:38 +00:00
|
|
|
message = ex.Message,
|
|
|
|
stacktrace = ex.StackTrace
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region private methods
|
|
|
|
|
|
|
|
private object ToTenantWrapper(Tenant t)
|
|
|
|
{
|
|
|
|
return new
|
|
|
|
{
|
|
|
|
tenantId = t.TenantId,
|
|
|
|
tenantAlias = t.TenantAlias,
|
2020-05-19 13:22:47 +00:00
|
|
|
tenantDomain = t.GetTenantDomain(CoreSettings),
|
2020-02-18 13:48:38 +00:00
|
|
|
hostedRegion = t.HostedRegion,
|
|
|
|
name = t.Name,
|
|
|
|
ownerId = t.OwnerId,
|
|
|
|
status = t.Status,
|
|
|
|
partnerId = t.PartnerId,
|
|
|
|
paymentId = t.PaymentId,
|
|
|
|
language = t.Language,
|
|
|
|
industry = t.Industry,
|
2020-05-19 13:22:47 +00:00
|
|
|
timeZoneName = TimeZoneConverter.GetTimeZone(t.TimeZone).DisplayName,
|
2020-02-18 13:48:38 +00:00
|
|
|
createdDateTime = t.CreatedDateTime
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private object ToTariffWrapper(Tariff t, TenantQuota q)
|
|
|
|
{
|
|
|
|
return new
|
|
|
|
{
|
|
|
|
t.DueDate,
|
|
|
|
t.State,
|
|
|
|
q.MaxTotalSize,
|
|
|
|
q.MaxFileSize,
|
|
|
|
q.ActiveUsers,
|
|
|
|
q.Features
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
private IActionResult CheckExistingNamePortal(string portalName)
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(portalName))
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
error = "portalNameEmpty"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
if (!string.IsNullOrEmpty(ApiSystemHelper.ApiCacheUrl))
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
ValidateDomain(portalName.Trim());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
HostedSolution.CheckTenantAddress(portalName.Trim());
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (TenantAlreadyExistsException ex)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = new[] { "portalNameExist" },
|
|
|
|
variants = ex.ExistsTenants.ToArray()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (TenantTooShortException)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = new[] { "tooShortError" }
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (TenantIncorrectCharsException)
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = new[] { "portalNameIncorrect" }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-05-19 13:22:47 +00:00
|
|
|
Log.Error(ex);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
errors = new[] { "error" },
|
|
|
|
message = ex.Message,
|
|
|
|
stacktrace = ex.StackTrace
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ValidateDomain(string domain)
|
|
|
|
{
|
|
|
|
// size
|
2020-05-19 13:22:47 +00:00
|
|
|
TenantDomainValidator.ValidateDomainLength(domain);
|
2020-02-18 13:48:38 +00:00
|
|
|
// characters
|
|
|
|
TenantDomainValidator.ValidateDomainCharacters(domain);
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var sameAliasTenants = ApiSystemHelper.FindTenantsInCache(domain, SecurityContext.CurrentAccount.ID);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
if (sameAliasTenants != null)
|
|
|
|
{
|
|
|
|
throw new TenantAlreadyExistsException("Address busy.", sameAliasTenants);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool CheckValidName(string name, out object error)
|
|
|
|
{
|
|
|
|
error = null;
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(name = (name ?? "").Trim()))
|
|
|
|
{
|
|
|
|
error = new
|
|
|
|
{
|
|
|
|
error = new[] { "error" },
|
|
|
|
message = "name is required"
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
if (!UserFormatter.IsValidUserName(name, string.Empty))
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
error = new
|
|
|
|
{
|
|
|
|
error = new[] { "error" },
|
|
|
|
message = "name is incorrect"
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CheckPasswordPolicy(string pwd, out object error)
|
|
|
|
{
|
|
|
|
error = null;
|
|
|
|
//Validate Password match
|
|
|
|
if (string.IsNullOrEmpty(pwd))
|
|
|
|
{
|
|
|
|
error = new
|
|
|
|
{
|
|
|
|
errors = new[] { "passEmpty" },
|
|
|
|
message = "password is empty"
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
var passwordSettings = (PasswordSettings)new PasswordSettings().GetDefault(Configuration);
|
2020-02-25 08:45:16 +00:00
|
|
|
|
2020-05-19 13:22:47 +00:00
|
|
|
if (!UserManagerWrapper.CheckPasswordRegex(passwordSettings, pwd))
|
2020-02-18 13:48:38 +00:00
|
|
|
{
|
|
|
|
error = new
|
|
|
|
{
|
|
|
|
errors = new[] { "passPolicyError" },
|
|
|
|
message = "password is incorrect"
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2020-05-19 17:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class RegistrationControllerExtention
|
|
|
|
{
|
|
|
|
public static DIHelper AddRegistrationController(this DIHelper services)
|
|
|
|
{
|
|
|
|
return services
|
|
|
|
.AddCommonMethods()
|
|
|
|
.AddTimeZonesProvider()
|
|
|
|
.AddCommonConstants()
|
|
|
|
.AddUserManagerService()
|
|
|
|
.AddUserFormatter()
|
|
|
|
.AddCoreSettingsService()
|
|
|
|
.AddHostedSolutionService()
|
|
|
|
.AddApiSystemHelper()
|
|
|
|
.AddSettingsManagerService()
|
|
|
|
.AddTenantManagerService()
|
|
|
|
.AddSecurityContextService()
|
|
|
|
;
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
}
|
|
|
|
}
|