public IHttpActionResult UpdateAccount(int accountId, AccountUserModel accountModel) { var account = _accountService.GetAccountUserById(accountId); if (account == null || account.Deleted) { return(NotFound()); } account = accountModel.ToEntity(account); var registerRole = _accountService.GetAccountUserRoleBySystemName(SystemAccountUserRoleNames.Registered); account.AccountUserRoles.Clear(); var role = _accountService.GetAccountUserRoleBySystemName(accountModel.RoleName); if (role != null && accountModel.RoleName != SystemAccountUserRoleNames.Registered) { account.AccountUserRoles.Add(role); } account.AccountUserRoles.Add(registerRole); if (accountModel.InitPassword) { account.Password = "******"; //设置初始密码 } //单位更新 if (accountModel.GovernmentId != account.Government.Id) { var governament = _governmentService.GetGovernmentUnitById(accountModel.GovernmentId); if (governament == null) { return(BadRequest("用户所属单位不存在")); } account.Government = governament; } //保存用户 _accountService.UpdateAccountUser(account); //activity log _accountUserActivityService.InsertActivity("UpdateAccount", "更新 名为 {0} 的用户的基本信息", account.UserName); //SuccessNotification(_localizationService.GetResource("Admin.Catalog.Categories.Added")); return(Ok(account.ToModel())); }
public IHttpActionResult Create(AccountUserModel accountModel) { var account = accountModel.ToEntity(); //问题: An entity object cannot be referenced by multiple instances of IEntityChangeTracker //状态:Fixed //原因:不明,但是应该和缓存机制有关 var government = _governmentService.GetGovernmentUnitById(accountModel.GovernmentId); if (government == null) { return(BadRequest("用户所属单位不存在")); } account.Government = government; var registerRole = _accountService.GetAccountUserRoleBySystemName(SystemAccountUserRoleNames.Registered); var adminRole = _accountService.GetAccountUserRoleBySystemName(SystemAccountUserRoleNames.Administrators); account.Password = "******"; //设置初始密码 var role = _accountService.GetAccountUserRoleBySystemName(accountModel.RoleName); if (role != null && accountModel.RoleName != SystemAccountUserRoleNames.Registered) { account.AccountUserRoles.Add(role); } var registrationRequest = new AccountUserRegistrationRequest(account, account.UserName, account.Password, _accountUserSettings.DefaultPasswordFormat, accountModel.Active); var registrationResult = _accountUserRegistrationService.RegisterAccountUser(registrationRequest); if (registrationResult.Success) { //保存用户 _accountService.InsertAccountUser(account); //activity log _accountUserActivityService.InsertActivity("AddNewAccount", "增加 名为 {0} 的用户", account.UserName); return(Ok(account.ToModel())); } else { return(BadRequest("添加用户失败")); } }