Example #1
0
        // delete a user account and all history: comments, posts and votes
        public static bool DeleteUser(string userName)
        {
            using (whoaverseEntities db = new whoaverseEntities())
            {
                using (UserManager<ApplicationUser> tmpUserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
                {
                    var tmpuser = tmpUserManager.FindByName(userName);
                    if (tmpuser != null)
                    {
                        //remove voting history for submisions
                        db.Votingtrackers.RemoveRange(db.Votingtrackers.Where(x => x.UserName == userName));

                        //remove voting history for comments
                        db.Commentvotingtrackers.RemoveRange(db.Commentvotingtrackers.Where(x => x.UserName == userName));

                        //remove all comments
                        var comments = db.Comments.Where(c => c.Name == userName);
                        foreach (Comment c in comments)
                        {
                            c.Name = "deleted";
                            c.CommentContent = "deleted";
                            db.SaveChangesAsync();
                        }

                        //remove all submissions
                        var submissions = db.Messages.Where(c => c.Name == userName);
                        foreach (Message s in submissions)
                        {
                            if (s.Type == 1)
                            {
                                s.Name = "deleted";
                                s.MessageContent = "deleted";
                                s.Title = "deleted";
                            }
                            else
                            {
                                s.Name = "deleted";
                                s.Linkdescription = "deleted";
                                s.MessageContent = "http://whoaverse.com";
                            }
                        }
                        db.SaveChangesAsync();

                        var result = tmpUserManager.DeleteAsync(tmpuser);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }

            }
        }
        public async Task RemoveTenantSampleData(MyHealthContext context, UserManager<ApplicationUser> userManager, int tenantId)
        {
            if (context.Tenants.Any(t => t.TenantId == tenantId))
            {
                context.HomeAppointments.RemoveRange(context.HomeAppointments.Where(t => t.TenantId == tenantId));
                context.ClinicAppointments.RemoveRange(context.ClinicAppointments.Where(t => t.TenantId == tenantId));
                context.Medicines.RemoveRange(context.Medicines.Where(t => t.TenantId == tenantId));

                var usersToDelete = context.Users.Where(t => t.TenantId == tenantId).ToList();

                foreach (var user in usersToDelete)
                    await userManager.DeleteAsync(user);

                context.Tenants.Remove(context.Tenants.FirstOrDefault(t => t.TenantId == tenantId));
                await context.SaveChangesAsync();
            }
        }
 public void DeleteUserByID(string userID)
 {
     try
     {
         var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context1));
         var userToDelete = userMgr.FindById(userID);
         userMgr.DeleteAsync(userToDelete);
     }
     catch (OptimisticConcurrencyException ocex)
     {
         throw ocex;
     }
     catch (ArgumentNullException argex)
     {
         throw argex;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Example #4
0
        public Boolean delete()
        {
            //delete from user table
            String deleteUserID = Request.Params["id"];
            int id = int.Parse(deleteUserID);
            IRepository<User> repository = new UserRepository();
            User deletedUser = repository.findById(id);
            if (deletedUser == null)
            {
                return false;
            }
            repository.Delete(deletedUser);

            //delete from asp.netUser table
            var context = new NetCafeWeb.Models.ApplicationDbContext();
            var UserManager = new UserManager<NetCafeWeb.Models.ApplicationUser>(new UserStore<NetCafeWeb.Models.ApplicationUser>(context));
            ApplicationUser aspSelectedUser = context.Users.Where(u => u.UserName.Equals((deletedUser.UserName), StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();

            UserManager.DeleteAsync(aspSelectedUser);

            return true;
        }
Example #5
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();
            }
        }
Example #6
0
 //delete employee
 public void DeleteEmployee(string email)
 {
     var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
     var user = manager.FindByEmail(email);
     if (user != null) {
         DeleteAllEmployeeShifts(email);
         manager.DeleteAsync(user);
     };
     UserManagerExtensions.Delete(manager, user);
     _repo.SaveChanges();
 }
Example #7
0
        private async Task Seed(IUnitOfWork uow  , UserManager<ApplicationUser> usrMan, RoleManager<IdentityRole> _RoleManager)
        {

            var email = "[email protected]";
            var RoleName = "CustomerAdmin";
            var role = await _RoleManager.FindByNameAsync(RoleName);
            if (role == null)
            {
                role = new IdentityRole { Name = RoleName };
                await _RoleManager.CreateAsync(role);
            }

        RoleName = "superadmin";
            role = await _RoleManager.FindByNameAsync( RoleName );
            if ( role == null)
            {
                role = new IdentityRole { Name = "SuperAdmin" };
                await _RoleManager.CreateAsync(role);
            }
            
            var appUser = await usrMan.FindByEmailAsync(email);
            if (appUser != null)
            {
                var result = await usrMan.DeleteAsync(appUser);
            }
            else 
            {
                appUser = new ApplicationUser() { UserName = email, Email = email };
                var result2 = await usrMan.CreateAsync(appUser, "Shafiro2,");
                if (result2.Succeeded)
                {
                    await usrMan.AddToRoleAsync(appUser,  RoleName ); 
                    var hrmUser = new User()
                    {
                        UserID = "7310209296",
                        FirstName = "Gabriel",
                        LastName = "Tekin",
                        UserName = "gabtek",
                        UserCode = "tekgab",
                        PhoneNumber = "0702385537"
                    };
                    if (uow.UserRepository.GetEagerLoad( w => w.UserID.Equals( hrmUser.UserID, StringComparison.OrdinalIgnoreCase )).FirstOrDefault()   == null )
                    {
                        uow.UserRepository.Insert(hrmUser);
                        uow.Save();
                    }
                    
                }
                else
                {
                    var code = result2.Errors.FirstOrDefault().Code;
                }
            }

            } // method seed