From de2e3abfe02c70fbe1a61d51b2409ed189138f67 Mon Sep 17 00:00:00 2001 From: TidusJar Date: Tue, 18 Sep 2018 14:15:26 +0100 Subject: [PATCH] Added the API to add user notification preferences --- src/Ombi.Store/Entities/OmbiUser.cs | 1 + .../Entities/UserNotificationPreferences.cs | 7 ++- src/Ombi/Controllers/IdentityController.cs | 50 +++++++++++++++++-- .../Identity/AddNotificationPreference.cs | 12 +++++ 4 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 src/Ombi/Models/Identity/AddNotificationPreference.cs diff --git a/src/Ombi.Store/Entities/OmbiUser.cs b/src/Ombi.Store/Entities/OmbiUser.cs index 9513df818..801a50cb1 100644 --- a/src/Ombi.Store/Entities/OmbiUser.cs +++ b/src/Ombi.Store/Entities/OmbiUser.cs @@ -28,6 +28,7 @@ namespace Ombi.Store.Entities public string UserAccessToken { get; set; } public List NotificationUserIds { get; set; } + public List UserNotificationPreferences { get; set; } [NotMapped] public bool IsEmbyConnect => UserType == UserType.EmbyUser && EmbyConnectUserId.HasValue(); diff --git a/src/Ombi.Store/Entities/UserNotificationPreferences.cs b/src/Ombi.Store/Entities/UserNotificationPreferences.cs index c779480c8..7196d38ca 100644 --- a/src/Ombi.Store/Entities/UserNotificationPreferences.cs +++ b/src/Ombi.Store/Entities/UserNotificationPreferences.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations.Schema; -using System.Text; +using System.ComponentModel.DataAnnotations.Schema; +using Newtonsoft.Json; using Ombi.Helpers; namespace Ombi.Store.Entities @@ -15,6 +13,7 @@ namespace Ombi.Store.Entities public string Value { get; set; } [ForeignKey(nameof(UserId))] + [JsonIgnore] public OmbiUser User { get; set; } } } diff --git a/src/Ombi/Controllers/IdentityController.cs b/src/Ombi/Controllers/IdentityController.cs index d7e556f26..71cac7a76 100644 --- a/src/Ombi/Controllers/IdentityController.cs +++ b/src/Ombi/Controllers/IdentityController.cs @@ -793,15 +793,14 @@ namespace Ombi.Controllers [HttpGet("notificationpreferences")] public async Task> GetUserPreferences() { - //TODO potentially use a view model var user = await UserManager.Users.FirstOrDefaultAsync(x => x.UserName == User.Identity.Name); var userPreferences = await _userNotificationPreferences.GetAll().Where(x => x.UserId == user.Id).ToListAsync(); var agents = Enum.GetValues(typeof(NotificationAgent)).Cast(); foreach (var a in agents) { - var hasAgent = userPreferences.Any(x => x.Agent == a); - if (!hasAgent) + var agent = userPreferences.FirstOrDefault(x => x.Agent == a); + if (agent == null) { // Create the default userPreferences.Add(new UserNotificationPreferences @@ -809,11 +808,56 @@ namespace Ombi.Controllers Agent = a, }); } + else + { + userPreferences.Add(agent); + } } return userPreferences; } + [HttpPost("NotificationPreferences")] + public async Task AddUserNotificationPreference([FromBody] AddNotificationPreference pref) + { + // 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> AddRoles(IEnumerable roles, OmbiUser ombiUser) { var roleResult = new List(); diff --git a/src/Ombi/Models/Identity/AddNotificationPreference.cs b/src/Ombi/Models/Identity/AddNotificationPreference.cs new file mode 100644 index 000000000..51dc7f6fe --- /dev/null +++ b/src/Ombi/Models/Identity/AddNotificationPreference.cs @@ -0,0 +1,12 @@ +using Ombi.Helpers; + +namespace Ombi.Models.Identity +{ + public class AddNotificationPreference + { + public NotificationAgent Agent { get; set; } + public string UserId { get; set; } + public string Value { get; set; } + public bool Enabled { get; set; } + } +} \ No newline at end of file