Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Huba2004 committed Mar 5, 2024
1 parent b8105ed commit f4bb5ff
Show file tree
Hide file tree
Showing 63 changed files with 2,362 additions and 260 deletions.
Binary file added .vs/Identity/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1,021 changes: 1,021 additions & 0 deletions .vs/Identity/config/applicationhost.config

Large diffs are not rendered by default.

Binary file added .vs/Identity/v17/.futdcache.v2
Binary file not shown.
Binary file added .vs/Identity/v17/.suo
Binary file not shown.
Binary file added .vs/Identity/v17/.wsuo
Binary file not shown.
139 changes: 139 additions & 0 deletions .vs/Identity/v17/DocumentLayout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"Version": 1,
"WorkspaceRootPath": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|c:\\users\\szalk\\documents\\github\\identity\\views\\userroles\\manage.cshtml||{40D31677-CBC0-4297-A9EF-89D907823A98}",
"RelativeMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|solutionrelative:views\\userroles\\manage.cshtml||{40D31677-CBC0-4297-A9EF-89D907823A98}"
},
{
"AbsoluteMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|c:\\users\\szalk\\documents\\github\\identity\\controllers\\userrolescontroller.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|solutionrelative:controllers\\userrolescontroller.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|c:\\users\\szalk\\documents\\github\\identity\\models\\userrolesviewmodel.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|solutionrelative:models\\userrolesviewmodel.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|c:\\users\\szalk\\documents\\github\\identity\\views\\userroles\\index.cshtml||{40D31677-CBC0-4297-A9EF-89D907823A98}",
"RelativeMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|solutionrelative:views\\userroles\\index.cshtml||{40D31677-CBC0-4297-A9EF-89D907823A98}"
},
{
"AbsoluteMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|c:\\users\\szalk\\documents\\github\\identity\\views\\rolemanager\\index.cshtml||{40D31677-CBC0-4297-A9EF-89D907823A98}",
"RelativeMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|solutionrelative:views\\rolemanager\\index.cshtml||{40D31677-CBC0-4297-A9EF-89D907823A98}"
},
{
"AbsoluteMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|c:\\users\\szalk\\documents\\github\\identity\\models\\manageuserrolesviewmodel.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|solutionrelative:models\\manageuserrolesviewmodel.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|c:\\users\\szalk\\documents\\github\\identity\\controllers\\rolemanagercontroller.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{0B954BD0-5237-409B-8B38-09B4FB91B11F}|Identity.csproj|solutionrelative:controllers\\rolemanagercontroller.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
}
],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 200,
"SelectedChildIndex": 3,
"Children": [
{
"$type": "Document",
"DocumentIndex": 2,
"Title": "UserRolesViewModel.cs",
"DocumentMoniker": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Models\\UserRolesViewModel.cs",
"RelativeDocumentMoniker": "Models\\UserRolesViewModel.cs",
"ToolTip": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Models\\UserRolesViewModel.cs",
"RelativeToolTip": "Models\\UserRolesViewModel.cs",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-03-05T11:24:13.203Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 6,
"Title": "RoleManagerController.cs",
"DocumentMoniker": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Controllers\\RoleManagerController.cs",
"RelativeDocumentMoniker": "Controllers\\RoleManagerController.cs",
"ToolTip": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Controllers\\RoleManagerController.cs",
"RelativeToolTip": "Controllers\\RoleManagerController.cs",
"ViewState": "AQIAAAMAAAAAAAAAAAAAAAAAAAAAAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-03-05T11:19:24.994Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 1,
"Title": "UserRolesController.cs",
"DocumentMoniker": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Controllers\\UserRolesController.cs",
"RelativeDocumentMoniker": "Controllers\\UserRolesController.cs",
"ToolTip": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Controllers\\UserRolesController.cs",
"RelativeToolTip": "Controllers\\UserRolesController.cs",
"ViewState": "AQIAAEAAAAAAAAAAAAAIwFkAAAAJAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-03-05T11:19:23.717Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "Manage.cshtml",
"DocumentMoniker": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Views\\UserRoles\\Manage.cshtml",
"RelativeDocumentMoniker": "Views\\UserRoles\\Manage.cshtml",
"ToolTip": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Views\\UserRoles\\Manage.cshtml",
"RelativeToolTip": "Views\\UserRoles\\Manage.cshtml",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAoAAAAOAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000759|",
"WhenOpened": "2024-03-05T11:18:51.876Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 3,
"Title": "Index.cshtml",
"DocumentMoniker": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Views\\UserRoles\\Index.cshtml",
"RelativeDocumentMoniker": "Views\\UserRoles\\Index.cshtml",
"ToolTip": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Views\\UserRoles\\Index.cshtml",
"RelativeToolTip": "Views\\UserRoles\\Index.cshtml",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAkAAAAMAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000759|",
"WhenOpened": "2024-03-05T11:18:44.661Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 4,
"Title": "Index.cshtml",
"DocumentMoniker": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Views\\RoleManager\\Index.cshtml",
"RelativeDocumentMoniker": "Views\\RoleManager\\Index.cshtml",
"ToolTip": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Views\\RoleManager\\Index.cshtml",
"RelativeToolTip": "Views\\RoleManager\\Index.cshtml",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000759|",
"WhenOpened": "2024-03-05T11:22:50.059Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 5,
"Title": "ManageUserRolesViewModel.cs",
"DocumentMoniker": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Models\\ManageUserRolesViewModel.cs",
"RelativeDocumentMoniker": "Models\\ManageUserRolesViewModel.cs",
"ToolTip": "C:\\Users\\szalk\\Documents\\GitHub\\Identity\\Models\\ManageUserRolesViewModel.cs",
"RelativeToolTip": "Models\\ManageUserRolesViewModel.cs",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-03-05T11:20:25.019Z",
"EditorCaption": ""
}
]
}
]
}
]
}
Binary file added .vs/ProjectEvaluation/identity.metadata.v7.bin
Binary file not shown.
Binary file added .vs/ProjectEvaluation/identity.projects.v7.bin
Binary file not shown.
3 changes: 3 additions & 0 deletions .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": null
}
7 changes: 7 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\Program.cs",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/slnx.sqlite
Binary file not shown.
4 changes: 2 additions & 2 deletions Areas/Identity/Pages/Account/Register.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ public async Task<IActionResult> OnPostAsync(string returnUrl = null)
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");

var userId = await _userManager.GetUserIdAsync(user);
await _userManager.AddToRoleAsync(user, Enums.Roles.Basic.ToString());
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var userId = await _userManager.GetUserIdAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
Expand Down
43 changes: 43 additions & 0 deletions ContextSeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Identity.Models;
using Microsoft.AspNetCore.Identity;

namespace Identity
{
public class ContextSeed
{
public static async Task SeedRolesAsync(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
//Seed Roles
await roleManager.CreateAsync(new IdentityRole(Enums.Roles.SuperAdmin.ToString()));
await roleManager.CreateAsync(new IdentityRole(Enums.Roles.Admin.ToString()));
await roleManager.CreateAsync(new IdentityRole(Enums.Roles.Moderator.ToString()));
await roleManager.CreateAsync(new IdentityRole(Enums.Roles.Basic.ToString()));
}
public static async Task SeedSuperAdminAsync(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
//Seed Default User
var defaultUser = new ApplicationUser
{
UserName = "superadmin",
Email = "superadmin@gmail.com",
FirstName = "Mukesh",
LastName = "Murugan",
EmailConfirmed = true,
PhoneNumberConfirmed = true
};
if (userManager.Users.All(u => u.Id != defaultUser.Id))
{
var user = await userManager.FindByEmailAsync(defaultUser.Email);
if (user == null)
{
await userManager.CreateAsync(defaultUser, "123Pa$$word.");
await userManager.AddToRoleAsync(defaultUser, Enums.Roles.Basic.ToString());
await userManager.AddToRoleAsync(defaultUser, Enums.Roles.Moderator.ToString());
await userManager.AddToRoleAsync(defaultUser, Enums.Roles.Admin.ToString());
await userManager.AddToRoleAsync(defaultUser, Enums.Roles.SuperAdmin.ToString());
}

}
}
}
}
30 changes: 30 additions & 0 deletions Controllers/RoleManagerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Identity.Controllers
{
public class RoleManagerController:Controller
{
private readonly RoleManager<IdentityRole> _roleManager;
public RoleManagerController(RoleManager<IdentityRole> roleManager)
{
_roleManager = roleManager;
}
public async Task<IActionResult> Index()
{
var roles = await _roleManager.Roles.ToListAsync();
return View(roles);
}
[HttpPost]
public async Task<IActionResult> AddRole(string roleName)
{
if (roleName != null)
{
await _roleManager.CreateAsync(new IdentityRole(roleName.Trim()));
}
return RedirectToAction("Index");
}
}
}
92 changes: 92 additions & 0 deletions Controllers/UserRolesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Identity.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Identity.Controllers
{
public class UserRolesController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;

public UserRolesController(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
_roleManager = roleManager;
_userManager = userManager;
}
public async Task<IActionResult> Index()
{
var users = await _userManager.Users.ToListAsync();
var userRolesViewModel = new List<UserRolesViewModel>();
foreach (ApplicationUser user in users)
{
var thisViewModel = new UserRolesViewModel();
thisViewModel.UserId = user.Id;
thisViewModel.Email = user.Email;
thisViewModel.FirstName = user.FirstName;
thisViewModel.LastName = user.LastName;
thisViewModel.Roles = await GetUserRoles(user);
userRolesViewModel.Add(thisViewModel);
}
return View(userRolesViewModel);
}
private async Task<List<string>> GetUserRoles(ApplicationUser user)
{
return new List<string>(await _userManager.GetRolesAsync(user));
}
public async Task<IActionResult> Manage(string userId)
{
ViewBag.userId = userId;
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
ViewBag.ErrorMessage = $"User with Id = {userId} cannot be found";
return View("NotFound");
}
ViewBag.UserName = user.UserName;
var model = new List<ManageUserRolesViewModel>();
foreach (var role in _roleManager.Roles)
{
var userRolesViewModel = new ManageUserRolesViewModel
{
RoleId = role.Id,
RoleName = role.Name
};
if (await _userManager.IsInRoleAsync(user, role.Name))
{
userRolesViewModel.Selected = true;
}
else
{
userRolesViewModel.Selected = false;
}
model.Add(userRolesViewModel);
}
return View(model);
}
[HttpPost]
public async Task<IActionResult> Manage(List<ManageUserRolesViewModel> model, string userId)
{
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return View();
}
var roles = await _userManager.GetRolesAsync(user);
var result = await _userManager.RemoveFromRolesAsync(user, roles);
if (!result.Succeeded)
{
ModelState.AddModelError("", "Cannot remove user existing roles");
return View(model);
}
result = await _userManager.AddToRolesAsync(user, model.Where(x => x.Selected).Select(y => y.RoleName));
if (!result.Succeeded)
{
ModelState.AddModelError("", "Cannot add selected roles to user");
return View(model);
}
return RedirectToAction("Index");
}
}
}
11 changes: 11 additions & 0 deletions Enums/Roles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Identity.Enums
{
public enum Roles
{
SuperAdmin,
Admin,
Moderator,
Basic
}

}
4 changes: 4 additions & 0 deletions Identity.csproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
<_SelectedScaffolderID>IdentityScaffolder</_SelectedScaffolderID>
<_SelectedScaffolderCategoryPath>root/Identity</_SelectedScaffolderCategoryPath>
<WebStackScaffolding_ViewDialogWidth>800</WebStackScaffolding_ViewDialogWidth>
<View_SelectedScaffolderID>RazorViewEmptyScaffolder</View_SelectedScaffolderID>
<View_SelectedScaffolderCategoryPath>root/Common/MVC/View</View_SelectedScaffolderCategoryPath>
<Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Common/MVC/Controller</Controller_SelectedScaffolderCategoryPath>
</PropertyGroup>
</Project>
25 changes: 25 additions & 0 deletions Identity.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34622.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Identity", "Identity.csproj", "{0B954BD0-5237-409B-8B38-09B4FB91B11F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0B954BD0-5237-409B-8B38-09B4FB91B11F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B954BD0-5237-409B-8B38-09B4FB91B11F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B954BD0-5237-409B-8B38-09B4FB91B11F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B954BD0-5237-409B-8B38-09B4FB91B11F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C3C6415E-4A8B-48C7-9B02-0C1411BDB6CA}
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions Models/ManageUserRolesViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Identity.Models
{
public class ManageUserRolesViewModel
{
public string RoleId { get; set; }
public string RoleName { get; set; }
public bool Selected { get; set; }
}
}
Loading

0 comments on commit f4bb5ff

Please sign in to comment.