private void RegisterResult(Result commandResult) { LoginOkResult login = commandResult as LoginOkResult; if (login != null) { AddJoinedUser(login.AuthorizedUsername); } RegisterOkResult register = commandResult as RegisterOkResult; if (register != null) { AddJoinedUser(register.FinalUsername); } }
public Result RegisterUser(User newUser) { // Enter the critical section in upgradeable mode. userDatabaseLock.EnterUpgradeableReadLock(); try { userDatabase = XDocument.Load(userDatabasePath); // Determine if the provided username has been already taken var sameUserName = (from u in userDatabase.Descendants("User") where u.Attribute("principal").Value.Equals(newUser.UserName) select u); // The username is already present. The user must choose another one. if (sameUserName.Count() != 0) { Result error = new RegisterErrorResult(); error.Comment = "Sorry, this UserName is already present."; return error; } else //Register the user { // Build the final username. string darPoolingUsername = newUser.UserName + "@" + baseHTTPAddress + NodeName; // Extract the next ID from the database int nextAvailableID = Convert.ToInt32( (from user in userDatabase.Descendants("User") orderby Convert.ToInt32(user.Element("UserID").Value) descending select user.Element("UserID").Value).FirstOrDefault()) + 1; newUser.UserID = nextAvailableID; // Create the XML entity that represent the User in the database. XElement newXmlUser = new XElement("User", new XElement("UserID", newUser.UserID), new XElement("UserName", darPoolingUsername), new XElement("Password", newUser.PasswordHash), // FIXME: should store hash, not password new XElement("Name", newUser.Name), new XElement("Sex", newUser.UserSex), new XElement("BirthDate", newUser.BirthDate), new XElement("Email", newUser.Email), new XElement("Smoker", newUser.Smoker), new XElement("SignupDate", newUser.SignupDate), new XElement("Whereabouts", newUser.Whereabouts) ); // Maintain the info about the provided username newXmlUser.SetAttributeValue("principal", newUser.UserName); //Register the user: upgrade to Write mode userDatabaseLock.EnterWriteLock(); //Console.WriteLine("{0} thread obtains the write lock", Thread.CurrentThread.Name); try { userDatabase.Element("Users").Add(newXmlUser); userDatabase.Save(userDatabasePath); } finally { //Console.WriteLine("{0} thread releases the write lock", Thread.CurrentThread.Name); userDatabaseLock.ExitWriteLock(); } RegisterOkResult success = new RegisterOkResult(); success.Comment = "User successfully registered! Your username is : " + darPoolingUsername; success.FinalUsername = darPoolingUsername; return success; } // End else } // End try upgradable finally { //Console.WriteLine("{0} thread releases the upgradeable lock", Thread.CurrentThread.Name); userDatabaseLock.ExitUpgradeableReadLock(); } }
public Result RegisterUser(User newUser) { // Enter the critical section in upgradeable mode. userDatabaseLock.EnterUpgradeableReadLock(); try { userDatabase = XDocument.Load(userDatabasePath); // Determine if the provided username has been already taken var sameUserName = (from u in userDatabase.Descendants("User") where u.Attribute("principal").Value.Equals(newUser.UserName) select u); // The username is already present. The user must choose another one. if (sameUserName.Count() != 0) { Result error = new RegisterErrorResult(); error.Comment = "Sorry, this UserName is already present."; return(error); } else //Register the user { // Build the final username. string darPoolingUsername = newUser.UserName + "@" + baseHTTPAddress + NodeName; // Extract the next ID from the database int nextAvailableID = Convert.ToInt32( (from user in userDatabase.Descendants("User") orderby Convert.ToInt32(user.Element("UserID").Value) descending select user.Element("UserID").Value).FirstOrDefault()) + 1; newUser.UserID = nextAvailableID; // Create the XML entity that represent the User in the database. XElement newXmlUser = new XElement("User", new XElement("UserID", newUser.UserID), new XElement("UserName", darPoolingUsername), new XElement("Password", newUser.PasswordHash), // FIXME: should store hash, not password new XElement("Name", newUser.Name), new XElement("Sex", newUser.UserSex), new XElement("BirthDate", newUser.BirthDate), new XElement("Email", newUser.Email), new XElement("Smoker", newUser.Smoker), new XElement("SignupDate", newUser.SignupDate), new XElement("Whereabouts", newUser.Whereabouts) ); // Maintain the info about the provided username newXmlUser.SetAttributeValue("principal", newUser.UserName); //Register the user: upgrade to Write mode userDatabaseLock.EnterWriteLock(); //Console.WriteLine("{0} thread obtains the write lock", Thread.CurrentThread.Name); try { userDatabase.Element("Users").Add(newXmlUser); userDatabase.Save(userDatabasePath); } finally { //Console.WriteLine("{0} thread releases the write lock", Thread.CurrentThread.Name); userDatabaseLock.ExitWriteLock(); } RegisterOkResult success = new RegisterOkResult(); success.Comment = "User successfully registered! Your username is : " + darPoolingUsername; success.FinalUsername = darPoolingUsername; return(success); } // End else } // End try upgradable finally { //Console.WriteLine("{0} thread releases the upgradeable lock", Thread.CurrentThread.Name); userDatabaseLock.ExitUpgradeableReadLock(); } }// End RegisterUser