예제 #1
0
        public async Task<ActionResult> Create(AspNetUsersViewModel model)
        {
            model.AspNetUserModel.UserName = model.AspNetUserModel.Email;
            #region Update
            if (!string.IsNullOrEmpty(model.AspNetUserModel.Id))
            {
                //Means Update

                // Get role
                var roleName = RoleManager.FindById(model.AspNetUserModel.RoleId).Name;
                AspNetUser userResult = UserManager.FindById(model.AspNetUserModel.Id);
                string userrRoleID = userResult.AspNetRoles.ToList()[0].Id;
                string userRoleName = RoleManager.FindById(userrRoleID).Name;

                // Check if role has been changed
                /************** DISABLING CHANGE ROLE IMPLEMENTATION/ UNCOMMENT TO RUN 
                if (userrRoleID != model.AspNetUserModel.RoleId)
                 {
                     // Update User Role
                     UserManager.RemoveFromRole(model.AspNetUserModel.Id, userRoleName);
                     UserManager.AddToRole(model.AspNetUserModel.Id, roleName);
                     TempData["message"] = new MessageViewModel { Message = "Role has been updated", IsUpdated = true };
                 }************************/
                // Password Reset
                if (!String.IsNullOrEmpty(model.AspNetUserModel.Password))
                {
                    var token = await UserManager.GeneratePasswordResetTokenAsync(model.AspNetUserModel.Id);
                    var resetPwdResults = await UserManager.ResetPasswordAsync(model.AspNetUserModel.Id, token, model.AspNetUserModel.Password);

                    if (resetPwdResults.Succeeded)
                    {
                        var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
                        if (user != null)
                        {
                            await SignInAsync(user, isPersistent: false);
                        }
                        TempData["message"] = new MessageViewModel
                        {
                            //Message = TMD.Web.Resources.HR.Account.UpdatePass,
                            IsUpdated = true
                        };
                    }
                }
                // Get user by UserId to Update User
                AspNetUser userToUpdate = UserManager.FindById(model.AspNetUserModel.Id);
                //if (userToUpdate.Email != model.AspNetUserModel.Email)
                //{

                if (userToUpdate != null)
                {
                    userToUpdate.UpdateUserTo(model.AspNetUserModel);
                }
                var updateUserResult = await UserManager.UpdateAsync(userToUpdate);
                if (updateUserResult.Succeeded)
                {
                    TempData["message"] = new MessageViewModel
                    {
                        Message = "User has been Updated",
                        IsUpdated = true
                    };
                }
                //}

                return RedirectToAction("Users");
            }
            #endregion
            // Add new User
            if (ModelState.IsValid)
            {
                // TODO:Check # of Users that Admin can create
                var user = new AspNetUser
                {
                    UserName = model.AspNetUserModel.UserName,
                    Email = model.AspNetUserModel.Email,
                    Address = model.AspNetUserModel.Address,
                    Telephone = model.AspNetUserModel.Telephone,
                    FirstName = model.AspNetUserModel.FirstName,
                    LastName = model.AspNetUserModel.LastName,
                    LockoutEnabled = false
                };
                user.EmailConfirmed = true;
                if (!String.IsNullOrEmpty(model.AspNetUserModel.Password))
                {
                    var result = await UserManager.CreateAsync(user, model.AspNetUserModel.Password);
                    if (result.Succeeded)
                    {
                        //Setting role
                        var roleManager = HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
                        var roleName = roleManager.FindById(model.AspNetUserModel.RoleId).Name;
                        UserManager.AddToRole(user.Id, roleName);
                        // Add User Preferences for Dashboards Widgets

                        TempData["message"] = new MessageViewModel
                        {
                            Message = "Employee has been created",
                            IsSaved = true
                        };
                        return RedirectToAction("Users");
                    }
                    else
                    {
                        var resultStr = "";
                        if (result.Errors.Count() > 0)
                            resultStr = result.Errors.ToList()[0].ToString();
                        TempData["message"] = new MessageViewModel
                        {
                            Message = resultStr,
                            IsError = true
                        };
                        ViewBag.MessageVM = TempData["message"] as MessageViewModel;
                    }
                }
            }
            // If we got this far, something failed, redisplay form
            model.Roles = HttpContext.GetOwinContext().Get<ApplicationRoleManager>().Roles.ToList();
            //TempData["message"] = new MessageViewModel { Message = TMD.Web.Resources.HR.Account.ChkFields, IsError = true };
            return View(model);
        }
예제 #2
0
        //
        // GET: /Account/Register
        //[SiteAuthorize(PermissionKey = "UserCreate")]
        public ActionResult Create(string userName)
        {
            // AspNetUsersViewModel Result = new AspNetUsersViewModel();
            AspNetUsersViewModel Result = new AspNetUsersViewModel();
            Result.AspNetUserModel = new AspNetUserModel();
            Result.Roles = RoleManager.Roles.Where(r => r.Name != "Admin").ToList();

            if (!string.IsNullOrEmpty(userName))
            {
                AspNetUser userToEdit = UserManager.FindByName(userName);
                Result.AspNetUserModel = userToEdit.CreateFrom();
                return View(Result);
            }
            return View(Result);
        }