public IHttpActionResult UpdateUser(int id, UserDO user) { User oldUser = this.unitOfWork.DbContext.Set<User>() .Include(e => e.Roles) .SingleOrDefault(e => e.UserId == id); //UnitUser oldUnitUser = this.unitOfWork.DbContext.Set<UnitUser>() // .SingleOrDefault(e => e.UserId == id); //if (oldUser == null) //{ // return NotFound(); //} oldUser.Fullname = user.Fullname; oldUser.CertificateThumbprint = user.CertificateThumbprint.Replace(" ", null).ToUpperInvariant(); oldUser.IsActive = user.IsActive; oldUser.AppointmentDate = user.AppointmentDate; oldUser.ResignationDate = user.ResignationDate; oldUser.Notes = user.Notes; oldUser.Version = user.Version; oldUser.Email = user.Email; //if (oldUnitUser != null) //{ // oldUnitUser.UnitId = user.UnitId.Value; //} //else //{ // UnitUser unitUser = new UnitUser(); // unitUser.UserId = oldUser.UserId; // unitUser.UnitId = user.UnitId.Value; // unitUser.IsActive = true; // this.unitOfWork.DbContext.Set<UnitUser>().Add(unitUser); //} if (!String.IsNullOrEmpty(user.Password)) { oldUser.SetPassword(user.Password); } bool newRole; bool oldRole; foreach (var role in this.unitOfWork.DbContext.Set<Role>().ToList()) { newRole = user.Roles.Any(r => r.RoleId == role.RoleId); oldRole = oldUser.Roles.Any(r => r.RoleId == role.RoleId); if (newRole && !oldRole) oldUser.Roles.Add(this.unitOfWork.DbContext.Set<Role>().Find(role.RoleId)); if (!newRole && oldRole) oldUser.Roles.Remove(this.unitOfWork.DbContext.Set<Role>().Find(role.RoleId)); } this.unitOfWork.Save(); // int unitId = user.UnitId.Value; //var oldUnitClassifications = this.unitOfWork.DbContext.Set<UnitClassification>() // .Where(e => e.UnitId == unitId) // .ToList(); //this.unitOfWork.DbContext.Set<UnitClassification>().RemoveRange(oldUnitClassifications); //List<int> roleIds = user.Roles.Select(e => e.RoleId).ToList(); //var roleClassifications = this.unitOfWork.DbContext.Set<RoleClassification>() // .Where(e => roleIds.Contains(e.RoleId)) // .ToList(); //foreach (var item in roleClassifications) //{ // UnitClassification uc = new UnitClassification(); // uc.UnitId = unitId; // uc.ClassificationId = item.ClassificationId; // uc.ClassificationPermissionId = item.ClassificationPermissionId; // this.unitOfWork.DbContext.Set<UnitClassification>().Add(uc); //} this.unitOfWork.Save(); this.userRepository.spSetUnitTokens(unitId); return Ok(); }
public IHttpActionResult CreateUser(UserDO user) { User newUser = new User(); newUser.Username = user.Username; newUser.Fullname = user.Fullname; newUser.HasPassword = true; newUser.CertificateThumbprint = user.CertificateThumbprint.Replace(" ", null).ToUpperInvariant(); newUser.IsActive = user.IsActive; newUser.AppointmentDate = user.AppointmentDate; newUser.ResignationDate = user.ResignationDate; newUser.Notes = user.Notes; newUser.Version = user.Version; newUser.Email = user.Email; //UnitUser unitUser = new UnitUser(); //unitUser.User = newUser; //unitUser.UnitId = user.UnitId.Value; //unitUser.IsActive = true; newUser.SetPassword(user.Password); foreach (var role in user.Roles) { newUser.Roles.Add(this.unitOfWork.DbContext.Set<Role>().Find(role.RoleId)); } this.unitOfWork.DbContext.Set<User>().Add(newUser); //this.unitOfWork.DbContext.Set<UnitUser>().Add(unitUser); this.unitOfWork.Save(); // //int unitId = user.UnitId.Value; //var oldUnitClassifications = this.unitOfWork.DbContext.Set<UnitClassification>() // .Where(e => e.UnitId == unitId) // .ToList(); //this.unitOfWork.DbContext.Set<UnitClassification>().RemoveRange(oldUnitClassifications); List<int> roleIds = user.Roles.Select(e => e.RoleId).ToList(); //var roleClassifications = this.unitOfWork.DbContext.Set<RoleClassification>() // .Where(e => roleIds.Contains(e.RoleId)) // .ToList(); //foreach (var item in roleClassifications) //{ // UnitClassification uc = new UnitClassification(); // uc.UnitId = unitId; // uc.ClassificationId = item.ClassificationId; // uc.ClassificationPermissionId = item.ClassificationPermissionId; // this.unitOfWork.DbContext.Set<UnitClassification>().Add(uc); //} this.unitOfWork.Save(); //this.userRepository.spSetUnitTokens(unitId); return Ok(); }
public IHttpActionResult GetUser(int id) { User user = this.unitOfWork.DbContext.Set<User>() .Include(e => e.Roles) .SingleOrDefault(e => e.UserId == id); //UnitUser unitUser = this.unitOfWork.DbContext.Set<UnitUser>() // .SingleOrDefault(e => e.UserId == id); UserDO returnValue = new UserDO(user); //if (unitUser != null) //{ // returnValue.UnitId = unitUser.UnitId; //} return Ok(returnValue); }