Skip to content

Commit

Permalink
編輯角色_修改名稱
Browse files Browse the repository at this point in the history
  • Loading branch information
malagege committed Aug 19, 2022
1 parent 3fa29c7 commit 6f56c8b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
71 changes: 69 additions & 2 deletions AuthSample/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using AuthSample.ViewModels.Admin;
using AuthSample.Models;
using AuthSample.ViewModels.Admin;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -11,10 +14,12 @@ namespace AuthSample.Controllers
public class AdminController : Controller
{
private readonly RoleManager<IdentityRole> _roleManager;
private readonly UserManager<ApplicationUser> _userManager;

public AdminController(RoleManager<IdentityRole> roleManager)
public AdminController(RoleManager<IdentityRole> roleManager,UserManager<ApplicationUser> userManager)
{
_roleManager = roleManager;
_userManager = userManager;
}
public IActionResult Index()
{
Expand Down Expand Up @@ -62,5 +67,67 @@ public IActionResult RoleList()
return View(roles);
}
#endregion

# region 編輯角色
[HttpGet]
public async Task<IActionResult> EditRoleAsync(string id)
{
// 通過參數 id 帶入 roleManager 查詢
IdentityRole role = await _roleManager.FindByIdAsync(id);
if( role == null)
{
ViewBag.ErrorMessage = $"角色ID:{id}不存在";
return View("NotFound");
}

var model = new EditRoleViewModel
{
Id = role.Id,
RoleName = role.Name,
};
IList<ApplicationUser> users = await _userManager.Users.ToListAsync();

foreach (var user in users)
{
// 每個使用者的 role 判斷是否有權限
if(await _userManager.IsInRoleAsync(user, role.Name))
{
model.Users.Add(user.UserName);
}
}

return View(model);
}

[HttpPost]
public async Task<IActionResult> EditRole(EditRoleViewModel model)
{
IdentityRole role = await _roleManager.FindByIdAsync(model.Id);

if(role == null)
{
ViewBag.ErrorMessage = $"角色Id={model.Id}不存在";
return View("NotFound");
}
else
{
role.Name = model.RoleName;

var result = await _roleManager.UpdateAsync(role);
if (result.Succeeded)
{
return RedirectToAction("RoleList");
}

foreach(var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}

return View(model);
}

}
#endregion
}
}
11 changes: 11 additions & 0 deletions AuthSample/ViewModels/Admin/EditRoleViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace AuthSample.ViewModels.Admin
{
public class EditRoleViewModel
{
public string Id { get; set; }
public string RoleName { get; set; }
public IList<string> Users { get; set; }
}
}
22 changes: 22 additions & 0 deletions AuthSample/Views/Admin/EditRole.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@*
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
@model AuthSample.ViewModels.Admin.EditRoleViewModel

<h1>編輯角色</h1>

<form method="post">
<div>
<input asp-for="Id" />
</div>
<div>
<input asp-for="RoleName" placeholder="角色名稱" />
<span asp-validation-for="RoleName"></span>
</div>
<div>
<button>更新</button>
<a asp-action="rolelist">取消</a>
</div>
</form>
2 changes: 1 addition & 1 deletion AuthSample/Views/Admin/RoleList.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
角色名稱:@role.Name
</div>
<div>
<!-- 編輯/刪除-->
<a asp-controller="Admin" asp-action="EditRole" asp-route-id="@role.Id">編輯</a>
</div>
<hr>
</div>
Expand Down

0 comments on commit 6f56c8b

Please sign in to comment.