コード例 #1
0
ファイル: AdminController.cs プロジェクト: npeskoe/portfolio
        public ActionResult AddUser(UserViewModel model, GuildCarsDbContext context)
        {
            var userMgr = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));
            var roleMgr = new RoleManager <ApplicationRole>(new RoleStore <ApplicationRole>(context));

            var user = new ApplicationUser();

            user.FirstName = model.FirstName;
            user.LastName  = model.LastName;
            user.UserName  = model.Email;
            user.Email     = model.Email;

            if (ModelState.IsValid)
            {
                userMgr.Create(user, model.Password);
                userMgr.AddToRole(user.Id, model.Role);
                return(RedirectToAction("Users"));
            }
            else
            {
                var repo = UsersRepositoryFactory.GetRepository();

                List <UserRoles> roles = repo.GetUserRoles();

                var userRoles = (from role in roles
                                 select role.Name).ToList();

                model.Roles = new SelectList(userRoles);

                return(View("AddUser", model));
            }
        }
コード例 #2
0
ファイル: AdminController.cs プロジェクト: npeskoe/portfolio
        public ActionResult EditUser(EditUserViewModel model, GuildCarsDbContext context)
        {
            var userMgr = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));
            var roleMgr = new RoleManager <ApplicationRole>(new RoleStore <ApplicationRole>(context));

            ApplicationUser user = userMgr.FindById(model.Id);

            user.FirstName = model.FirstName;
            user.LastName  = model.LastName;
            user.Email     = model.Email;
            user.UserName  = model.Email;

            var repo = UsersRepositoryFactory.GetRepository();

            List <UserRoles> roles = repo.GetUserRoles();

            List <Users> users = repo.GetAll();

            var newUserRole = (from role in roles
                               where model.Role == role.Name
                               select role.Name).FirstOrDefault();

            var oldUserRole = (from u in users
                               where model.FirstName == user.FirstName
                               select u.Role).FirstOrDefault();

            if (ModelState.IsValid)
            {
                userMgr.RemoveFromRole(user.Id, oldUserRole);
                userMgr.AddToRole(user.Id, model.Role);
                userMgr.Update(user);

                return(RedirectToAction("Users"));
            }
            else
            {
                List <UserRoles> rolenames = repo.GetUserRoles();

                var userRoles = (from role in rolenames
                                 select role.Name).ToList();

                model.Roles = new SelectList(userRoles);

                return(View("EditUser", model));
            }
        }
コード例 #3
0
ファイル: AdminController.cs プロジェクト: npeskoe/portfolio
        public async System.Threading.Tasks.Task <ActionResult> ChangePasswordAsync(ChangePasswordViewModel model, GuildCarsDbContext context)
        {
            var userMgr = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

            var result = await userMgr.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);

            return(RedirectToAction("Index", "Home"));
        }