public async Task <ActionResult> Profile(string userId) { var user = usersRepo.FindUserById(userId); if (user == null) { return(HttpNotFound()); } if (!systemAccessesRepo.HasSystemAccess(User.Identity.GetUserId(), SystemAccessType.ViewAllProfiles) && !User.IsSystemAdministrator()) { return(HttpNotFound()); } var logins = await userManager.GetLoginsAsync(userId); var userCoursesIds = visitsRepo.GetUserCourses(user.Id).Select(s => s.ToLower()); var userCourses = courseManager.GetCourses().Where(c => userCoursesIds.Contains(c.Id.ToLower())).OrderBy(c => c.Title).ToList(); var allCourses = courseManager.GetCourses().ToDictionary(c => c.Id, c => c, StringComparer.InvariantCultureIgnoreCase); var tempCourseIds = tempCoursesRepo.GetTempCourses() .Select(c => c.CourseId) .Where(c => allCourses.ContainsKey(c)) .ToHashSet(); var certificates = certificatesRepo.GetUserCertificates(user.Id).OrderBy(c => allCourses.GetOrDefault(c.Template.CourseId)?.Title ?? "<курс удалён>").ToList(); var courseGroups = userCourses.ToDictionary(c => c.Id, c => groupsRepo.GetUserGroupsNamesAsString(c.Id, userId, User, actual: true, archived: false, maxCount: 10)); var courseArchivedGroups = userCourses.ToDictionary(c => c.Id, c => groupsRepo.GetUserGroupsNamesAsString(c.Id, userId, User, actual: false, archived: true, maxCount: 10)); var coursesWithRoles = (await userRolesRepo.GetUserRolesHistory(userId)).Select(x => x.CourseId.ToLower()).Distinct().ToList(); var coursesWithAccess = (await coursesRepo.GetUserAccessHistory(userId)).Select(x => x.CourseId.ToLower()).Distinct().ToList(); return(View(new ProfileModel { User = user, Logins = logins, UserCourses = userCourses, CourseGroups = courseGroups, CourseArchivedGroups = courseArchivedGroups, Certificates = certificates, AllCourses = allCourses, TempCoursesIds = tempCourseIds, CoursesWithRoles = coursesWithRoles, CoursesWithAccess = coursesWithAccess })); }
/* Instructor can view student if he is course admin or if student is member of one of accessable for instructor group */ public async Task <bool> CanInstructorViewStudentAsync(ClaimsPrincipal instructor, string studentId) { if (instructor.HasAccess(CourseRole.CourseAdmin)) { return(true); } var coursesIds = courseManager.GetCourses().Select(c => c.Id).ToList(); var groups = GetAvailableForUserGroups(coursesIds, instructor); var members = await GetGroupsMembersAsync(groups.Select(g => g.Id).ToList()); return(members.Select(m => m.UserId).Contains(studentId)); }
/* Instructor can view student if he is a course admin or if student is member of one of accessible for instructor group */ public async Task <bool> CanInstructorViewStudentAsync(string instructorId, string studentId) { if (await courseRolesRepo.HasUserAccessToAnyCourseAsync(instructorId, CourseRoleType.CourseAdmin).ConfigureAwait(false)) { return(true); } var coursesIds = courseManager.GetCourses().Select(c => c.Id).ToList(); var groups = await GetAvailableForUserGroupsAsync(coursesIds, instructorId, false).ConfigureAwait(false); var members = await groupMembersRepo.GetGroupsMembersAsync(groups.Select(g => g.Id).ToList()).ConfigureAwait(false); return(members.Select(m => m.UserId).Contains(studentId)); }
public async Task <ActionResult> Courses(string courseId = null, string courseTitle = null) { var isSystemAdministrator = User.IsSystemAdministrator(); var userId = User.Identity.GetUserId(); var courses = courseManager.GetCourses(); // Неопубликованные курсы не покажем тем, кто не имеет роли в них. if (!isSystemAdministrator) { var visibleCourses = unitsRepo.GetVisibleCourses(); var coursesInWhichUserHasAnyRole = userRolesRepo.GetCoursesWhereUserIsInRole(userId, CourseRole.Tester); var coursesWhereIAmStudent = groupsRepo.GetUserGroups(userId) .Select(g => g.CourseId) .Distinct(StringComparer.OrdinalIgnoreCase) .Where(c => visibleCourses.Contains(c)).ToList(); courses = courses.Where(c => coursesInWhichUserHasAnyRole.Contains(c.Id, StringComparer.OrdinalIgnoreCase) || coursesWhereIAmStudent.Contains(c.Id, StringComparer.OrdinalIgnoreCase)); } var incorrectChars = new string(CourseManager.GetInvalidCharacters().OrderBy(c => c).Where(c => 32 <= c).ToArray()); if (isSystemAdministrator) { courses = courses.OrderBy(course => course.Id, StringComparer.InvariantCultureIgnoreCase); } else { courses = courses.OrderBy(course => course.Title, StringComparer.InvariantCultureIgnoreCase); } var tempCourses = tempCoursesRepo.GetTempCourses().Select(c => c.CourseId).ToHashSet(); var model = new CourseListViewModel { Courses = courses .Select(course => new CourseViewModel { Id = course.Id, Title = course.Title, LastWriteTime = courseManager.GetLastWriteTime(course.Id), IsTemp = tempCourses.Contains(course.Id) }) .ToList(), LastTryCourseId = courseId, LastTryCourseTitle = courseTitle, InvalidCharacters = incorrectChars }; return(View(model)); }