コード例 #1
0
        // GET: Employees/Edit/5
        public ActionResult Edit(string id)
        {
            // idが無い場合、不正なリクエストとして処理
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            // DBからidで検索して該当するユーザーを取得
            ApplicationUser applicationUser = db.Users.Find(id);

            if (applicationUser == null)
            {
                return(HttpNotFound());
            }
            // ビューモデルにデータを詰め替える
            EmployeesEditViewModel employee = new EmployeesEditViewModel
            {
                Id           = applicationUser.Id,
                Email        = applicationUser.Email,
                EmployeeName = applicationUser.EmployeeName
            };

            //従業員の権限(role)がAdminならAdminに、そうでなければNormalにする。
            if (UserManager.IsInRole(applicationUser.Id, "Admin"))
            {
                employee.AdminFlag = RolesEnum.Admin;
            }
            else
            {
                employee.AdminFlag = RolesEnum.Normal;
            }

            return(View(employee));
        }
コード例 #2
0
        // GET: Employees/Edit/5
        public ActionResult Edit(string id)
        {
            // idが無い場合、不正なリクエストとして処理
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            // DBからidで検索して該当するユーザーを取得
            ApplicationUser applicationUser = db.Users.Find(id);

            if (applicationUser == null)
            {
                return(HttpNotFound());
            }
            // ビューモデルにデータを詰め替える
            EmployeesEditViewModel employee = new EmployeesEditViewModel
            {
                Id           = applicationUser.Id,
                Email        = applicationUser.Email,
                EmployeeName = applicationUser.EmployeeName
            };

            if (UserManager.IsInRole(applicationUser.Id, "Chief"))
            {
                employee.AdminFlag = RolesEnum.Chief;
            }
            else if (UserManager.IsInRole(applicationUser.Id, "Manager"))
            {
                employee.AdminFlag = RolesEnum.Manager;
            }
            else if (UserManager.IsInRole(applicationUser.Id, "GeneralManager"))
            {
                employee.AdminFlag = RolesEnum.GeneralManager;
            }
            else if (UserManager.IsInRole(applicationUser.Id, "ManagingDirector"))
            {
                employee.AdminFlag = RolesEnum.ManagingDirector;
            }
            else if (UserManager.IsInRole(applicationUser.Id, "President"))
            {
                employee.AdminFlag = RolesEnum.President;
            }
            else
            {
                employee.AdminFlag = RolesEnum.Normal;
            }
            return(View(employee));
        }
コード例 #3
0
        public async Task <IActionResult> EditEmp(Guid id, EmployeesEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                await _employeesManage.EditEmp(id, model.Password, model.Name, model.Sex, model.Age,
                                               model.Phone, model.Email, model.Address, model.Image, model.Remarks, model.Status, model.BranchId);

                return(Ok(new EndState()
                {
                    Code = 200, IsSucceed = true, ErrorMessage = "修改成功"
                }));
            }
            return(Ok(new EndState()
            {
                Code = 500, IsSucceed = false, ErrorMessage = "数据模型验证失败"
            }));
        }
コード例 #4
0
        public ActionResult Terminate(EmployeesEditViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var employee = viewModel.Employee;


                _empRepo.Terminate(employee);

                TempData["Message"] = "Employee terminated.";

                return(RedirectToAction("Detail", new { id = employee.Id }));
            }

            viewModel.Init(Context);

            return(View(viewModel));
        }
コード例 #5
0
        public ActionResult Edit(EmployeesEditViewModel viewModel)
        {
            ValidateEmployee(viewModel.Employee);

            if (ModelState.IsValid)
            {
                var employee = viewModel.Employee;

                _empRepo.Update(employee);

                TempData["Message"] = "Employee was successfully updated!";

                return(RedirectToAction("Detail", new { id = employee.Id }));
            }

            viewModel.Init(Context);

            return(View(viewModel));
        }
コード例 #6
0
 public ActionResult <Employees> PutEmployees(int?ID, EmployeesEditViewModel viewModel)
 {
     if (ID == null)
     {
         return(BadRequest("The ID is not Found in Request"));
     }
     if (ModelState.IsValid)
     {
         try
         {
             Employees employees = EmployeesRepostiry.Get(ID.Value);
             Mapper.Map <EmployeesEditViewModel, Employees>(viewModel, employees);
             EmployeesRepostiry.Edit(employees);
             EmployeesRepostiry.SaveAll();
             return(Ok());
         }
         catch (DBConcurrencyException)
         {
             return(Conflict());
         }
     }
     return(BadRequest("Error Employees Data is Not valid"));
 }
コード例 #7
0
        public ActionResult Terminate(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var employee = _empRepo.Get((int)id);

            if (employee == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new EmployeesEditViewModel()
            {
                Employee = employee
            };

            viewModel.Init(Context);

            return(View(viewModel));
        }
コード例 #8
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Email,EmployeeName,Password,AdminFlag")] EmployeesEditViewModel employee)
        {
            if (ModelState.IsValid)
            {
                // DBからidのユーザーを検索し取得。そのユーザーに対し変更をする
                ApplicationUser applicationUser = db.Users.Find(employee.Id);
                // IdentityアカウントのUserNameにはメールアドレスを入れる必要がある
                applicationUser.UserName     = employee.Email;
                applicationUser.Email        = employee.Email;
                applicationUser.EmployeeName = employee.EmployeeName;
                applicationUser.UpdatedAt    = DateTime.Now;
                // passwordが空でなければパスワード変更する。
                if (!String.IsNullOrEmpty(employee.Password))
                {
                    // パスワードの入力検証
                    var result = await UserManager.PasswordValidator.ValidateAsync(employee.Password);

                    // パスワードの検証に失敗したら、エラーを追加しEditビューをもう一度描画
                    if (!result.Succeeded)
                    {
                        AddErrors(result);
                        return(View(employee));
                    }
                    // パスワードはハッシュ化したものをDBに登録する必要があるので、PasswordHasherでハッシュ化する
                    applicationUser.PasswordHash = UserManager.PasswordHasher.HashPassword(employee.Password);
                }
                // StateをModifiedにしてUPDATE文を行うように設定
                db.Entry(applicationUser).State = EntityState.Modified;
                db.SaveChanges();

                // mode.AdminFlagの内容によって、処理をswitchで変える。
                switch (employee.AdminFlag)
                {
                case RolesEnum.Chief:
                    if (UserManager.IsInRole(applicationUser.Id, "Chief"))
                    {
                        break;
                    }
                    UserManager.AddToRole(applicationUser.Id, "Chief");
                    break;

                case RolesEnum.Manager:
                    if (UserManager.IsInRole(applicationUser.Id, "Manager"))
                    {
                        break;
                    }
                    UserManager.AddToRole(applicationUser.Id, "Manager");
                    break;

                case RolesEnum.GeneralManager:
                    if (UserManager.IsInRole(applicationUser.Id, "GeneralManager"))
                    {
                        break;
                    }
                    UserManager.AddToRole(applicationUser.Id, "GeneralManager");
                    break;

                case RolesEnum.ManagingDirector:
                    if (UserManager.IsInRole(applicationUser.Id, "ManagingDirector"))
                    {
                        break;
                    }
                    UserManager.AddToRole(applicationUser.Id, "ManagingDirector");
                    break;

                case RolesEnum.President:
                    if (UserManager.IsInRole(applicationUser.Id, "President"))
                    {
                        break;
                    }
                    UserManager.AddToRole(applicationUser.Id, "President");
                    break;

                default:
                    if (UserManager.IsInRole(applicationUser.Id, "Chief"))
                    {
                        UserManager.RemoveFromRole(applicationUser.Id, "Chief");
                    }
                    if (UserManager.IsInRole(applicationUser.Id, "Manager"))
                    {
                        UserManager.RemoveFromRole(applicationUser.Id, "Manager");
                    }
                    if (UserManager.IsInRole(applicationUser.Id, "GeneralManager"))
                    {
                        UserManager.RemoveFromRole(applicationUser.Id, "GeneralManager");
                    }
                    if (UserManager.IsInRole(applicationUser.Id, "ManagingDirector"))
                    {
                        UserManager.RemoveFromRole(applicationUser.Id, "ManagingDirector");
                    }
                    if (UserManager.IsInRole(applicationUser.Id, "President"))
                    {
                        UserManager.RemoveFromRole(applicationUser.Id, "President");
                    }
                    break;
                }

                // TempDataにフラッシュメッセージを入れておく。TempDataは現在のリクエストと次のリクエストまで存在
                TempData["flush"] = String.Format("{0}さんの情報を更新しました。", applicationUser.EmployeeName);

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

            return(View(employee));
        }