public async Task OnGetAsync(string serviceOrganization, string searchString) { var userName = UserManager.GetUserId(User); // Use LINQ to get list of genres. IQueryable <string> serviceQuery = from m in Context.ServiceForm orderby m.ServiceDescription select m.ServiceDescription; var serviceForms = from s in Context.ServiceForm select s; var currentUserId = UserManager.GetUserId(User); CHSYesWebAppUser webAppUser = await UserManager.GetUserAsync(User); var isAuthorized = await UserManager.IsInRoleAsync(webAppUser, Constants.ServiceFormManagersRole) || await UserManager.IsInRoleAsync(webAppUser, Constants.ServiceFormAdministratorsRole); if (!isAuthorized) { var chartQuery = from p in Context.ServiceForm where p.OwnerID == currentUserId group p by p.ServiceDate.Year into g select new HoursByYear() { ServiceDate = g.Key, TotalHours = g.Sum(p => p.HourOfService).ToString() }; ChartHoursByYear = await chartQuery.ToListAsync(); } else { serviceForms = serviceForms.Where(x => x.OwnerID == currentUserId); var chartQuery = from p in Context.ServiceForm group p by p.ServiceDate.Year into g select new HoursByYear() { ServiceDate = g.Key, TotalHours = g.Sum(p => p.HourOfService).ToString() }; ChartHoursByYear = await chartQuery.ToListAsync(); } }
private static async Task <string> EnsureUser(IServiceProvider serviceProvider, string testUserPw, string UserName) { var userManager = serviceProvider.GetService <UserManager <CHSYesWebAppUser> >(); var user = await userManager.FindByNameAsync(UserName); if (user == null) { user = new CHSYesWebAppUser { UserName = UserName }; await userManager.CreateAsync(user, testUserPw); } return(user.Id); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new CHSYesWebAppUser { UserName = Input.Email, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, ClassOf = Input.ClassOf, ThirdPeriodTeacher = Input.ThirdPeriodTeacher }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } ServiceForm = await Context.ServiceForm.FirstOrDefaultAsync(m => m.ServiceFormId == id); if (ServiceForm == null) { return(NotFound()); } var currentUserId = UserManager.GetUserId(User); CHSYesWebAppUser webAppUser = await UserManager.GetUserAsync(User); var isAuthorized = await UserManager.IsInRoleAsync(webAppUser, Constants.ServiceFormManagersRole) || await UserManager.IsInRoleAsync(webAppUser, Constants.ServiceFormAdministratorsRole); //var isAuthorized = User.IsInRole(Constants.ServiceFormManagersRole) || // User.IsInRole(Constants.ServiceFormAdministratorsRole); if (!isAuthorized && currentUserId != ServiceForm.OwnerID && ServiceForm.Status != ContactStatus.Approved) { return(new ChallengeResult()); } return(Page()); }
/* public async Task OnGetAsync() * { * ServiceForm = await Context.ServiceForm.ToListAsync(); * } * * public async Task OnGetAsync(string searchString) * { * var serviceForms = from s in Context.ServiceForm * select s; * * if (!String.IsNullOrEmpty(searchString)) * { * serviceForms = serviceForms.Where(s => s.OrganizationName.Contains(searchString)); * * } * ServiceForm = await serviceForms.ToListAsync(); * } */ public async Task OnGetAsync(string serviceOrganization, string searchString) { var userName = UserManager.GetUserId(User); // Use LINQ to get list of genres. IQueryable <string> serviceQuery = from m in Context.ServiceForm orderby m.ServiceDescription select m.ServiceDescription; /* var query = from m in Context.ServiceForm * join u in Context.Users * on m.OwnerID equals u.Id * orderby m.ServiceDescription * select new { m.ServiceDescription, u.LastName};*/ var serviceForms = from s in Context.ServiceForm select s; if (!String.IsNullOrEmpty(searchString)) { serviceForms = serviceForms.Where(s => s.OrganizationName.Contains(searchString)); } if (!String.IsNullOrEmpty(serviceOrganization)) { serviceForms = serviceForms.Where(x => x.ServiceDescription == serviceOrganization); } var currentUserId = UserManager.GetUserId(User); CHSYesWebAppUser webAppUser = await UserManager.GetUserAsync(User); var isAuthorized = await UserManager.IsInRoleAsync(webAppUser, Constants.ServiceFormManagersRole) || await UserManager.IsInRoleAsync(webAppUser, Constants.ServiceFormAdministratorsRole); /* * var IsAdmin = await UserManager.IsInRoleAsync(webAppUser, Constants.ServiceFormAdministratorsRole); * var IsManager = await UserManager.IsInRoleAsync(webAppUser, Constants.ServiceFormManagersRole); */ /* * HttpContext.Session.SetString("IsAdmin", IsAdmin.ToString()); * HttpContext.Session.SetString("IsManager", IsManager.ToString()); */ // Only approved contacts are shown UNLESS you're authorized to see them // or you are the owner. if (!isAuthorized) { if (!String.IsNullOrEmpty(searchString)) { serviceForms = serviceForms.Where(s => s.OrganizationName.Contains(searchString) && s.Status == ContactStatus.Approved || s.OwnerID == currentUserId ); } else if (!String.IsNullOrEmpty(serviceOrganization)) { serviceForms = serviceForms.Where(x => x.ServiceDescription == serviceOrganization && x.Status == ContactStatus.Approved || x.OwnerID == currentUserId ); } else { serviceForms = serviceForms.Where(x => x.OwnerID == currentUserId ); } } Organizaiton = new SelectList(await serviceQuery.Distinct().ToListAsync()); ServiceForm = await serviceForms.ToListAsync(); }