private void ParsePolicyPrefixSuffix(string policyValue, out string prefix, out string suffix)
        {
            prefix = string.Empty;
            suffix = string.Empty;

            // parse and enforce policy
            if (string.IsNullOrEmpty(policyValue))
            {
                return;
            }

            try
            {
                // parse settings
                string[] parts   = policyValue.Split(';');
                bool     enabled = Utils.ParseBool(parts[0], false);
                prefix = parts[4];
                suffix = parts[5];

                // apply policy
                if (enabled)
                {
                    // prefix
                    if (!String.IsNullOrEmpty(prefix))
                    {
                        // substitute vars
                        prefix = Utils.ReplaceStringVariable(prefix, "user_id", _policyUser.UserId.ToString(CultureInfo.InvariantCulture));
                        prefix = Utils.ReplaceStringVariable(prefix, "user_name", _policyUser.Username);
                    }

                    // suffix
                    if (!String.IsNullOrEmpty(suffix))
                    {
                        // substitute vars
                        suffix = Utils.ReplaceStringVariable(suffix, "user_id", _policyUser.UserId.ToString(CultureInfo.InvariantCulture));
                        suffix = Utils.ReplaceStringVariable(suffix, "user_name", _policyUser.Username);
                    }
                }
            }
            catch
            {
                /* skip */
            }
        }
Exemplo n.º 2
0
        private void ToggleControls()
        {
            // hide/show controls
            litPrefix.Visible           = ((!EditMode) && !String.IsNullOrEmpty(litPrefix.Text));
            txtName.Visible             = !EditMode;
            lblName.Visible             = EditMode;
            litSuffix.Visible           = ((!EditMode) && !String.IsNullOrEmpty(litSuffix.Text));
            valRequireUsername.Enabled  = RequiredField && !EditMode;
            valCorrectUsername.Enabled  = !EditMode;
            valCorrectMinLength.Enabled = !EditMode;

            if (EditMode)
            {
                return;
            }

            // require validator
            valRequireUsername.ErrorMessage = GetLocalizedString("CantBeBlank.Text");

            // disable min length validator
            valCorrectMinLength.Enabled = false;

            // username validator
            string defAllowedRegexp = PanelGlobals.UsernameDefaultAllowedRegExp;
            string defAllowedText   = "a-z  A-Z  0-9  .  _";

            // parse and enforce policy
            if (PolicyValue != null)
            {
                bool   enabled        = false;
                string allowedSymbols = null;
                int    minLength      = -1;
                int    maxLength      = -1;
                string prefix         = null;
                string suffix         = null;

                try
                {
                    // parse settings
                    string[] parts = PolicyValue.Split(';');
                    enabled        = Utils.ParseBool(parts[0], false);
                    allowedSymbols = parts[1];
                    minLength      = Utils.ParseInt(parts[2], -1);
                    maxLength      = Utils.ParseInt(parts[3], -1);
                    prefix         = parts[4];
                    suffix         = parts[5];
                }
                catch { /* skip */ }

                // apply policy
                if (enabled)
                {
                    // prefix
                    if (!String.IsNullOrEmpty(prefix))
                    {
                        // substitute vars
                        prefix = Utils.ReplaceStringVariable(prefix, "user_id", PolicyUser.UserId.ToString());
                        prefix = Utils.ReplaceStringVariable(prefix, "user_name", PolicyUser.Username);

                        // display
                        litPrefix.Text = prefix;

                        // adjust max length
                        maxLength -= prefix.Length;
                    }

                    // suffix
                    if (!String.IsNullOrEmpty(suffix))
                    {
                        // substitute vars
                        suffix = Utils.ReplaceStringVariable(suffix, "user_id", PolicyUser.UserId.ToString());
                        suffix = Utils.ReplaceStringVariable(suffix, "user_name", PolicyUser.Username);

                        // display
                        litSuffix.Text = suffix;

                        // adjust max length
                        maxLength -= suffix.Length;
                    }

                    // min length
                    if (minLength > 0)
                    {
                        valCorrectMinLength.Enabled = true;
                        valCorrectMinLength.ValidationExpression = "^.{" + minLength.ToString() + ",}$";
                        valCorrectMinLength.ErrorMessage         = String.Format(
                            GetLocalizedString("MinLength.Text"), minLength);
                    }

                    // max length
                    if (maxLength > 0)
                    {
                        txtName.MaxLength = maxLength;
                    }

                    // process allowed symbols
                    if (!String.IsNullOrEmpty(allowedSymbols))
                    {
                        StringBuilder sb = new StringBuilder(defAllowedRegexp);
                        for (int i = 0; i < allowedSymbols.Length; i++)
                        {
                            // Escape characters only if required
                            if (PanelGlobals.MetaCharacters2Escape.IndexOf(allowedSymbols[i]) > -1)
                            {
                                sb.Append(@"\").Append(allowedSymbols[i]);
                            }
                            else
                            {
                                sb.Append(allowedSymbols[i]);
                            }
                            //
                            defAllowedText += "&nbsp;&nbsp;" + allowedSymbols[i];
                        }
                        defAllowedRegexp = sb.ToString();
                    }
                } // if(enabled)
            }     // if (PolicyValue != null)

            valCorrectUsername.ValidationExpression = @"^[" + defAllowedRegexp + @"]*$";
            valCorrectUsername.ErrorMessage         = String.Format(GetLocalizedString("AllowedSymbols.Text"),
                                                                    defAllowedText);
        } // ToggleControls()