Files
everything/EveryThing/Pages/AdministrationUsers/Create.cshtml.cs
David Štaleker db0cc8d3de prvi
2025-07-18 05:33:16 +02:00

129 lines
4.5 KiB
C#

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;
namespace EveryThing.Pages.AdministrationUsers
{
[Authorize(Roles = "Administrator")]
public class CreateModel : PageModel
{
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityApplicationUser> _userManager;
private readonly SignInManager<IdentityApplicationUser> _loginManager;
private readonly RoleManager<IdentityApplicationRole> _roleManager;
public CreateModel(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; }
public IActionResult OnGet(int idCompany)
{
ViewData["IdCompanyFk"] = new SelectList(_context.CodeTableCompanies, "IdCompany", "Title");
ViewData["IdCompany"] = idCompany;
Input = new InputModel()
{
IdCompanyFk = idCompany
};
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
if (ModelState.IsValid)
{
IdentityApplicationUser identityApplicationUser = new IdentityApplicationUser
{
Name = Input.Name,
Surname = Input.LastName,
UserName = Input.UserName,
NormalizedUserName = Input.UserName.ToLower(),
Email = Input.Email,
NormalizedEmail = Input.Email.ToLower(),
EmailConfirmed = true,
DateCreated = DateTime.Now,
DateValidUntil = Input.DateValidUntil,
PhoneNumber = Input.Phone,
PhoneNumberConfirmed = true,
Active = true,
IdCompanyFk = Input.IdCompanyFk
};
IdentityResult identityResult = await _userManager.CreateAsync(identityApplicationUser, Input.Password);
if (identityResult.Succeeded)
{
//await _userManager.AddToRoleAsync(identityApplicationUser, "NormalUser");
return RedirectToPage("/AdministrationUsers/Edit", new { idCompany = identityApplicationUser.IdCompanyFk, idUser = identityApplicationUser.Id });
//return RedirectToPage("/Administration/Users/Index");
}
else
{
ModelState.AddModelError("", string.Join(",", identityResult.Errors.Select(x => x.Description)));
}
ViewData["IdCompanyFk"] = new SelectList(_context.CodeTableCompanies, "IdCompany", "Title");
}
return Page();
}
public class InputModel
{
public int IdCompanyFk { 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; }
}
}
}