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 4ab3828 commit 2a21605
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
95 changes: 95 additions & 0 deletions AuthSample/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,100 @@ public async Task<IActionResult> EditRoleAsync(EditRoleViewModel model)

}
#endregion

#region 角色加入使用者設定
public async Task<IActionResult> EditUserInRoleAsync(string roleId)
{
ViewBag.roleId = roleId;

IdentityRole role = await _roleManager.FindByIdAsync(roleId);
if(role == null)
{
ViewBag.ErrorMessage = $"角色{roleId}不存在";
return View("NotFound");
}
var model = new List<UserRoleViewModel>();
var users = await _userManager.Users.ToListAsync();
foreach (var user in users)
{
var userRoleViewModel = new UserRoleViewModel
{
UserId = user.Id,
UserName = user.UserName,
};
// 判斷使用者有無此角色
// 注意第二個參數是 role.Name
var isInRole = await _userManager.IsInRoleAsync(user, role.Name);

if (isInRole)
{
userRoleViewModel.IsSelected = true;
}
else
{
userRoleViewModel.IsSelected = false;
}
model.Add(userRoleViewModel);
}

return View(model);
}

[HttpPost]
public async Task<IActionResult> EditUserInRoleAsync(IList<UserRoleViewModel> model, string roleId)
{
IdentityRole role = await _roleManager.FindByIdAsync(roleId);
if (role == null)
{
ViewBag.ErrorMessage = $"角色id={roleId}不存在";
return View("NotFound");
}

for(int i=0; i<model.Count; i++)
{
ApplicationUser user = await _userManager.FindByIdAsync(model[i].UserId);

IdentityResult result;
// ***重點*** Start
// 選擇跟查詢結果不一樣,更改資料
bool isInRole = await _userManager.IsInRoleAsync(user, role.Name);
if (model[i].IsSelected && !(isInRole))
{
result = await _userManager.AddToRoleAsync(user, role.Name); // 使用者角色新增 role.Name
}
else if (!model[i].IsSelected && (isInRole))
{
result = await _userManager.RemoveFromRoleAsync(user, role.Name); // 使用者角色移除 role.Name
}
else
{
continue; //與資料庫一樣不做處理
}

if (result.Succeeded)
{
if(i < (model.Count - 1)) //簡單來說,第一次和中間次數跑 continue 執行最後一次會跑 else 內容
{
continue;
}
else
{
return RedirectToAction("EditRole", new { Id = roleId });
}
}
// ***重點*** End

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

}

//return View(model); 這邊要指定一個 roleId
return RedirectToAction("EditRole", new { Id = roleId });
}

#endregion
}
}
9 changes: 9 additions & 0 deletions AuthSample/ViewModels/Admin/UserRoleViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AuthSample.ViewModels.Admin
{
public class UserRoleViewModel
{
public string UserId { get; set; }
public string UserName { get; set; }
public bool IsSelected { get; set; }
}
}
4 changes: 2 additions & 2 deletions AuthSample/Views/Admin/EditRole.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<form method="post">
<div>
<input asp-for="Id" />
<input asp-for="Id" disabled />
</div>
<div>
<input asp-for="RoleName" placeholder="角色名稱" />
Expand Down Expand Up @@ -38,7 +38,7 @@
}
</div>
<div>
<!-- 添加使用者到角色裡面-->
<a asp-action="EditUserInRole" asp-route-roleId="@Model.Id">添加/刪除使用者</a>
</div>
</div>
</form>
26 changes: 26 additions & 0 deletions AuthSample/Views/Admin/EditUserInRole.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@using AuthSample.ViewModels.Admin
@*
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
var roleId = ViewBag.roleId;
}
@model List<UserRoleViewModel>;

<form method="post">
<h2>角色下 新增 / 刪除 使用者</h2>
@for (var i = 0; i < Model.Count; i++)
{
<div>
<input type="hidden" asp-for="@Model[i].UserId" />
<input type="hidden" asp-for="@Model[i].UserName"/>
<input asp-for="@Model[i].IsSelected"/>
<label asp-for="@Model[i].IsSelected">
@Model[i].UserName
</label>
</div>
}
<button>更新</button>
<a asp-action="EditRole" asp-route-id="@roleId">取消</a>

</form>

0 comments on commit 2a21605

Please sign in to comment.