Example #1
0
        /// <summary>
        /// Asks the confirm user account.
        /// </summary>
        /// <returns>The confirm user account.</returns>
        /// <param name="user">User.</param>
        /// <param name="userPassword">User password.</param>
        public static ServerPacketConfirmation AskConfirmUserAccount(PLFUser user, String userPassword)
        {
            ClientPacketAskUserActivateAccout clientPacketAskUserActivateAccout = new ClientPacketAskUserActivateAccout(user, userPassword.HashSHA256());

            ServerPacketConfirmation serverPacketConfirmation = TCPClient.SendPacket(clientPacketAskUserActivateAccout) as ServerPacketConfirmation;

            if (serverPacketConfirmation == null)
            {
                return(new ServerPacketConfirmation(false, NetworkError.SERVER_UNAVAILABLE));
            }

            return(serverPacketConfirmation);
        }
        public override Packet OnPacketReceive(Packet receivedPacket)
        {
            ClientPacketAskUserActivateAccout clientPacketAskUserActivateAccout = receivedPacket as ClientPacketAskUserActivateAccout;

            ConsoleHelper.Write("Receive - ClientPacketAskUserActivateAccout");

            PLFUser user          = clientPacketAskUserActivateAccout.User;
            String  userPassword  = clientPacketAskUserActivateAccout.UserPassword.ToSQL();
            String  userSecretKey = KeyHelper.CreateRandomKey();

            //insert new profile into DB
            MySqlCommand checkUserIdentityCommand = new MySqlCommand();

            checkUserIdentityCommand.Connection  = Program.mySQLManager.MySQLConnection.MysqlConnection;
            checkUserIdentityCommand.CommandText =
                $"SELECT * FROM `T_User` WHERE userNick='{user.UserNickName}' AND userPassword='******'";

            PLFUser downloadedUser = null;

            MySqlDataReader mysqlDataReader = null;

            ServerPacketConfirmation serverPacketConfirmation;

            try
            {
                mysqlDataReader = checkUserIdentityCommand.ExecuteReader();

                //open reader
                while (mysqlDataReader.Read())
                {
                    downloadedUser = new PLFUser(mysqlDataReader.GetInt32(0), mysqlDataReader.GetString(2), mysqlDataReader.GetString(3), mysqlDataReader.GetString(4), mysqlDataReader.GetString(5));
                }

                if (mysqlDataReader != null && !mysqlDataReader.IsClosed)
                {
                    mysqlDataReader.Close();
                }

                if (downloadedUser != null)
                {
                    //insert new profile into DB
                    MySqlCommand registerNewConfirmKey = new MySqlCommand();
                    registerNewConfirmKey.Connection  = Program.mySQLManager.MySQLConnection.MysqlConnection;
                    registerNewConfirmKey.CommandText =
                        $"DELETE FROM `T_RegisterKey` WHERE `regUserMail`='{downloadedUser.UserEMail}';" +
                        $"INSERT INTO `T_RegisterKey`(`regUserMail`, `regKey`, `regKeyDeliver`) VALUES ('{downloadedUser.UserEMail}','{userSecretKey}','{DateTime.Now.Ticks}');";

                    registerNewConfirmKey.ExecuteNonQuery();
                    new Thread(() =>
                    {
                        Program.mailManager.SendMail(downloadedUser.UserEMail, "PET LA FORME - Confirmation",
                                                     $"Bonjour {downloadedUser.UserName}! Voici votre code d'activation de votre compte: "
                                                     + $"{userSecretKey}. Merci de la rentrer dans votre application pour pouvoir activer votre compte ! Bonne journée !");
                    }
                               ).Start();

                    serverPacketConfirmation = new ServerPacketConfirmation(true, NetworkError.NONE);
                }
                else
                {
                    serverPacketConfirmation = new ServerPacketConfirmation(false, NetworkError.SQL_USER_UNKNOWN);
                }
            }
            catch (MySqlException e)
            {
                Console.WriteLine(e.Number + " - " + e.Message);
                NetworkError networkError;
                switch (e.Number)
                {
                case 1062:
                    networkError = NetworkError.SQL_USER_EXIST;
                    break;

                case 1064:
                    networkError = NetworkError.GLOBAL_UNKNOWN;
                    break;

                default:
                    networkError = NetworkError.GLOBAL_UNKNOWN;
                    break;
                }
                serverPacketConfirmation = new ServerPacketConfirmation(false, networkError);
            }
            catch (Exception e)
            {
                serverPacketConfirmation = new ServerPacketConfirmation(false, NetworkError.GLOBAL_UNKNOWN);
                Console.WriteLine(e.Message);
            }

            ConsoleHelper.Write("Send - ServerPacketConfirmation");

            return(serverPacketConfirmation);
        }