@ -62,6 +62,8 @@ namespace Ombi.Controllers
IRepository < NotificationUserId > notificationRepository ,
IRepository < RequestSubscription > subscriptionRepository ,
ISettingsService < UserManagementSettings > umSettings ,
IRepository < UserNotificationPreferences > notificationPreferences ,
IMusicRequestRepository musicRepo ) ,
IMovieRequestEngine movieRequestEngine ,
ITvRequestEngine tvRequestEngine )
{
@ -73,6 +75,7 @@ namespace Ombi.Controllers
CustomizationSettings = c ;
WelcomeEmail = welcome ;
MovieRepo = m ;
MusicRepo = musicRepo ;
TvRepo = t ;
_log = l ;
_plexApi = plexApi ;
@ -86,6 +89,7 @@ namespace Ombi.Controllers
_userManagementSettings = umSettings ;
TvRequestEngine = tvRequestEngine ;
MovieRequestEngine = movieRequestEngine ;
_userNotificationPreferences = notificationPreferences ;
}
private OmbiUserManager UserManager { get ; }
@ -101,6 +105,7 @@ namespace Ombi.Controllers
private ITvRequestRepository TvRepo { get ; }
private IMovieRequestEngine MovieRequestEngine { get ; }
private ITvRequestEngine TvRequestEngine { get ; }
private IMusicRequestRepository MusicRepo { get ; }
private readonly ILogger < IdentityController > _log ;
private readonly IPlexApi _plexApi ;
private readonly ISettingsService < PlexSettings > _plexSettings ;
@ -109,6 +114,7 @@ namespace Ombi.Controllers
private readonly IRepository < RequestLog > _requestLogRepository ;
private readonly IRepository < NotificationUserId > _notificationRepository ;
private readonly IRepository < RequestSubscription > _requestSubscriptionRepository ;
private readonly IRepository < UserNotificationPreferences > _userNotificationPreferences ;
/// <summary>
/// This is what the Wizard will call when creating the user for the very first time.
@ -128,7 +134,7 @@ namespace Ombi.Controllers
if ( users . Any ( x = > ! x . UserName . Equals ( "api" , StringComparison . InvariantCultureIgnoreCase ) ) )
{
// No one should be calling this. Only the wizard
return new SaveWizardResult { Result = false , Errors = new List < string > { "Looks like there is an existing user!" } } ;
return new SaveWizardResult { Result = false , Errors = new List < string > { "Looks like there is an existing user!" } } ;
}
if ( user . UsePlexAdminAccount )
@ -298,7 +304,8 @@ namespace Ombi.Controllers
LastLoggedIn = user . LastLoggedIn ,
HasLoggedIn = user . LastLoggedIn . HasValue ,
EpisodeRequestLimit = user . EpisodeRequestLimit ? ? 0 ,
MovieRequestLimit = user . MovieRequestLimit ? ? 0
MovieRequestLimit = user . MovieRequestLimit ? ? 0 ,
MusicRequestLimit = user . MusicRequestLimit ? ? 0 ,
} ;
foreach ( var role in userRoles )
@ -360,6 +367,7 @@ namespace Ombi.Controllers
UserType = UserType . LocalUser ,
MovieRequestLimit = user . MovieRequestLimit ,
EpisodeRequestLimit = user . EpisodeRequestLimit ,
MusicRequestLimit = user . MusicRequestLimit ,
UserAccessToken = Guid . NewGuid ( ) . ToString ( "N" ) ,
} ;
var userResult = await UserManager . CreateAsync ( ombiUser , user . Password ) ;
@ -503,6 +511,7 @@ namespace Ombi.Controllers
user . Email = ui . EmailAddress ;
user . MovieRequestLimit = ui . MovieRequestLimit ;
user . EpisodeRequestLimit = ui . EpisodeRequestLimit ;
user . MusicRequestLimit = ui . MusicRequestLimit ;
var updateResult = await UserManager . UpdateAsync ( user ) ;
if ( ! updateResult . Succeeded )
{
@ -575,7 +584,8 @@ namespace Ombi.Controllers
// We need to delete all the requests first
var moviesUserRequested = MovieRepo . GetAll ( ) . Where ( x = > x . RequestedUserId = = userId ) ;
var tvUserRequested = TvRepo . GetChild ( ) . Where ( x = > x . RequestedUserId = = userId ) ;
var musicRequested = MusicRepo . GetAll ( ) . Where ( x = > x . RequestedUserId = = userId ) ;
if ( moviesUserRequested . Any ( ) )
{
await MovieRepo . DeleteRange ( moviesUserRequested ) ;
@ -585,6 +595,11 @@ namespace Ombi.Controllers
await TvRepo . DeleteChildRange ( tvUserRequested ) ;
}
if ( musicRequested . Any ( ) )
{
await MusicRepo . DeleteRange ( musicRequested ) ;
}
// Delete any issues and request logs
var issues = _issuesRepository . GetAll ( ) . Where ( x = > x . UserReportedId = = userId ) ;
var issueComments = _issueCommentsRepository . GetAll ( ) . Where ( x = > x . UserId = = userId ) ;
@ -601,9 +616,9 @@ namespace Ombi.Controllers
{
await _issueCommentsRepository . DeleteRange ( issueComments ) ;
}
// Delete the Subscriptions and mobile notification ids
var subs = _requestSubscriptionRepository . GetAll ( ) . Where ( x = > x . UserId = = userId ) ;
var subs = _requestSubscriptionRepository . GetAll ( ) . Where ( x = > x . UserId = = userId ) ;
var mobileIds = _notificationRepository . GetAll ( ) . Where ( x = > x . UserId = = userId ) ;
if ( subs . Any ( ) )
{
@ -803,6 +818,92 @@ namespace Ombi.Controllers
return user . UserAccessToken ;
}
[HttpGet("notificationpreferences")]
public async Task < List < UserNotificationPreferences > > GetUserPreferences ( )
{
var user = await UserManager . Users . FirstOrDefaultAsync ( x = > x . UserName = = User . Identity . Name ) ;
return await GetPreferences ( user ) ;
}
[HttpGet("notificationpreferences/{userId}")]
public async Task < List < UserNotificationPreferences > > GetUserPreferences ( string userId )
{
var user = await UserManager . Users . FirstOrDefaultAsync ( x = > x . Id = = userId ) ;
return await GetPreferences ( user ) ;
}
private readonly List < NotificationAgent > _excludedAgents = new List < NotificationAgent >
{
NotificationAgent . Email ,
NotificationAgent . Mobile
} ;
private async Task < List < UserNotificationPreferences > > GetPreferences ( OmbiUser user )
{
var userPreferences = await _userNotificationPreferences . GetAll ( ) . Where ( x = > x . UserId = = user . Id ) . ToListAsync ( ) ;
var agents = Enum . GetValues ( typeof ( NotificationAgent ) ) . Cast < NotificationAgent > ( ) . Where ( x = > ! _excludedAgents . Contains ( x ) ) ;
foreach ( var a in agents )
{
var agent = userPreferences . FirstOrDefault ( x = > x . Agent = = a ) ;
if ( agent = = null )
{
// Create the default
userPreferences . Add ( new UserNotificationPreferences
{
Agent = a ,
UserId = user . Id ,
} ) ;
}
}
return userPreferences ;
}
[HttpPost("NotificationPreferences")]
public async Task < IActionResult > AddUserNotificationPreference ( [ FromBody ] List < AddNotificationPreference > preferences )
{
foreach ( var pref in preferences )
{
// Make sure the user exists
var user = await UserManager . Users . FirstOrDefaultAsync ( x = > x . Id = = pref . UserId ) ;
if ( user = = null )
{
return NotFound ( ) ;
}
// Check if we are editing a different user than ourself, if we are then we need to power user role
var me = await UserManager . Users . FirstOrDefaultAsync ( x = > x . UserName = = User . Identity . Name ) ;
if ( ! me . Id . Equals ( user . Id , StringComparison . InvariantCultureIgnoreCase ) )
{
var isPowerUser = await UserManager . IsInRoleAsync ( me , OmbiRoles . PowerUser ) ;
var isAdmin = await UserManager . IsInRoleAsync ( me , OmbiRoles . Admin ) ;
if ( ! isPowerUser & & ! isAdmin )
{
return Unauthorized ( ) ;
}
}
// Make sure we don't already have a preference for this agent
var existingPreference = await _userNotificationPreferences . GetAll ( )
. FirstOrDefaultAsync ( x = > x . UserId = = user . Id & & x . Agent = = pref . Agent ) ;
if ( existingPreference ! = null )
{
// Update it
existingPreference . Value = pref . Value ;
existingPreference . Enabled = pref . Enabled ;
}
await _userNotificationPreferences . Add ( new UserNotificationPreferences
{
Agent = pref . Agent ,
Enabled = pref . Enabled ,
UserId = pref . UserId ,
Value = pref . Value
} ) ;
}
return Json ( true ) ;
}
private async Task < List < IdentityResult > > AddRoles ( IEnumerable < ClaimCheckboxes > roles , OmbiUser ombiUser )
{
var roleResult = new List < IdentityResult > ( ) ;