Exemple #1
0
        protected void MUserInRolesGridView_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            string userId = string.Empty;
            string role   = string.Empty;
            string roleId = string.Empty;

            if (e.Parameters.Equals("add"))
            {
                List <Object> userSelectedValues = MUsersGridView.GetSelectedFieldValues("Id");
                List <Object> roleSelectedValues = MRolesGridView.GetSelectedFieldValues("Name");
                if ((userSelectedValues != null && userSelectedValues.Count > 0) && (roleSelectedValues != null && roleSelectedValues.Count > 0))
                {
                    var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>();
                    userId            = userSelectedValues[0].ToString();
                    Session["UserId"] = userId;
                    foreach (var item in roleSelectedValues)
                    {
                        role = item.ToString();
                        if (!manager.IsInRole(userId, role))
                        {
                            IdentityResult result = manager.AddToRole(userId, role);
                            if (!result.Succeeded)
                            {
                                throw new Exception(result.Errors.FirstOrDefault());
                            }
                        }
                    }
                }
            }
            else if (e.Parameters.Equals("del"))
            {
                List <Object> userSelectedValues = MUsersGridView.GetSelectedFieldValues("Id");
                List <Object> roleSelectedValues = MUserInRolesGridView.GetSelectedFieldValues("RoleId");
                if ((userSelectedValues != null && userSelectedValues.Count > 0) && (roleSelectedValues != null && roleSelectedValues.Count > 0))
                {
                    userId            = userSelectedValues[0].ToString();
                    Session["UserId"] = userId;
                    var manager     = Context.GetOwinContext().GetUserManager <ApplicationUserManager>();
                    var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext()));
                    foreach (var item in roleSelectedValues)
                    {
                        roleId = item.ToString();
                        role   = roleManager?.FindById(roleId)?.Name ?? string.Empty;
                        if (manager.IsInRole(userId, role))
                        {
                            IdentityResult result = manager.RemoveFromRole(userId, role);
                            if (!result.Succeeded)
                            {
                                throw new Exception(result.Errors.FirstOrDefault());
                            }
                        }
                    }
                }
            }
            else
            {
                Session["UserId"] = e.Parameters;
            }
            MUserInRolesGridView.DataBind();
        }
Exemple #2
0
        protected void MUserInRolesGridView_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            int    rowIndex = int.Parse(e.Parameters);
            string userId   = string.Empty;
            string role     = string.Empty;
            string roleId   = string.Empty;

            if (rowIndex >= 0)
            {
                Session["UserId"] = MUsersGridView.GetRowValues(int.Parse(e.Parameters), "Id").ToString();
            }
            else
            {
                var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>();

                //var context = new ApplicationDbContext();
                //var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
                //var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

                if (rowIndex == -2)
                {
                    if (MRolesGridView.FocusedRowIndex > -1 && MUsersGridView.FocusedRowIndex > -1)
                    {
                        role   = MRolesGridView.GetRowValues(MRolesGridView.FocusedRowIndex, "Name").ToString();
                        userId = MUsersGridView.GetRowValues(MUsersGridView.FocusedRowIndex, "Id").ToString();
                        if (!manager.IsInRole(userId, role))
                        {
                            IdentityResult result = manager.AddToRole(userId, role);
                            if (!result.Succeeded)
                            {
                                throw new Exception(result.Errors.FirstOrDefault());
                            }
                        }
                    }
                }
                else if (rowIndex == -3)
                {
                    if (MUsersGridView.FocusedRowIndex > -1)
                    {
                        userId = MUsersGridView.GetRowValues(MUsersGridView.FocusedRowIndex, "Id").ToString();
                        List <Object> selectedItems = MRolesGridView.GetSelectedFieldValues(new string[] { "Id", "Name" });
                        foreach (object[] selectedItem in selectedItems)
                        {
                            role = selectedItem[1].ToString();
                            if (!manager.IsInRole(userId, role))
                            {
                                IdentityResult result = manager.AddToRole(userId, role);
                                if (!result.Succeeded)
                                {
                                    throw new Exception(result.Errors.FirstOrDefault());
                                }
                            }
                        }
                    }
                }
                else if (rowIndex == -4)
                {
                    if (MUsersGridView.FocusedRowIndex > -1 && MUserInRolesGridView.FocusedRowIndex > -1)
                    {
                        userId = MUsersGridView.GetRowValues(MUsersGridView.FocusedRowIndex, "Id").ToString();
                        roleId = MUserInRolesGridView.GetRowValues(MUserInRolesGridView.FocusedRowIndex, "RoleId").ToString();
                        var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext()));
                        role = roleManager?.FindById(roleId)?.Name ?? string.Empty;

                        if (manager.IsInRole(userId, role))
                        {
                            IdentityResult result = manager.RemoveFromRole(userId, role);
                            if (!result.Succeeded)
                            {
                                throw new Exception(result.Errors.FirstOrDefault());
                            }
                        }
                    }
                }
                else if (rowIndex == -5)
                {
                    if (MUsersGridView.FocusedRowIndex > -1 && MUserInRolesGridView.VisibleRowCount > 0)
                    {
                        userId = MUsersGridView.GetRowValues(MUsersGridView.FocusedRowIndex, "Id").ToString();
                        var           roleManager   = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext()));
                        List <Object> selectedItems = MUserInRolesGridView.GetSelectedFieldValues(new string[] { "UserId", "RoleId" });
                        foreach (object[] selectedItem in selectedItems)
                        {
                            roleId = selectedItem[1].ToString();
                            role   = roleManager?.FindById(roleId)?.Name ?? string.Empty;
                            if (manager.IsInRole(userId, role))
                            {
                                IdentityResult result = manager.RemoveFromRole(userId, role);
                                if (!result.Succeeded)
                                {
                                    throw new Exception(result.Errors.FirstOrDefault());
                                }
                            }
                        }
                    }
                }

                //context.SaveChanges();
            }
            MUserInRolesGridView.DataBind();
        }