public void CreateOrUpdateUserAndRolesPackage(UserAndRolesPackage userPackage) { try { if (userPackage != null && userPackage.User != null) { using (SmartWorkingEntities context = new SmartWorkingEntities()) { User existingObject = context.Users.Include("Roles").Where(x => x.Id == userPackage.User.Id).FirstOrDefault(); //no record of this item in the DB, item being passed in has a PK if (existingObject == null && userPackage.User.Id > 0) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(new Exception("Błąd zapisu do bazy")), "Obiekt nie istniał w bazie, a jego Id jest większe od 0."); } //Item has no PK value, must be new //Item has no PK value, must be new); if (userPackage.User.Id <= 0) { User user = userPackage.User.GetEntity(); user.PasswordSalz = GenerateSalt(); user.Password = EncodePassword(user.Password, user.PasswordSalz); context.Users.AddObject(user); foreach (RolePrimitive rolePrimitive in userPackage.Roles) { Role role = rolePrimitive.GetEntity(); role.Users.Add(user); } } //Item was retrieved, and the item passed has a valid ID, do an update else { List<RolePrimitive> existingElements = existingObject.Roles.Select(x => x.GetPrimitive()).ToList(); List<RolePrimitive> newElements = userPackage.Roles.ToList(); List<RolePrimitive> theSameElements = newElements.Where(x => existingElements.Select(y => y.Id).Contains(x.Id)).ToList(); existingElements.RemoveAll(x => theSameElements.Select(y => y.Id).Contains(x.Id)); newElements.RemoveAll(x => theSameElements.Select(y => y.Id).Contains(x.Id)); //remove if (existingElements.Count() > 0) { List<int> existingRecipeComponentIds = existingElements.Select(x => x.Id).ToList(); List<RolePrimitive> recipeComponentListToDelete = context.Roles.Where(x => existingRecipeComponentIds.Contains(x.Id)).Select(y => y.GetPrimitive()).ToList(); foreach (RolePrimitive recipeComponent in recipeComponentListToDelete) { context.Roles.DeleteObject(recipeComponent.GetEntity()); } } //add foreach (RolePrimitive newRecipeComponent in newElements) { context.Roles.AddObject(newRecipeComponent.GetEntity()); } //if is empty don't change password if (string.IsNullOrEmpty(userPackage.User.Password)) { userPackage.User.Password = existingObject.Password; } else { userPackage.User.Password = EncodePassword(userPackage.User.Password, existingObject.PasswordSalz); } context.Users.ApplyCurrentValues(userPackage.User.GetEntity()); } context.SaveChanges(); } } else { throw new Exception("str_Inpute parameter was wrong."); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public static UserAndRolesPackage GetPackageCopy(this UserAndRolesPackage source) { UserAndRolesPackage result = new UserAndRolesPackage(); if (source != null) { if (source.User != null) { result.User = source.User.GetPrimitiveCopy(); foreach (RolePrimitive role in source.Roles) { result.Roles.Add(role.GetPrimitiveCopy()); } } } return result; }
public static UserAndRolesPackage GetUserAndRolesPackage(this User user) { UserAndRolesPackage result = new UserAndRolesPackage(); if (user != null) { result.User = user.GetPrimitive(); if (user.Roles != null) { foreach (Role role in user.Roles) { result.Roles.Add(role.GetPrimitive()); } } } return result; }