private void PopulateUser(PageSubscriptionUser userInfo, XDoc userDoc)
        {
            var email = userDoc["email"].AsText;

            if (string.IsNullOrEmpty(email))
            {
                throw new UserException("no email for user");
            }
            userInfo.Email    = email;
            userInfo.Username = userDoc["fullname"].AsText.IfNullOrEmpty(userDoc["username"].AsText);
            var language = userDoc["language"].AsText;

            if (!string.IsNullOrEmpty(language))
            {
                userInfo.Culture = CultureUtil.GetNonNeutralCulture(language);
            }
            var timezone = userDoc["timezone"].AsText;

            if (!string.IsNullOrEmpty(timezone))
            {
                // only update timezone if the user has it defined
                userInfo.Timezone = timezone;
            }
            var perms = (userDoc["permissions.effective/operations"].AsText ?? "").Split(',');

            userInfo.IsAdmin = false;
            foreach (var perm in perms)
            {
                if ("ADMIN".EqualsInvariantIgnoreCase(perm.Trim()))
                {
                    userInfo.IsAdmin = true;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Extract <see cref="CultureInfo"/> from a header value.
        /// </summary>
        /// <param name="header">Header value to be parsed.</param>
        /// <param name="def">Default <see cref="CultureInfo"/> to return in case no culture can be parsed from the header.</param>
        /// <returns>Parsed or default culture.</returns>
        public static CultureInfo GetCultureInfoFromHeader(string header, CultureInfo def)
        {
            if (!string.IsNullOrEmpty(header))
            {
                // NOTE: we attempt to find the best acceptable language; format is: da, en-gb;q=0.8, en;q=0.7, *

                // convert language header into sorted list of languages
                List <Tuplet <string, double> > choices = new List <Tuplet <string, double> >();
                foreach (string choice in header.Split(','))
                {
                    string[] parts = choice.Split(';');
                    string   name  = parts[0].Trim();

                    // parse optional quality parameter
                    double quality = (name == "*") ? 0.0 : 1.0;
                    if ((parts.Length == 2))
                    {
                        string value = parts[1].Trim();
                        if (value.StartsWith("q="))
                        {
                            double.TryParse(value.Substring(2), out quality);
                        }
                    }

                    // add language option
                    choices.Add(new Tuplet <string, double>(name, quality));
                }
                choices.Sort(delegate(Tuplet <string, double> left, Tuplet <string, double> right) {
                    // reverse order sort based on quality
                    return(Math.Sign(right.Item2 - left.Item2));
                });

                // find the first acceptable language
                for (int i = 0; i < choices.Count; ++i)
                {
                    // check for wildcard
                    if (choices[0].Item1 == "*")
                    {
                        return(def);
                    }

                    // expand language to full culture
                    CultureInfo culture = CultureUtil.GetNonNeutralCulture(choices[i].Item1);
                    if (culture != null)
                    {
                        return(culture);
                    }
                }
            }
            return(def);
        }
        public Yield GetLocalizedString(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            string      resource = context.GetParam("resource");
            string      language = context.GetParam("lang", null);
            CultureInfo culture  = CultureUtil.GetNonNeutralCulture(language) ?? DreamContext.Current.Culture;
            string      value    = ResourceManager.GetString(resource, culture, null);

            if (value == null)
            {
                throw new SiteNoSuchLocalizationResourceNotFoundException(resource);
            }
            response.Return(DreamMessage.Ok(MimeType.TEXT_UTF8, value));
            yield break;
        }
Beispiel #4
0
        //--- Constructors ---
        public PageSubscriptionInstance(string wikiId, XDoc config, IContainer container)
        {
            _log.DebugFormat("created PageSubscriptionInstance for wikiid '{0}'", wikiId);
            _wikiId = wikiId;
            _pageSubscriptionSessionFactory = container.Resolve <IPageSubscriptionDataSessionFactory>(new NamedParameter("config", config));

            // derive siteinfo
            _sitename = config["ui/sitename"].AsText;
            if (string.IsNullOrEmpty(_sitename))
            {
                _log.WarnFormat("missing ui/sitename for instance {0}", _wikiId);
            }
            _timezone = config["ui/timezone"].AsText;
            var emailFromAddress = config["page-subscription/from-address"].AsText;

            if (string.IsNullOrEmpty(emailFromAddress))
            {
                emailFromAddress = config["admin/email"].AsText;
            }
            if (string.IsNullOrEmpty(emailFromAddress))
            {
                _log.WarnFormat("missing page-subscription/from-address and admin/email for instance {0}", _wikiId);
            }
            else
            {
                var address = new MailAddress(emailFromAddress);
                if (string.IsNullOrEmpty(address.DisplayName))
                {
                    address = new MailAddress(emailFromAddress, emailFromAddress);
                }
                _emailFromAddress = address.ToString();
            }
            _emailFormat          = config["page-subscription/email-format"].AsText;
            _useShortEmailAddress = config["page-subscription/use-short-email-address"].AsBool ?? false;
            _culture = CultureUtil.GetNonNeutralCulture(config["ui/language"].AsText) ?? CultureInfo.GetCultureInfo("en-us");
        }
Beispiel #5
0
 public void Should_get_en_us_from_en()
 {
     Assert.AreEqual(CultureInfo.GetCultureInfo("en-us"), CultureUtil.GetNonNeutralCulture(CultureInfo.GetCultureInfo("en"), _default));
 }
Beispiel #6
0
 public void Should_get_null_from_empty_language_string()
 {
     Assert.IsNull(CultureUtil.GetNonNeutralCulture(string.Empty));
 }
Beispiel #7
0
 public void Should_get_default_on_null_culture()
 {
     Assert.AreEqual(_default, CultureUtil.GetNonNeutralCulture(null, _default));
 }