protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(GetType());

            Page.RegisterBodyScripts("~/UserControls/Management/MailDomainSettings/js/maildomainsettings.js")
            .RegisterStyle("~/UserControls/Management/MailDomainSettings/css/maildomainsettings.less");

            _currentTenant = CoreContext.TenantManager.GetCurrentTenant();
            _studioTrustedDomainSettings = StudioTrustedDomainSettings.Load();
            _enableInviteUsers           = TenantStatisticsProvider.GetUsersCount() < TenantExtra.GetTenantQuota().ActiveUsers;

            if (!_enableInviteUsers)
            {
                _studioTrustedDomainSettings.InviteUsersAsVisitors = true;
            }

            var managementPage = Page as Studio.Management;

            _tenantAccessAnyone = managementPage != null ?
                                  managementPage.TenantAccess.Anyone :
                                  TenantAccessSettings.Load().Anyone;

            HelpLink = CommonLinkUtility.GetHelpLink();
        }
        public object SendJoinInviteMail(string email)
        {
            try
            {
                if (!EnabledJoin)
                {
                    throw new MethodAccessException("Method not available");
                }

                if (!email.TestEmailRegex())
                {
                    throw new Exception(Resource.ErrorNotCorrectEmail);
                }

                var user = CoreContext.UserManager.GetUserByEmail(email);
                if (!user.ID.Equals(Constants.LostUser.ID))
                {
                    throw new Exception(CustomNamingPeople.Substitute <Resource>("ErrorEmailAlreadyExists"));
                }

                var tenant   = CoreContext.TenantManager.GetCurrentTenant();
                var settings = IPRestrictionsSettings.Load();

                if (settings.Enable && !IPSecurity.IPSecurity.Verify(tenant))
                {
                    throw new Exception(Resource.ErrorAccessRestricted);
                }

                var trustedDomainSettings = StudioTrustedDomainSettings.Load();
                var emplType          = trustedDomainSettings.InviteUsersAsVisitors ? EmployeeType.Visitor : EmployeeType.User;
                var enableInviteUsers = TenantStatisticsProvider.GetUsersCount() <
                                        TenantExtra.GetTenantQuota().ActiveUsers;

                if (!enableInviteUsers)
                {
                    emplType = EmployeeType.Visitor;
                }

                switch (tenant.TrustedDomainsType)
                {
                case TenantTrustedDomainsType.Custom:
                {
                    var address = new MailAddress(email);
                    if (
                        tenant.TrustedDomains.Any(
                            d => address.Address.EndsWith("@" + d, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        StudioNotifyService.Instance.SendJoinMsg(email, emplType);
                        MessageService.Send(HttpContext.Current.Request, MessageInitiator.System,
                                            MessageAction.SentInviteInstructions, email);
                        return(new { Status = 1, Message = Resource.FinishInviteJoinEmailMessage });
                    }

                    throw new Exception(Resource.ErrorEmailDomainNotAllowed);
                }

                case TenantTrustedDomainsType.All:
                {
                    StudioNotifyService.Instance.SendJoinMsg(email, emplType);
                    MessageService.Send(HttpContext.Current.Request, MessageInitiator.System,
                                        MessageAction.SentInviteInstructions, email);
                    return(new { Status = 1, Message = Resource.FinishInviteJoinEmailMessage });
                }

                default:
                    throw new Exception(Resource.ErrorNotCorrectEmail);
                }
            }
            catch (FormatException)
            {
                return(new { Status = 0, Message = Resource.ErrorNotCorrectEmail });
            }
            catch (Exception e)
            {
                return(new { Status = 0, Message = e.Message.HtmlEncode() });
            }
        }
        public AjaxResponse SendJoinInviteMail(string email)
        {
            email = (email ?? "").Trim();
            var resp = new AjaxResponse {
                rs1 = "0"
            };

            try
            {
                if (String.IsNullOrEmpty(email))
                {
                    resp.rs2 = Resource.ErrorNotCorrectEmail;
                    return(resp);
                }

                if (!email.TestEmailRegex())
                {
                    resp.rs2 = Resource.ErrorNotCorrectEmail;
                }

                var user = CoreContext.UserManager.GetUserByEmail(email);
                if (!user.ID.Equals(ASC.Core.Users.Constants.LostUser.ID))
                {
                    resp.rs1 = "0";
                    resp.rs2 = CustomNamingPeople.Substitute <Resource>("ErrorEmailAlreadyExists").HtmlEncode();
                    return(resp);
                }

                var tenant = CoreContext.TenantManager.GetCurrentTenant();
                if (tenant != null)
                {
                    var settings = IPRestrictionsSettings.Load();
                    if (settings.Enable && !IPSecurity.IPSecurity.Verify(tenant))
                    {
                        resp.rs2 = Resource.ErrorAccessRestricted;
                        return(resp);
                    }
                }


                var trustedDomainSettings = StudioTrustedDomainSettings.Load();
                var emplType          = trustedDomainSettings.InviteUsersAsVisitors ? EmployeeType.Visitor : EmployeeType.User;
                var enableInviteUsers = TenantStatisticsProvider.GetUsersCount() < TenantExtra.GetTenantQuota().ActiveUsers;

                if (!enableInviteUsers)
                {
                    emplType = EmployeeType.Visitor;
                }

                switch (tenant.TrustedDomainsType)
                {
                case TenantTrustedDomainsType.Custom:
                {
                    var address = new MailAddress(email);
                    if (tenant.TrustedDomains.Any(d => address.Address.EndsWith("@" + d, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        StudioNotifyService.Instance.InviteUsers(email, "", true, emplType);
                        MessageService.Send(HttpContext.Current.Request, MessageInitiator.System, MessageAction.SentInviteInstructions, email);
                        resp.rs1 = "1";
                        resp.rs2 = Resource.FinishInviteJoinEmailMessage;
                        return(resp);
                    }
                    else
                    {
                        resp.rs2 = Resource.ErrorEmailDomainNotAllowed;
                    }
                }
                break;

                case TenantTrustedDomainsType.All:
                    StudioNotifyService.Instance.InviteUsers(email, "", true, emplType);
                    MessageService.Send(HttpContext.Current.Request, MessageInitiator.System, MessageAction.SentInviteInstructions, email);
                    resp.rs1 = "1";
                    resp.rs2 = Resource.FinishInviteJoinEmailMessage;
                    return(resp);

                default:
                    resp.rs2 = Resource.ErrorNotCorrectEmail;
                    break;
                }
            }
            catch (FormatException)
            {
                resp.rs2 = Resource.ErrorNotCorrectEmail;
            }
            catch (Exception e)
            {
                resp.rs2 = HttpUtility.HtmlEncode(e.Message);
            }

            return(resp);
        }