This repository has been archived on 2025-05-14. You can view files and clone it, but cannot push or open issues or pull requests.
Shaun Walker 02fde9cec3
rolled back change creating an Infrastructure.Interfaces namespace, modified IModule interface to be strongly typed (#343)
* upgrade to .NET Core 3.2 Preview 3 and fixes for issues created by #314

* Components based on Bootstrap4 for Sections and  TabStrip to increase productivity and promote uniformity in Module UIs

* rolled back change creating an Infrastructure.Interfaces namespace, modified IModule interface to be strongly typed
2020-04-05 14:39:08 -04:00

111 lines
3.8 KiB
C#

using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Oqtane.Enums;
using Oqtane.Models;
using Oqtane.Shared;
using Oqtane.Infrastructure;
using Oqtane.Repository;
using Oqtane.Security;
namespace Oqtane.Controllers
{
[Route("{site}/api/[controller]")]
public class NotificationController : Controller
{
private readonly INotificationRepository _notifications;
private readonly IUserPermissions _userPermissions;
private readonly ILogManager _logger;
public NotificationController(INotificationRepository notifications, IUserPermissions userPermissions, ILogManager logger)
{
_notifications = notifications;
_userPermissions = userPermissions;
_logger = logger;
}
// GET: api/<controller>?siteid=x&type=y&userid=z
[HttpGet]
[Authorize(Roles = Constants.RegisteredRole)]
public IEnumerable<Notification> Get(string siteid, string direction, string userid)
{
IEnumerable<Notification> notifications = null;
if (IsAuthorized(int.Parse(userid)))
{
if (direction == "to")
{
notifications = _notifications.GetNotifications(int.Parse(siteid), -1, int.Parse(userid));
}
else
{
notifications = _notifications.GetNotifications(int.Parse(siteid), int.Parse(userid), -1);
}
}
return notifications;
}
// GET api/<controller>/5
[HttpGet("{id}")]
[Authorize(Roles = Constants.RegisteredRole)]
public Notification Get(int id)
{
Notification notification = _notifications.GetNotification(id);
if (!(IsAuthorized(notification.FromUserId) || IsAuthorized(notification.ToUserId)))
{
notification = null;
}
return notification;
}
// POST api/<controller>
[HttpPost]
[Authorize(Roles = Constants.RegisteredRole)]
public Notification Post([FromBody] Notification notification)
{
if (IsAuthorized(notification.FromUserId))
{
notification = _notifications.AddNotification(notification);
_logger.Log(LogLevel.Information, this, LogFunction.Create, "Notification Added {Notification}", notification);
}
return notification;
}
// PUT api/<controller>/5
[HttpPut("{id}")]
[Authorize(Roles = Constants.RegisteredRole)]
public Notification Put(int id, [FromBody] Notification notification)
{
if (IsAuthorized(notification.FromUserId))
{
notification = _notifications.UpdateNotification(notification);
_logger.Log(LogLevel.Information, this, LogFunction.Update, "Notification Updated {Folder}", notification);
}
return notification;
}
// DELETE api/<controller>/5
[HttpDelete("{id}")]
[Authorize(Roles = Constants.RegisteredRole)]
public void Delete(int id)
{
Notification notification = _notifications.GetNotification(id);
if (IsAuthorized(notification.FromUserId) || IsAuthorized(notification.ToUserId))
{
_notifications.DeleteNotification(id);
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "Notification Deleted {NotificationId}", id);
}
}
private bool IsAuthorized(int? userid)
{
bool authorized = true;
if (userid != null)
{
authorized = (_userPermissions.GetUser(User).UserId == userid);
}
return authorized;
}
}
}