public IEnumerable <dynamic> Index() { var user = _userGetter.GetCurrentUser(u => u.Include(t => t.Teacher.Messages)); return(user.Teacher.Messages.OrderByDescending(m => m.Date).Select(m => new { id = m.Id, date = m.Date.ToString("G"), title = m.Title, read = m.Read, })); }
public async Task <IActionResult> GetUserInfo() { if (User.IsInRole(Roles.Scanner)) { return(Json(new { loggedIn = true, role = "scanner", })); } var user = _userGetter.GetCurrentUser(u => u.Include(u2 => u2.Teacher).ThenInclude(t => t.Messages)); #if DEBUG var devVersion = _hostingEnv.IsDevelopment(); #else var devVersion = false; #endif if (user == null) { return(Json(new { loggedIn = false, devVersion = devVersion, })); } return(Json(new { loggedIn = true, devVersion = devVersion, teacherId = user.Teacher.Id, username = $"{user.Teacher.Name} {user.Teacher.Surname}", messageCount = user.Teacher.Messages.Count(m => !m.Read), role = (await _userManager.GetRolesAsync(user)).FirstOrDefault()?.ToLower() ?? "user", })); }
public async Task <IActionResult> Update(int id, [FromBody] NewTeacherModel model) { var teacher = _context.Teachers.FirstOrDefault(t => t.Id == id); if (teacher == null) { return(BadRequest("Taki nauczyciel nie istnieje.")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (_context.Teachers.Any(t => t.Id != teacher.Id && t.Name.ToLower() == model.Name.ToLower() && t.Surname.ToLower() == model.Surname.ToLower())) { return(BadRequest("Istnieje już nauczyciel z takim imieniem i nazwiskiem.")); } if (_context.Teachers.Any(t => t.Id != teacher.Id && t.BarCode.ToLower() == model.BarCode.ToLower())) { return(BadRequest("Istnieje już nauczyciel z takim kodem kreskowym.")); } if (_context.Items.Any(i => i.ShortId.ToLower() == model.BarCode.ToLower())) { return(BadRequest("Istnieje już przedmiot z takim kodem kreskowym.")); } bool accountCreated = false; string generatedPassword = null; if (model.EnableAccount) { if (String.IsNullOrEmpty(model.Username)) { return(BadRequest("Nie podano nazwy użytkownika.")); } if (model.Username.Length < 2) { return(BadRequest("Nazwa użytkownika musi mieć przynajmniej 2 znaki.")); } if (model.Role != "administrator" && model.Role != "moderator" && model.Role != "user") { return(BadRequest("Rola nie jest prawidłowa.")); } var user = _context.Users.Include(u => u.Teacher).FirstOrDefault(u => u.Teacher.Id == teacher.Id); // TODO: Check if username or email already exists. // Create new user account if (user == null) { user = new ApplicationUser() { Teacher = teacher, UserName = model.Username.Trim(), Email = model.Email.Trim(), }; // TODO: Better password generator. generatedPassword = new Random().Next(10000000, 99999999).ToString(); await _userManager.CreateAsync(user, generatedPassword); _messageService.SendMessage(teacher, "AccountEnabled", user.UserName, generatedPassword, $"{Request.Scheme}://{Request.Host}/"); accountCreated = true; } // Update user account else { user.UserName = model.Username.Trim(); user.Email = model.Email.Trim(); } var currentRole = (await _userManager.GetRolesAsync(user)).FirstOrDefault()?.ToLower() ?? "user"; if (currentRole != model.Role) { var currentUser = _userGetter.GetCurrentUser(); if (currentUser.Id == user.Id) { return(BadRequest("Nie możesz zmienić swojej roli.")); } if (currentRole != "user") { await _userManager.RemoveFromRoleAsync(user, currentRole); } if (model.Role == "administrator") { await _userManager.AddToRoleAsync(user, Roles.Administrator); } else if (model.Role == "moderator") { await _userManager.AddToRoleAsync(user, Roles.Moderator); } } } else { var user = _context.Users.Include(u => u.Teacher).FirstOrDefault(u => u.Teacher.Id == teacher.Id); var currentUser = _userGetter.GetCurrentUser(); if (currentUser.Id == user?.Id) { return(BadRequest("Nie możesz zablokować logowania na własnym koncie.")); } // Remove user account if exists if (user != null) { await _userManager.DeleteAsync(user); } } var oldBarcode = teacher.BarCode; teacher.Name = model.Name; teacher.Surname = model.Surname; teacher.BarCode = model.BarCode?.Trim().ToUpper() ?? ""; // Send a message that the barcode has been changed if (!string.IsNullOrEmpty(teacher.BarCode) && oldBarcode.ToUpper().Trim() != teacher.BarCode) { _messageService.SendMessage(teacher, "NewBarcode", model.BarCode?.Trim().ToUpper(), $"{Request.Scheme}://{Request.Host}/api/BarCode/Generate?text={model.BarCode.Trim().ToUpper()}"); } _context.SaveChanges(); if (accountCreated) { return(Json(new { generatedPassword = generatedPassword, })); } return(Ok()); }