public async Task<ActionResult<CoursesListResponse>> CoursesList([FromQuery] CourseRoleType? role = null) { if (role.HasValue && !IsAuthenticated) return Unauthorized(); if (role == CourseRoleType.Student) return NotFound(new ErrorResponse("Role can not be student. Specify tester, instructor or courseAdmin")); var courses = await courseManager.GetCoursesAsync().ConfigureAwait(false); var isSystemAdministrator = await IsSystemAdministratorAsync().ConfigureAwait(false); // Фильтрация по роли. У администратора высшая роль. if (role.HasValue && !isSystemAdministrator) { var courseIdsAsRole = await courseRolesRepo.GetCoursesWhereUserIsInRoleAsync(UserId, role.Value).ConfigureAwait(false); courses = courses.Where(c => courseIdsAsRole.Contains(c.Id, StringComparer.InvariantCultureIgnoreCase)); } // Неопубликованные курсы не покажем тем, кто не имеет роли в них. if (!isSystemAdministrator) { var visibleCourses = unitsRepo.GetVisibleCourses(); var coursesInWhichUserHasAnyRole = await courseRolesRepo.GetCoursesWhereUserIsInRoleAsync(UserId, CourseRoleType.Tester).ConfigureAwait(false); courses = courses.Where(c => visibleCourses.Contains(c.Id) || coursesInWhichUserHasAnyRole.Contains(c.Id, StringComparer.OrdinalIgnoreCase)); } // Администратор видит все курсы. Покажем сверху те, в которых он преподаватель. if (isSystemAdministrator) { var instructorCourseIds = await courseRolesRepo.GetCoursesWhereUserIsInStrictRoleAsync(UserId, CourseRoleType.Instructor).ConfigureAwait(false); courses = courses.OrderBy(c => !instructorCourseIds.Contains(c.Id, StringComparer.InvariantCultureIgnoreCase)).ThenBy(c => c.Title); } else courses = courses.OrderBy(c => c.Title); var tempCourseLabel = "Временный - "; var tempCoursesIds = (await tempCoursesRepo.GetTempCoursesAsync()) .Select(t => t.CourseId) .ToHashSet(); var coursesList = courses.ToList(); var coursesLastVisits = await visitsRepo.GetLastVisitsForCourses(coursesList.Select(c => c.Id).ToHashSet(), UserId); return new CoursesListResponse { Courses = coursesList .Select(c => new ShortCourseInfo { Id = c.Id, Title = tempCoursesIds.Contains(c.Id) ? tempCourseLabel + c.Title : c.Title, ApiUrl = Url.Action("CourseInfo", "Courses", new { courseId = c.Id }), IsTempCourse = tempCoursesIds.Contains(c.Id), Timestamp = coursesLastVisits.TryGetValue(c.Id, out var date) ? date : null, }
private async Task <List <string> > GetCoursesWhereUserCanSeeAllGroupsAsync(string userId) { if (await usersRepo.IsSystemAdministrator(userId) || await systemAccessesRepo.HasSystemAccessAsync(userId, SystemAccessType.ViewAllGroupMembers).ConfigureAwait(false)) { return(await coursesRepo.GetPublishedCourseIdsAsync().ConfigureAwait(false)); } var coursesAsCourseAdmin = await courseRolesRepo.GetCoursesWhereUserIsInRoleAsync(userId, CourseRoleType.CourseAdmin).ConfigureAwait(false); var coursesWithCourseAccess = await coursesRepo.GetCoursesUserHasAccessTo(userId, CourseAccessType.ViewAllGroupMembers).ConfigureAwait(false); return(new HashSet <string>(coursesAsCourseAdmin).Concat(coursesWithCourseAccess).ToList()); }
public async Task <ActionResult <CoursesListResponse> > CoursesList([FromQuery] CourseRoleType?role = null) { if (role.HasValue && !IsAuthenticated) { return(Unauthorized()); } if (role == CourseRoleType.Student) { return(NotFound(new ErrorResponse("Role can not be student. Specify tester, instructor or courseAdmin"))); } var courses = await courseManager.GetCoursesAsync(coursesRepo).ConfigureAwait(false); if (role.HasValue && !await IsSystemAdministratorAsync().ConfigureAwait(false)) { var instructorCourseIds = await courseRolesRepo.GetCoursesWhereUserIsInRoleAsync(UserId, role.Value).ConfigureAwait(false); courses = courses.Where(c => instructorCourseIds.Contains(c.Id, StringComparer.InvariantCultureIgnoreCase)); } return(new CoursesListResponse { Courses = courses.Select( c => new ShortCourseInfo { Id = c.Id, Title = c.Title, ApiUrl = Url.Action(nameof(CourseInfo), "Courses", new { courseId = c.Id }) } ).ToList() }); }