Skip to content

Commit 55eb6ce

Browse files
author
Roman Eriksen
committed
Role check implemented in cohort endpoints
1 parent ddd1bda commit 55eb6ce

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

exercise.wwwapi/Endpoints/CohortEndpoints.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
using exercise.wwwapi.DTOs;
33
using exercise.wwwapi.DTOs.Cohort;
44
using exercise.wwwapi.DTOs.Posts;
5+
using exercise.wwwapi.Helpers;
56
using exercise.wwwapi.Models;
67
using exercise.wwwapi.Repository;
78
using Microsoft.AspNetCore.Authorization;
89
using Microsoft.AspNetCore.Mvc;
910
using Microsoft.EntityFrameworkCore;
1011
using System.Linq;
12+
using System.Security.Claims;
1113

1214
namespace exercise.wwwapi.Endpoints
1315
{
@@ -86,9 +88,19 @@ public static async Task<IResult> GetCohortByUserId(IRepository<Cohort> cohortRe
8688
}
8789

8890
[Authorize]
91+
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
8992
[ProducesResponseType(StatusCodes.Status200OK)]
90-
public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortService, IMapper mapper)
93+
public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortService, IMapper mapper, ClaimsPrincipal user)
9194
{
95+
if (user.Role() != (int)Roles.teacher)
96+
{
97+
var forbiddenResponse = new ResponseDTO<object>
98+
{
99+
Message = "You are not authorized to get all cohorts."
100+
};
101+
return TypedResults.Json(forbiddenResponse, statusCode: StatusCodes.Status403Forbidden);
102+
}
103+
92104
var results = cohortService.GetWithIncludes(q => q
93105
.Include(c => c.CohortCourses)
94106
.ThenInclude(cc => cc.Course)
@@ -108,14 +120,24 @@ public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortServic
108120
}
109121

110122
[Authorize]
123+
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
111124
[ProducesResponseType(StatusCodes.Status400BadRequest)]
112125
[ProducesResponseType(StatusCodes.Status201Created)]
113126
public static async Task<IResult> CreateCohort(
114127
IRepository<Cohort> cohortService,
115128
IRepository<Course> courseService,
129+
ClaimsPrincipal user,
116130
IMapper mapper,
117131
CreateCohortDTO request)
118132
{
133+
if (user.Role() != (int)Roles.teacher)
134+
{
135+
var forbiddenResponse = new ResponseDTO<object>
136+
{
137+
Message = "You are not authorized to create a new cohort."
138+
};
139+
return TypedResults.Json(forbiddenResponse, statusCode: StatusCodes.Status403Forbidden);
140+
}
119141

120142
var results = cohortService.GetAllFiltered(c => c.Title == request.Title);
121143
Console.WriteLine(results);
@@ -164,17 +186,28 @@ public static async Task<IResult> CreateCohort(
164186

165187
[Authorize]
166188
[ProducesResponseType(StatusCodes.Status200OK)]
189+
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
167190
[ProducesResponseType(StatusCodes.Status400BadRequest)]
168191
public static async Task<IResult> AddUserToCohort(
169192
IRepository<Cohort> cohortService,
170193
IRepository<User> userService,
171194
IRepository<CohortCourse> cohortCourseService,
172195
IRepository<CohortCourseUser> cohortCourseUserService,
173196
IMapper mapper,
197+
ClaimsPrincipal userCheck,
174198
int userId,
175199
int cohortId,
176200
int courseId)
177201
{
202+
if (userCheck.Role() != (int)Roles.teacher)
203+
{
204+
var forbiddenResponse = new ResponseDTO<object>
205+
{
206+
Message = "You are not authorized to add a user to a cohort."
207+
};
208+
return TypedResults.Json(forbiddenResponse, statusCode: StatusCodes.Status403Forbidden);
209+
}
210+
178211
// 1. Get the user
179212
var user = userService.GetById(userId);
180213
if (user == null)
@@ -253,17 +286,29 @@ public static async Task<IResult> AddUserToCohort(
253286

254287
[Authorize]
255288
[ProducesResponseType(StatusCodes.Status200OK)]
289+
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
256290
[ProducesResponseType(StatusCodes.Status400BadRequest)]
257291
public static async Task<IResult> DeleteUserFromCohort(
258292
IRepository<Cohort> cohortService,
259293
IRepository<User> userService,
260294
IRepository<CohortCourse> cohortCourseService,
261295
IRepository<CohortCourseUser> cohortCourseUserService,
262296
IMapper mapper,
297+
ClaimsPrincipal userCheck,
263298
int userId,
264299
int cohortId,
265300
int courseId)
266301
{
302+
303+
if (userCheck.Role() != (int)Roles.teacher)
304+
{
305+
var forbiddenResponse = new ResponseDTO<object>
306+
{
307+
Message = "You are not authorized to delete a user from a cohort."
308+
};
309+
return TypedResults.Json(forbiddenResponse, statusCode: StatusCodes.Status403Forbidden);
310+
}
311+
267312
// 1. Get the user
268313
var user = userService.GetById(userId);
269314
if (user == null) return TypedResults.BadRequest(new ResponseDTO<object>

0 commit comments

Comments
 (0)