Exemple #1
0
        private ItemVersion OverrideVersion(ItemVersion overridden, ItemVersion over)
        {
            bool changed = false;

            var v = overridden.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            // detected if we have recursed into this (on the current thread/request) and don't apply override if so
            bool blocked = RequestThreadCache.Current.ContainsKey(overrideBlockKey + UniqueId)
                ? ((bool?)RequestThreadCache.Current[overrideBlockKey + UniqueId] ?? false)
                : false;

            if (over.Count > 0 && !blocked)
            {
                IUser u = null;
                RequestThreadCache.Current[overrideBlockKey + UniqueId] = true;
                try
                {
                    u = SecurityManager.Current?.User;
                }
                finally
                {
                    RequestThreadCache.Current[overrideBlockKey + UniqueId] = false;
                }

                foreach (var versioner in Versioners)
                {
                    if (over.ContainsKey(versioner.VersionKey))
                    {
                        var legalVals = u == null
                            ? new List <object> {
                            versioner.PublicVersionValue
                        }
                            : versioner.GetAllowedVersions(u);
                        var overVal = over[versioner.VersionKey];
                        if (legalVals.Any(lVal => lVal == null || lVal.Equals(overVal)))
                        {
                            if (!v.ContainsKey(versioner.VersionKey) ||
                                (v[versioner.VersionKey] == null && overVal != null) ||
                                !v[versioner.VersionKey].Equals(overVal))
                            {
                                v[versioner.VersionKey] = overVal;
                                changed = true;
                            }
                        }
                    }
                }
            }

            return(changed ? new ItemVersion(v) : null);
        }