2017-07-20 11:21:28 +02:00
using System ;
2014-01-13 16:13:53 +11:00
using System.Globalization ;
using Umbraco.Core ;
using Umbraco.Core.Configuration ;
2014-05-12 14:32:34 +10:00
using Umbraco.Core.Logging ;
using Umbraco.Core.Models.Membership ;
2014-01-13 16:13:53 +11:00
using Umbraco.Core.Services ;
2016-03-15 19:54:21 +01:00
using Umbraco.Core.Models ;
2016-03-17 15:28:46 +01:00
using Umbraco.Web._Legacy.Actions ;
2016-06-28 12:03:36 -06:00
using System.Collections.Generic ;
2018-01-15 11:32:30 +01:00
using Umbraco.Core.Models.Entities ;
2017-05-30 18:13:11 +02:00
using Umbraco.Web.Composing ;
2014-01-13 16:13:53 +11:00
namespace Umbraco.Web
{
2016-09-01 19:06:08 +02:00
// TODO: all of these require an UmbracoContext because currently to send the notifications we need an HttpContext, this is based on legacy code
2014-05-12 14:32:34 +10:00
// for which probably requires updating so that these can be sent outside of the http context.
2014-01-13 16:13:53 +11:00
internal static class NotificationServiceExtensions
{
2016-09-01 19:06:08 +02:00
internal static void SendNotification ( this INotificationService service , IUmbracoEntity entity , IAction action )
2014-01-13 16:13:53 +11:00
{
2016-09-01 19:06:08 +02:00
if ( Current . UmbracoContext == null )
2014-05-12 14:32:34 +10:00
{
2016-09-11 19:57:33 +02:00
Current . Logger . Warn ( typeof ( NotificationServiceExtensions ), "Cannot send notifications, there is no current UmbracoContext" );
2014-05-12 14:32:34 +10:00
return ;
}
2018-04-06 13:51:54 +10:00
service . SendNotification ( entity , action , Current . UmbracoContext , Current . Services . TextService , UmbracoConfig . For . GlobalSettings ());
2014-01-13 16:13:53 +11:00
}
2016-09-01 19:06:08 +02:00
internal static void SendNotification ( this INotificationService service , IEnumerable < IUmbracoEntity > entities , IAction action )
2016-06-28 12:03:36 -06:00
{
2016-09-01 19:06:08 +02:00
if ( Current . UmbracoContext == null )
2016-06-28 12:03:36 -06:00
{
2016-09-11 19:57:33 +02:00
Current . Logger . Warn ( typeof ( NotificationServiceExtensions ), "Cannot send notifications, there is no current UmbracoContext" );
2016-06-28 12:03:36 -06:00
return ;
}
2018-04-06 13:51:54 +10:00
service . SendNotification ( entities , action , Current . UmbracoContext , Current . Services . TextService , UmbracoConfig . For . GlobalSettings ());
2016-06-28 12:03:36 -06:00
}
2016-09-01 19:06:08 +02:00
//internal static void SendNotification(this INotificationService service, IUmbracoEntity entity, IAction action, UmbracoContext umbracoContext)
//{
// if (umbracoContext == null)
// {
2016-09-11 19:57:33 +02:00
// Current.Logger.Warn(typeof(NotificationServiceExtensions), "Cannot send notifications, there is no current UmbracoContext");
2016-09-01 19:06:08 +02:00
// return;
// }
// service.SendNotification(entity, action, umbracoContext);
//}
//internal static void SendNotification(this INotificationService service, IEnumerable<IUmbracoEntity> entities, IAction action, UmbracoContext umbracoContext)
//{
// if (umbracoContext == null)
// {
2016-09-11 19:57:33 +02:00
// Current.Logger.Warn(typeof(NotificationServiceExtensions), "Cannot send notifications, there is no current UmbracoContext");
2016-09-01 19:06:08 +02:00
// return;
// }
// service.SendNotification(entities, action, umbracoContext);
//}
2018-04-06 13:51:54 +10:00
internal static void SendNotification ( this INotificationService service , IUmbracoEntity entity , IAction action , UmbracoContext umbracoContext , ILocalizedTextService textService , IGlobalSettings globalSettings )
2014-01-13 16:13:53 +11:00
{
2014-05-12 14:32:34 +10:00
if ( umbracoContext == null )
{
2016-09-11 19:57:33 +02:00
Current . Logger . Warn ( typeof ( NotificationServiceExtensions ), "Cannot send notifications, there is no current UmbracoContext" );
2014-05-12 14:32:34 +10:00
return ;
}
2016-09-01 19:06:08 +02:00
var user = umbracoContext . Security . CurrentUser ;
var userService = Current . Services . UserService ; // fixme inject
2017-07-20 11:21:28 +02:00
//if there is no current user, then use the admin
2016-09-01 19:06:08 +02:00
if ( user == null )
{
2016-09-11 19:57:33 +02:00
Current . Logger . Debug ( typeof ( NotificationServiceExtensions ), "There is no current Umbraco user logged in, the notifications will be sent from the administrator" );
2018-05-31 23:05:35 +10:00
user = userService . GetUserById ( Constants . Security . SuperUserId );
2016-09-01 19:06:08 +02:00
if ( user == null )
{
2018-05-31 23:05:35 +10:00
Current . Logger . Warn ( typeof ( NotificationServiceExtensions ), $"Noticiations can not be sent, no admin user with id {Constants.Security.SuperUserId} could be resolved" );
2016-09-01 19:06:08 +02:00
return ;
}
}
2018-04-06 13:51:54 +10:00
service . SendNotification ( user , entity , action , umbracoContext , textService , globalSettings );
2014-01-13 16:13:53 +11:00
}
2018-04-06 13:51:54 +10:00
internal static void SendNotification ( this INotificationService service , IEnumerable < IUmbracoEntity > entities , IAction action , UmbracoContext umbracoContext , ILocalizedTextService textService , IGlobalSettings globalSettings )
2014-01-13 16:13:53 +11:00
{
2014-05-12 14:32:34 +10:00
if ( umbracoContext == null )
{
2016-09-11 19:57:33 +02:00
Current . Logger . Warn ( typeof ( NotificationServiceExtensions ), "Cannot send notifications, there is no current UmbracoContext" );
2014-05-12 14:32:34 +10:00
return ;
}
var user = umbracoContext . Security . CurrentUser ;
2016-09-01 19:06:08 +02:00
var userService = Current . Services . UserService ; // fixme inject
2014-05-12 14:32:34 +10:00
2017-07-20 11:21:28 +02:00
//if there is no current user, then use the admin
2014-05-12 14:32:34 +10:00
if ( user == null )
{
2016-09-11 19:57:33 +02:00
Current . Logger . Debug ( typeof ( NotificationServiceExtensions ), "There is no current Umbraco user logged in, the notifications will be sent from the administrator" );
2018-05-31 23:05:35 +10:00
user = userService . GetUserById ( Constants . Security . SuperUserId );
2014-05-12 14:32:34 +10:00
if ( user == null )
{
2018-05-31 23:05:35 +10:00
Current . Logger . Warn ( typeof ( NotificationServiceExtensions ), $"Noticiations can not be sent, no admin user with id {Constants.Security.SuperUserId} could be resolved" );
2014-05-12 14:32:34 +10:00
return ;
}
}
2018-04-06 13:51:54 +10:00
service . SendNotification ( user , entities , action , umbracoContext , textService , globalSettings );
2014-05-12 14:32:34 +10:00
}
2018-04-06 13:51:54 +10:00
internal static void SendNotification ( this INotificationService service , IUser sender , IUmbracoEntity entity , IAction action , UmbracoContext umbracoContext , ILocalizedTextService textService , IGlobalSettings globalSettings )
2014-05-12 14:32:34 +10:00
{
2016-07-08 17:58:01 +02:00
if ( sender == null ) throw new ArgumentNullException ( nameof ( sender ));
if ( umbracoContext == null ) throw new ArgumentNullException ( nameof ( umbracoContext ));
2014-01-13 16:13:53 +11:00
2016-09-01 19:06:08 +02:00
service . SendNotifications (
2014-05-12 14:32:34 +10:00
sender ,
2014-01-13 16:13:53 +11:00
entity ,
action . Letter . ToString ( CultureInfo . InvariantCulture ),
2016-09-01 19:06:08 +02:00
textService . Localize ( "actions" , action . Alias ),
2014-01-13 16:13:53 +11:00
umbracoContext . HttpContext ,
2018-04-06 13:51:54 +10:00
( mailingUser , strings ) => textService . Localize ( "notifications/mailSubject" , mailingUser . GetUserCulture ( textService , globalSettings ), strings ),
2014-01-13 16:13:53 +11:00
( mailingUser , strings ) => UmbracoConfig . For . UmbracoSettings (). Content . DisableHtmlEmail
2018-04-06 13:51:54 +10:00
? textService . Localize ( "notifications/mailBody" , mailingUser . GetUserCulture ( textService , globalSettings ), strings )
: textService . Localize ( "notifications/mailBodyHtml" , mailingUser . GetUserCulture ( textService , globalSettings ), strings ));
2014-01-13 16:13:53 +11:00
}
2016-06-28 12:03:36 -06:00
2018-04-06 13:51:54 +10:00
internal static void SendNotification ( this INotificationService service , IUser sender , IEnumerable < IUmbracoEntity > entities , IAction action , UmbracoContext umbracoContext , ILocalizedTextService textService , IGlobalSettings globalSettings )
2016-06-28 12:03:36 -06:00
{
2016-07-08 17:58:01 +02:00
if ( sender == null ) throw new ArgumentNullException ( nameof ( sender ));
if ( umbracoContext == null ) throw new ArgumentNullException ( nameof ( umbracoContext ));
2016-06-28 12:03:36 -06:00
2016-09-01 19:06:08 +02:00
service . SendNotifications (
2016-06-28 12:03:36 -06:00
sender ,
entities ,
action . Letter . ToString ( CultureInfo . InvariantCulture ),
2016-09-01 19:06:08 +02:00
textService . Localize ( "actions" , action . Alias ),
2016-06-28 12:03:36 -06:00
umbracoContext . HttpContext ,
2018-04-06 13:51:54 +10:00
( mailingUser , strings ) => textService . Localize ( "notifications/mailSubject" , mailingUser . GetUserCulture ( textService , globalSettings ), strings ),
2016-06-28 12:03:36 -06:00
( mailingUser , strings ) => UmbracoConfig . For . UmbracoSettings (). Content . DisableHtmlEmail
2018-04-06 13:51:54 +10:00
? textService . Localize ( "notifications/mailBody" , mailingUser . GetUserCulture ( textService , globalSettings ), strings )
: textService . Localize ( "notifications/mailBodyHtml" , mailingUser . GetUserCulture ( textService , globalSettings ), strings ));
2016-06-28 12:03:36 -06:00
}
2014-01-13 16:13:53 +11:00
}
2017-07-20 11:21:28 +02:00
}