Ejemplo n.º 1
0
        public ActionResult DeleteConfirmed(ApplicationUser userId)
        {
            ApplicationUser user = _db.Users.Find(userId.Id);

            if (user.IsAPilot)
            {
                var pm = new PilotManager();
                pm.RemovePilot(user);
            }

            List <IdentityUserClaim> claims = user.Claims.ToList();

            foreach (IdentityUserClaim claim in claims)
            {
                user.Claims.Remove(claim);
            }


            _db.Users.Remove(user);

            _db.SaveChanges();

            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Edit(EditViewModel idModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(idModel));
            }
            List <IdentityRole> roleUserList = _db.Roles.ToList();

            foreach (IdentityRole userRole in roleUserList)
            {
                List <IdentityUserRole> userRoleList = userRole.Users.ToList();
                foreach (IdentityUserRole u in userRoleList.Where(u => u.UserId == idModel.Id))
                {
                    AddOrRemoveFromRole(u.UserId, u.RoleId);
                }
            }
            AddOrRemoveFromRole(idModel.Id, idModel.RoleId);

            _db.SaveChanges();
            ApplicationUser user = await UserManager.FindByIdAsync(idModel.Id);

            user.UserName     = idModel.Email;
            user.Email        = idModel.Email;
            user.PhoneNumber  = idModel.PhoneNumber;
            user.FirstName    = idModel.FirstName;
            user.LastName     = idModel.LastName;
            user.ViewToPublic = idModel.ViewToPublic;
            user.IsAPilot     = GetRoleNameFromUserId(idModel.Id) == "Pilot" || idModel.IsAPilot;


            IdentityResult result = await UserManager.UpdateAsync(user);

            if (result.Succeeded)
            {
                var pm = new PilotManager();
                if (user.IsAPilot)
                {
                    if (!pm.PilotExists(user))
                    {
                        pm.AddPilot(user);
                    }
                }
                else
                {
                    if (pm.PilotExists(user))
                    {
                        pm.RemovePilot(user);
                    }
                }
                if (GetRoleNameFromRoleId(idModel.RoleId) != "Admin")
                {
                    return(RedirectToAction("Index"));
                }
                List <Plane>  planes = _db.Planes.ToList();
                List <string> claims =
                    user.Claims.Where(c => c.ClaimType == "PlaneAccess").Select(c => c.ClaimValue).ToList();
                foreach (Plane plane in planes)
                {
                    if (!claims.Contains(plane.TailNumber))
                    {
                        UserManager.AddClaim(user.Id, new Claim("PlaneAccess", plane.TailNumber));
                    }
                }
                return(RedirectToAction("Index"));
            }
            MessageBox.Show(result.Errors.FirstOrDefault());
            return(View(idModel));
        }