Exemplo n.º 1
0
        public IActionResult Register(TenantModel model)
        {
            if (model == null)
            {
                return(BadRequest(new
                {
                    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);
                }

                return(Ok(new
                {
                    errors
                }));
            }

            var sw = Stopwatch.StartNew();

            object error;

            if (string.IsNullOrEmpty(model.PasswordHash) && !string.IsNullOrEmpty(model.Password))
            {
                if (!CheckPasswordPolicy(model.Password, out error))
                {
                    sw.Stop();

                    return(BadRequest(error));
                }
                model.PasswordHash = PasswordHasher.GetClientPassword(model.Password);
            }

            if (!CheckValidName(model.FirstName.Trim() + model.LastName.Trim(), out error))
            {
                sw.Stop();

                return(BadRequest(error));
            }

            var checkTenantBusyPesp = CheckExistingNamePortal(model.PortalName.Trim());

            if (checkTenantBusyPesp != null)
            {
                sw.Stop();

                return(checkTenantBusyPesp);
            }

            Log.DebugFormat("PortalName = {0}; Elapsed ms. CheckExistingNamePortal: {1}", model.PortalName, sw.ElapsedMilliseconds);

            var clientIP = CommonMethods.GetClientIp();

            Log.DebugFormat("clientIP = {0}", clientIP);

            if (CommonMethods.CheckMuchRegistration(model, clientIP, sw))
            {
                return(BadRequest(new
                {
                    errors = new[] { "tooMuchAttempts" }
                }));
            }

            if (CommonConstants.RecaptchaRequired && !CommonMethods.IsTestEmail(model.Email))
            {
                /*** validate recaptcha ***/
                if (!CommonMethods.ValidateRecaptcha(model.RecaptchaResponse, clientIP))
                {
                    Log.DebugFormat("PortalName = {0}; Elapsed ms. ValidateRecaptcha: {1}", model.PortalName, sw.ElapsedMilliseconds);

                    sw.Stop();

                    return(BadRequest(new
                    {
                        errors = new[] { "recaptchaInvalid" },
                        message = "Recaptcha is invalid"
                    }));
                }

                Log.DebugFormat("PortalName = {0}; Elapsed ms. ValidateRecaptcha: {1}", model.PortalName, sw.ElapsedMilliseconds);
            }

            //check payment portal count
            if (Configuration["core:base-domain"] == "localhost")
            {
                var tenants     = HostedSolution.GetTenants(DateTime.MinValue);
                var firstTenant = tenants.FirstOrDefault();

                if (firstTenant != null)
                {
                    var activePortals = tenants.Count(r => r.Status != TenantStatus.Suspended && r.Status != TenantStatus.RemovePending);

                    var quota = HostedSolution.GetTenantQuota(firstTenant.TenantId);

                    if (quota.CountPortals > 0 && quota.CountPortals <= activePortals)
                    {
                        return(BadRequest(new
                        {
                            errors = new[] { "portalsCountTooMuch" },
                            message = "Too much portals registered already",
                        }));
                    }
                }
            }

            var language = model.Language ?? string.Empty;

            var tz = TimeZonesProvider.GetCurrentTimeZoneInfo(language);

            Log.DebugFormat("PortalName = {0}; Elapsed ms. TimeZonesProvider.GetCurrentTimeZoneInfo: {1}", model.PortalName, sw.ElapsedMilliseconds);

            if (!string.IsNullOrEmpty(model.TimeZoneName))
            {
                tz = TimeZoneConverter.GetTimeZone(model.TimeZoneName.Trim(), false) ?? tz;

                Log.DebugFormat("PortalName = {0}; Elapsed ms. TimeZonesProvider.OlsonTimeZoneToTimeZoneInfo: {1}", model.PortalName, sw.ElapsedMilliseconds);
            }

            var lang = TimeZonesProvider.GetCurrentCulture(language);

            Log.DebugFormat("PortalName = {0}; model.Language = {1}, resultLang.DisplayName = {2}", model.PortalName, language, lang.DisplayName);

            var info = new TenantRegistrationInfo
            {
                Name                = Configuration["web:portal-name"] ?? "Cloud Office Applications",
                Address             = model.PortalName,
                Culture             = lang,
                FirstName           = model.FirstName.Trim(),
                LastName            = model.LastName.Trim(),
                PasswordHash        = String.IsNullOrEmpty(model.PasswordHash) ? null : model.PasswordHash,
                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,
                LimitedControlPanel = model.LimitedControlPanel
            };

            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!!!*****/
                if (!string.IsNullOrEmpty(ApiSystemHelper.ApiCacheUrl))
                {
                    ApiSystemHelper.AddTenantToCache(info.Address, SecurityContext.CurrentAccount.ID);

                    Log.DebugFormat("PortalName = {0}; Elapsed ms. CacheController.AddTenantToCache: {1}", model.PortalName, sw.ElapsedMilliseconds);
                }

                HostedSolution.RegisterTenant(info, out t);

                /*********/

                Log.DebugFormat("PortalName = {0}; Elapsed ms. HostedSolution.RegisterTenant: {1}", model.PortalName, sw.ElapsedMilliseconds);
            }
            catch (Exception e)
            {
                sw.Stop();

                Log.Error(e);

                return(StatusCode(StatusCodes.Status500InternalServerError, new
                {
                    errors = new[] { "registerNewTenantError" },
                    message = e.Message,
                    stacktrace = e.StackTrace
                }));
            }

            var isFirst = true;

            string sendCongratulationsAddress = null;

            if (!String.IsNullOrEmpty(model.PasswordHash))
            {
                isFirst = !CommonMethods.SendCongratulations(Request.Scheme, t, model.SkipWelcome, out sendCongratulationsAddress);
            }
            else if (Configuration["core:base-domain"] == "localhost")
            {
                try
                {
                    /* set wizard not completed*/
                    TenantManager.SetCurrentTenant(t);

                    var settings = SettingsManager.Load <WizardSettings>();

                    settings.Completed = false;

                    SettingsManager.Save(settings);
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            }

            var reference = CommonMethods.CreateReference(Request.Scheme, t.GetTenantDomain(CoreSettings), info.Email, isFirst, model.Module);

            Log.DebugFormat("PortalName = {0}; Elapsed ms. CreateReferenceByCookie...: {1}", model.PortalName, sw.ElapsedMilliseconds);

            sw.Stop();

            return(Ok(new
            {
                errors = "",
                reference,
                tenant = ToTenantWrapper(t),
                referenceWelcome = sendCongratulationsAddress,
            }));
        }
Exemplo n.º 2
0
        public void RegisterTenant(TenantRegistrationInfo ri, out Tenant tenant)
        {
            tenant = null;

            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 (string.IsNullOrEmpty(ri.Password))
            {
                ri.Password = Crypto.GeneratePassword(6);
            }

            // create tenant
            tenant = new Tenant(ri.Address.ToLowerInvariant())
            {
                Name         = ri.Name,
                Language     = ri.Culture.Name,
                TimeZone     = ri.TimeZoneInfo,
                HostedRegion = ri.HostedRegion,
                PartnerId    = ri.PartnerId,
                AffiliateId  = ri.AffiliateId,
                Industry     = ri.Industry
            };

            tenant = tenantService.SaveTenant(tenant);

            // 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
            };

            user = userService.SaveUser(tenant.TenantId, user);
            userService.SetUserPassword(tenant.TenantId, user.ID, ri.Password);
            userService.SaveUserGroupRef(tenant.TenantId, new UserGroupRef(user.ID, Constants.GroupAdmin.ID, UserGroupRefType.Contains));

            // save tenant owner
            tenant.OwnerId = user.ID;
            tenant         = tenantService.SaveTenant(tenant);
        }
Exemplo n.º 3
0
 public void RegisterTenant(string region, TenantRegistrationInfo ri, out Tenant tenant)
 {
     ri.HostedRegion = region;
     GetRegionService(region).RegisterTenant(ri, out tenant);
 }
Exemplo n.º 4
0
        public void RegisterTenant(TenantRegistrationInfo ri, out Tenant tenant)
        {
            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();
            }

            // create tenant
            tenant = new Tenant(ri.Address.ToLowerInvariant())
            {
                Name         = ri.Name,
                Language     = ri.Culture.Name,
                TimeZone     = ri.TimeZoneInfo.Id,
                HostedRegion = ri.HostedRegion,
                PartnerId    = ri.PartnerId,
                AffiliateId  = ri.AffiliateId,
                Campaign     = ri.Campaign,
                Industry     = ri.Industry,
                Spam         = ri.Spam,
                Calls        = ri.Calls
            };

            tenant = TenantService.SaveTenant(CoreSettings, tenant);

            // 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
            };

            user = UserService.SaveUser(tenant.TenantId, user);
            UserService.SetUserPasswordHash(tenant.TenantId, user.ID, ri.PasswordHash);
            UserService.SaveUserGroupRef(tenant.TenantId, new UserGroupRef(user.ID, Constants.GroupAdmin.ID, UserGroupRefType.Contains));

            // save tenant owner
            tenant.OwnerId = user.ID;
            tenant         = TenantService.SaveTenant(CoreSettings, tenant);

            SettingsManager.SaveSettings(new TenantAnalyticsSettings()
            {
                Analytics = ri.Analytics
            }, tenant.TenantId);
            SettingsManager.SaveSettings(new TenantControlPanelSettings {
                LimitedAccess = ri.LimitedControlPanel
            }, tenant.TenantId);
        }
Exemplo n.º 5
0
 public string RegisterTenant(string region, TenantRegistrationInfo ri, out Tenant tenant)
 {
     ri.HostedRegion = region;
     return(GetRegionService(region).RegisterTenant(ri, out tenant));
 }
Exemplo n.º 6
0
        public IActionResult Register(TenantModel model)
        {
            if (model == null)
            {
                return BadRequest(new
                {
                    error = "portalNameEmpty",
                    message = "PortalName is required"
                });
            }

            if (!ModelState.IsValid)
            {
                var message = new JArray();

                foreach (var k in ModelState.Keys)
                {
                    message.Add(ModelState[k].Errors.FirstOrDefault().ErrorMessage);
                }

                return BadRequest(new
                {
                    error = "params",
                    message
                });
            }

            var sw = Stopwatch.StartNew();

            if (string.IsNullOrEmpty(model.PasswordHash))
            {
                if (!CheckPasswordPolicy(model.Password, out var error1))
                {
                    sw.Stop();
                    return BadRequest(error1);
                }

                if (!string.IsNullOrEmpty(model.Password))
                {
                    model.PasswordHash = PasswordHasher.GetClientPassword(model.Password);
                }

            }
            model.FirstName = (model.FirstName ?? "").Trim();
            model.LastName = (model.LastName ?? "").Trim();

            if (!CheckValidName(model.FirstName + model.LastName, out var error))
            {
                sw.Stop();

                return BadRequest(error);
            }

            model.PortalName = (model.PortalName ?? "").Trim();

            if (!CheckExistingNamePortal(model.PortalName, out error))
            {
                sw.Stop();

                return BadRequest(error);
            }

            Log.DebugFormat("PortalName = {0}; Elapsed ms. CheckExistingNamePortal: {1}", model.PortalName, sw.ElapsedMilliseconds);

            var clientIP = CommonMethods.GetClientIp();

            if (CommonMethods.CheckMuchRegistration(model, clientIP, sw))
            {
                return BadRequest(new
                {
                    error = "tooMuchAttempts",
                    message = "Too much attempts already"
                });
            }

            if (!CheckRecaptcha(model, clientIP, sw, out error))
            {
                return BadRequest(error);
            }

            if (!CheckRegistrationPayment(out error))
            {
                return BadRequest(error);
            }

            var language = model.Language ?? string.Empty;

            var tz = TimeZonesProvider.GetCurrentTimeZoneInfo(language);

            Log.DebugFormat("PortalName = {0}; Elapsed ms. TimeZonesProvider.GetCurrentTimeZoneInfo: {1}", model.PortalName, sw.ElapsedMilliseconds);

            if (!string.IsNullOrEmpty(model.TimeZoneName))
            {
                tz = TimeZoneConverter.GetTimeZone(model.TimeZoneName.Trim(), false) ?? tz;

                Log.DebugFormat("PortalName = {0}; Elapsed ms. TimeZonesProvider.OlsonTimeZoneToTimeZoneInfo: {1}", model.PortalName, sw.ElapsedMilliseconds);
            }

            var lang = TimeZonesProvider.GetCurrentCulture(language);

            Log.DebugFormat("PortalName = {0}; model.Language = {1}, resultLang.DisplayName = {2}", model.PortalName, language, lang.DisplayName);

            var info = new TenantRegistrationInfo
            {
                Name = Configuration["web:portal-name"] ?? "Cloud Office Applications",
                Address = model.PortalName,
                Culture = lang,
                FirstName = model.FirstName,
                LastName = model.LastName,
                PasswordHash = string.IsNullOrEmpty(model.PasswordHash) ? null : model.PasswordHash,
                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,
                LimitedControlPanel = model.LimitedControlPanel
            };

            if (!string.IsNullOrEmpty(model.PartnerId))
            {
                if (Guid.TryParse(model.PartnerId, out _))
                {
                    // valid guid
                    info.PartnerId = model.PartnerId;
                }
            }

            if (!string.IsNullOrEmpty(model.AffiliateId))
            {
                info.AffiliateId = model.AffiliateId;
            }

            if (!string.IsNullOrEmpty(model.Campaign))
            {
                info.Campaign = model.Campaign;
            }

            Tenant t;

            try
            {
                /****REGISTRATION!!!*****/
                if (!string.IsNullOrEmpty(ApiSystemHelper.ApiCacheUrl))
                {
                    ApiSystemHelper.AddTenantToCache(info.Address, SecurityContext.CurrentAccount.ID);

                    Log.DebugFormat("PortalName = {0}; Elapsed ms. CacheController.AddTenantToCache: {1}", model.PortalName, sw.ElapsedMilliseconds);
                }

                HostedSolution.RegisterTenant(info, out t);

                /*********/

                Log.DebugFormat("PortalName = {0}; Elapsed ms. HostedSolution.RegisterTenant: {1}", model.PortalName, sw.ElapsedMilliseconds);
            }
            catch (Exception e)
            {
                sw.Stop();

                Log.Error(e);

                return StatusCode(StatusCodes.Status500InternalServerError, new
                {
                    error = "registerNewTenantError",
                    message = e.Message,
                    stacktrace = e.StackTrace
                });
            }

            var trialQuota = Configuration["trial-quota"];
            if (!string.IsNullOrEmpty(trialQuota))
            {
                if (int.TryParse(trialQuota, out var trialQuotaId))
                {
                    var dueDate = DateTime.MaxValue;
                    if (int.TryParse(Configuration["trial-due"], out var dueTrial))
                    {
                        dueDate = DateTime.UtcNow.AddDays(dueTrial);
                    }

                    var tariff = new Tariff
                    {
                        QuotaId = trialQuotaId,
                        DueDate = dueDate
                    };
                    HostedSolution.SetTariff(t.TenantId, tariff);
                }
            }


            var isFirst = true;
            string sendCongratulationsAddress = null;

            if (!string.IsNullOrEmpty(model.PasswordHash))
            {
                isFirst = !CommonMethods.SendCongratulations(Request.Scheme, t, model.SkipWelcome, out sendCongratulationsAddress);
            }
            else if (Configuration["core:base-domain"] == "localhost")
            {
                try
                {
                    /* set wizard not completed*/
                    TenantManager.SetCurrentTenant(t);

                    var settings = SettingsManager.Load<WizardSettings>();

                    settings.Completed = false;

                    SettingsManager.Save(settings);
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            }

            var reference = CommonMethods.CreateReference(Request.Scheme, t.GetTenantDomain(CoreSettings), info.Email, isFirst);

            Log.DebugFormat("PortalName = {0}; Elapsed ms. CreateReferenceByCookie...: {1}", model.PortalName, sw.ElapsedMilliseconds);

            sw.Stop();

            return Ok(new
            {
                reference,
                tenant = CommonMethods.ToTenantWrapper(t),
                referenceWelcome = sendCongratulationsAddress
            });
        }
Exemplo n.º 7
0
        public string RegisterTenant(TenantRegistrationInfo ri, out Tenant tenant)
        {
            tenant = null;

            if (ri == null)
            {
                throw new ArgumentNullException("registrationInfo");
            }
            if (string.IsNullOrEmpty(ri.Name))
            {
                throw new Exception("Community name can not be empty");
            }
            if (string.IsNullOrEmpty(ri.Address))
            {
                throw new Exception("Community address can not be empty");
            }

            if (string.IsNullOrEmpty(ri.Email))
            {
                throw new Exception("Account email can not be empty");
            }
            if (string.IsNullOrEmpty(ri.FirstName))
            {
                throw new Exception("Account firstname can not be empty");
            }
            if (string.IsNullOrEmpty(ri.LastName))
            {
                throw new Exception("Account lastname can not be empty");
            }
            if (string.IsNullOrEmpty(ri.Password))
            {
                ri.Password = Crypto.GeneratePassword(6);
            }

            // create tenant
            tenant = new Tenant(ri.Address.ToLowerInvariant())
            {
                Name         = ri.Name,
                Language     = ri.Culture.Name,
                TimeZone     = ri.TimeZoneInfo,
                HostedRegion = ri.HostedRegion,
                PartnerId    = ri.PartnerId
            };

            tenant = tenantService.SaveTenant(tenant);

            // 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
            };

            user = userService.SaveUser(tenant.TenantId, user);
            userService.SetUserPassword(tenant.TenantId, user.ID, ri.Password);
            userService.SaveUserGroupRef(tenant.TenantId, new UserGroupRef(user.ID, Constants.GroupAdmin.ID, UserGroupRefType.Contains));

            // save tenant owner
            tenant.OwnerId = user.ID;
            tenant         = tenantService.SaveTenant(tenant);

            return(CreateAuthenticationCookie(tenant.TenantId, user.ID));
        }