yavsc/src/Yavsc/ApiControllers/Relationship/ChatRoomAccessApiController.cs

182 lines
5.8 KiB
C#
Raw Normal View History

2019-05-08 03:13:07 +01:00
using System.Security.Claims;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2019-05-08 03:13:07 +01:00
using Yavsc.Models;
using Yavsc.Models.Chat;
namespace Yavsc.Controllers
{
[Produces("application/json")]
[Route("api/ChatRoomAccessApi")]
public class ChatRoomAccessApiController : Controller
{
2020-10-09 19:35:39 +01:00
private readonly ApplicationDbContext _context;
2019-05-08 03:13:07 +01:00
public ChatRoomAccessApiController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/ChatRoomAccessApi
[HttpGet, Authorize("AdministratorOnly")]
public IEnumerable<ChatRoomAccess> GetChatRoomAccess()
{
return _context.ChatRoomAccess;
}
// GET: api/ChatRoomAccessApi/5
[HttpGet("{id}", Name = "GetChatRoomAccess"), Authorize("AdministratorOnly")]
public async Task<IActionResult> GetChatRoomAccess([FromRoute] string id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-08 03:13:07 +01:00
}
ChatRoomAccess chatRoomAccess = await _context.ChatRoomAccess.SingleAsync(m => m.ChannelName == id);
if (chatRoomAccess == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-05-08 03:13:07 +01:00
}
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2019-05-08 03:13:07 +01:00
if (uid != chatRoomAccess.UserId && uid != chatRoomAccess.Room.OwnerId
&& ! User.IsInRole(Constants.AdminGroupName))
{
ModelState.AddModelError("UserId","get refused");
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-08 03:13:07 +01:00
}
return Ok(chatRoomAccess);
}
// PUT: api/ChatRoomAccessApi/5
[HttpPut("{id}")]
public async Task<IActionResult> PutChatRoomAccess([FromRoute] string id, [FromBody] ChatRoomAccess chatRoomAccess)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-08 03:13:07 +01:00
}
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2019-05-08 03:13:07 +01:00
if (id != chatRoomAccess.ChannelName)
{
2023-03-19 17:57:55 +00:00
return BadRequest();
2019-05-08 03:13:07 +01:00
}
var room = _context.ChatRoom.First(channel => channel.Name == chatRoomAccess.ChannelName );
if (uid != room.OwnerId && ! User.IsInRole(Constants.AdminGroupName))
{
ModelState.AddModelError("ChannelName", "access put refused");
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-08 03:13:07 +01:00
}
_context.Entry(chatRoomAccess).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ChatRoomAccessExists(id))
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-05-08 03:13:07 +01:00
}
else
{
throw;
}
}
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status204NoContent);
2019-05-08 03:13:07 +01:00
}
// POST: api/ChatRoomAccessApi
[HttpPost]
public async Task<IActionResult> PostChatRoomAccess([FromBody] ChatRoomAccess chatRoomAccess)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-08 03:13:07 +01:00
}
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2019-05-08 03:13:07 +01:00
var room = _context.ChatRoom.First(channel => channel.Name == chatRoomAccess.ChannelName );
if (room == null || (uid != room.OwnerId && ! User.IsInRole(Constants.AdminGroupName)))
{
ModelState.AddModelError("ChannelName", "access post refused");
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-08 03:13:07 +01:00
}
_context.ChatRoomAccess.Add(chatRoomAccess);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (ChatRoomAccessExists(chatRoomAccess.ChannelName))
{
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status409Conflict);
2019-05-08 03:13:07 +01:00
}
else
{
throw;
}
}
return CreatedAtRoute("GetChatRoomAccess", new { id = chatRoomAccess.ChannelName }, chatRoomAccess);
}
// DELETE: api/ChatRoomAccessApi/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteChatRoomAccess([FromRoute] string id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-08 03:13:07 +01:00
}
ChatRoomAccess chatRoomAccess = await _context.ChatRoomAccess.Include(acc => acc.Room).SingleAsync(m => m.ChannelName == id);
if (chatRoomAccess == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-05-08 03:13:07 +01:00
}
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2019-05-08 03:13:07 +01:00
var room = _context.ChatRoom.First(channel => channel.Name == chatRoomAccess.ChannelName );
if (room == null || (uid != room.OwnerId && chatRoomAccess.UserId != uid && ! User.IsInRole(Constants.AdminGroupName)))
{
ModelState.AddModelError("UserId", "access drop refused");
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-08 03:13:07 +01:00
}
_context.ChatRoomAccess.Remove(chatRoomAccess);
await _context.SaveChangesAsync();
return Ok(chatRoomAccess);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool ChatRoomAccessExists(string id)
{
return _context.ChatRoomAccess.Count(e => e.ChannelName == id) > 0;
}
}
2020-10-09 19:35:39 +01:00
}