Example #1
0
        public IHttpActionResult RemoveAdminFromUser(string username)
        {
            var user = this.Data.Users.All().ToList().FirstOrDefault(u => u.UserName == username);

            var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

            string adminCheck = um.GetRoles(user.Id).FirstOrDefault(a => a.Contains("AppAdmin"));

            if (adminCheck == string.Empty)
            {
                return this.BadRequest(string.Format("user {0} is not admin", user.UserName));
            }

            um.RemoveFromRoleAsync(user.Id, "AppAdmin");

            this.Data.SaveChanges();

            return this.Ok(string.Format("user: {0} is not admin now", user.UserName));
        }
        public ActionResult Demote(string id)
        {
            var currentUser = this.Data.Users.All().First(u => u.UserName == User.Identity.Name);
            var isSameUser = id == currentUser.Id;

            if (!isSameUser)
            {
                var store = new UserStore<User>(this.Data.Context);
                var manager = new UserManager<User>(store);

                manager.RemoveFromRoleAsync(id, GlobalConstants.AdminRole);

                this.Notify(GlobalConstants.DemoteUserSuccess, NotificationType.success);
            }
            else
            {
                this.Notify(GlobalConstants.DemoteUserFail, NotificationType.error);
            }

            return this.RedirectToAction("Manage");
        }
        public async Task<IHttpActionResult> UpdateRuoloUtente(UpdateRuoloUtenteModel Model)
        {
            if (Model == null || !ModelState.IsValid)
                return BadRequest(ModelState);

            using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
            {
                var utente = await userManager.FindByNameAsync(Model.Username);
                if (utente == null)
                    return NotFound();

                if (Model.NuovoStato)
                {
                    await userManager.AddToRoleAsync(utente.Id, Model.Ruolo);
                }
                else
                {
                    await userManager.RemoveFromRoleAsync(utente.Id, Model.Ruolo);
                }
            }

            return Ok();
        }
Example #4
0
        public async Task<ActionResult> DeleteConfirmed(string id) {

            var roleStore = new RoleStore<IdentityRole>(db);
            var roleManager = new RoleManager<IdentityRole>(roleStore);

            var userStore = new UserStore<ApplicationUser>(db);
            var userManager = new UserManager<ApplicationUser>(userStore);

            if (ModelState.IsValid) {
                if (id == null) {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }

                var user = await userManager.FindByIdAsync(id);
                
                // ev. 3parts inloggningar
                var logins = user.Logins;
                foreach (var login in logins.ToList()) {
                    await userManager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
                }

                var rolesForUser = await userManager.GetRolesAsync(id);

                if (rolesForUser.Count() > 0) {
                    foreach (var item in rolesForUser.ToList()) {
                        // item should be the name of the role
                        var result = await userManager.RemoveFromRoleAsync(user.Id, item);
                    }
                }

                if (user.Documents.Count() > 0) {
                    foreach (var doc in user.Documents.ToList()) {
                        db.Documents.Remove(doc);
                    }
                }

                await userManager.DeleteAsync(user);

                return RedirectToAction("Index");
            }
            else {
                return View();
            }
        }
    protected void RoleCheckBox_CheckChanged(object sender, EventArgs e)
    {
        // Reference the CheckBox that raised this event
        CheckBox RoleCheckBox = sender as CheckBox;

        // Get the currently selected user and role
        string selectedUserName = UserList.SelectedValue;
        var um = new UserManager();
        var user = um.FindByName(selectedUserName);
        string roleName = RoleCheckBox.Text;

        // Determine if we need to add or remove the user from this role
        if (RoleCheckBox.Checked)
        {
            // Add the user to the role
            um.AddToRoleAsync(user.Id, roleName);

            // Display a status message
            ActionStatus.Text = string.Format("User {0} was added to role {1}.", selectedUserName, roleName);
        }
        else
        {
            // Remove the user from the role
            um.RemoveFromRoleAsync(user.Id, roleName);

            // Display a status message
            ActionStatus.Text = string.Format("User {0} was removed from role {1}.", selectedUserName, roleName);
        }

        // Refresh the "by role" interface
        DisplayUsersBelongingToRole();
    }
        /// <summary>
        /// remove all current permission for the given user and insert new given permissions. 
        /// </summary>
        /// <param name="newRoles">Accepts a list of RolesName type string</param>
        /// <param name="UserID">Accepts a 128 chr userID type string</param>
        /// <returns></returns>
        public async Task<string> EditbuildingUserPermission(List<string> newRoles, string UserID)
        {
            var result = "";
            var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            //get all roles for the current user
            List<string> AllRoles = UserManager.GetRoles(UserID).ToList();
            //delete all the roles
            foreach (var item in AllRoles)
            {
                await UserManager.RemoveFromRoleAsync(UserID, item);
            }
            //add new roles 
            foreach (var item in newRoles)
            {
                if (RoleManager.RoleExists(item))
                {
                    await UserManager.AddToRoleAsync(UserID, item);
                }
            }

            return result;

        }