Exemplo n.º 1
0
 public ActionResult EditUserPost(EditUserModel viewModel)
 {
     try
     {
         if (viewModel.Id.HasValue)
         {
             _userAction.UpdateUser(viewModel);
         }
         else
         {
             _userAction.NewUser(viewModel);
         }
         return RedirectToMessage("用户信息保存成功!");
     }
     catch (Exception exception)
     {
         return RedirectToMessage("保存用户时发生错误:" + exception.Message);
     }
 }
Exemplo n.º 2
0
 public EditUserModel InitializeEditUserModel(Guid? id)
 {
     var model = new EditUserModel();
     if (id.HasValue)
     {
         var user = _userRepository.Find(id.Value);
         model.FormData.Account = user.Account;
         model.FormData.DepartmentId = user.Department != null ? user.Department.Id : default(Guid?);
         model.FormData.Mobile = user.PhoneBookRecord != null ? user.PhoneBookRecord.Mobile : string.Empty;
         model.FormData.Name = user.Name;
         model.FormData.QQ = user.QQ;
         model.FormData.OrderSort = user.OrderSort;
         var roleUserRepository = new RoleUserRepository();
         var roleUser = roleUserRepository.GetRoleUserByUserId(id.Value);
         if (roleUser != null)
             model.FormData.RoleNames = string.Join(",", roleUser.Roles.Select(o => o.Code));
     }
     var roleRepository = new RoleRepository();
     model.Roles = roleRepository.GetAllRoleByUnitId(NpcContext.CurrentUser.Unit.Id);
     return model;
 }
Exemplo n.º 3
0
 private void FillUser(User user, EditUserModel viewModel)
 {
     if (viewModel.FormData.DepartmentId == null)
         throw new ArgumentException("所属部门不能为空");
     user.Name = viewModel.FormData.Name;
     if (!(string.IsNullOrEmpty(viewModel.FormData.Pwd) || string.IsNullOrEmpty(viewModel.FormData.RePwd)) && viewModel.FormData.Pwd == viewModel.FormData.RePwd)
     {
         user.Pwd = Md5Utility.GetMd5HashCode(viewModel.FormData.Pwd);
     }
     user.QQ = viewModel.FormData.QQ;
     user.OrderSort = viewModel.FormData.OrderSort;
     user.Department = _departmentRepository.Find(viewModel.FormData.DepartmentId.Value);
 }
Exemplo n.º 4
0
 public void UpdateUser(EditUserModel viewModel)
 {
     var userInContext = NpcContext.CurrentUser;
     if (viewModel.Id == null)
         throw new ArgumentException("id不能为空");
     var user = _userRepository.Find(viewModel.Id.Value);
     FillUser(user, viewModel);
     if (user.PhoneBookRecord == null)
     {
         var phoneBookService = new PhoneBookService();
         var phoneBook = phoneBookService.CreateOrGetDefaultPhoneBook(userInContext.Unit);
         var phoneBookRecord = new PhoneBookRecord();
         phoneBookRecord.Mobile = viewModel.FormData.Mobile;
         phoneBookRecord.Name = viewModel.FormData.Name;
         phoneBookRecord.User = user;
         phoneBookRecord.PhoneBook = phoneBook;
         phoneBookRecord.RecordDescription.CreateBy(userInContext);
         _phoneBookRecordRepository.Save(phoneBookRecord);
         user.PhoneBookRecord = phoneBookRecord;
     }
     else
     {
         user.PhoneBookRecord.Name = viewModel.FormData.Name;
         user.PhoneBookRecord.RecordDescription.UpdateBy(userInContext);
         _phoneBookRecordRepository.Save(user.PhoneBookRecord);
     }
     _userRepository.Save(user);
     if (!string.IsNullOrEmpty(viewModel.FormData.RoleNames))
         SaveOrUpdateRoleUser(user.Id, viewModel.FormData.RoleNames.Split(new[] { ',' }));
 }
Exemplo n.º 5
0
 public void NewUser(EditUserModel viewModel)
 {
     var userInContext = NpcContext.CurrentUser;
     if (_userRepository.IsRepeatAccount(viewModel.FormData.Mobile.Trim(), userInContext.Unit.Id))
     {
         throw new ArgumentException(string.Format("手机{0}已被使用,请选择其它的手机号码", viewModel.FormData.Mobile));
     }
     var user = new User();
     user.Account = viewModel.FormData.Mobile;
     FillUser(user, viewModel);
     user.Unit = userInContext.Unit;
     user.RecordDescription.CreateBy(userInContext);
     _userRepository.Save(user);
     var phoneBookService = new PhoneBookService();
     var phoneBook = phoneBookService.CreateOrGetDefaultPhoneBook(userInContext.Unit);
     var phoneBookRecord = new PhoneBookRecord();
     phoneBookRecord.Mobile = viewModel.FormData.Mobile;
     phoneBookRecord.Name = viewModel.FormData.Name;
     phoneBookRecord.User = user;
     phoneBookRecord.PhoneBook = phoneBook;
     phoneBookRecord.RecordDescription.CreateBy(userInContext);
     _phoneBookRecordRepository.Save(phoneBookRecord);
     user.PhoneBookRecord = phoneBookRecord;
     _userRepository.Save(user);
     if (!string.IsNullOrEmpty(viewModel.FormData.RoleNames))
         SaveOrUpdateRoleUser(user.Id, viewModel.FormData.RoleNames.Split(new[] { ',' }));
 }