Exemplo n.º 1
0
        public async Task <bool> HasUserAccessToCourse(string userId, string courseId, CourseRoleType minCourseRoleType)
        {
            var user = await usersRepo.FindUserById(userId).ConfigureAwait(false);

            if (usersRepo.IsSystemAdministrator(user))
            {
                return(true);
            }

            return((await Internal_GetActualUserRoles(userId)).Any(r => string.Equals(r.CourseId, courseId, StringComparison.OrdinalIgnoreCase) && r.Role <= minCourseRoleType));
        }
Exemplo n.º 2
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();
            }
        }
Exemplo n.º 3
0
        protected async Task <bool> IsSystemAdministratorAsync()
        {
            var user = await usersRepo.FindUserById(UserId).ConfigureAwait(false);

            return(usersRepo.IsSystemAdministrator(user));
        }