22using exercise . wwwapi . DTOs ;
33using exercise . wwwapi . DTOs . Cohort ;
44using exercise . wwwapi . DTOs . Posts ;
5+ using exercise . wwwapi . Helpers ;
56using exercise . wwwapi . Models ;
67using exercise . wwwapi . Repository ;
78using Microsoft . AspNetCore . Authorization ;
89using Microsoft . AspNetCore . Mvc ;
910using Microsoft . EntityFrameworkCore ;
1011using System . Linq ;
12+ using System . Security . Claims ;
1113
1214namespace 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