Prvi commit
This commit is contained in:
107
EveryThing/Pages/AdministrationCompanies/Setup.cshtml.cs
Normal file
107
EveryThing/Pages/AdministrationCompanies/Setup.cshtml.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using EveryThing.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace EveryThing.Pages.AdministrationCompanies
|
||||
{
|
||||
[Authorize(Roles = "Administrator")]
|
||||
public class SetupModel : PageModel
|
||||
{
|
||||
private readonly UserManager<IdentityApplicationUser> _userManager;
|
||||
private readonly SignInManager<IdentityApplicationUser> _signInManager;
|
||||
private readonly RoleManager<IdentityApplicationRole> _roleManager;
|
||||
private readonly ILogger<SetupModel> _logger;
|
||||
|
||||
public SetupModel(UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> signInManager, ILogger<SetupModel> logger, RoleManager<IdentityApplicationRole> roleManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_signInManager = signInManager;
|
||||
_logger = logger;
|
||||
_roleManager = roleManager;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnGetAsync()
|
||||
{
|
||||
//TODO osnovno podjetje?
|
||||
var rolesDefinitions = new List<(string RoleName, string RoleDescription)>
|
||||
{
|
||||
("TransportThingUser", "TransporThing uporabniki"),
|
||||
("ProjecThingUser", "ProjecThing uporabniki"),
|
||||
("InvoicingUser", "Fakturiranje uporabniki"),
|
||||
("Administrator", "Administratorji"),
|
||||
};
|
||||
|
||||
foreach (var roleDefinition in rolesDefinitions)
|
||||
{
|
||||
if (!_roleManager.RoleExistsAsync(roleDefinition.RoleName).Result)
|
||||
{
|
||||
var role = new IdentityApplicationRole
|
||||
{
|
||||
Name = roleDefinition.RoleName,
|
||||
Description = roleDefinition.RoleDescription
|
||||
};
|
||||
|
||||
_roleManager.CreateAsync(role).Wait();
|
||||
}
|
||||
}
|
||||
|
||||
if (_userManager.FindByNameAsync("admin") == null)
|
||||
{
|
||||
IdentityApplicationUser identityApplicationUser = new IdentityApplicationUser
|
||||
{
|
||||
Name = "Master",
|
||||
Surname = "Admin",
|
||||
UserName = "admin",
|
||||
NormalizedUserName = "admin",
|
||||
Email = "admin@domain.com",
|
||||
NormalizedEmail = "admin@domain.com",
|
||||
EmailConfirmed = true,
|
||||
DateCreated = DateTime.Now,
|
||||
DateValidUntil = DateTime.MaxValue,
|
||||
PhoneNumber = "123456789",
|
||||
PhoneNumberConfirmed = true,
|
||||
Active = true,
|
||||
IdCompanyFk = 1
|
||||
};
|
||||
|
||||
var result = await _userManager.CreateAsync(identityApplicationUser, "Master#Admin22!");
|
||||
|
||||
if (result.Succeeded)
|
||||
{
|
||||
_userManager.AddToRoleAsync(identityApplicationUser, "Administrator").Wait();
|
||||
//return RedirectToPage("/Administration/Users/Index");
|
||||
}
|
||||
else
|
||||
{
|
||||
//ModelState.AddModelError("", string.Join(",", identityResult.Errors.Select(x => x.Description)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return RedirectToPage("/User/Login");
|
||||
}
|
||||
|
||||
//public async Task<IActionResult> OnPostAsync(string returnUrl = null)
|
||||
//{
|
||||
// var user = await _userManager.GetUserAsync(User);
|
||||
|
||||
// await _signInManager.SignOutAsync();
|
||||
|
||||
// _logger.LogInformation($"Logout: {user.Name} {user.Surname} - {user.Company.Title}");
|
||||
|
||||
// if (returnUrl != null)
|
||||
// {
|
||||
// return LocalRedirect(returnUrl);
|
||||
// }
|
||||
|
||||
// return RedirectToPage();
|
||||
//}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user