예제 #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();
        }
예제 #2
0
        private void LoadUsersData()
        {
            if (UsersTable == null)
            {
                UsersTable = new DataTable("Users");

                UsersTable.Columns.Add("Id", typeof(string));
                UsersTable.Columns.Add("UserName", typeof(string));
                UsersTable.Columns.Add("Email", typeof(string));
                UsersTable.Columns.Add("PhoneNumber", typeof(string));
                DataColumn[] keys = new DataColumn[] { UsersTable.Columns[0] };
                UsersTable.PrimaryKey = keys;
            }
            else
            {
                UsersTable.Rows.Clear();
            }

            var context     = new ApplicationDbContext();
            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));
            var users       = userManager.Users;

            foreach (var user in users)
            {
                var row = UsersTable.NewRow();
                row["Id"]          = user.Id;
                row["UserName"]    = user.UserName;
                row["Email"]       = user.Email;
                row["PhoneNumber"] = user.PhoneNumber;
                UsersTable.Rows.Add(row);
                UsersTable.AcceptChanges();
            }
            UsersGridView.DataSource = UsersTable;
            UsersGridView.DataBind();

            MUsersGridView.DataSource = UsersTable;
            MUsersGridView.DataBind();

            if (RolesTable == null)
            {
                RolesTable = new DataTable("Roles");
                RolesTable.Columns.Add("Id", typeof(string));
                RolesTable.Columns.Add("Name", typeof(string));
                RolesTable.PrimaryKey = new DataColumn[] { RolesTable.Columns["Id"] };
            }
            else
            {
                RolesTable.Rows.Clear();
            }

            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var roles       = roleManager.Roles;

            foreach (var role in roles)
            {
                var row = RolesTable.NewRow();
                row["Id"]   = role.Id;
                row["Name"] = role.Name;
                RolesTable.Rows.Add(row);
                RolesTable.AcceptChanges();
            }
            RolesGridView.DataSource = RolesTable;
            RolesGridView.DataBind();

            MRolesGridView.DataSource = RolesTable;
            MRolesGridView.DataBind();

            if (UserInRoles == null)
            {
                UserInRoles = new DataTable("UserInRoles");
                UserInRoles.Columns.Add("Id", typeof(string));
                UserInRoles.Columns.Add("Name", typeof(string));
                UserInRoles.PrimaryKey = new DataColumn[] { UserInRoles.Columns["Id"] };
            }
            MUserInRolesGridView.DataSource = UserInRoles;
        }
예제 #3
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();
        }