/// <summary> /// Checks to make sure the sender's email address domain is one from the /// SystemGuid.DefinedType.COMMUNICATION_SAFE_SENDER_DOMAINS. If it is not /// it will replace the From address with the one defined by the OrganizationEmail /// global attribute. /// </summary> /// <param name="message">The message.</param> /// <param name="globalAttributes">The global attributes.</param> private void CheckSafeSender( MailMessage message, GlobalAttributesCache globalAttributes ) { if ( message != null && message.From != null ) { string from = message.From.Address; string fromName = message.From.DisplayName; // Check to make sure sending domain is a safe sender var safeDomains = DefinedTypeCache.Read( SystemGuid.DefinedType.COMMUNICATION_SAFE_SENDER_DOMAINS.AsGuid() ).DefinedValues.Select( v => v.Value ).ToList(); var emailParts = from.Split( new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries ); if ( emailParts.Length != 2 || !safeDomains.Contains( emailParts[1], StringComparer.OrdinalIgnoreCase ) ) { string orgEmail = globalAttributes.GetValue( "OrganizationEmail" ); if ( !string.IsNullOrWhiteSpace( orgEmail ) && !orgEmail.Equals( from, StringComparison.OrdinalIgnoreCase ) ) { message.From = new MailAddress( orgEmail ); bool addReplyTo = true; foreach ( var replyTo in message.ReplyToList ) { if ( replyTo.Address.Equals( from, StringComparison.OrdinalIgnoreCase ) ) { addReplyTo = false; break; } } if ( addReplyTo ) { message.ReplyToList.Add( new MailAddress( from, fromName ) ); } } } } }
/// <summary> /// Returns Global Attributes from cache. If they are not already in cache, they /// will be read and added to cache /// </summary> /// <returns></returns> public static GlobalAttributesCache Read( RockContext rockContext = null ) { string cacheKey = GlobalAttributesCache.CacheKey(); ObjectCache cache = MemoryCache.Default; GlobalAttributesCache globalAttributes = cache[cacheKey] as GlobalAttributesCache; if ( globalAttributes != null ) { return globalAttributes; } else { globalAttributes = new GlobalAttributesCache(); globalAttributes.Attributes = new List<AttributeCache>(); globalAttributes.AttributeValues = new Dictionary<string, KeyValuePair<string, string>>(); rockContext = rockContext ?? new RockContext(); var attributeService = new Rock.Model.AttributeService( rockContext ); var attributeValueService = new Rock.Model.AttributeValueService( rockContext ); foreach ( Rock.Model.Attribute attribute in attributeService.GetGlobalAttributes() ) { var attributeCache = AttributeCache.Read( attribute ); globalAttributes.Attributes.Add( attributeCache ); var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, null ).FirstOrDefault(); string value = ( attributeValue != null && !string.IsNullOrEmpty( attributeValue.Value ) ) ? attributeValue.Value : attributeCache.DefaultValue; globalAttributes.AttributeValues.Add( attributeCache.Key, new KeyValuePair<string, string>( attributeCache.Name, value ) ); } cache.Set( cacheKey, globalAttributes, new CacheItemPolicy() ); return globalAttributes; } }
/// <summary> /// Reads the specified rock context. /// </summary> /// <param name="rockContext">The rock context.</param> /// <returns></returns> public static GlobalAttributesCache Read(RockContext rockContext) { return(GetOrAddExisting(GlobalAttributesCache.CacheKey(), () => Load(rockContext))); }
private static GlobalAttributesCache Load2( RockContext rockContext ) { var globalAttributes = new GlobalAttributesCache(); globalAttributes.AttributeValues = new ConcurrentDictionary<string, string>(); globalAttributes.AttributeValuesFormatted = new ConcurrentDictionary<string, string>(); return globalAttributes; }
/// <summary> /// Removes Global Attributes from cache /// </summary> public static void Flush() { ObjectCache cache = MemoryCache.Default; cache.Remove(GlobalAttributesCache.CacheKey()); }