public ActionResult UserProfile()
        {
            var user = this.Data.ApplicationUsers.GetById(this.User.Identity.GetUserId());
            var viewModel = new UserViewModel()
            {
                UserName = user.UserName
            };

            return this.View(viewModel);
        }
Ejemplo n.º 2
0
 public void EditUser(UserViewModel user)
 {
     try
     {
         userContext.Update(UserConverter.ConvertViewModelToDB(user));
     }
     catch (Exception e)
     {
         Logger.Log.Error(e.Message);
     }
 }
Ejemplo n.º 3
0
        public ActionResult MyPosts(int?id)
        {
            int   UserID = Convert.ToInt32(Session["UserID"].ToString());
            Posts posts  = db.Posts.Find(id);
            var   p      = db.Posts.Where(m => m.UserID == UserID).OrderByDescending(n => n.PTime).ToList();
            var   index  = new Models.UserViewModel
            {
                posts = p,
            };

            return(View(index));
        }
Ejemplo n.º 4
0
        public static User ConvertViewModelToDB(UserViewModel user)
        {
            var newUser = new User
            {
                First_name = user.First_name,
                Last_name = user.Last_name,
                Email = user.Email,
                Id = user.Id,
            };

            return newUser;
        }
Ejemplo n.º 5
0
        public static UserViewModel ConvertToViewModel(User user)
        {
            var userViewModel = new UserViewModel
            {
                 Id = user.Id,
                 First_name = user.First_name,
                 Last_name = user.Last_name,
                 Email = user.Email
            };

            return userViewModel;
        }
Ejemplo n.º 6
0
        public ActionResult Users_Create([DataSourceRequest]DataSourceRequest request, UserViewModel model)
        {
            if (ModelState.IsValid)
            {
                this.users.Create(
                    model.Email,
                    model.UserName,
                    model.Password,
                    model.IsAdmin);
            }

            return Json(new[] { model }.ToDataSourceResult(request, ModelState));
        }
Ejemplo n.º 7
0
 public bool Edit(UserViewModel user)
 {
     if (ModelState.IsValid)
     {
         if (userService.IsEmailUnique(user.Email))
         {
             userService.EditUser(user);
             var currentUser = (UserViewModel)HttpContext.Session["CurrentUser"];
             Logger.Log.Debug("user  ID:" + currentUser.Id + " " + currentUser.Email + " updated own information");
         }
         else { return false; }
         return true;
     }
     return false;
 }
Ejemplo n.º 8
0
 public static UserViewModel ToViewModel(this IdentityUser user)
 {
     string Delete="<a class='btn btn-danger' href='javascript:void(0);'><i class='fa fa-remove'></i> 删除</a></td>";
     string Editor = "<a class='btn btn-success' href='javascript:void(0);'><i class='fa fa-cog'></i> 编辑</a>";
     string Add = "<a class='btn btn-primary' href='javascript:void(0);'><i class='fa fa-cog'></i> 分配角色</a>";
     UserViewModel userView = new UserViewModel()
     {
         Id=user.Id,
         Name=user.UserName,
         Email=user.Email,
         PhoneNumber=user.PhoneNumber,
         Other = string.Format(Delete + Editor + Add)
     };
     return userView;
 }
        public ActionResult Create(UserViewModel user)
        {
            if (ModelState.IsValid)
            {
                var hasher = new PasswordHasher();
                var hashedPassword = hasher.HashPassword(user.PasswordHash);

                var newUser = new User()
                {
                    UserName = user.UserName,
                    Email = user.Email,
                    EmailConfirmed = false,
                    PasswordHash = hashedPassword,
                    SecurityStamp = Guid.NewGuid().ToString(),
                    PhoneNumber = user.PhoneNumber,
                    PhoneNumberConfirmed = false,
                    TwoFactorEnabled = user.TwoFactorEnabled,
                    LockoutEnabled = true,
                    IsAdmin = user.IsAdmin,
                    ProtopopiateId = user.ProtopopiateId
                };

                if (newUser.IsAdmin)
                {
                    var roleId = this.Data.IdentityRoles
                        .All()
                        .FirstOrDefault(r => r.Name == "Admin")
                        .Id;

                    var identityUserRole = new IdentityUserRole()
                    {
                        RoleId = roleId,
                        UserId = newUser.Id
                    };
                    this.Data.IdentityUserRoles.Add(identityUserRole);
                }

                this.Data.Users.Add(newUser);
                this.Data.SaveChanges();

                return RedirectToAction("Index");
            }

            return View(user);
        }
Ejemplo n.º 10
0
        public ActionResult Users_Destroy([DataSourceRequest]DataSourceRequest request, UserViewModel model)
        {
            this.users.Delete(model.Id);

            return Json(new[] { model }.ToDataSourceResult(request, ModelState));
        }
        public ActionResult Edit(UserViewModel user)
        {
            if (ModelState.IsValid)
            {
                var userFromDb = this.Data.Users
                    .All()
                    .FirstOrDefault(u => u.Id == user.Id);

                if (userFromDb.IsAdmin != user.IsAdmin)
                {
                    userFromDb.IsAdmin = user.IsAdmin;

                    var roleId = this.Data.IdentityRoles
                        .All()
                        .FirstOrDefault(r => r.Name == "Admin")
                        .Id;

                    if (user.IsAdmin)
                    {
                        var identityUserRole = new IdentityUserRole()
                        {
                            RoleId = roleId,
                            UserId = userFromDb.Id
                        };
                        this.Data.IdentityUserRoles.Add(identityUserRole);
                    }
                    else
                    {
                        var identityUserRoles = this.Data.IdentityUserRoles
                            .All()
                            .FirstOrDefault(r => r.RoleId == roleId && r.UserId == userFromDb.Id);

                        this.Data.IdentityUserRoles.Delete(identityUserRoles);
                    }
                }

                userFromDb.UserName = user.UserName;
                userFromDb.Email = user.Email;
                userFromDb.PhoneNumber = user.PhoneNumber;
                userFromDb.ProtopopiateId = user.ProtopopiateId;

                if (user.PasswordHash != "")
                {
                    var hasher = new PasswordHasher();
                    var hashedPassword = hasher.HashPassword(user.PasswordHash);

                    userFromDb.PasswordHash = hashedPassword;
                }
                this.Data.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(user);
        }