Esempio n. 1
0
        protected async Task <bool> CanUserSeeNotApprovedCommentsAsync(string userId, string courseId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                return(false);
            }

            var hasCourseAccessForCommentEditing = await coursesRepo.HasCourseAccess(userId, courseId, CourseAccessType.EditPinAndRemoveComments).ConfigureAwait(false);

            var isCourseAdmin = await courseRolesRepo.HasUserAccessToCourse(userId, courseId, CourseRoleType.CourseAdmin).ConfigureAwait(false);

            return(isCourseAdmin || hasCourseAccessForCommentEditing);
        }
Esempio n. 2
0
        public async Task <bool> CanUserSeeAllCourseGroupsAsync(string userId, string courseId, bool?isSystemAdministrator = null)
        {
            if (isSystemAdministrator == true || isSystemAdministrator == null && await usersRepo.IsSystemAdministrator(userId))
            {
                return(true);
            }
            var canViewAllGroupMembersGlobal = await systemAccessesRepo.HasSystemAccessAsync(userId, SystemAccessType.ViewAllGroupMembers).ConfigureAwait(false);

            var canViewAllGroupMembersInCourse = await coursesRepo.HasCourseAccess(userId, courseId, CourseAccessType.ViewAllGroupMembers).ConfigureAwait(false);

            var isCourseAdmin = await courseRolesRepo.HasUserAccessToCourse(userId, courseId, CourseRoleType.CourseAdmin).ConfigureAwait(false);

            return(isCourseAdmin || canViewAllGroupMembersGlobal || canViewAllGroupMembersInCourse);
        }
Esempio n. 3
0
        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, CourseAccessRequirement requirement)
        {
            /* Get MVC context. See https://docs.microsoft.com/en-US/aspnet/core/security/authorization/policies#accessing-mvc-request-context-in-handlers */
            if (!(context.Resource is AuthorizationFilterContext mvcContext))
            {
                log.Error("Can't get MVC context in CourseRoleAuthenticationHandler");
                context.Fail();
                return;
            }

            var courseId = GetCourseIdFromRequestAsync(mvcContext);

            if (string.IsNullOrEmpty(courseId))
            {
                context.Fail();
                return;
            }

            if (!context.User.Identity.IsAuthenticated)
            {
                context.Fail();
                return;
            }

            var userId = context.User.GetUserId();
            var user   = await usersRepo.FindUserById(userId).ConfigureAwait(false);

            if (user == null)
            {
                context.Fail();
                return;
            }

            if (usersRepo.IsSystemAdministrator(user))
            {
                context.Succeed(requirement);
                return;
            }

            var isCourseAdmin = await courseRolesRepo.HasUserAccessToCourse(userId, courseId, CourseRoleType.CourseAdmin).ConfigureAwait(false);

            if (isCourseAdmin || await coursesRepo.HasCourseAccess(userId, courseId, requirement.CourseAccessType).ConfigureAwait(false))
            {
                context.Succeed(requirement);
            }
            else
            {
                context.Fail();
            }
        }