Exemplo n.º 1
0
        private void PrepareRoleViewModel(MemberShipRoleViewModel model, MembershipRole entity)
        {
            model.isAdmin = (WorkContext.CurrentMembershipUser.MembershipRoles.Any(x => x.Id == new Guid("7a2f0eca-4daf-4aa5-8c1d-9cffd6aad69f")));

            if (model.isAdmin)
            {
                IList <SelectListItem> selList = new List <SelectListItem>();

                var companyList = _companyService.GetAll();
                if (entity == null)
                {
                    foreach (var item in companyList)
                    {
                        selList.Add(new SelectListItem {
                            Text = item.CompanyName, Value = item.Id.ToString(), Selected = (item.Id == WorkContext.CurrentMembershipUser.Company.Id)
                        });
                    }
                }
                else
                {
                    foreach (var item in companyList)
                    {
                        selList.Add(new SelectListItem {
                            Text = item.CompanyName, Value = item.Id.ToString(), Selected = (item.Id == entity.Company.Id)
                        });
                    }
                }
                model.Companys = selList;
            }
        }
Exemplo n.º 2
0
 public ActionResult CreateOrUpdate(Guid?id = null)
 {
     try
     {
         if (id != null)
         {
             var entity = _membershipService.GetRole(id.Value);
             if (entity != null)
             {
                 var model = new MemberShipRoleViewModel {
                     Id      = entity.Id,
                     Name    = entity.Name,
                     Company = entity.Company
                 };
                 PrepareRoleViewModel(model, entity);
                 return(View(model));
             }
             else
             {
                 ErrorNotification(new Exception("加载失败,未找到该角色"));
                 return(RedirectToAction("RoleManage"));
             }
         }
         else
         {
             MemberShipRoleViewModel model = new MemberShipRoleViewModel();
             MembershipUser          user  = WorkContext.CurrentMembershipUser;
             PrepareRoleViewModel(model, null);
             return(View(model));
         }
     }
     catch (Exception ex)
     {
         ErrorNotification(new Exception("角色编辑页面加载失败" + ex.Message));
         return(RedirectToAction("RoleManage"));
     }
 }
Exemplo n.º 3
0
        public ActionResult CreateOrUpdate(MemberShipRoleViewModel model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.Name))
                {
                    ModelState.AddModelError("Name", "角色名不能为空");
                }
                if (ModelState.IsValid)
                {
                    if (model.Id.ToString() == "00000000-0000-0000-0000-000000000000")
                    {
                        Guid company_Id = Guid.Empty;
                        if (model.isAdmin)
                        {
                            company_Id = Guid.Parse(model.Company_Id);
                        }
                        else
                        {
                            company_Id = WorkContext.CurrentMembershipUser.Company.Id;
                        }

                        var company = _companyService.GetById(company_Id);

                        #region 新增
                        using (var unitOfwork = UnitOfWorkManager.NewUnitOfWork())
                        {
                            var entity = new MembershipRole {
                                Id          = Guid.NewGuid(),
                                Name        = model.Name,
                                Create_Date = DateTime.Now,
                                Create_By   = WorkContext.CurrentMembershipUser.Username,
                                Company     = company
                            };

                            _membershipService.AddRole(entity);

                            unitOfwork.Commit();

                            SuccessNotification("添加成功");
                            PrepareRoleViewModel(model, entity);
                            return(View(model));
                        }
                        #endregion
                    }
                    else
                    {
                        #region 修改
                        var  entity     = _membershipService.GetRole(model.Id);
                        Guid company_Id = Guid.Empty;
                        if (model.isAdmin)
                        {
                            company_Id = Guid.Parse(model.Company_Id);
                        }
                        else
                        {
                            company_Id = WorkContext.CurrentMembershipUser.Company.Id;
                        }

                        var company = _companyService.GetById(company_Id);

                        if (entity != null)
                        {
                            using (var unitOfwork = UnitOfWorkManager.NewUnitOfWork())
                            {
                                entity.Name        = model.Name;
                                entity.Update_By   = WorkContext.CurrentMembershipUser.Username;
                                entity.Update_Date = DateTime.Now;
                                entity.Company     = company;
                                unitOfwork.Commit();

                                SuccessNotification("编辑成功");
                                PrepareRoleViewModel(model, entity);
                                return(View(model));
                            }
                        }
                        else
                        {
                            ErrorNotification(new Exception("编辑失败,未找到对应的角色"));
                            return(RedirectToAction("RoleManage"));
                        }
                        #endregion
                    }
                }
                else
                {
                    ErrorNotification(new Exception("编辑失败,输入信息有误"));
                    //PrepareCompanyViewModel(model, null);
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                ErrorNotification(new Exception("编辑失败" + ex.Message));
                return(RedirectToAction("RoleManage"));
            }
        }