public IHttpActionResult SetPropertyConut() { var goverments = _governmentService.GetAllGovernmentUnits(); var role = _accountUserService.GetAccountUserRoleBySystemName(SystemAccountUserRoleNames.ParentGovernmentorAuditor); foreach (var g in goverments) { g.PropertyConut = g.Properties.Count; if (g.ParentGovernmentId != 0) { var parent = _governmentService.GetGovernmentUnitById(g.ParentGovernmentId); g.ParentName = parent.Name; } _governmentService.UpdateGovernmentUnit(g); var users = g.Users; foreach (var user in users) { if (g.ParentGovernmentId == 0) { if (user.AccountUserRoles.Where(ur => ur.Name == SystemAccountUserRoleNames.ParentGovernmentorAuditor).Count() == 0) { user.AccountUserRoles.Add(role); _accountUserService.UpdateAccountUser(user); } } } } return(Ok("赋值完成")); }
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("添加用户失败")); } }
/// <summary> /// 用户注册 /// </summary> /// <param name="request"></param> /// <returns></returns> public virtual AccountUserRegistrationResult RegisterAccountUser(AccountUserRegistrationRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (request.AccountUser == null) { throw new ArgumentNullException("无法加载当前用户"); } var result = new AccountUserRegistrationResult(); if (request.AccountUser.IsSearchEngineAccount()) { result.AddError("搜索引擎用户无法注册"); return(result); } if (request.AccountUser.IsBackgroundTaskAccount()) { result.AddError("Background task account can't be registered"); return(result); } if (String.IsNullOrWhiteSpace(request.Password)) { result.AddError("密码不能为空"); return(result); } //暂时 if (_customerSettings.UsernamesEnabled) { if (String.IsNullOrEmpty(request.Username)) { result.AddError("用户名不能为空"); return(result); } } if (_accountUserService.GetAccountUserByUsername(request.Username) != null) { result.AddError(string.Format("用户名已经注册用户")); return(result); } //at this point request is valid request.AccountUser.UserName = request.Username; request.AccountUser.PasswordFormat = request.PasswordFormat; switch (request.PasswordFormat) { case PasswordFormat.Clear: { request.AccountUser.Password = request.Password; } break; case PasswordFormat.Encrypted: { request.AccountUser.Password = _encryptionService.EncryptText(request.Password); } break; case PasswordFormat.Hashed: { string saltKey = _encryptionService.CreateSaltKey(5); request.AccountUser.PasswordSalt = saltKey; request.AccountUser.Password = _encryptionService.CreatePasswordHash(request.Password, saltKey, _customerSettings.HashedPasswordFormat); } break; default: break; } request.AccountUser.Active = request.IsApproved; //add to 'Registered' role var registeredRole = _accountUserService.GetAccountUserRoleBySystemName(SystemAccountUserRoleNames.Registered); if (registeredRole == null) { throw new CSCZJException("'Registered' role could not be loaded"); } request.AccountUser.AccountUserRoles.Add(registeredRole); ////remove from 'Guests' role //var guestRole = request.AccountUser.AccountUserRoles.FirstOrDefault(cr => cr.SystemName == SystemAccountUserRoleNames.Guests); //if (guestRole != null) // request.AccountUser.AccountUserRoles.Remove(guestRole); //_accountUserActivityService.UpdateAccountUser(request.AccountUser); return(result); }