예제 #1
0
        private async Task <List <string> > GetCoursesWhereUserCanSeeAllGroupsAsync(string userId)
        {
            if (await usersRepo.IsSystemAdministrator(userId) || await systemAccessesRepo.HasSystemAccessAsync(userId, SystemAccessType.ViewAllGroupMembers).ConfigureAwait(false))
            {
                return(await coursesRepo.GetPublishedCourseIds().ConfigureAwait(false));
            }

            var coursesAsCourseAdmin = await courseRolesRepo.GetCoursesWhereUserIsInRole(userId, CourseRoleType.CourseAdmin).ConfigureAwait(false);

            var coursesWithCourseAccess = await coursesRepo.GetCoursesUserHasAccessTo(userId, CourseAccessType.ViewAllGroupMembers).ConfigureAwait(false);

            return(new HashSet <string>(coursesAsCourseAdmin).Concat(coursesWithCourseAccess).ToList());
        }
예제 #2
0
        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.GetCoursesWhereUserIsInRole(UserId, role.Value).ConfigureAwait(false);

                courses = courses.Where(c => courseIdsAsRole.Contains(c.Id, StringComparer.InvariantCultureIgnoreCase));
            }

            // Неопубликованные курсы не покажем тем, кто не имеет роли в них.
            if (!isSystemAdministrator)
            {
                var visibleCourses = await unitsRepo.GetVisibleCourses();

                var coursesInWhichUserHasAnyRole = await courseRolesRepo.GetCoursesWhereUserIsInRole(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.GetCoursesWhereUserIsInStrictRole(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(StringComparer.OrdinalIgnoreCase);
            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,
                }