protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, UpdateOfferingRequirement requirement, Offering offering) { if (context == null) { return; } var user = await _userUtils.GetUser(context.User); var InstructorRole = await _roleManager.FindByNameAsync(Globals.ROLE_INSTRUCTOR); if (context.User.IsInRole(Globals.ROLE_ADMIN)) { context.Succeed(requirement); } if (_ctDbContext.UserOfferings.Where((uo) => uo.ApplicationUserId == user.Id && uo.OfferingId == offering.Id && uo.IdentityRoleId == InstructorRole.Id).Any()) { context.Succeed(requirement); } if (user != null) { _ctDbContext.Entry(user).State = EntityState.Detached; } }
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, ReadOfferingRequirement requirement, Offering offering) { if (context == null) { return; } var user = await _userUtils.GetUser(context.User); if (offering != null && offering.AccessType == AccessTypes.Public) { context.Succeed(requirement); } else if (offering != null && offering.AccessType == AccessTypes.AuthenticatedOnly && user != null) { context.Succeed(requirement); } else if (offering != null && offering.AccessType == AccessTypes.UniversityOnly && user != null) { var universityId = await _ctDbContext.CourseOfferings.Where(co => co.OfferingId == offering.Id) .Select(c => c.Course.Department.UniversityId).FirstAsync(); if (user.UniversityId == universityId) { context.Succeed(requirement); } } else if (offering != null && offering.AccessType == AccessTypes.StudentsOnly && user != null && offering.OfferingUsers.Select(ou => ou.ApplicationUser).Contains(user)) { context.Succeed(requirement); } if (context.User.IsInRole(Globals.ROLE_ADMIN)) { context.Succeed(requirement); } if (user != null) { _ctDbContext.Entry(user).State = EntityState.Detached; } }
public async Task <IActionResult> PostWatchHistory(string mediaId, JObject json) { var media = await _context.Medias.FindAsync(mediaId); if (media == null || json == null) { return(BadRequest()); } var user = await _userUtils.GetUser(User); if (user != null) { var watchHistory = await _context.WatchHistories .Where(w => w.MediaId == mediaId && w.ApplicationUserId == user.Id) .FirstOrDefaultAsync(); if (watchHistory == null) { watchHistory = new WatchHistory { ApplicationUserId = user.Id, MediaId = mediaId, Json = json }; await _context.WatchHistories.AddAsync(watchHistory); } else { watchHistory.Json = json; _context.Entry(watchHistory).State = EntityState.Modified; } await _context.SaveChangesAsync(); } else { return(Unauthorized()); } return(NoContent()); }