public bool deleteUser(int Id) { Database.Domain.SystemUser systemUser = systemUserRepository.GetById(Id); if (systemUser == null) { return(false); } if (systemUserRepository.Remove(systemUser)) { systemUserCache.remove(Id); return(true); } return(false); //SqlConnection connection = new SqlConnection(connectionString); //SqlCommand cmd = new SqlCommand(); //cmd.CommandText = "DELETE FROM SystemUsers WHERE Id=@Id"; //cmd.CommandType = CommandType.Text; //cmd.Connection = connection; //cmd.Parameters.AddWithValue("@Id", Id); //connection.Open(); //bool ans = cmd.ExecuteNonQuery() > 0; //connection.Close(); //return ans; }
public SystemUser getUserByEmail(string email) { SystemUser systemUser = systemUserCache.getByEmail(email); if (systemUser != null) { return(systemUser); } Database.Domain.SystemUser DatabaseUser = systemUserRepository.GetByEmail(email); if (DatabaseUser == null) { return(null); } systemUser = new SystemUser(DatabaseUser.Id, DatabaseUser.UserName, DatabaseUser.Email, DatabaseUser.Image, DatabaseUser.Money, DatabaseUser.Rank, DatabaseUser.GamesPlayed); // Try to get the image from the database. try { // Get the user's profile picture file from memory. var returnedImage = Image.FromFile(systemUser.userImage); // Convert user's profile picture into byte array in order to send over TCP systemUser.userImageByteArray = imageToByteArray(returnedImage); } catch { } systemUserCache.addOrUpdate(systemUser); return(systemUser); //SqlConnection connection = new SqlConnection(connectionString); //SqlCommand cmd = new SqlCommand(); //SqlDataReader reader; //cmd.CommandText = "SELECT Id,UserName,image,money,rank,gamesPlayed FROM SystemUsers WHERE email=@email"; //cmd.CommandType = CommandType.Text; //cmd.Connection = connection; //cmd.Parameters.AddWithValue("@email", email); //connection.Open(); //reader = cmd.ExecuteReader(); //if (!reader.HasRows || !reader.Read()) // return null; //SystemUser su = new SystemUser(int.Parse(reader["Id"].ToString()), reader["UserName"].ToString(), email, reader["image"].ToString(), int.Parse(reader["money"].ToString()), int.Parse(reader["rank"].ToString()), int.Parse(reader["gamesPlayed"].ToString())); //connection.Close(); //// Try to get the image from the database. //try //{ // // Get the user's profile picture file from memory. // var returnedImage = Image.FromFile(su.userImage); // // Convert user's profile picture into byte array in order to send over TCP // su.userImageByteArray = imageToByteArray(returnedImage); //} //catch { } //return su; }
public bool EditUserLeaderBoardsById(int Id, int?highestCashInGame, int?totalGrossProfit) { Database.Domain.SystemUser user = systemUserRepository.GetById(Id); if (highestCashInGame != null) { user.HighestCashInGame = Math.Max(user.HighestCashInGame, (int)highestCashInGame); } if (totalGrossProfit != null) { user.TotalGrossProfit += (int)totalGrossProfit; } if (!systemUserRepository.Update(user)) { return(false); } systemUserCache.addOrUpdate(databaseSystemUserToBackendSystemUser(systemUserRepository.GetById(Id))); return(true); //SqlConnection connection = new SqlConnection(connectionString); //SqlCommand cmd = new SqlCommand(); //int psikCount = -1 + //(highetsCashInAGame == null ? 0 : 1) + //(totalGrossProfit == null ? 0 : 1); //cmd.CommandText = "Update SystemUsers SET " + // (highetsCashInAGame == null ? "" : "highetsCashInAGame=" + // "(CASE WHEN highetsCashInAGame<@highetsCashInAGame " + // "THEN @highetsCashInAGame ELSE highetsCashInAGame " + // "END)" + (psikCount-- > 0 ? "," : "")) + // (totalGrossProfit == null ? "" : "totalGrossProfit= totalGrossProfit+@totalGrossProfit" + (psikCount-- > 0 ? "," : "")) + // " WHERE Id=@Id"; //cmd.CommandType = CommandType.Text; //cmd.Connection = connection; //cmd.Parameters.AddWithValue("@Id", Id); //if (highetsCashInAGame != null) cmd.Parameters.AddWithValue("@highetsCashInAGame", highetsCashInAGame); //if (totalGrossProfit != null) cmd.Parameters.AddWithValue("@totalGrossProfit", totalGrossProfit); //connection.Open(); //bool ans = cmd.ExecuteNonQuery() > 0; //connection.Close(); //return ans; }
/// <summary> /// Register a new user to the system. /// </summary> /// <param name="UserName"></param> /// <param name="password"></param> /// <param name="email"></param> /// <param name="image"></param> /// <returns>true if the user has been added</returns> public bool RegisterUser(string UserName, string password, string email, Image image) { string filePath = String.Join("_", Guid.NewGuid(), UserName); string imagesDirectory = Path.Combine(Environment.CurrentDirectory, "Images", filePath); // Save image to disc. (produces error but saves it anyway. we will just wrap it with a 'try' clause. try { image.Save(imagesDirectory); } catch { } Database.Domain.SystemUser user = new Database.Domain.SystemUser(); user.UserName = UserName; user.Salt = generateSalt(); user.Password = GetMd5Hash(password + user.Salt); user.Email = email; user.Image = imagesDirectory; return(systemUserRepository.Add(user)); ////password = GetMd5Hash(string.Concat(new string[] { password, salt })); //SqlConnection connection = new SqlConnection(connectionString); //SqlCommand cmd = new SqlCommand(); //cmd.CommandText = "INSERT SystemUsers (UserName,password,email,image,salt) " + // "VALUES (@UserName,HASHBYTES(\'MD5\', CONCAT(@password,@salt)),@email,@image,@salt)"; //cmd.CommandType = CommandType.Text; //cmd.Connection = connection; //cmd.Parameters.AddWithValue("@UserName", UserName); //cmd.Parameters.AddWithValue("@password", password); //cmd.Parameters.AddWithValue("@email", email); //cmd.Parameters.AddWithValue("@image", image); //cmd.Parameters.AddWithValue("@salt", getRandomSalt()); //connection.Open(); //bool ans = cmd.ExecuteNonQuery() > 0; //connection.Close(); //return ans; }
/// <summary> /// Login mechanism /// </summary> /// <param name="UserName"></param> /// <param name="password"></param> /// <returns>if success returns the id of the user, else returns -1</returns> public int Login(string UserName, string password) { Database.Domain.SystemUser user = systemUserRepository.GetByName(UserName); if (user == null) { return(-1); } if (VerifyMd5Hash(password + user.Salt, user.Password)) { // triggers the cache so it would be in it. systemUserRepository.GetById(user.Id); return(user.Id); } return(-1); //SqlConnection connection = new SqlConnection(connectionString); //SqlCommand cmd = new SqlCommand(); //SqlDataReader reader; //cmd.CommandText = "SELECT Id FROM SystemUsers WHERE UserName=@UserName AND password=HASHBYTES(\'MD5\', CONCAT(@password,salt))"; //cmd.CommandType = CommandType.Text; //cmd.Connection = connection; //cmd.Parameters.AddWithValue("@UserName", UserName); //cmd.Parameters.AddWithValue("@password", password); //connection.Open(); //reader = cmd.ExecuteReader(); //if (!reader.HasRows || !reader.Read()) // return -1; //int ans = (int)reader["Id"]; //connection.Close(); //return ans; }
//private byte[] getRandomSalt() //{ // var salt = new byte[SALT_SIZE]; // using (var random = new RNGCryptoServiceProvider()) // { // random.GetNonZeroBytes(salt); // } // return salt; //} private SystemUser databaseSystemUserToBackendSystemUser(Database.Domain.SystemUser dbUser) { return(new SystemUser(dbUser.Id, dbUser.UserName, dbUser.Email, dbUser.Image, dbUser.Money, dbUser.Rank, dbUser.GamesPlayed)); }
/// <summary> /// Edit user profile by ID, if you don't want to change some of the fields just put null there. /// </summary> /// <param name="Id"></param> /// <param name="UserName"></param> /// <param name="password"></param> /// <param name="email"></param> /// <param name="image"></param> /// <param name="moneyToAdd">a delta, can also be negative</param> /// <param name="rankToAdd">a delta, can also be negative</param> /// <param name="playedAnotherGame"></param> /// <returns>true if user has been edited succesfully</returns> public bool EditUserById(int Id, string UserName, string password, string email, Image image, int?moneyToAdd, int?rankToAdd, bool playedAnotherGame) { string imagesDirectory = String.Empty; if (image != null) { string filePath = String.Join("_", Guid.NewGuid(), UserName, "updated"); imagesDirectory = Path.Combine(Environment.CurrentDirectory, "images", filePath); // Save image to disc. (produces error but saves it anyway. we will just wrap it with a 'try' clause. try { image.Save(imagesDirectory); } catch { } } Database.Domain.SystemUser user = systemUserRepository.GetById(Id); if (UserName != null) { user.UserName = UserName; } if (password != null) { user.Salt = generateSalt(); user.Password = GetMd5Hash(password + user.Salt); } if (email != null) { user.Email = email; } if (image != null) { user.Email = email; } if (image != null) { user.Image = imagesDirectory; } if (moneyToAdd != null) { user.Money = Math.Max(0, user.Money + (int)moneyToAdd); } if (rankToAdd != null) { user.Rank = Math.Max(0, user.Rank + (int)rankToAdd); } if (playedAnotherGame) { user.GamesPlayed++; } if (!systemUserRepository.Update(user)) { return(false); } var updatedUser = databaseSystemUserToBackendSystemUser(systemUserRepository.GetById(Id)); if (image != null) { updatedUser.userImageByteArray = imageToByteArray(image); } systemUserCache.addOrUpdate(updatedUser); return(true); //SqlConnection connection = new SqlConnection(connectionString); //SqlCommand cmd = new SqlCommand(); //int psikCount = -1 + //(UserName == null ? 0 : 1) + //(password == null ? 0 : 1) + //(email == null ? 0 : 1) + //(image == null ? 0 : 1) + //(money == null ? 0 : 1) + //(rankToAdd == null ? 0 : 1) + //(playedAnotherGame ? 1 : 0); //cmd.CommandText = "Update SystemUsers SET " + // (UserName == null ? "" : "UserName=@UserName" + (psikCount-- > 0 ? "," : "")) + // (password == null ? "" : "password=HASHBYTES(\'MD5\', CONCAT(@password,@salt)),salt=@salt" + (psikCount-- > 0 ? "," : "")) + // (email == null ? "" : "email=@email" + (psikCount-- > 0 ? "," : "")) + // (image == null ? "" : "image=@image" + (psikCount-- > 0 ? "," : "")) + // (money == null ? "" : "money=money+@money" + (psikCount-- > 0 ? "," : "")) + // (rankToAdd == null ? "" : "rank=(CASE WHEN rank+@rankToAdd > 0 THEN rank+@rankToAdd ELSE 0 END)" + (psikCount-- > 0 ? "," : "")) + // (!playedAnotherGame ? "" : "gamesPlayed=gamesPlayed+1") + // " WHERE Id=@Id"; //cmd.CommandType = CommandType.Text; //cmd.Connection = connection; //cmd.Parameters.AddWithValue("@Id", Id); //if (UserName != null) cmd.Parameters.AddWithValue("@UserName", UserName); //if (password != null) cmd.Parameters.AddWithValue("@password", password); //if (email != null) cmd.Parameters.AddWithValue("@email", email); //if (image != null) cmd.Parameters.AddWithValue("@image", image); //if (password != null) cmd.Parameters.AddWithValue("@salt", getRandomSalt()); //if (money != null) cmd.Parameters.AddWithValue("@money", money); //if (rankToAdd != null) cmd.Parameters.AddWithValue("@rankToAdd", rankToAdd); //connection.Open(); //bool ans = cmd.ExecuteNonQuery() > 0; //connection.Close(); //return ans; }