private void ApplySecuritySettings(SPListItem item, Rule r)
        {
            List<PermissionAssigment> permissionAssignments = r.PermissionAssignments;
            using (SPSite site = new SPSite(item.Web.Site.ID))
            {
                using (SPWeb web = site.OpenWeb(item.Web.ID))
                {
                    SPListItem listItem = web.GetListItem(web.Url + "/" + item.Url);
                    SPUser owner = listItem.GetOwner();
                    if (!r.PreserveExistingSecurity)
                    {
                        listItem.RemoveAllPermissions();
                    }
                    if (!string.IsNullOrEmpty(r.OwnerPermission))
                        listItem.SetPermissions(r.OwnerPermission, owner.LoginName);

                    foreach (PermissionAssigment pa in permissionAssignments)
                    {
                        setPerrmissionToListItem(listItem, pa);
                    }

                    //remove system account
                    if (listItem.HasUniqueRoleAssignments)
                    {
                        //RunWithElevatedPrivileges => CurrentUser is System Account
                        SPPrincipal sysAccount = web.CurrentUser;
                        listItem.RoleAssignments.Remove(sysAccount);
                    }
                }
            }
        }
        private bool ValidateItem(SPListItem item, Rule rule)
        {
            if (!string.IsNullOrEmpty(rule.ContentTypeId) &&
                item[SPBuiltInFieldId.ContentTypeId].ToString() != rule.ContentTypeId)
                return false;

            if (!validateDocumentTypes(item, rule))
            {
                return false;
            }

            foreach (Criteria c in rule.CriteriaList)
            {
                Guid fieldId = new Guid(c.FieldId);
                if (item[fieldId] == null) return false;

                if (FieldValueHelper.ValidateItemProperty(item, fieldId, c))
                    continue;

                return false;
            }
            return true;
        }
        private bool ValidateRule(Rule r, SPListItem item, SPEventReceiverType eventType)
        {
            if (r.RunOnAdded && eventType == SPEventReceiverType.ItemAdded)
                return true;

            if (r.RunOnAnyUpdate == true &&
                eventType == SPEventReceiverType.ItemUpdated &&
                item.Level != SPFileLevel.Checkout)
                return true;

            if (r.RunOnFirstUpdate == true &&
                eventType == SPEventReceiverType.ItemUpdated &&
                item.Level != SPFileLevel.Checkout)
            {
                if (item[SPBuiltInFieldId.owshiddenversion].ToString() == "2")
                {
                    return true;
                }
            }
            return false;
        }
        private bool validateDocumentTypes(SPListItem item, Rule rule)
        {
            if (!string.IsNullOrEmpty(rule.DocumentTypes) && item.File != null)
            {
                string ext = item.File.Name.Substring(item.File.Name.LastIndexOf('.') + 1).ToLower();

                foreach (string srtExt in rule.DocumentTypes.Split(new char[] { ';' }))
                {
                    if (string.Compare(ext, srtExt.Trim(), true) == 0)
                    {
                        return true;
                    }
                }
                // If don't match any document type
                return false;

            }
            return true;
        }