/// <summary>
        /// Does the entry apply to user.
        /// </summary>
        /// <param name="rule">The rule to check</param>
        /// <param name="user">The current user.</param>
        /// <returns>
        ///      <c>true</c> if the entry applies to the user; otherwise <c>false</c>
        /// </returns>
        private static bool DoesRuleApplyToUser(WebSiteControllerRule rule, SPUser user)
        {
            if (rule.PrincipalType == WebSiteControllerPrincipalType.None)
            {
                return(true);
            }

            if (rule.PrincipalType == WebSiteControllerPrincipalType.User)
            {
                if (user != null &&
                    user.LoginName.Equals(rule.Principal, StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else if (rule.PrincipalType == WebSiteControllerPrincipalType.Group && user != null)
            {
                foreach (SPGroup group in user.Groups)
                {
                    if (group.Name.Equals(rule.Principal, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="propertie"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static WebSiteControllerRule GetRule(string propertie, string name)
        {
            WebSiteControllerRule _rule = null;
            int  code = 100;
            bool NaN  = int.TryParse(name, out code);

            if (!NaN || code > 299)
            {
                WebSiteControllerRulesCollection rules = GetFromConfigDB().rules;
                foreach (WebSiteControllerRule rule in rules)
                {
                    try
                    {
                        if (rule.Properties.ContainsValue(name))
                        {
                            if (rule.Properties[propertie].Equals(name))
                            {
                                _rule = rule;
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }
                }
            }
            return(_rule);
            //return GetFromConfigDB().rules[name];
        }
        /// <summary>
        /// Checks if the passed in rule is equivalent to this instance.
        /// </summary>
        /// <param name="rule">The rule to compare.</param>
        /// <returns>true if the rules have the same Url, Principal, PrincipalType and RuleType; otherwise false</returns>
        public bool Equals(WebSiteControllerRule rule)
        {
            if (rule == null)
            {
                return(false);
            }

            bool baseFieldsEqual =
                this.RuleType.Equals(rule.RuleType) &&
                this.Url.Equals(rule.Url) &&
                this.Principal.Equals(rule.Principal) &&
                this.PrincipalType.Equals(rule.PrincipalType);

            if (baseFieldsEqual)
            {
                foreach (DictionaryEntry property in rule.Properties)
                {
                    if (!this.Properties[property.Key].Equals(property.Value))
                    {
                        return(false);
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Determines whether the rule already exists in the rule collection.
        /// </summary>
        /// <param name="rule">The rule being checked</param>
        /// <returns>
        ///      <c>true</c> if the rule is a duplicate; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsDuplicateRule(WebSiteControllerRule rule)
        {
            foreach (WebSiteControllerRule ruleToCheck in
                     GetRulesForSiteCollection(new Uri(rule.SiteCollection), rule.RuleType))
            {
                if (ruleToCheck.Equals(rule))
                {
                    return(true);
                }
            }

            return(false);
        }
 public static int CompareBySequence(WebSiteControllerRule lhs, WebSiteControllerRule rhs)
 {
     if (lhs.Sequence == rhs.Sequence)
     {
         return(0);
     }
     else if (lhs.Sequence > rhs.Sequence)
     {
         return(1);
     }
     else
     {
         return(-1);
     }
 }
        public static List <WebSiteControllerRule> GetRulesForPage(SPWebApplication Webapp, Uri url, string ruleType, SPUser user)
        {
            WebApp = Webapp;
            List <WebSiteControllerRule> list = new List <WebSiteControllerRule>();

            if (url == null)
            {
                return(list);
            }

            WebSiteControllerModulesCollection modules = GetFromConfigDB().modules;

            foreach (PersistedWebSiteControllerModule module in modules)
            {
                IWebSiteControllerModule imodule = GetModule(Webapp, module.Id);
                if (imodule.AlwaysRun)
                {
                    try
                    {
                        WebSiteControllerRule _rule = GetRule(imodule.RuleType);
                        if (_rule == null)
                        {
                            _rule = new WebSiteControllerRule();
                        }
                        //WebSiteControllerRule temp = new WebSiteControllerRule(SPContext.Current.Site.Url, url.ToString(), ruleType, String.Empty, WebSiteControllerPrincipalType.None, false, true, 0, _rule.Parent);
                        list.Add(_rule);
                    }
                    catch (Exception ex)
                    {
                        SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data);
                        //ex.ToString();
                    }
                }
            }

            WebSiteControllerRulesCollection rules = GetFromConfigDB().rules;

            foreach (WebSiteControllerRule rule in rules)
            {
                if (IsSinglePageControlled(rule, url, ruleType, user))
                {
                    list.Add(rule);
                }
            }

            list.Sort(WebSiteControllerRule.CompareBySequence);
            return(list);
        }
        /// <summary>
        /// Adds a new WebSiteControllerRule.
        /// </summary>
        /// <param name="siteCollection">The site collection.</param>
        /// <param name="page">The page to which the rule applied</param>
        /// <param name="ruleType">Type of the rule.</param>
        /// <param name="principal">The principal.</param>
        /// <param name="principalType">Type of the principal.</param>
        /// <param name="disabled">if set to <c>true</c> the rule is disabled.</param>
        /// <param name="appliesToSsl">if set to <c>true</c> the rule applies to SSL.</param>
        /// <param name="sequence">The sequence of the rule compared to other rules.</param>
        /// <param name="properties">The properties.</param>
        /// <returns>The resulting WebSiteControllerRule</returns>
        public static WebSiteControllerRule AddRule(
            SPWebApplication Webapp,
            string siteCollection,
            string page,
            string ruleType,
            string principal,
            WebSiteControllerPrincipalType principalType,
            bool disabled,
            bool appliesToSsl,
            int sequence,
            Hashtable properties)
        {
            WebApp = Webapp;
            WebSiteControllerConfig config = GetFromConfigDB();
            WebSiteControllerRule   rule   = new WebSiteControllerRule(
                siteCollection,
                page,
                ruleType,
                principal,
                principalType,
                disabled,
                appliesToSsl,
                sequence,
                config);

            rule.Name = rule.Id.ToString();

            if (properties != null)
            {
                foreach (DictionaryEntry property in properties)
                {
                    rule.Properties.Add(property.Key, property.Value);
                }
            }

            if (!IsDuplicateRule(rule))
            {
                config.rules.Add(rule);
                config.Update();
            }
            else
            {
                throw new SPDuplicateObjectException(rule + " is the same as another rule in the WebSiteControllerRules collection");
            }

            return(rule);
        }
        /// <summary>
        /// Gets the rule.
        /// </summary>
        /// <param name="id">The id of the rule</param>
        /// <returns>The WebSiteControllerRule</returns>
        public static WebSiteControllerRule GetRule(SPWebApplication Webapp, Uri url, string ruleType)
        {
            WebSiteControllerRule            darule = null;
            WebSiteControllerRulesCollection rules  = GetFromConfigDB().rules;

            foreach (WebSiteControllerRule rule in rules)
            {
                try
                {
                    if (rule.RuleType == ruleType && rule.Url == url.ToString())
                    {
                        darule = rule;
                    }
                }
                catch (Exception ex)
                {
                    SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data);
                    //ex.ToString();
                }
            }

            return(darule);
            //return GetFromConfigDB().rules[name];
        }
Example #9
0
        protected override bool ProcessWorkItems(SPContentDatabase contentDatabase, SPWorkItemCollection workItems, SPJobState jobState)
        {
            foreach (SPWorkItem workItem in workItems)
            {
                if (workItem != null)
                {
                    try
                    {
                        SPSite site = new SPSite(workItem.SiteId);
                        IWebSiteControllerModule module = null;
                        WebSiteControllerRule    rule   = null;
                        bool _deleted = false;
                        //int ItemID = workItem.ItemId;

                        if (workItem.ItemId > 0)
                        {
                            rule = WebSiteControllerConfig.GetRule(contentDatabase.WebApplication, workItem.ItemGuid);
                        }
                        else
                        {
                            module = WebSiteControllerConfig.GetModule(contentDatabase.WebApplication, workItem.ItemGuid);
                        }

                        if (rule != null)
                        {
                            WebSiteControllerConfig.RemoveRule(rule.Id);
                            _deleted = true;
                        }

                        if (workItem.ItemId < 0 || _deleted)
                        {
                            string[] data                = workItem.TextPayload.Split(new char[] { '#' });
                            string   parameterRule       = data[0];
                            string   parameterProperties = data[1];
                            string[] rules               = parameterRule.Split(new char[] { ';' });

                            string url      = rules[0];
                            bool   disabled = bool.Parse(rules[1]);
                            bool   ssl      = bool.Parse(rules[2]);
                            int    sequence = int.Parse(rules[3]);

                            WebSiteControllerPrincipalType principalType = WebSiteControllerPrincipalType.None;

                            if (!String.IsNullOrEmpty(rules[4]))
                            {
                                principalType = (WebSiteControllerPrincipalType)Enum.Parse(typeof(WebSiteControllerPrincipalType), rules[4]);
                            }
                            string principal = rules[5];

                            string ruletype = string.Empty;

                            if (module != null || String.IsNullOrEmpty(rule.RuleType))
                            {
                                ruletype = module.RuleType;
                            }
                            else if (rule != null && ruletype == string.Empty)
                            {
                                ruletype = rule.RuleType;
                            }

                            string[]  properties = parameterProperties.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                            Hashtable props      = new Hashtable();

                            foreach (string prop in properties)
                            {
                                try
                                {
                                    if (!string.IsNullOrEmpty(prop))
                                    {
                                        string[] keyval = prop.Split(new char[] { ':' });
                                        props.Add(keyval[0], keyval[1]);
                                    }
                                }
                                catch { };
                            }

                            if (_deleted && workItem.ItemId != 1)
                            {
                                WebSiteControllerConfig.AddRule(contentDatabase.WebApplication,
                                                                site.Url + "/",
                                                                url,
                                                                rule.RuleType,
                                                                rule.Principal,
                                                                rule.PrincipalType,
                                                                disabled,
                                                                ssl,
                                                                sequence,
                                                                props
                                                                );
                            }

                            if (workItem.ItemId == -1)
                            {
                                WebSiteControllerConfig.AddRule(contentDatabase.WebApplication,
                                                                site.Url + "/",
                                                                url,
                                                                ruletype,
                                                                principal,
                                                                principalType,
                                                                disabled,
                                                                ssl,
                                                                sequence,
                                                                props
                                                                );
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                        //throw (ex);
                    }
                    finally
                    {
                        DeleteWorkItem(workItems, workItem);
                        //using (SPSite site = new SPSite(workItem.SiteId))
                        //{

                        //    using (SPWeb web = site.OpenWeb(workItem.WebId))
                        //    {
                        //        SPWorkItemCollection deletableCollection = workItems.SubCollection(site, web, (uint)workItem.i, (uint)itemIndex + 1);
                        //        deletableCollection.DeleteWorkItem(currentItem.Id);

                        //        //workItems.CompleteInProgressWorkItems(workItem.ParentId, workItem.Type, workItem.BatchId);
                        //        //workItems.SubCollection(site, web, 0, (uint)workItems.Count).DeleteWorkItem(workItem.Id);

                        //    }

                        //}

                        ////workItems.DeleteWorkItem(workItem.Id);
                    }
                }
            }
            return(true);
        }
        /// <summary>
        /// Determines whether a specfic rule applies to a page.
        /// </summary>
        /// <param name="rule">The rule being checked.</param>
        /// <param name="url">The page URL.</param>
        /// <param name="ruleType">Type of the rule.</param>
        /// <param name="user">The current user.</param>
        /// <returns>
        ///      <c>true</c> if the page has a rule associated with it of the rule type; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsSinglePageControlled(WebSiteControllerRule rule, Uri url, string ruleType, SPUser user)
        {
            if (rule == null)
            {
                return(false);
            }

            if (rule.IsDisabled)
            {
                return(false);
            }

            if (String.IsNullOrEmpty(ruleType) || rule.RuleType.Equals(ruleType, StringComparison.InvariantCultureIgnoreCase))
            {
                Regex regEx = new Regex("^" + rule.Url.ToUpperInvariant());

                /*
                 * if (rule.Properties.ContainsKey("OriginalUrl"))
                 * {
                 *  string OriginalUrl = rule.Properties["OriginalUrl"].ToString();
                 *  regEx = new Regex("^" + OriginalUrl.ToUpperInvariant());
                 *
                 * }
                 * else
                 * {
                 *  regEx = new Regex("^" + rule.Url.ToUpperInvariant());
                 * }
                 */
                // Put anchor at beginning
                //Regex regEx = new Regex(rule.Url.ToUpperInvariant());
                bool ruleAppliesToUser = DoesRuleApplyToUser(rule, user);

                if (regEx != null)
                {
                    Match m = regEx.Match(url.ToString().ToUpperInvariant());

                    while (m.Success)
                    {
                        //do things with your matching text

                        if (rule.Url.ToUpperInvariant() == m.Value.ToUpperInvariant())
                        {
                            return(true);
                        }
                        m = m.NextMatch();
                    }

                    /*
                     * if (regEx.IsMatch(url.ToString().ToUpperInvariant()) && ruleAppliesToUser)
                     * {
                     *  return true;
                     * }
                     */

                    if (rule.AppliesToSsl && url.ToString().StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (regEx.IsMatch(url.ToString().Replace("https://", "http://").ToUpperInvariant()) && ruleAppliesToUser)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
 /// <summary>
 /// Removes the rule.
 /// </summary>
 /// <param name="rule">The rule to remove</param>
 public static void RemoveRule(WebSiteControllerRule rule)
 {
     RemoveRule(rule.Id.ToString());
 }
 /// <summary>
 /// Determines whether the WebSiteControllerRules contains a rule
 /// </summary>
 /// <param name="rule">The id of the rule</param>
 /// <returns>
 ///      <c>true</c> if the specified rules exists; otherwise, <c>false</c>.
 /// </returns>
 public static bool ContainsRule(WebSiteControllerRule rule)
 {
     return(ContainsRule(rule.Id.ToString()));
 }