public async Task <ActionResult <ResultSINnerPut> > PutSIN([FromRoute] Guid id, IFormFile uploadedFile) { ResultSINnerPut res; ApplicationUser user = null; SINner dbsinner = null; try { var sin = await _context.SINners.Include(a => a.SINnerMetaData.Visibility.UserRights).FirstOrDefaultAsync(a => a.Id == id); if (sin == null) { var e = new ArgumentException("Sinner with Id " + id + " not found!"); res = new ResultSINnerPut(e); return(NotFound(res)); } user = await _signInManager.UserManager.FindByNameAsync(User.Identity.Name); dbsinner = await CheckIfUpdateSINnerFile(id, user); if (dbsinner == null) { var e = new NoUserRightException("You may not edit this (existing) sinner with id " + id + "."); res = new ResultSINnerPut(e); return(Conflict(res)); } sin.GoogleDriveFileId = dbsinner.GoogleDriveFileId; if (user == null) { var e = new NoUserRightException("User not found!"); res = new ResultSINnerPut(e); return(NotFound(res)); } sin.DownloadUrl = Startup.GDrive.StoreXmlInCloud(sin, uploadedFile); dbsinner.DownloadUrl = sin.DownloadUrl; //_context.Entry(dbsinner).CurrentValues.SetValues(sin); try { var tc = new Microsoft.ApplicationInsights.TelemetryClient(); Microsoft.ApplicationInsights.DataContracts.EventTelemetry telemetry = new Microsoft.ApplicationInsights.DataContracts.EventTelemetry("PutStoreXmlInCloud"); telemetry.Properties.Add("User", user.Email); telemetry.Properties.Add("SINnerId", sin.Id.ToString()); telemetry.Properties.Add("FileName", uploadedFile.FileName?.ToString()); telemetry.Metrics.Add("FileSize", uploadedFile.Length); tc.TrackEvent(telemetry); } catch (Exception e) { _logger.LogError(e.ToString()); } try { int x = await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException e) { res = new ResultSINnerPut(e); return(Conflict(res)); } res = new ResultSINnerPut(sin); return(Ok(res)); } catch (NoUserRightException e) { res = new ResultSINnerPut(e); return(BadRequest(res)); } catch (Exception e) { try { var tc = new Microsoft.ApplicationInsights.TelemetryClient(); Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry telemetry = new Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry(e); telemetry.Properties.Add("User", user?.Email); telemetry.Properties.Add("SINnerId", dbsinner?.Id?.ToString()); telemetry.Properties.Add("FileName", uploadedFile.FileName?.ToString()); telemetry.Metrics.Add("FileSize", uploadedFile.Length); tc.TrackException(telemetry); } catch (Exception ex) { _logger.LogError(ex.ToString()); } res = new ResultSINnerPut(e); return(BadRequest(res)); } }
private async Task <ActionResult <ResultSinnerPostSIN> > PostSINnerInternal(UploadInfoObject uploadInfo) { ResultSinnerPostSIN res; _logger.LogTrace("Post SINnerInternalt: " + uploadInfo + "."); ApplicationUser user = null; SINner sinner = null; try { if (!ModelState.IsValid) { var errors = ModelState.Select(x => x.Value.Errors) .Where(y => y.Count > 0) .ToList(); string msg = "ModelState is invalid: "; foreach (var err in errors) { foreach (var singleerr in err) { msg += Environment.NewLine + "\t" + singleerr.ToString(); } } var e = new HubException(msg); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } if (uploadInfo.UploadDateTime == null) { uploadInfo.UploadDateTime = DateTime.Now; } if (uploadInfo.Client != null) { if (!UploadClientExists(uploadInfo.Client.Id)) { _context.UploadClients.Add(uploadInfo.Client); } else { _context.UploadClients.Attach(uploadInfo.Client); _context.Entry(uploadInfo.Client).State = EntityState.Modified; _context.Entry(uploadInfo.Client).CurrentValues.SetValues(uploadInfo.Client); } } var returncode = HttpStatusCode.OK; user = await _signInManager.UserManager.FindByNameAsync(User.Identity.Name); foreach (var tempsinner in uploadInfo.SINners) { sinner = tempsinner; if (sinner.Id.ToString() == "string") { sinner.Id = Guid.Empty; } if (String.IsNullOrEmpty(sinner.MyExtendedAttributes.JsonSummary)) { var e = new ArgumentException("sinner " + sinner.Id + ": JsonSummary == null"); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } if (sinner.LastChange == null) { var e = new ArgumentException("Sinner " + sinner.Id + ": LastChange not set!"); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } if ((sinner.SINnerMetaData.Visibility.Id == null) || (sinner.SINnerMetaData.Visibility.Id == Guid.Empty)) { sinner.SINnerMetaData.Visibility.Id = Guid.NewGuid(); } if ((sinner.MyExtendedAttributes.Id == null) || (sinner.MyExtendedAttributes.Id == Guid.Empty)) { sinner.MyExtendedAttributes.Id = Guid.NewGuid(); } var oldsinner = (from a in _context.SINners .Include(a => a.MyExtendedAttributes) .Include(a => a.SINnerMetaData) .Include(a => a.SINnerMetaData.Visibility) .Include(a => a.SINnerMetaData.Visibility.UserRights) .Include(b => b.MyGroup) where a.Id == sinner.Id select a).FirstOrDefault(); if (oldsinner != null) { var canedit = await CheckIfUpdateSINnerFile(oldsinner.Id.Value, user); if (canedit == null) { string msg = "SINner " + sinner.Id + " is not editable for user " + user.Email + "."; var e = new NoUserRightException(msg); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } var olduserrights = oldsinner.SINnerMetaData.Visibility.UserRights.ToList(); oldsinner.SINnerMetaData.Visibility.UserRights.Clear(); _context.UserRights.RemoveRange(olduserrights); //check if ANY visibility-data was uploaded if (sinner.SINnerMetaData.Visibility.UserRights.Any()) { bool userfound = false; foreach (var ur in sinner.SINnerMetaData.Visibility.UserRights) { if (ur.EMail.ToLowerInvariant() == user.Email.ToLowerInvariant()) { ur.CanEdit = true; userfound = true; } ur.Id = Guid.NewGuid(); ur.SINnerId = sinner.Id; _context.UserRights.Add(ur); } if (!userfound) { SINnerUserRight ownUser = new SINnerUserRight(); ownUser.Id = Guid.NewGuid(); ownUser.SINnerId = sinner.Id; ownUser.CanEdit = true; ownUser.EMail = user.Email; sinner.SINnerMetaData.Visibility.UserRights.Add(ownUser); _context.UserRights.Add(ownUser); } } else { //no userrights where uploaded. sinner.SINnerMetaData.Visibility.UserRights = olduserrights; } } else { var ownuserfound = false; var list = sinner.SINnerMetaData.Visibility.UserRights.ToList(); foreach (var ur in list) { ur.SINnerId = sinner.Id; if (ur.EMail.ToLowerInvariant() == "*****@*****.**".ToLowerInvariant()) { sinner.SINnerMetaData.Visibility.UserRights.Remove(ur); } if (ur.EMail.ToLowerInvariant() == user.Email.ToLowerInvariant()) { ownuserfound = true; } } if (!ownuserfound) { SINnerUserRight ownright = new SINnerUserRight(); ownright.CanEdit = true; ownright.EMail = user.Email; ownright.SINnerId = sinner.Id; ownright.Id = Guid.NewGuid(); sinner.SINnerMetaData.Visibility.UserRights.Add(ownright); } } foreach (var tag in sinner.SINnerMetaData.Tags) { tag.SetSinnerIdRecursive(sinner.Id); } sinner.UploadClientId = uploadInfo.Client.Id; SINner dbsinner = await CheckIfUpdateSINnerFile(sinner.Id.Value, user, true); SINnerGroup oldgroup = null; if (dbsinner != null) { oldgroup = dbsinner.MyGroup; //_context.SINners.Attach(dbsinner); if (String.IsNullOrEmpty(sinner.GoogleDriveFileId)) { sinner.GoogleDriveFileId = dbsinner.GoogleDriveFileId; } if (String.IsNullOrEmpty(sinner.DownloadUrl)) { sinner.DownloadUrl = dbsinner.DownloadUrl; } var alltags = await _context.Tags.Where(a => a.SINnerId == dbsinner.Id).Select(a => a.Id).ToListAsync(); foreach (var id in alltags) { var tag = from a in _context.Tags where a.Id == id select a; if (tag.Any()) { _context.Tags.Remove(tag.FirstOrDefault()); } } _context.UserRights.RemoveRange(dbsinner.SINnerMetaData.Visibility.UserRights); _context.SINnerVisibility.Remove(dbsinner.SINnerMetaData.Visibility); _context.SINnerExtendedMetaData.Remove(dbsinner.MyExtendedAttributes); _context.SINnerMetaData.Remove(dbsinner.SINnerMetaData); _context.SINners.Remove(dbsinner); dbsinner.SINnerMetaData.Visibility.UserRights.Clear(); dbsinner.SINnerMetaData.Visibility.UserRights = null; dbsinner.SINnerMetaData.Visibility = null; dbsinner.SINnerMetaData.Tags = null; dbsinner.SINnerMetaData = null; dbsinner.MyExtendedAttributes = null; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { foreach (var entry in ex.Entries) { if (entry.Entity is SINner || entry.Entity is Tag || entry.Entity is SINnerExtended || entry.Entity is SINnerGroup || entry.Entity is SINnerUserRight || entry.Entity is SINnerMetaData) { try { Utils.DbUpdateConcurrencyExceptionHandler(entry, _logger); } catch (Exception e) { res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } } else { var e = new NotSupportedException( "(Codepoint 1) Don't know how to handle concurrency conflicts for " + entry.Metadata.Name); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } } } catch (DbUpdateException ex) { res = new ResultSinnerPostSIN(ex); return(BadRequest(res)); } await _context.SINners.AddAsync(sinner); string msg = "Sinner " + sinner.Id + " updated: " + _context.Entry(dbsinner).State.ToString(); msg += Environment.NewLine + Environment.NewLine + "LastChange: " + dbsinner.LastChange; _logger.LogError(msg); List <Tag> taglist = sinner.SINnerMetaData.Tags; UpdateEntityEntries(taglist); } else { returncode = HttpStatusCode.Created; sinner.MyGroup = null; _context.SINners.Add(sinner); } try { await _context.SaveChangesAsync(); if (oldgroup != null) { var roles = await _userManager.GetRolesAsync(user); await SINnerGroupController.PutSiNerInGroupInternal(oldgroup.Id.Value, sinner.Id.Value, user, _context, _logger, oldgroup.PasswordHash, roles); } } catch (DbUpdateConcurrencyException ex) { foreach (var entry in ex.Entries) { if (entry.Entity is SINner || entry.Entity is Tag) { try { Utils.DbUpdateConcurrencyExceptionHandler(entry, _logger); } catch (Exception e) { res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } } else { var e = new NotSupportedException( "Don't know how to handle concurrency conflicts for " + entry.Metadata.Name); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } } } catch (DbUpdateException ex) { res = new ResultSinnerPostSIN(ex); return(BadRequest(res)); } catch (Exception e) { try { var tc = new Microsoft.ApplicationInsights.TelemetryClient(); Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry telemetry = new Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry(e); telemetry.Properties.Add("User", user?.Email); telemetry.Properties.Add("SINnerId", sinner?.Id?.ToString()); tc.TrackException(telemetry); } catch (Exception ex) { _logger.LogError(ex.ToString()); } res = new ResultSinnerPostSIN(e); return(Conflict(res)); } } List <Guid> myids = (from a in uploadInfo.SINners select a.Id.Value).ToList(); List <SINner> sinlist = new List <SINner>(); foreach (var id in myids) { var sin = from a in _context.SINners where a.Id == id select a; if (sin.Any()) { sinlist.Add(sin.FirstOrDefault()); } } res = new ResultSinnerPostSIN(sinlist); switch (returncode) { case HttpStatusCode.OK: return(Accepted(res)); case HttpStatusCode.Created: return(Created("SINnerPostSIN", res)); default: return(Ok(res)); } } catch (Exception e) { try { var tc = new Microsoft.ApplicationInsights.TelemetryClient(); Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry telemetry = new Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry(e); telemetry.Properties.Add("User", user?.Email); telemetry.Properties.Add("SINnerId", sinner?.Id?.ToString()); tc.TrackException(telemetry); } catch (Exception ex) { _logger.LogError(ex.ToString()); } res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } }
public async Task <ActionResult <ResultSinnerGetSINById> > GetSINById([FromRoute] Guid id) { ResultSinnerGetSINById res; try { ApplicationUser user = null; if (!String.IsNullOrEmpty(User?.Identity?.Name)) { user = await _signInManager.UserManager.FindByNameAsync(User.Identity.Name); } var sin = await _context.SINners .Include(a => a.MyExtendedAttributes) .Include(a => a.SINnerMetaData.Visibility.UserRights) .Include(a => a.MyGroup) .Include(b => b.MyGroup.MySettings) .Where(a => a.Id == id).Take(1) .FirstOrDefaultAsync(a => a.Id == id); res = new ResultSinnerGetSINById(sin); if (sin == null) { return(NotFound(res)); } bool oktoDownload = false; if ((!oktoDownload) && (sin.SINnerMetaData.Visibility.IsPublic == true)) { oktoDownload = true; } if ((!oktoDownload) && (sin.MyGroup != null && sin.MyGroup.IsPublic == true)) { oktoDownload = true; } if ((!oktoDownload) && (user != null && sin.SINnerMetaData.Visibility.UserRights.Any(a => a.EMail.ToLowerInvariant() == user.Email.ToLowerInvariant()))) { oktoDownload = true; } if (!oktoDownload) { var e = new ArgumentException("User " + user?.UserName + " or public is not allowed to download " + id.ToString()); res = new ResultSinnerGetSINById(e); return(BadRequest(res)); } res = new ResultSinnerGetSINById(sin); if (sin.SINnerMetaData.Visibility.IsPublic == true) { return(Ok(res)); } var list = (from a in sin.SINnerMetaData.Visibility.UserRights where a.EMail.ToUpperInvariant() == user.NormalizedEmail select a); if (list.Any()) { return(Ok(res)); } var e1 = new NoUserRightException("SINner is not viewable for public or groupmembers."); res = new ResultSinnerGetSINById(e1); return(BadRequest(res)); } catch (Exception e) { res = new ResultSinnerGetSINById(e); return(BadRequest(res)); } }