/// <summary> /// Converts a UserEntity object to a User object. /// </summary> /// <param name="entity">The UserEntity that has to be converted.</param> /// <returns>The user object.</returns> public User UserEntityToUser(UserEntity entity) { User user = new User(); user.Id = entity.Id; user.FirstName = entity.FirstName; user.Infix = entity.Infix; user.LastName = entity.LastName; user.Type = entity.Type; user.CreationTime = entity.CreationTime; user.Sources = SourceEntityListToSources(entity.Sources); return user; }
/// <summary> /// Method for creating a new user based on an instance of class OpenIDUserInfo. /// </summary> /// <param name="userInfo">The instance of class OpenIDUserInfo that will be used to create the new user.</param> /// <param name="createdUser">Reference to an object of class User - will be set to an instance of class User on success or null if the user could not be created.</param> /// <returns>Boolean value indicating if the user could be created.</returns> public bool TryCreateNewUser(OpenIDUserInfo userInfo, out User createdUser) { // User object that will contain the newly created User object on success createdUser = null; // Only attempt to create a new user when an instance of class OpenIDUserInfo has been supplied if (userInfo != null) { // No user could be matched; create a new user based on the retrieved user info UserEntity userEntity = null; using (IntelliCloudContext context = new IntelliCloudContext()) { // Create a new user based on the retrieved user info userEntity = new UserEntity() { FirstName = userInfo.GivenName, LastName = userInfo.FamilyName, Type = UserType.Customer, CreationTime = DateTime.UtcNow }; context.Users.Add(userEntity); // Create a new source for the user's email address SourceDefinitionEntity mailSourceDefinition = context.SourceDefinitions.SingleOrDefault(sd => sd.Name.Equals("Mail")); SourceEntity mailSourceEntity = new SourceEntity() { Value = userInfo.Email, CreationTime = DateTime.UtcNow, SourceDefinition = mailSourceDefinition, User = userEntity, }; context.Sources.Add(mailSourceEntity); // Create a new source for the user's id from the issuer SourceDefinitionEntity issuerSourceDefinition = context.SourceDefinitions.SingleOrDefault(sd => sd.Name.Equals(userInfo.Issuer)); SourceEntity issuerSourceEntity = new SourceEntity() { Value = userInfo.Sub, CreationTime = DateTime.UtcNow, SourceDefinition = issuerSourceDefinition, User = userEntity, }; context.Sources.Add(issuerSourceEntity); // Save the changes to the context context.SaveChanges(); } // Convert the UserEntity instance to an instance of class User and set it in the matchedUser variable createdUser = this.convertEntities.UserEntityToUser(userEntity); } // Return true or false indicating if the user has been created return (createdUser != null) ? true : false; }
/// <summary> /// Method for matching a user based on an instance of class OpenIDUserInfo. /// </summary> /// <param name="userInfo">The instance of class OpenIDUserInfo that will be used to match a user.</param> /// <param name="matchedUser">Reference to an object of class User - will be set to an instance of class User on success or null if no user could be matched.</param> /// <returns>Boolean value indicating if a user could be matched.</returns> public bool TryMatchUser(OpenIDUserInfo userInfo, out User matchedUser) { // User object that will contain the matched User object on success matchedUser = null; // Only attempt to match a user when an instance of class OpenIDUserInfo has been supplied if (userInfo != null) { using (IntelliCloudContext context = new IntelliCloudContext()) { // Get the user entity from the context UserEntity userEntity = context.Users .Include(u => u.Sources.Select(s => s.SourceDefinition)) .SingleOrDefault(s => s.Sources.Any(a => (a.SourceDefinition.Name == "Mail" && a.Value == userInfo.Email) || (a.SourceDefinition.Name == userInfo.Issuer && a.Value == userInfo.Sub))); // Only continue if the user entity was found if (userEntity != null) { // Update the user's first name and last name userEntity.FirstName = userInfo.GivenName; userEntity.LastName = userInfo.FamilyName; // Update the user's id from the issuer userEntity.Sources.Where(s => s.SourceDefinition.Name == userInfo.Issuer) .Select(s => { s.Value = userInfo.Sub; return s; }); // Save the changes to the context context.SaveChanges(); // Convert the UserEntity to an instance of class User and set in the reference matchedUser = this.convertEntities.UserEntityToUser(userEntity); } } } // Return true or false indicating if a user could be matched return (matchedUser != null) ? true : false; }