public async Task <IdentityResult> AddClaimAsync(int id, Claim claim, IList <Claim> existingClaims) { var role = await GetByIdAsync(id); var claims = await GetClaimsAsync(role.Name); ClaimChecker.CheckClaimStatus(claim, claims, existingClaims); return(await _roleManager.AddClaimAsync(role, claim)); }
public async Task <IdentityResult> AddClaimAsync(int id, Claim claim, IList <Claim> existingClaims) { var user = await GetByIdAsync(id); var claims = await GetUserClaimsAsync(user.Id); ClaimChecker.CheckClaimStatus(claim, claims, existingClaims); return(await _userManager.AddClaimAsync(user, claim)); }
static void Run() { var fields = new Dictionary <string, string[]>() { { "Age", new[] { "<18", "<40", ">40" } }, { "Gender", new[] { "M", "F" } }, { "Bought_Candy", new[] { "True", "False" } }, { "Bought_Bread", new[] { "True", "False" } }, { "Bought_Butter", new[] { "True", "False" } }, { "Bought_Soda", new[] { "True", "False" } }, { "Bought_Fruits", new[] { "True", "False" } }, { "Bought_CocaCola", new[] { "True", "False" } } }; var claims = new ClaimGenerator(4).GenerateClaims(fields, "Bought_CocaCola"); var randomData = DataGenerator.GetRandomData(fields).ToList(); foreach (var claim in claims) { ClaimChecker.Check(randomData, claim); } var confidentClaims = claims.OrderByDescending(c => c.Confidence).Take(10); Console.WriteLine("Confident Claims:"); foreach (var claim in confidentClaims) { Console.WriteLine(claim); } Console.WriteLine(); var supportedClaims = claims.OrderByDescending(c => c.Support).Take(10); Console.WriteLine("Supported Claims:"); foreach (var claim in supportedClaims) { Console.WriteLine(claim); } }
public async Task <IList <Claim> > AddClaimsAsync(int id, IList <Claim> claims, IList <Claim> existingClaims) { var addedClaims = new List <Claim>(); var role = await GetByIdAsync(id); var claimsInRole = await GetClaimsAsync(role.Name); foreach (var claim in claims) { Claim checkedClaim; bool claimCheckSuccess = false; try { //CheckClaimStaus metodu hata oluştuğunda Exception fırlatıyor. //Fakat bu metotta claim değerleri bir dizi halinde olduğu için //Başarısız işlemlerde hata üretilmesini değil, diziye eklenmemesini istiyoruz //Bu yüzden boş bir try catch bloğu içerisine alındı checkedClaim = ClaimChecker.CheckClaimStatus(claim, claimsInRole, existingClaims); claimCheckSuccess = true; } catch (KeyNotFoundException) { throw new KeyNotFoundException(nameof(Claim)); } catch { } if (claimCheckSuccess) { await _roleManager.AddClaimAsync(role, claim); addedClaims.Add(claim); } } return(addedClaims); }
public void OnGet(bool forcerefresh = false, string setimdbuserid = null) { string userId = ClaimChecker.UserId(User.Identity); if (userId == null) { return; } string connectionStringMovies = configuration.GetConnectionString("FxMoviesDb"); string connectionStringImdb = configuration.GetConnectionString("ImdbDb"); if (setimdbuserid == null) { using (var db = FxMoviesDbContextFactory.Create(connectionStringMovies)) { var user = db.Users.Find(userId); if (user != null) { ImdbUserId = user.ImdbUserId; } } } else if (setimdbuserid == "remove") { using (var db = FxMoviesDbContextFactory.Create(connectionStringMovies)) { db.Users.Remove(db.Users.Find(userId)); db.UserRatings.RemoveRange(db.UserRatings.Where(ur => ur.UserId == userId)); db.SaveChanges(); } ImdbUserId = null; } else { var match = Regex.Match(setimdbuserid, @"(ur\d+)"); if (match.Success) { var imdbuserid = match.Groups[1].Value; int expirationDays = configuration.GetValue("LoginCookieExpirationDays", 30); CookieOptions options = new CookieOptions(); options.Expires = DateTime.Now.AddDays(expirationDays); ImdbUserId = imdbuserid; forcerefresh = true; } else { ErrorMessage = string.Format("Er werd een ongeldige IMDb Gebruikers ID opgegeven: {0}.", setimdbuserid); } } using (var dbMovies = FxMoviesDbContextFactory.Create(connectionStringMovies)) using (var dbImdb = ImdbDbContextFactory.Create(connectionStringImdb)) if (ImdbUserId != null) { var user = dbMovies.Users.Find(userId); if (user == null) { user = new User(); user.UserId = userId; user.ImdbUserId = ImdbUserId; dbMovies.Users.Add(user); } if (forcerefresh) { user.RefreshRequestTime = DateTime.UtcNow; } RefreshRequestTime = user.RefreshRequestTime; LastRefreshRatingsTime = user.LastRefreshRatingsTime; LastRefreshRatingsResult = user.LastRefreshRatingsResult; LastRefreshSuccess = user.LastRefreshSuccess; WatchListLastRefreshTime = user.WatchListLastRefreshTime; WatchListLastRefreshRatingsResult = user.WatchListLastRefreshResult; WatchListLastRefreshSuccess = user.WatchListLastRefreshSuccess; user.LastUsageTime = DateTime.UtcNow; dbMovies.SaveChanges(); UserRatingCount = dbMovies.UserRatings.Where(ur => ur.UserId == userId).Count(); UserWatchListCount = dbMovies.UserWatchLists.Where(ur => ur.UserId == userId).Count(); var ratingLast = dbMovies.UserRatings .Where(ur => ur.UserId == userId) .OrderByDescending(ur => ur.RatingDate) .FirstOrDefault(); if (ratingLast != null) { RatingLastDate = ratingLast.RatingDate; RatingLastRating = ratingLast.Rating; RatingLastMovie = ratingLast.ImdbMovieId; var movie = dbImdb.Movies.Find(RatingLastMovie); if (movie != null) { RatingLastMovie = movie.PrimaryTitle; } } var watchListLast = dbMovies.UserWatchLists .Where(uw => uw.UserId == userId) .OrderByDescending(uw => uw.AddedDate) .FirstOrDefault(); if (watchListLast != null) { WatchListLastDate = watchListLast.AddedDate; WatchListLastMovie = watchListLast.ImdbMovieId; var movie = dbImdb.Movies.Find(WatchListLastMovie); if (movie != null) { WatchListLastMovie = movie.PrimaryTitle; } } } }
private void OnPostWatchlist() { string userId = ClaimChecker.UserId(User.Identity); int existingCount = 0; int newCount = 0; LastImportErrors.Clear(); string connectionString = configuration.GetConnectionString("FxMoviesDb"); using (var db = FxMoviesDbContextFactory.Create(connectionString)) foreach (var file in Request.Form.Files) { try { var engine = new FileHelperAsyncEngine <ImdbUserWatchlistRecord>(); var movieIdsInFile = new SortedSet <string>(); using (var reader = new StreamReader(file.OpenReadStream())) using (engine.BeginReadStream(reader)) foreach (var record in engine) { try { string _const = record.Const; movieIdsInFile.Add(_const); DateTime date = DateTime.ParseExact(record.Created, new string[] { "yyyy-MM-dd", "ddd MMM d HH:mm:ss yyyy", "ddd MMM dd HH:mm:ss yyyy" }, CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces); // Watchlist // Position,Const,Created,Modified,Description,Title,URL,Title Type,IMDb Rating,Runtime (mins),Year,Genres,Num Votes,Release Date,Directors var userWatchList = db.UserWatchLists.Find(userId, _const); if (userWatchList == null) { userWatchList = new UserWatchListItem(); userWatchList.UserId = userId; userWatchList.ImdbMovieId = _const; db.UserWatchLists.Add(userWatchList); newCount++; } else { existingCount++; } userWatchList.AddedDate = date; } catch (Exception x) { LastImportErrors.Add( Tuple.Create( $"Lijn {engine.LineNumber - 1} uit het watchlist bestand '{file.FileName}' kon niet verwerkt worden. " + "De meest voorkomende reden is een aanpassing aan het bestandsformaat door IMDb.", x.ToString(), "danger")); } } List <UserWatchListItem> itemsToRemove = db.UserWatchLists .Where(ur => ur.UserId == userId && !movieIdsInFile.Contains(ur.ImdbMovieId)) .ToList(); db.UserWatchLists.RemoveRange(itemsToRemove); LastImportErrors.Add( Tuple.Create( $"Het watchlist bestand '{file.FileName}' werd ingelezen. " + $"{newCount} nieuwe en {existingCount} bestaande films. {itemsToRemove.Count} films verwijderd.", (string)null, "success")); } catch (Exception x) { LastImportErrors.Add( Tuple.Create( $"Het watchlist bestand '{file.FileName}' kon niet ingelezen worden.\n" + "De meest voorkomende reden is het omwisselen van Ratings en Watchlist bestanden, of een aanpassing aan " + "het bestandsformaat door IMDb.", x.ToString(), "danger")); } db.SaveChanges(); } }
public void OnGet(int?m = null, int?typeMask = null, decimal?minrating = null, bool?notyetrated = null, Cert cert = Cert.all, int?movieeventid = null, string setimdbid = null, int?maxdays = null) { string userId = ClaimChecker.UserId(User.Identity); var now = DateTime.Now; // Get the connection string string connectionString = configuration.GetConnectionString("FxMoviesDb"); string connectionStringImdb = configuration.GetConnectionString("ImdbDb"); AdsInterval = configuration.GetValue("AdsInterval", AdsInterval); FilterMaxDaysDefault = configuration.GetValue("DefaultMaxDays", FilterMaxDaysDefault); FilterMaxDays = FilterMaxDaysDefault; FilterTypeMask = FilterTypeMaskDefault; EditImdbLinks = ClaimChecker.Has(User.Identity, ClaimEditImdbLinks); if (typeMask.HasValue) { FilterTypeMask = typeMask.Value; } if (minrating.HasValue) { FilterMinRating = minrating.Value; } // Only allow setting more days when authenticated if (maxdays.HasValue && User.Identity.IsAuthenticated) { FilterMaxDays = maxdays.Value; } FilterNotYetRated = notyetrated; FilterCert = cert & Cert.all2; if (FilterCert == Cert.all2) { FilterCert = Cert.all; } using (var db = FxMoviesDbContextFactory.Create(connectionString)) { if (EditImdbLinks && movieeventid.HasValue && !string.IsNullOrEmpty(setimdbid)) { bool overwrite = false; var match = Regex.Match(setimdbid, @"(tt\d+)"); if (match.Success) { setimdbid = match.Groups[0].Value; overwrite = true; } else if (setimdbid.Equals("remove", StringComparison.InvariantCultureIgnoreCase)) { setimdbid = null; overwrite = true; } if (overwrite) { var movieEvent = db.MovieEvents.Find(movieeventid.Value); if (movieEvent != null) { if (setimdbid != null) { using (var dbImdb = ImdbDbContextFactory.Create(connectionStringImdb)) { var imdbMovie = dbImdb.Movies.Find(setimdbid); if (imdbMovie != null) { movieEvent.ImdbRating = imdbMovie.Rating; movieEvent.ImdbVotes = imdbMovie.Votes; } } } movieEvent.ImdbId = setimdbid; db.SaveChanges(); } } } if (userId != null) { var user = db.Users.Find(userId); if (user != null) { RefreshRequestTime = user.RefreshRequestTime; LastRefreshRatingsTime = user.LastRefreshRatingsTime; LastRefreshSuccess = user.LastRefreshSuccess; user.Usages++; user.LastUsageTime = DateTime.UtcNow; ImdbUserId = user.ImdbUserId; db.SaveChanges(); } } if (m.HasValue) { if (m.Value == -2) { throw new Exception("Sentry test exception"); } else { MovieEvent = db.MovieEvents.Find(m.Value); if (MovieEvent != null) { int days = (int)(MovieEvent.StartTime.Date - DateTime.Now.Date).TotalDays; if (FilterMaxDays < days) { FilterMaxDays = days; } } } } Count = db.MovieEvents.Count(); CountTypeFilm = db.MovieEvents.Where(me => me.Type == 1).Count(); CountTypeShort = db.MovieEvents.Where(me => me.Type == 2).Count(); CountTypeSerie = db.MovieEvents.Where(me => me.Type == 3).Count(); CountMinRating5 = db.MovieEvents.Where(me => me.ImdbRating >= 50).Count(); CountMinRating6 = db.MovieEvents.Where(me => me.ImdbRating >= 60).Count(); CountMinRating7 = db.MovieEvents.Where(me => me.ImdbRating >= 70).Count(); CountMinRating8 = db.MovieEvents.Where(me => me.ImdbRating >= 80).Count(); CountMinRating9 = db.MovieEvents.Where(me => me.ImdbRating >= 90).Count(); CountNotOnImdb = db.MovieEvents.Where(me => string.IsNullOrEmpty(me.ImdbId)).Count(); CountNotRatedOnImdb = db.MovieEvents.Where(me => me.ImdbRating == null).Count(); CountCertNone = db.MovieEvents.Where(me => string.IsNullOrEmpty(me.Certification)).Count(); CountCertG = db.MovieEvents.Where(me => me.Certification == "US:G").Count(); CountCertPG = db.MovieEvents.Where(me => me.Certification == "US:PG").Count(); CountCertPG13 = db.MovieEvents.Where(me => me.Certification == "US:PG-13").Count(); CountCertR = db.MovieEvents.Where(me => me.Certification == "US:R").Count(); CountCertNC17 = db.MovieEvents.Where(me => me.Certification == "US:NC-17").Count(); CountCertOther = Count - CountCertNone - CountCertG - CountCertPG - CountCertPG13 - CountCertR - CountCertNC17; CountRated = db.MovieEvents.Where( me => db.UserRatings.Where(ur => ur.UserId == userId).Any(ur => ur.ImdbMovieId == me.ImdbId)).Count(); CountNotYetRated = Count - CountRated; Count3days = db.MovieEvents.Where(me => me.StartTime.Date <= now.Date.AddDays(3)).Count(); Count5days = db.MovieEvents.Where(me => me.StartTime.Date <= now.Date.AddDays(5)).Count(); Count8days = db.MovieEvents.Where(me => me.StartTime.Date <= now.Date.AddDays(8)).Count(); Records = ( from me in db.MovieEvents.Include(me => me.Channel) join ur in db.UserRatings.Where(ur => ur.UserId == userId) on me.ImdbId equals ur.ImdbMovieId into urGroup from ur in urGroup.DefaultIfEmpty(null) join uw in db.UserWatchLists.Where(ur => ur.UserId == userId) on me.ImdbId equals uw.ImdbMovieId into uwGroup from uw in uwGroup.DefaultIfEmpty(null) where (FilterMaxDays == 0 || me.StartTime.Date <= now.Date.AddDays(FilterMaxDays)) && (me.EndTime >= now && me.StartTime >= now.AddMinutes(-30)) && ( ((FilterTypeMask & 1) == 1 && me.Type == 1) || ((FilterTypeMask & 2) == 2 && me.Type == 2) || ((FilterTypeMask & 4) == 4 && me.Type == 3) ) && (!FilterMinRating.HasValue || (FilterMinRating.Value == NO_IMDB_ID && string.IsNullOrEmpty(me.ImdbId)) || (FilterMinRating.Value == NO_IMDB_RATING && me.ImdbRating == null) || (FilterMinRating.Value >= 0.0m && (me.ImdbRating >= FilterMinRating.Value * 10))) && (!FilterNotYetRated.HasValue || FilterNotYetRated.Value == (ur == null)) && (FilterCert == Cert.all || (ParseCertification(me.Certification) & FilterCert) != 0) select new Record() { MovieEvent = me, UserRating = ur, UserWatchListItem = uw } ).ToList(); // MovieEvents = db.MovieEvents.Include(m => m.Channel) // .Where(m => !MinRating.HasValue || m.ImdbRating >= MinRating.Value * 10) // .ToList(); } }