public async Task <IActionResult> Create([Bind("Teamid,TeamName,Record")] Teams teams) { if (ModelState.IsValid) { _context.Add(teams); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(teams)); }
internal static async Task SendAsync(EmailDto dto) { using (MailMessage mail = new MailMessage()) { mail.From = new MailAddress(Utility.GetConfigValue("notifications:defaultFromAddress")); mail.To.Add(dto.To[0]); mail.Subject = dto.Subject; mail.Body = dto.Body; mail.IsBodyHtml = false; mail.Attachments.Add(new Attachment(dto.AttachmentS3Url)); using (SmtpClient smtp = new SmtpClient(mail.From.ToString(), Convert.ToInt32(Utility.GetConfigValue("notifications:port")))) { smtp.UseDefaultCredentials = false; smtp.Host = Utility.GetConfigValue("notifications:smtpserver"); smtp.Credentials = new NetworkCredential(Utility.GetConfigValue("notifications:defaultFromAddress"), Utility.GetConfigValue("notifications:password")); smtp.EnableSsl = true; try { smtp.Send(mail); } catch (Exception ex) { LoggerService.LogException("", "Email Service", "SendAsync", ex, true); } } } using (TeamDbContext dbContext = new TeamDbContext()) { dbContext.Emails.Add(dto); await dbContext.SaveChangesAsync().ConfigureAwait(false); } }
internal static async Task LogAsync(TeamHttpContext teamContext, TeamDbContext dbContext, string parentType, string parentId, string name, object data) { dbContext.Events.Add(new AppEvent { ParentType = parentType, ParentId = parentId, Name = name, Data = JsonConvert.SerializeObject(data) }); await dbContext.SaveChangesAsync().ConfigureAwait(false); }
private static async Task CreateSessionAsync(string userId, string sessionId) { using (TeamDbContext dbContext = new TeamDbContext()) { dbContext.UserSessions.Add(new UserSession { UserId = userId, SessionId = sessionId, LastApiCall = DateTime.Now }); await dbContext.SaveChangesAsync().ConfigureAwait(false); } }
public static async Task NotifyErrorsAsync(TeamHttpContext teamContext) { using (TeamDbContext dbContext = new TeamDbContext()) { List <AppLogEntry> errors = dbContext.AppLogEntries.Where(e => e.Level == "ERROR" && !e.AdminNotified).Take(25).ToList(); HashSet <string> sentErrors = new HashSet <string>(); StringBuilder sb = new StringBuilder(); foreach (AppLogEntry item in errors) { // don't notify for the same error string messageKey = $"{item.User}:{item.ExceptionMessage}"; // skip certain errors // check that we have not notified this user/error combination. if (!Utility.IsDevelopment && !item.ExceptionMessage.Contains("The timeout period elapsed prior to completion of the", StringComparison.InvariantCulture) && !item.ExceptionMessage.Contains("Unauthorized, Please login to continue", StringComparison.InvariantCulture) && !sentErrors.Contains(messageKey)) { sb.Append(Utility.GetFormattedLabelValue("Id", item.Id)); sb.Append(Utility.GetFormattedLabelValue("User", item.User)); sb.Append(Utility.GetFormattedLabelValue("Thread", item.RequestId)); sb.Append(Utility.GetFormattedLabelValue("Time", item.Date.ToString(CultureInfo.InvariantCulture))); sb.Append(Utility.GetFormattedLabelValue("Location", item.Message)); sb.Append(Utility.GetFormattedLabelValue("Message", item.ExceptionMessage)); sb.Append("<br/><br/>"); sentErrors.Add(messageKey); } item.AdminNotified = true; } string body = sb.ToString(); if (body.Length != 0) { string adminMailAddress = Utility.GetConfigValue("notifications:sysAdminEmailAddress"); await EmailService.SendAsync(adminMailAddress, "IA:Application Errors", body, string.Empty).ConfigureAwait(false); } if (errors.Count > 0) { await dbContext.SaveChangesAsync().ConfigureAwait(false); } } }
internal static async Task <ResponseBase> DeleteWidgetAsync(TeamHttpContext teamHttpContext, int widgetId) { try { using (TeamDbContext dbContext = new TeamDbContext()) { var widget = await dbContext.UserDashboards.FindAsync(widgetId).ConfigureAwait(false); if (widget != null) { dbContext.UserDashboards.Remove(widget); await dbContext.SaveChangesAsync().ConfigureAwait(false); } } return(GetResponse(teamHttpContext)); } catch (Exception ex) { return(GetResponse(teamHttpContext, HttpStatusCode.BadRequest, ex.Message.ToString())); } }
public static async Task <ResponseBase> LogoutAsync(TeamHttpContext teamHttpContext, string LogoutuserId) { if (teamHttpContext == null) { throw new ArgumentNullException(nameof(teamHttpContext)); } string userId = LogoutuserId.ToUpper(CultureInfo.InvariantCulture); using (TeamDbContext dbContext = new TeamDbContext()) { List <UserSession> activeSessions = dbContext.UserSessions.Where(e => e.UserId == userId && !e.Deleted && e.Active).ToList(); foreach (UserSession item in activeSessions) { item.Active = false; await CacheService.RemoveSessionAsync(teamHttpContext, item.SessionId).ConfigureAwait(false); } await dbContext.SaveChangesAsync().ConfigureAwait(false); } await CacheService.RemoveUserAsync(teamHttpContext, LogoutuserId).ConfigureAwait(false); return(GetResponse(teamHttpContext)); }
public static async Task <ResponseBase <LoginResponseModel> > LoginAsync(TeamHttpContext teamHttpContext, LoginModel loginModel) { if (teamHttpContext == null) { throw new ArgumentNullException(nameof(teamHttpContext)); } if (loginModel == null) { throw new ArgumentNullException(nameof(loginModel)); } LoginResponseModel loginResponse = new LoginResponseModel { Status = LoginStatus.InValid }; User user = new User(); user = await ValidatePassword(teamHttpContext, loginModel).ConfigureAwait(false); if (user != null) { using (TeamDbContext dbContext = new TeamDbContext()) { if (user.UserId != null) { teamHttpContext.SetValue(Constants.CONTEXT_USER_ID, user.UserId); loginResponse.Status = LoginStatus.LoggedIn; } else { loginResponse.Status = LoginStatus.NoAccess; } if (loginResponse.Status == LoginStatus.LoggedIn) { user.LastLoggedIn = DateTime.Now; await dbContext.SaveChangesAsync().ConfigureAwait(false); } } } if (loginResponse.Status == LoginStatus.LoggedIn) { string sessionId = Guid.NewGuid().ToString(); await CreateSessionAsync(user.UserId, sessionId).ConfigureAwait(false); await CacheService.StoreSessionAsync(teamHttpContext, sessionId).ConfigureAwait(false); await CacheService.StoreUserAsync(teamHttpContext, user).ConfigureAwait(false); int validity = Utility.IsProduction ? 1 : 7; long validUntil = DateTime.UtcNow.AddDays(validity).GetUnixTimeStamp(); loginResponse.Token = Jwt.CreateToken(user, sessionId, validUntil); loginResponse.Email = user.Email; loginResponse.UserId = user.UserId; loginResponse.Type = user.UserType; loginResponse.SessionId = sessionId; loginResponse.ExpiryTimestamp = validUntil; loginResponse.IsAddRights = user.IsAdd; loginResponse.IsEditRights = user.IsEdit; loginResponse.IsDeleteRights = user.IsDelete; return(GetTypedResponse <LoginResponseModel>(teamHttpContext, loginResponse)); } else { return(GetUnauthorizedResponse <LoginResponseModel>(teamHttpContext, loginResponse)); } }
public async Task Seed() { var members1 = new Member[] { new Member { FirstName = "Person-1", LastName = "LastName-1", MemberId = Guid.NewGuid() }, new Member { FirstName = "Person-2", LastName = "LastName-2", MemberId = Guid.NewGuid() }, new Member { FirstName = "Person-3", LastName = "LastName-3", MemberId = Guid.NewGuid() }, }; var members2 = new Member[] { new Member { FirstName = "Person-4", LastName = "LastName-4", MemberId = Guid.NewGuid() }, new Member { FirstName = "Person-5", LastName = "LastName-5", MemberId = Guid.NewGuid() }, new Member { FirstName = "Person-6", LastName = "LastName-6", MemberId = Guid.NewGuid() }, }; var teams = new Team[] { new Team { Name = "Team-1", TeamId = Guid.NewGuid(), Members = members1 }, new Team { Name = "Team-2", TeamId = Guid.NewGuid(), Members = members2 } }; await _context.Teams.AddRangeAsync(teams); await _context.Members.AddRangeAsync(members1); await _context.Members.AddRangeAsync(members2); await _context.SaveChangesAsync(); }