Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JWT Authorization #33

Merged
merged 6 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Glue/Controllers/AdminCensorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PetFoster.DAL;
using System.Data;
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

Expand All @@ -26,6 +27,7 @@ public PostModel()
}
}
// GET: api/<AdminCensorController>
[Authorize(Policy = "AdminOnly")]
[HttpGet("check")]
public IActionResult Get()
{
Expand Down Expand Up @@ -97,6 +99,7 @@ public void Delete(int id)
}
*/
// PATCH api/<AdminCensorController>
[Authorize(Policy = "AdminOnly")]
[HttpPatch("check-info-update")]
public IActionResult Patch([FromBody] PostModel PostData)
{
Expand Down
7 changes: 6 additions & 1 deletion Glue/Controllers/AdminUserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PetFoster.DAL;
using System.Data;
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

Expand All @@ -20,8 +21,9 @@ public class UserModel
public string? address { get; set; }
public string? account_status { get; set; }
}

// GET: api/<AdminUserController>
[Authorize(Policy = "AdminOnly")]
[HttpGet("tableuser")]
public IActionResult Get()
{
Expand Down Expand Up @@ -98,19 +100,22 @@ private IActionResult _ChangeUserStatus(string id, string target_status)
}

// POST api/<AdminUserController>
[Authorize(Policy = "AdminOnly")]
[HttpPost("block")] // 封号
public IActionResult PostBlock([FromBody] string id)
{
return _ChangeUserStatus(id, "Banned");
}
// POST api/<AdminUserController>
[Authorize(Policy = "AdminOnly")]
[HttpPost("ban")] // 禁言
public IActionResult PostBan([FromBody] string id)
{
if (UserServer.GetStatus(id) == "Banned")
return Ok();
return _ChangeUserStatus(id, "Under Review");
}
[Authorize(Policy = "AdminOnly")]
[HttpPost("remove-block")]
[HttpPost("remove-ban")] // 解除禁言/解除封号,逻辑由UserManager.Ban实现,故在这里共用同一函数
public IActionResult PostRemoveBan([FromBody] string id)
Expand Down
7 changes: 6 additions & 1 deletion Glue/Controllers/DoctorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PetFoster.DAL;
using PetFoster.Model;
using System.Data;
using Microsoft.AspNetCore.Authorization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

Expand Down Expand Up @@ -42,6 +43,7 @@ private List<Doctor> ConvertDataTableToVetList(DataTable dataTable)
}

// GET: api/<DoctorController>
[Authorize(Policy = "AdminOnly")]
[HttpGet("doctor")]
public IActionResult Get()
{
Expand All @@ -56,6 +58,7 @@ public IActionResult Get()
return StatusCode(500, ex.Message);
}
}
[Authorize(Policy = "AdminOnly")]
[HttpPost("add-doctor")]
public IActionResult AddEmployee([FromBody] Doctor vet)
{
Expand Down Expand Up @@ -103,6 +106,7 @@ public IEnumerable<Doctor> Post()
}
*/
// PUT api/<DoctorController>/5
[Authorize(Policy = "AdminOnly")]
[HttpPut("edit-doctor/{doctorId}")]
public IActionResult Put(int doctorId, [FromBody] Doctor vet)
{
Expand Down Expand Up @@ -137,8 +141,9 @@ public IActionResult Put(int doctorId, [FromBody] Doctor vet)
return StatusCode(500, ex.Message);
}
}

// DELETE api/<DoctorController>/5
[Authorize(Policy = "AdminOnly")]
[HttpDelete("delete-doctor/{id}")]
public IActionResult Delete(int id)
{
Expand Down
3 changes: 2 additions & 1 deletion Glue/Controllers/DonateController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using PetFoster.BLL;
using static Glue.Controllers.RegisterController;
using Microsoft.AspNetCore.Authorization;

namespace Glue.Controllers
{
Expand All @@ -19,7 +20,7 @@ public DonateModel()
Amount = 0;
}
}

[Authorize]
[HttpPost]
public IActionResult Donate([FromBody] DonateModel donateModel)
{
Expand Down
7 changes: 6 additions & 1 deletion Glue/Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using PetFoster.DAL;
using PetFoster.Model;
using System.Data;
using Microsoft.AspNetCore.Authorization;

using static WebApplicationTest1.UserInfoController;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
Expand All @@ -23,6 +25,7 @@ public class EmployeeModel
public string? salary { get; set; }
}
// GET: api/<EmployeeController>
[Authorize(Policy = "AdminOnly")]
[HttpGet("employee")]
public IActionResult Get()
{
Expand Down Expand Up @@ -76,7 +79,7 @@ public IActionResult Get()
}

}

[Authorize(Policy = "AdminOnly")]
[HttpPost("add-employee")]
public IActionResult AddEmployee([FromBody] EmployeeModel employee)
{
Expand Down Expand Up @@ -131,6 +134,7 @@ public void Post([FromBody] string value)
}
*/
// PUT api/<EmployeeController>/5
[Authorize(Policy = "AdminOnly")]
[HttpPut("edit-employee/{employeeId}")]
public IActionResult Put(int employeeId, [FromBody] EmployeeModel employee)
{
Expand Down Expand Up @@ -196,6 +200,7 @@ public IActionResult Put(int employeeId, [FromBody] EmployeeModel employee)
}

// DELETE api/<EmployeeController>/5
[Authorize(Policy = "AdminOnly")]
[HttpDelete("delete-employee/{employeeId}")]
public IActionResult Delete(int employeeId)
{
Expand Down
7 changes: 6 additions & 1 deletion Glue/Controllers/ForumController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Glue.PetFoster.Model;
using static Glue.Controllers.RegisterController;
using System.Net.NetworkInformation;
using Microsoft.AspNetCore.Authorization;

namespace Glue.Controllers
{
Expand Down Expand Up @@ -61,6 +62,7 @@ public class PostRequestModel
public string? post_content { get; set; }
public List<IFormFile>? filename { get; set; }
}
[Authorize]
[HttpPost("postcontent")]
public async Task<IActionResult> PostContent([FromForm] PostRequestModel postModel)
{
Expand Down Expand Up @@ -115,7 +117,7 @@ public IActionResult PostComment([FromBody] PostModel postModel)
Console.WriteLine("收到帖子评论列表请求:" + postModel.post_id);
return Ok(AllComment);
}

[Authorize]
[HttpPost("addcomment")]
public IActionResult AddComment([FromBody] PostModel postModel)
{
Expand All @@ -128,6 +130,7 @@ public IActionResult AddComment([FromBody] PostModel postModel)
return Ok("评论成功");
return BadRequest(-1);
}
[Authorize]
[HttpPost("likepost")]
public IActionResult LikePost([FromBody] PostModel postModel)
{
Expand All @@ -142,13 +145,15 @@ public IActionResult IfLikePost([FromBody] PostModel postModel)
int status = LikePostManager.IfLike(postModel.user_id, postModel.post_id);
return Ok(status);
}
[Authorize]
[HttpPost("deletecomment")]
public IActionResult DeleteComment([FromBody] PostModel postModel)
{
Console.WriteLine("收到获取点赞信息请求,帖子ID:" + postModel.post_id + "点赞人ID:" + postModel.user_id);
CommentPostManager.UndoACommentPost(postModel.user_id, postModel.post_id,postModel.comment_time);
return Ok();
}
[Authorize]
[HttpPost("deletepost")]
public IActionResult DeletePost([FromBody] PostModel postModel)
{
Expand Down
13 changes: 13 additions & 0 deletions Glue/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@
using System.Text.Json;
using System.Text;
using System.Security.Cryptography;
using System.Security.Claims;
using Glue.Controllers;

namespace WebApplicationTest1
{

[Route("api/[controller]")]
[ApiController]
public class LoginController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly TokenHelper _tokenHelper;
public LoginController(IConfiguration configuration)
{
_configuration = configuration;
_tokenHelper = new TokenHelper(configuration);
}
public class LoginModel
{
public string Username { get; set; }
Expand Down Expand Up @@ -76,11 +86,14 @@ public IActionResult Login([FromBody] LoginModel loginModel)
}
else
{
//用户身份验证成功,生成JWT令牌
string tokenString = _tokenHelper.GenerateToken(candidate.User_ID);

var responseData = new
{
data = new
{
token = tokenString,
User_ID = candidate.User_ID,
User_Name = candidate.User_Name,
Password = candidate.Password,
Expand Down
3 changes: 3 additions & 0 deletions Glue/Controllers/ManageAdoptController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text.Json;
using PetFoster.DAL;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Glue.Controllers
Expand Down Expand Up @@ -35,6 +36,7 @@ public AdoptionRecord()
}

// GET: api/<ManageAdoptController>
[Authorize(Policy = "AdminOnly")]
[HttpGet("manage-adopt")]
public IActionResult Get()
{
Expand Down Expand Up @@ -98,6 +100,7 @@ public IActionResult Get()
}

// PATCH: api/<ManageAdoptController>
[Authorize(Policy = "AdminOnly")]
[HttpPatch("manage-adopt-update")]
public IActionResult UpdateAdoptRecord([FromBody] AdoptionRecord record)
{
Expand Down
6 changes: 4 additions & 2 deletions Glue/Controllers/ManageFosterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Data;
using System.Text.Json;
//using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.Authorization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

Expand Down Expand Up @@ -45,7 +46,7 @@ public void print()
}

// GET: api/<ManageFosterController>

[Authorize(Policy = "AdminOnly")]
[HttpGet("manage-foster")]
public IActionResult Get()
{
Expand Down Expand Up @@ -108,8 +109,9 @@ public IActionResult Get()
}
return Ok(jsondata);
}

// PATCH: api/<ManageFosterController>
[Authorize(Policy = "AdminOnly")]
[HttpPatch("manage-foster-update")]
public IActionResult UpdateFosterRecord([FromBody] FosterRecord record)
{
Expand Down
4 changes: 4 additions & 0 deletions Glue/Controllers/ManageNoticeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Security.Cryptography;
using System;
using System.Numerics;
using Microsoft.AspNetCore.Authorization;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Glue.Controllers
Expand Down Expand Up @@ -111,6 +112,7 @@ public IActionResult getDonation()
}*/

// 发送新公告
[Authorize(Policy = "AdminOnly")]
[HttpPost("send-notice")]
public IActionResult sendNewNotice([FromBody] NoticeModel notice)
{
Expand Down Expand Up @@ -161,6 +163,7 @@ public IActionResult getNoticeContent(int noticeId)
}*/

// 发送编辑过的公告
[Authorize(Policy = "AdminOnly")]
[HttpPost("send-edited-notice")]
public IActionResult sendEditedNotice([FromBody] NoticeModel notice)
{
Expand Down Expand Up @@ -195,6 +198,7 @@ public IActionResult sendEditedNotice([FromBody] NoticeModel notice)
}

// 删除公告
[Authorize(Policy = "AdminOnly")]
[HttpDelete("delete-notice/{noticeId}")]
public IActionResult deleteNotice(int noticeId)
{
Expand Down
5 changes: 5 additions & 0 deletions Glue/Controllers/ManagePetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PetFoster.DAL;
using System.Data;
using System.Data.Common;
using Microsoft.AspNetCore.Authorization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

Expand All @@ -18,6 +19,7 @@ public ManagePetController(FileHelper fileHelper)
_fileHelper = fileHelper;
}
// GET: api/<ManagePetController>
[Authorize(Policy = "AdminOnly")]
[HttpGet("petlist")]
public IActionResult Get()
{
Expand Down Expand Up @@ -113,6 +115,7 @@ public class PetModelWithImgs
}

// POST api/<ManagePetController>
[Authorize(Policy = "AdminOnly")]
[HttpPost("add-pet")]
public IActionResult AddPet([FromBody] PetModel pet)
{
Expand Down Expand Up @@ -192,6 +195,7 @@ public IActionResult AddPet([FromBody] PetModel pet)
}

// POST api/<ManagePetController>/5
[Authorize(Policy = "AdminOnly")]
[HttpPost("edited-pet")]
public async Task<IActionResult> Post([FromForm] PetModelWithImgs pet)
{
Expand Down Expand Up @@ -252,6 +256,7 @@ public class DeletePetRequest
{
public string? pid { get; set; }
}
[Authorize(Policy = "AdminOnly")]
[HttpDelete("delete-pet")]
public IActionResult Delete([FromBody] DeletePetRequest request)
{
Expand Down
Loading
Loading