forked from robson-paproski/AspNetCore.Identity.PostgreSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit models created and startup.cs modified
- Loading branch information
1 parent
06dced8
commit dd4b880
Showing
8 changed files
with
144 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,34 @@ | ||
using System; | ||
namespace WebExample.Code | ||
{ | ||
public class ModalSize | ||
public enum ModalSize | ||
{ | ||
public ModalSize() | ||
Small, | ||
Large, | ||
Medium | ||
} | ||
|
||
public class BootstrapModel | ||
{ | ||
public string ID { get; set; } | ||
public string AreaLabeledId { get; set; } | ||
public ModalSize Size { get; set; } | ||
public string Message { get; set; } | ||
public string ModalSizeClass | ||
{ | ||
get | ||
{ | ||
switch (this.Size) | ||
{ | ||
case ModalSize.Small: | ||
return "modal-sm"; | ||
case ModalSize.Large: | ||
return "modal-lg"; | ||
case ModalSize.Medium: | ||
default: | ||
return ""; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,107 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using WebExample.Models; | ||
using IdentityRole = AspNetCore.Identity.PostgreSQL.IdentityRole; | ||
namespace WebExample.Controllers | ||
{ | ||
public class ApplicationRoleController | ||
public class ApplicationRoleController : Controller | ||
{ | ||
public ApplicationRoleController() | ||
private readonly RoleManager<IdentityRole> roleManager; | ||
|
||
public ApplicationRoleController(RoleManager<IdentityRole> roleManager) | ||
{ | ||
this.roleManager = roleManager; | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult Index() | ||
{ | ||
List<ApplicationRoleListViewModel> model = new List<ApplicationRoleListViewModel>(); | ||
model = roleManager.Roles.Select(r => new ApplicationRoleListViewModel | ||
{ | ||
RoleName = r.Name, | ||
Id = r.Id, | ||
Description = r.Name, | ||
//NumberOfUsers = r.Users.Count | ||
}).ToList(); | ||
return View(model); | ||
} | ||
[HttpGet] | ||
public async Task<IActionResult> AddEditApplicationRole(string id) | ||
{ | ||
ApplicationRoleViewModel model = new ApplicationRoleViewModel(); | ||
if (!String.IsNullOrEmpty(id)) | ||
{ | ||
IdentityRole applicationRole = await roleManager.FindByIdAsync(id); | ||
if (applicationRole != null) | ||
{ | ||
model.Id = applicationRole.Id; | ||
model.RoleName = applicationRole.Name; | ||
// model.Description = applicationRole.Description; | ||
} | ||
} | ||
return PartialView("_AddEditApplicationRole", model); | ||
} | ||
[HttpPost] | ||
public async Task<IActionResult> AddEditApplicationRole(string id, ApplicationRoleViewModel model) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
bool isExist = !String.IsNullOrEmpty(id); | ||
IdentityRole applicationRole = isExist ? await roleManager.FindByIdAsync(id) : | ||
new IdentityRole | ||
{ | ||
//CreatedDate = DateTime.UtcNow | ||
}; | ||
applicationRole.Name = model.RoleName; | ||
/* applicationRole.Description = model.Description; | ||
applicationRole.IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();*/ | ||
IdentityResult roleRuslt = isExist ? await roleManager.UpdateAsync(applicationRole) | ||
: await roleManager.CreateAsync(applicationRole); | ||
if (roleRuslt.Succeeded) | ||
{ | ||
return RedirectToAction("Index"); | ||
} | ||
} | ||
return RedirectToAction("Index"); | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> DeleteApplicationRole(string id) | ||
{ | ||
string name = string.Empty; | ||
if (!String.IsNullOrEmpty(id)) | ||
{ | ||
IdentityRole applicationRole = await roleManager.FindByIdAsync(id); | ||
if (applicationRole != null) | ||
{ | ||
name = applicationRole.Name; | ||
} | ||
} | ||
return PartialView("_DeleteApplicationRole", name); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> DeleteApplicationRole(string id, FormCollection form) | ||
{ | ||
if (!String.IsNullOrEmpty(id)) | ||
{ | ||
IdentityRole applicationRole = await roleManager.FindByIdAsync(id); | ||
if (applicationRole != null) | ||
{ | ||
IdentityResult roleRuslt = roleManager.DeleteAsync(applicationRole).Result; | ||
if (roleRuslt.Succeeded) | ||
{ | ||
return RedirectToAction("Index"); | ||
} | ||
} | ||
} | ||
return View(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
namespace WebExample.Models | ||
{ | ||
public class ApplicationRoleViewModel | ||
{ | ||
public ApplicationRoleViewModel() | ||
{ | ||
} | ||
public Guid Id { get; set; } | ||
[Display(Name = "Role Name")] | ||
public string RoleName { get; set; } | ||
public string Description { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
WebExample/Views/ApplicationRole/_AddEditApplicationRole.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters