nmduc073 posted on August 30, 2009 04:54::1015 view(s)
Dạo gần đây, mình có ngâm cứu phiên bản MVC của TheBeerHouse. Nó có 1 lỗi khi update role của 1 user. Giả sử một user có role A. Nó không có role B, C, D, E. Khi 1 admin vào edit user đó, check them role B cho user đó. Rồi click update. Lập tức các role khác đều được check cả.
Thực sự không hiểu sao nhóm phát triển lại để lỗi đó xảy ra. Mình đã loay hoay tìm lỗi và ngồi sửa lại được. Đó là hàm EditUser trong UserController.cs. Và hàm được sửa lại như sau:
[AcceptVerbs("POST")]
public ActionResult EditUser(string id, bool approved)
{
//Is a list of all the user roles
ArrayList removeRoleList = new ArrayList(Roles.GetAllRoles());
//We are requesting the form variables directly from the form
foreach (string key in Request.Form.Keys)
{
if (key.StartsWith("role."))
{
String userRole = key.Substring(5, key.Length - 5);
if (Request.Form[key] == "true" || Request.Form[key] == "true,false")
{
removeRoleList.Remove(userRole);
if (!Roles.IsUserInRole(id, userRole))
{
[Read the rest of this article...]