Files
everything2/EveryThing/Pages/AdministrationUsers/Edit.cshtml.cs
David Štaleker 03b92525d7 Prvi commit
2023-05-12 09:00:07 +02:00

219 lines
7.2 KiB
C#
Raw Blame History

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using EveryThing.Data;
using EveryThing.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
namespace EveryThing.Pages.AdministrationUsers
{
[Authorize(Roles = "Administrator")]
public class EditModel : PageModel
{
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityApplicationUser> _userManager;
private readonly SignInManager<IdentityApplicationUser> _loginManager;
private readonly RoleManager<IdentityApplicationRole> _roleManager;
public EditModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> roleManager)
{
_context = context;
_userManager = userManager;
_loginManager = loginManager;
_roleManager = roleManager;
}
[BindProperty]
public InputModel Input { get; set; }
[BindProperty]
public List<InputRole> Roles { get; set; }
public async Task<IActionResult> OnGetAsync(int? idCompany, int? idUser)
{
if (idUser == null || idCompany == null)
{
return NotFound();
}
var user = await _userManager.Users.FirstAsync(x => x.IdCompanyFk == idCompany && x.Id == idUser);
if (user == null)
{
return NotFound();
}
var userRoles = await _userManager.GetRolesAsync(user);
ViewData["IdCompany"] = user.IdCompanyFk;
Input = new()
{
//UserName = user.UserName,
Name = user.Name,
LastName = user.Surname,
Email = user.Email,
Phone = user.PhoneNumber,
DateValidUntil = user.DateValidUntil,
Active = user.Active,
IdUser = user.Id
};
Roles = _roleManager.Roles.Select(x => new InputRole
{
RoleName = x.Name,
RoleDescription = x.Description,
InRole = userRoles.Contains(x.Name)
}).ToList();
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
var user = await _userManager.FindByIdAsync(Input.IdUser.ToString());
if (user == null)
{
ModelState.AddModelError("", "Napaka uporabnika");
return Page();
}
user.Name = Input.Name;
user.Surname = Input.LastName;
user.Email = Input.Email;
user.NormalizedEmail = Input.Email.ToUpper();
user.PhoneNumber = Input.Phone;
user.DateValidUntil = Input.DateValidUntil;
user.Active = Input.Active;
var result = await _userManager.UpdateAsync(user);
var userRoles = await _userManager.GetRolesAsync(user);
if (!result.Succeeded)
{
ModelState.AddModelError("", "Napaka uporabnika");
return Page();
}
foreach (var role in Roles)
{
if (role.InRole)
{
//Dodam ce se ni
if (!userRoles.Contains(role.RoleName))
await _userManager.AddToRoleAsync(user, role.RoleName);
}
else
{
//Ce je v roli ga odstranim
if (userRoles.Contains(role.RoleName))
await _userManager.RemoveFromRoleAsync(user, role.RoleName);
}
}
//if (!string.IsNullOrEmpty(Input.Password))
//{
// var token = await _userManager.GeneratePasswordResetTokenAsync(user);
// result = await _userManager.ResetPasswordAsync(user, token, Input.Password);
// if (!result.Succeeded)
// {
// ModelState.AddModelError("", "Napaka shranjevanje novega gesla");
// }
//}
return RedirectToPage("/AdministrationCompanies/Edit", new { id = user.IdCompanyFk});
//if (identityResult.Succeeded)
//{
// if (!_roleManager.RoleExistsAsync("NormalUser").Result)
// {
// IdentityApplicationRole normalUserRole = new IdentityApplicationRole
// {
// Name = "NormalUser",
// Description = "Splo<6C>ni uporabniki"
// };
// await _roleManager.CreateAsync(normalUserRole);
// }
// await _userManager.AddToRoleAsync(identityApplicationUser, "NormalUser");
// return RedirectToPage("/Administration/Users/Index");
//}
//else
//{
// ModelState.AddModelError("", string.Join(",", identityResult.Errors.Select(x => x.Description)));
//}
//ViewData["IdCompanyFk"] = new SelectList(_context.Companies, "IdCompany", "Title");
//await _userService.UpdateDisplayName(User, Input.DisplayName);
//return RedirectToPage("/User/Login");
}
public class InputModel
{
[Required]
public int IdUser { get; set; }
[Required]
[Display(Name = "Ime")]
public string Name { get; set; }
[Required]
[Display(Name = "Priimek")]
public string LastName { get; set; }
//[Required]
//[Display(Name = "Uporabniško ime")]
//public string UserName { get; set; }
//[Required]
//[DataType(DataType.Password)]
//[Display(Name = "Geslo")]
//public string Password { get; set; }
[Required]
[Display(Name = "E-pošta")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Display(Name = "Telefon")]
public string Phone { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Veljavnost uporabnika do")]
public DateTime DateValidUntil { get; set; }
[Required]
[Display(Name = "Aktiven")]
public bool Active { get; set; }
}
public class InputRole
{
public string RoleName { get; set; }
[Display(Name = "Pravica")]
public string RoleDescription{ get; set; }
[Display(Name = "Omogoči")]
public bool InRole { get; set; }
}
}
}