Ejemplo n.º 1
0
        public ActionResult Create()
        {
            var model = new MembershipUserEditViewModel();

            PrepareUserCreateViewModel(model, null);
            return(View(model));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(MembershipUserEditViewModel model)
        {
            try
            {
                using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                {
                    var user = MembershipService.GetUser(model.Id.Value);
                    if (user != null)
                    {
                        if (user.Username != model.UserName)
                        {
                            var user2 = MembershipService.GetUser(model.UserName);
                            if (user2 != null)
                            {
                                throw new Exception("该用户名已被占用!");
                            }
                        }

                        user.Username = model.UserName;
                        user.Email    = model.Email;
                        if (user.Company.Id.ToString() != model.Company_Id)
                        {
                            Guid company_Id = Guid.Parse(model.Company_Id);
                            var  company    = _companyService.GetById(company_Id);
                            user.Company = company;
                        }

                        if (!user.MembershipRoles.Any(x => x.Id.ToString() == model.Role_Id))
                        {
                            user.MembershipRoles.Clear();

                            Guid role_Id = Guid.Parse(model.Role_Id);
                            var  role    = MembershipService.GetRole(role_Id);
                            user.MembershipRoles.Add(role);
                        }


                        unitOfWork.Commit();
                        PrepareUserCreateViewModel(model, user);

                        SuccessNotification("编辑成功");
                        return(View(model));
                    }
                    else
                    {
                        throw new Exception("未能找到该用户!");
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorNotification(ex);
                return(RedirectToAction("Edit", new { id = model.Id }));
            }
        }
Ejemplo n.º 3
0
        private void PrepareUserCreateViewModel(MembershipUserEditViewModel model, MembershipUser entity)
        {
            List <SelectListItem> companylist = new List <SelectListItem>();
            var companys = _companyService.GetAll();

            if (entity == null)
            {
                foreach (var item in companys)
                {
                    companylist.Add(new SelectListItem {
                        Text = item.CompanyName, Value = item.Id.ToString(), Selected = (item.Id == WorkContext.CurrentMembershipUser.Company.Id)
                    });
                }
            }
            else
            {
                foreach (var item in companys)
                {
                    companylist.Add(new SelectListItem {
                        Text = item.CompanyName, Value = item.Id.ToString(), Selected = (item.Id == entity.Company.Id)
                    });
                }
            }
            model.Companys = companylist;

            List <SelectListItem> roleList = new List <SelectListItem>();

            if (entity == null)
            {
                var company_Id = string.IsNullOrEmpty(model.Company_Id) ? WorkContext.CurrentMembershipUser.Company.Id : Guid.Parse(model.Company_Id);
                var roles      = _membershipService.GetAllRolesByCompany(company_Id);
                foreach (var item in roles)
                {
                    roleList.Add(new SelectListItem {
                        Text = item.Name, Value = item.Id.ToString(), Selected = !string.IsNullOrEmpty(model.Role_Id) && Guid.Parse(model.Role_Id) == item.Id
                    });
                }
            }
            else
            {
                var roles = _membershipService.GetAllRolesByCompany(entity.Company.Id);
                foreach (var item in roles)
                {
                    roleList.Add(new SelectListItem {
                        Text = item.Name, Value = item.Id.ToString(), Selected = entity.MembershipRoles.Any(x => x.Id == item.Id)
                    });
                }
            }
            model.Roles = roleList;
        }
Ejemplo n.º 4
0
        public ActionResult Edit(Guid id)
        {
            var model = new MembershipUserEditViewModel();

            model.Id = id;

            var user = MembershipService.GetUser(id);

            if (user == null)
            {
                ErrorNotification(new Exception("未能找到该用户!"));
                return(RedirectToAction("Manage"));
            }
            else
            {
                model.UserName = user.Username;
                model.Email    = user.Email;
                PrepareUserCreateViewModel(model, user);
                return(View(model));
            }
        }
Ejemplo n.º 5
0
        public ActionResult Create(MembershipUserEditViewModel model)
        {
            try
            {
                using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                {
                    var user = _membershipService.GetUser(model.UserName);
                    if (user != null)
                    {
                        throw new Exception("用户名已被使用");
                    }
                    user           = new MembershipUser();
                    user.Username  = model.UserName;
                    user.Email     = model.Email;
                    user.Password  = "******";
                    user.Deleted   = false;
                    user.IsActived = true;

                    Guid role_Id = Guid.Parse(model.Role_Id);
                    var  role    = _membershipService.GetRole(role_Id);
                    user.MembershipRoles = new List <MembershipRole>();
                    user.MembershipRoles.Add(role);

                    Guid company_Id = Guid.Parse(model.Company_Id);
                    var  company    = _companyService.GetById(company_Id);
                    user.Company = company;

                    MembershipService.AddUser(user);
                    unitOfWork.Commit();
                    SuccessNotification(string.Format("用户{0}创建成功!", model.UserName));
                    return(RedirectToAction("Manage"));
                }
            }
            catch (Exception ex)
            {
                PrepareUserCreateViewModel(model, null);
                ErrorNotification(ex);
                return(View(model));
            }
        }