@@ -96,9 +96,14 @@ private static async Task<IResult> GetUsersByCohort(IRepository<Cohort> reposito
9696 var results = response . CohortCourses . SelectMany ( a => a . UserCCs ) . Select ( a => a . User ) . ToList ( ) ;
9797 var dto_results = results . Select ( a => new UserDTO ( a ) ) ;
9898
99+ var userRole = claimsPrincipal . Role ( ) ;
100+ var authorizedAsTeacher = AuthorizeTeacher ( claimsPrincipal ) ;
101+
99102 var userData = new UsersSuccessDTO
100103 {
101- Users = results . Select ( u => new UserDTO ( u ) ) . ToList ( ) //if teacher loads students, also load notes for students.
104+ Users = results . Select ( user => authorizedAsTeacher
105+ ? new UserDTO ( user , PrivilegeLevel . Teacher ) //if teacher loads students, also load notes for students.
106+ : new UserDTO ( user , PrivilegeLevel . Student ) ) . ToList ( ) //if teacher loads students, also load notes for students.
102107 } ;
103108
104109 var responseObject = new ResponseDTO < UsersSuccessDTO >
@@ -117,7 +122,23 @@ private static async Task<IResult> GetUsersByCohortCourse(IRepository<CohortCour
117122 var results = response . UserCCs . Select ( a => a . User ) . ToList ( ) ;
118123 var dto_results = results . Select ( a => new UserDTO ( a ) ) ;
119124
120- return TypedResults . Ok ( dto_results ) ;
125+ var userRole = claimsPrincipal . Role ( ) ;
126+ var authorizedAsTeacher = AuthorizeTeacher ( claimsPrincipal ) ;
127+
128+ var userData = new UsersSuccessDTO
129+ {
130+ Users = results . Select ( user => authorizedAsTeacher
131+ ? new UserDTO ( user , PrivilegeLevel . Teacher ) //if teacher loads students, also load notes for students.
132+ : new UserDTO ( user , PrivilegeLevel . Student ) ) . ToList ( ) //if teacher loads students, also load notes for students.
133+ } ;
134+
135+ var responseObject = new ResponseDTO < UsersSuccessDTO >
136+ {
137+ Status = "success" ,
138+ Data = userData
139+ } ;
140+
141+ return TypedResults . Ok ( responseObject ) ;
121142 }
122143
123144 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
@@ -252,13 +273,20 @@ public static async Task<IResult> GetUserById(IRepository<User> userRepository,
252273 return TypedResults . NotFound ( ) ;
253274 }
254275
255- var userData = new UserDTO ( response ) ;
256- // userData.CurrentStartdate = response.User_CC.ElementAt(0).CohortCourse.Cohort.StartDate;
276+ var userRole = claimsPrincipal . Role ( ) ;
277+ var authorizedAsTeacher = AuthorizeTeacher ( claimsPrincipal ) ;
278+
279+ var userData = authorizedAsTeacher
280+ ? new UserDTO ( response , PrivilegeLevel . Teacher ) //if teacher loads students, also load notes for students.
281+ : new UserDTO ( response , PrivilegeLevel . Student ) ; //if teacher loads students, also load notes for students.
282+
283+
257284 var responseObject = new ResponseDTO < UserDTO >
258285 {
259286 Status = "success" ,
260287 Data = userData
261288 } ;
289+
262290 return TypedResults . Ok ( responseObject ) ;
263291 }
264292
0 commit comments