Exemplo n.º 1
0
        /// <summary>
        /// Fill the object configuration properties.
        /// </summary>
        public void Fill()
        {
            foreach (ObjectConfigurationPropertyInfo objectProperty in _properties)
            {
                Console.Write(objectProperty.Display);

                switch (objectProperty.Type)
                {
                case ObjectPropertyType.String:
                    objectProperty.Value = objectProperty.IsPassword ?
                                           _consoleHelper.ReadPassword() :
                                           _consoleHelper.ReadStringOrDefault(objectProperty.Value?.ToString());
                    break;

                case ObjectPropertyType.Number:
                    objectProperty.Value = _consoleHelper.ReadIntegerOrDefault(Convert.ToInt32(objectProperty.Value));
                    break;

                case ObjectPropertyType.YesNo:
                    objectProperty.Value = _consoleHelper.AskConfirmation();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Executes the "configure cluster" command.
        /// </summary>
        public void OnExecute()
        {
            var clusterServerConfiguration = ConfigurationHelper.Load <ClusterConfiguration>(ConfigurationFile, ConfigurationConstants.ClusterServer);
            var coreServerConfiguratinon   = ConfigurationHelper.Load <CoreConfiguration>(ConfigurationFile, ConfigurationConstants.CoreServer);
            var clusterConfiguration       = new ObjectConfigurationFiller <ClusterConfiguration>(clusterServerConfiguration);
            var coreConfiguration          = new ObjectConfigurationFiller <CoreConfiguration>(coreServerConfiguratinon);

            Console.WriteLine("----- Cluster Server -----");
            clusterConfiguration.Fill();
            Console.WriteLine("----- Core Server -----");
            coreConfiguration.Fill();
            coreConfiguration.Value.Password = MD5.GetMD5Hash(coreConfiguration.Value.Password);

            Console.WriteLine("##### Configuration review #####");
            clusterConfiguration.Show("Cluster Server configuration");
            coreConfiguration.Show("Core server configuration");

            bool response = _consoleHelper.AskConfirmation("Save this configuration?");

            if (response)
            {
                var configuration = new Dictionary <string, object>
                {
                    { ConfigurationConstants.ClusterServer, clusterConfiguration.Value },
                    { ConfigurationConstants.CoreServer, coreConfiguration.Value }
                };

                ConfigurationHelper.Save(ConfigurationFile, configuration);
                Console.WriteLine($"Cluster Server configuration saved in {ConfigurationFile}!");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Executes the "configure login" command.
        /// </summary>
        public void OnExecute()
        {
            var loginServerConfiguration = ConfigurationHelper.Load <LoginConfiguration>(ConfigurationFile, ConfigurationConstants.LoginServer);
            var coreServerConfiguration  = ConfigurationHelper.Load <CoreConfiguration>(ConfigurationFile, ConfigurationConstants.CoreServer);
            var loginConfiguration       = new ObjectConfigurationFiller <LoginConfiguration>(loginServerConfiguration);
            var coreConfiguration        = new ObjectConfigurationFiller <CoreConfiguration>(coreServerConfiguration);

            Console.WriteLine("----- Login Server -----");
            loginConfiguration.Fill();
            Console.WriteLine("----- Core Server -----");
            coreConfiguration.Fill();
            coreConfiguration.Value.Password = MD5.GetMD5Hash(coreConfiguration.Value.Password);

            Console.WriteLine("##### Configuration review #####");
            loginConfiguration.Show("Login Server configuration");
            coreConfiguration.Show("Core server configuration");

            bool response = _consoleHelper.AskConfirmation("Save this configuration?");

            if (!response)
            {
                return;
            }

            ConfigurationHelper.Save(ConfigurationFile, new LoginServerConfigurationModel
            {
                CoreConfiguration  = coreConfiguration.Value,
                LoginConfiguration = loginConfiguration.Value
            });
            Console.WriteLine($"Login Server configuration saved in {ConfigurationFile}!");
        }
Exemplo n.º 4
0
        public void OnExecute(CommandLineApplication app, IConsole console)
        {
            if (string.IsNullOrEmpty(DatabaseConfigurationFile))
            {
                DatabaseConfigurationFile = Application.DefaultDatabaseConfigurationFile;
            }

            var dbConfiguration = new DatabaseConfiguration()
            {
                EncryptionKey = Convert.ToBase64String(AesProvider.GenerateKey(AesKeySize.AES256Bits).Key)
            };

            Console.WriteLine("Select one of the available providers:");
            ConsoleHelper.DisplayEnum <DatabaseProvider>();
            Console.Write("Database provider: ");
            dbConfiguration.Provider = ConsoleHelper.ReadEnum <DatabaseProvider>();

            if (dbConfiguration.Provider == DatabaseProvider.MySql)
            {
                Console.Write("Port (3306): ");
                dbConfiguration.Port = ConsoleHelper.ReadIntegerOrDefault(3306);
            }

            Console.Write("Host (localhost): ");
            dbConfiguration.Host = ConsoleHelper.ReadStringOrDefault("localhost");

            Console.Write("Username (root): ");
            dbConfiguration.Username = ConsoleHelper.ReadStringOrDefault("root");

            Console.Write("Password: "******"Database name (rhisis): ");
            dbConfiguration.Database = ConsoleHelper.ReadStringOrDefault("rhisis");

            Console.WriteLine("--------------------------------");
            Console.WriteLine("Configuration:");
            Console.WriteLine($"Database Provider: {dbConfiguration.Provider.ToString()}");
            Console.WriteLine($"Host: {dbConfiguration.Host}");
            Console.WriteLine($"Username: {dbConfiguration.Username}");
            Console.WriteLine($"Database name: {dbConfiguration.Database}");

            if (dbConfiguration.Provider == DatabaseProvider.MySql)
            {
                Console.WriteLine($"Port: {dbConfiguration.Port}");
            }

            Console.WriteLine("--------------------------------");

            bool response = ConsoleHelper.AskConfirmation("Save this configuration?");

            if (response)
            {
                ConfigurationHelper.Save(DatabaseConfigurationFile, dbConfiguration);
                Console.WriteLine($"Database configuration saved in '{DatabaseConfigurationFile}'.");
            }
        }
Exemplo n.º 5
0
        private void ConfigureLoginServer()
        {
            var loginConfiguration = new LoginConfiguration();

            Console.WriteLine("#### Configuring Login Server ####");

            Console.Write("Host (127.0.0.1): ");
            loginConfiguration.Host = ConsoleHelper.ReadStringOrDefault("127.0.0.1");

            Console.Write("Port (23000): ");
            loginConfiguration.Port = ConsoleHelper.ReadIntegerOrDefault(23000);

            Console.Write("Client Build Version (20100412): ");
            loginConfiguration.BuildVersion = ConsoleHelper.ReadStringOrDefault("20100412");

            loginConfiguration.AccountVerification = ConsoleHelper.AskConfirmation("Use account verification?");
            loginConfiguration.PasswordEncryption  = ConsoleHelper.AskConfirmation("Use password encryption?");

            if (loginConfiguration.PasswordEncryption)
            {
                Console.Write("Encryption key: (dldhsvmflvm): ");
                loginConfiguration.EncryptionKey = ConsoleHelper.ReadStringOrDefault("dldhsvmflvm");
            }

            Console.Write("ISC Host (127.0.0.1): ");
            loginConfiguration.ISC.Host = ConsoleHelper.ReadStringOrDefault("127.0.0.1");

            Console.Write("ISC Port (15000): ");
            loginConfiguration.ISC.Port = ConsoleHelper.ReadIntegerOrDefault(15000);

            Console.Write("ISC Password: "******"--------------------------------");
            Console.WriteLine("Login Server Configuration:");
            Console.WriteLine($"Host: {loginConfiguration.Host}");
            Console.WriteLine($"Port: {loginConfiguration.Port}");
            Console.WriteLine($"Client build version: {loginConfiguration.BuildVersion}");
            Console.WriteLine($"Use account verification: {loginConfiguration.AccountVerification}");
            Console.WriteLine($"Use password encryption: {loginConfiguration.PasswordEncryption}");
            Console.WriteLine($"ISC Host: {loginConfiguration.ISC.Host}");
            Console.WriteLine($"ISC Port: {loginConfiguration.ISC.Port}");

            bool response = ConsoleHelper.AskConfirmation("Save this configuration?");

            if (response)
            {
                ConfigurationHelper.Save(LoginServerConfigurationPath, loginConfiguration);
                Console.WriteLine($"Login Server configuration saved in {LoginServerConfigurationPath}!");
            }
        }
Exemplo n.º 6
0
        public void OnExecute()
        {
            if (string.IsNullOrEmpty(DatabaseConfigurationFile))
            {
                DatabaseConfigurationFile = Application.DefaultDatabaseConfigurationFile;
            }

            var dbConfiguration = new DatabaseConfiguration()
            {
                EncryptionKey = Convert.ToBase64String(AesProvider.GenerateKey(AesKeySize.AES256Bits).Key)
            };

            Console.WriteLine("### Database configuration ###");

            Console.Write("Host (localhost): ");
            dbConfiguration.Host = _consoleHelper.ReadStringOrDefault("localhost");

            Console.Write("Port (3306): ");
            dbConfiguration.Port = _consoleHelper.ReadIntegerOrDefault(3306);

            Console.Write("Username (root): ");
            dbConfiguration.Username = _consoleHelper.ReadStringOrDefault("root");

            Console.Write("Password: "******"Database name (rhisis): ");
            dbConfiguration.Database = _consoleHelper.ReadStringOrDefault("rhisis");

            Console.WriteLine("--------------------------------");
            Console.WriteLine("Configuration:");
            Console.WriteLine($"Host: {dbConfiguration.Host}");
            Console.WriteLine($"Username: {dbConfiguration.Username}");
            Console.WriteLine($"Database name: {dbConfiguration.Database}");
            Console.WriteLine($"Port: {dbConfiguration.Port}");
            Console.WriteLine("--------------------------------");

            bool response = _consoleHelper.AskConfirmation("Save this configuration?");

            if (response)
            {
                ConfigurationHelper.Save(DatabaseConfigurationFile, dbConfiguration);
                Console.WriteLine($"Database configuration saved in '{DatabaseConfigurationFile}'.");
            }
        }
Exemplo n.º 7
0
        private void ConfigureClusterServer()
        {
            var clusterConfiguration = new ClusterConfiguration();

            Console.WriteLine("#### Configuring Cluster Server ####");
            Console.Write("Host (127.0.0.1): ");
            clusterConfiguration.Host = ConsoleHelper.ReadStringOrDefault("127.0.0.1");

            Console.Write("Port (28000): ");
            clusterConfiguration.Port = ConsoleHelper.ReadIntegerOrDefault(28000);

            Console.Write("Cluster Id: ");
            clusterConfiguration.Id = ConsoleHelper.ReadIntegerOrDefault();

            Console.Write("Cluster name (Rhisis): ");
            clusterConfiguration.Name = ConsoleHelper.ReadStringOrDefault("Rhisis");

            clusterConfiguration.EnableLoginProtect = ConsoleHelper.AskConfirmation("Enable second password verification (LoginProtect)");

            Console.Write("ISC Server Host (127.0.0.1): ");
            clusterConfiguration.ISC.Host = ConsoleHelper.ReadStringOrDefault("127.0.0.1");

            Console.Write("ISC Port (15000): ");
            clusterConfiguration.ISC.Port = ConsoleHelper.ReadIntegerOrDefault(15000);

            Console.Write("ISC Password: "******"--------------------------------");
            Console.WriteLine("Cluster Server Configuration:");
            Console.WriteLine($"Host: {clusterConfiguration.Host}");
            Console.WriteLine($"Port: {clusterConfiguration.Port}");
            Console.WriteLine($"Use login protect: {clusterConfiguration.EnableLoginProtect}");
            Console.WriteLine($"ISC Host: {clusterConfiguration.ISC.Host}");
            Console.WriteLine($"ISC Port: {clusterConfiguration.ISC.Port}");

            bool response = ConsoleHelper.AskConfirmation("Save this configuration?");

            if (response)
            {
                ConfigurationHelper.Save(ClusterServerConfigurationPath, clusterConfiguration);
                Console.WriteLine($"Cluster Server configuration saved in {ClusterServerConfigurationPath}!");
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Executes the "database configure" command.
        /// </summary>
        public void OnExecute()
        {
            if (string.IsNullOrEmpty(DatabaseConfigurationFile))
            {
                DatabaseConfigurationFile = ConfigurationConstants.DatabasePath;
            }

            var databaseConfiguration = ConfigurationHelper.Load <DatabaseConfiguration>(DatabaseConfigurationFile, ConfigurationConstants.DatabaseConfiguration);
            var dbConfiguration       = new ObjectConfigurationFiller <DatabaseConfiguration>(databaseConfiguration);

            dbConfiguration.Fill();

            if (dbConfiguration.Value.UseEncryption)
            {
                if (string.IsNullOrEmpty(dbConfiguration.Value.EncryptionKey))
                {
                    dbConfiguration.Value.EncryptionKey = Convert.ToBase64String(AesProvider.GenerateKey(AesKeySize.AES256Bits).Key);
                }
                else
                {
                    Console.WriteLine("Warning: your database configuration already contains an encryption key.");
                }
            }

            dbConfiguration.Show("Database configuration");
            Console.WriteLine($"Encryption key: {dbConfiguration.Value.EncryptionKey}");

            bool response = _consoleHelper.AskConfirmation("Save this configuration?");

            if (response)
            {
                ConfigurationHelper.Save(DatabaseConfigurationFile, new DatabaseConfigurationModel()
                {
                    DatabaseConfiguration = dbConfiguration.Value
                });
                Console.WriteLine($"Database configuration saved in '{DatabaseConfigurationFile}'.");
            }
        }
        /// <summary>
        /// Executes the "configure world" command.
        /// </summary>
        public void OnExecute()
        {
            var worldServerConfiguration = ConfigurationHelper.Load <WorldConfiguration>(ConfigurationFile, ConfigurationConstants.WorldServer);
            var coreServerConfiguratinon = ConfigurationHelper.Load <CoreConfiguration>(ConfigurationFile, ConfigurationConstants.CoreServer);
            var worldConfiguration       = new ObjectConfigurationFiller <WorldConfiguration>(worldServerConfiguration);
            var coreConfiguration        = new ObjectConfigurationFiller <CoreConfiguration>(coreServerConfiguratinon);

            Console.WriteLine("----- World Server -----");
            worldConfiguration.Fill();
            worldConfiguration.Value.Maps = new List <string>
            {
                "WI_WORLD_MADRIGAL",
                "WI_DUNGEON_FL_MAS"
            };

            Console.WriteLine("----- Core Server -----");
            coreConfiguration.Fill();
            coreConfiguration.Value.Password = MD5.GetMD5Hash(coreConfiguration.Value.Password);

            Console.WriteLine("##### Configuration review #####");
            worldConfiguration.Show("World Server configuration");
            coreConfiguration.Show("Core server configuration");

            bool response = _consoleHelper.AskConfirmation("Save this configuration?");

            if (response)
            {
                var configuration = new Dictionary <string, object>
                {
                    { ConfigurationConstants.WorldServer, worldConfiguration.Value },
                    { ConfigurationConstants.CoreServer, coreConfiguration.Value }
                };

                ConfigurationHelper.Save(ConfigurationFile, configuration);
                Console.WriteLine($"World Server configuration saved in {ConfigurationFile}!");
            }
        }
Exemplo n.º 10
0
        private void ConfigureWorldServer()
        {
            var worldConfiguration = new WorldConfiguration();

            Console.WriteLine("#### Configuring World Server ####");
            Console.Write("Host (127.0.0.1): ");
            worldConfiguration.Host = ConsoleHelper.ReadStringOrDefault("127.0.0.1");

            Console.Write("Port (5400): ");
            worldConfiguration.Port = ConsoleHelper.ReadIntegerOrDefault(5400);

            Console.Write("Parent cluster Id: ");
            worldConfiguration.ClusterId = ConsoleHelper.ReadIntegerOrDefault();

            Console.Write("World channel Id: ");
            worldConfiguration.Id = ConsoleHelper.ReadIntegerOrDefault();

            Console.Write("World channel name (Channel 1): ");
            worldConfiguration.Name = ConsoleHelper.ReadStringOrDefault("Channel 1");

            Console.Write("ISC Server Host (127.0.0.1): ");
            worldConfiguration.ISC.Host = ConsoleHelper.ReadStringOrDefault("127.0.0.1");

            Console.Write("ISC Port (15000): ");
            worldConfiguration.ISC.Port = ConsoleHelper.ReadIntegerOrDefault(15000);

            Console.Write("ISC Password: "******"----- Drops -----");
            Console.Write("Drop rate (1): ");
            worldConfiguration.Rates.Drop = ConsoleHelper.ReadIntegerOrDefault(1);

            Console.Write("Gold drop rate (1): ");
            worldConfiguration.Rates.Gold = ConsoleHelper.ReadIntegerOrDefault(1);

            Console.Write("Experience rate (1): ");
            worldConfiguration.Rates.Experience = ConsoleHelper.ReadIntegerOrDefault(1);

            worldConfiguration.Maps = new List <string>
            {
                "WI_WORLD_MADRIGAL"
            };
            worldConfiguration.Language            = "en";
            worldConfiguration.MailShippingCost    = 500;
            worldConfiguration.Drops.OwnershipTime = 7;
            worldConfiguration.Drops.DespawnTime   = 120;

            Console.WriteLine("--------------------------------");
            Console.WriteLine("World Server Configuration:");
            Console.WriteLine($"Host: {worldConfiguration.Host}");
            Console.WriteLine($"Port: {worldConfiguration.Port}");
            Console.WriteLine($"ISC Host: {worldConfiguration.ISC.Host}");
            Console.WriteLine($"ISC Port: {worldConfiguration.ISC.Port}");

            Console.WriteLine("Rates:");
            Console.WriteLine($"Drop: x{worldConfiguration.Rates.Drop}");
            Console.WriteLine($"Gold: x{worldConfiguration.Rates.Gold}");
            Console.WriteLine($"Experience: x{worldConfiguration.Rates.Experience}");

            Console.WriteLine("Maps:");
            for (int i = 0; i < worldConfiguration.Maps.Count(); i++)
            {
                Console.WriteLine($"- {worldConfiguration.Maps.ElementAt(i)}");
            }

            bool response = ConsoleHelper.AskConfirmation("Save this configuration?");

            if (response)
            {
                ConfigurationHelper.Save(WorldServerConfigurationPath, worldConfiguration);
                Console.WriteLine($"World Server configuration saved in {WorldServerConfigurationPath}!");
            }
        }
Exemplo n.º 11
0
        public void OnExecute(CommandLineApplication app, IConsole console)
        {
            if (string.IsNullOrEmpty(DatabaseConfigurationFile))
            {
                DatabaseConfigurationFile = Application.DefaultDatabaseConfigurationFile;
            }

            var user = new DbUser();

            Console.Write("Username: "******"Email: ");
            user.Email = Console.ReadLine();

            Console.Write("Password: "******"Password confirmation: ");
            string passwordConfirmation = ConsoleHelper.ReadPassword();

            Console.Write("Password salt: ");
            string passwordSalt = ConsoleHelper.ReadStringOrDefault();

            Console.WriteLine("Authority: ");
            ConsoleHelper.DisplayEnum <AuthorityType>();
            user.Authority = (int)ConsoleHelper.ReadEnum <AuthorityType>();

            Console.WriteLine("--------------------------------");
            Console.WriteLine("User account informations:");
            Console.WriteLine($"Username: {user.Username}");
            Console.WriteLine($"Email: {user.Email}");
            Console.WriteLine($"Authority: {(AuthorityType)user.Authority}");
            Console.WriteLine("--------------------------------");

            bool response = ConsoleHelper.AskConfirmation("Create user?");

            if (response)
            {
                DatabaseFactory.Instance.Initialize(this.DatabaseConfigurationFile);
                this._database = new Rhisis.Database.Database();

                if (this._database.Users.HasAny(x => x.Username.Equals(user.Username, StringComparison.OrdinalIgnoreCase)))
                {
                    Console.WriteLine($"User '{user.Username}' is already used.");
                    return;
                }

                if (!user.Email.IsValidEmail())
                {
                    Console.WriteLine($"Email '{user.Email}' is not valid.");
                    return;
                }

                if (_database.Users.HasAny(x => x.Email.Equals(user.Email, StringComparison.OrdinalIgnoreCase)))
                {
                    Console.WriteLine($"Email '{user.Email}' is already used.");
                    return;
                }

                if (!user.Password.Equals(passwordConfirmation))
                {
                    Console.WriteLine("Passwords doesn't match.");
                    return;
                }

                user.Password = MD5.GetMD5Hash(passwordSalt, user.Password);

                this._database.Users.Create(user);
                this._database.Complete();
                Console.WriteLine($"User '{user.Username}' created.");
            }
        }
Exemplo n.º 12
0
        public void OnExecute()
        {
            if (string.IsNullOrEmpty(DatabaseConfigurationFile))
            {
                DatabaseConfigurationFile = ConfigurationConstants.DatabasePath;
            }

            var dbConfig = ConfigurationHelper.Load <DatabaseConfiguration>(DatabaseConfigurationFile, ConfigurationConstants.DatabaseConfiguration);

            if (dbConfig is null)
            {
                Console.WriteLine("Couldn't load database configuration file during execution of user create command.");
                return;
            }

            Console.Write("Username to update: ");
            string username = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(username))
            {
                Console.WriteLine("You must type a username.");
                return;
            }

            using IRhisisDatabase database = _databaseFactory.CreateDatabaseInstance(dbConfig);
            DbUser user = database.Users.FirstOrDefault(x => x.Username == username);

            if (user == null)
            {
                Console.WriteLine($"Could not locate any username named '{username}'.");
                return;
            }

            bool response = _consoleHelper.AskConfirmation($"Are you sure you want to update the account '{user.Username}'");

            if (!response)
            {
                return;
            }

            bool changeEmail = _consoleHelper.AskConfirmation($"Would you like to change the email? '{user.Email}'");

            if (changeEmail)
            {
                Console.Write("Type the new email: ");
                user.Email = Console.ReadLine();
            }

            bool   changePassword       = _consoleHelper.AskConfirmation("Would you like to change the password?");
            string passwordConfirmation = string.Empty;
            string passwordSalt         = string.Empty;

            if (changePassword)
            {
                Console.Write("New password: "******"New password confirmation: ");
                passwordConfirmation = _consoleHelper.ReadPassword();

                Console.Write("Password salt: ");
                passwordSalt = _consoleHelper.ReadStringOrDefault();
            }

            bool changeAuthority = _consoleHelper.AskConfirmation("Would you like to change the account authority?");

            if (changeAuthority)
            {
                Console.WriteLine("New authority: ");
                _consoleHelper.DisplayEnum <AuthorityType>();
                user.Authority = (int)_consoleHelper.ReadEnum <AuthorityType>();
            }

            Console.WriteLine("--------------------------------");
            Console.WriteLine("User account information:");
            Console.WriteLine($"Username: {user.Username}");
            Console.WriteLine($"Email: {user.Email}");
            Console.WriteLine($"Authority: {(AuthorityType)user.Authority}");
            Console.WriteLine("--------------------------------");

            bool updateUser = (changeEmail || changePassword || changeAuthority) && _consoleHelper.AskConfirmation("Update user?");

            if (updateUser)
            {
                if (changeEmail)
                {
                    if (!user.Email.IsValidEmail())
                    {
                        Console.WriteLine($"Email '{user.Email}' is not valid.");
                        return;
                    }

                    if (database.Users.Any(x => x.Email.Equals(user.Email, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        Console.WriteLine($"Email '{user.Email}' is already used.");
                        return;
                    }
                }

                if (changePassword)
                {
                    if (!user.Password.Equals(passwordConfirmation))
                    {
                        Console.WriteLine("Passwords doesn't match.");
                        return;
                    }

                    user.Password = MD5.GetMD5Hash(passwordSalt, user.Password);
                }

                database.Users.Update(user);
                database.SaveChanges();
                Console.WriteLine($"User '{user.Username}' has been updated.");
            }
        }
Exemplo n.º 13
0
        public void OnExecute()
        {
            var user = new DbUser();

            Console.Write("Username: "******"Email: ");
            user.Email = Console.ReadLine();

            Console.Write("Password: "******"Password confirmation: ");
            string passwordConfirmation = _consoleHelper.ReadPassword();

            Console.Write("Password salt: ");
            string passwordSalt = _consoleHelper.ReadStringOrDefault();

            Console.WriteLine("Authority: ");
            _consoleHelper.DisplayEnum <AuthorityType>();
            user.Authority = (int)_consoleHelper.ReadEnum <AuthorityType>();

            Console.WriteLine("--------------------------------");
            Console.WriteLine("User account informations:");
            Console.WriteLine($"Username: {user.Username}");
            Console.WriteLine($"Email: {user.Email}");
            Console.WriteLine($"Authority: {(AuthorityType)user.Authority}");
            Console.WriteLine("--------------------------------");

            bool response = _consoleHelper.AskConfirmation("Create user?");

            if (response)
            {
                if (this._database.Users.HasAny(x => x.Username.Equals(user.Username, StringComparison.OrdinalIgnoreCase)))
                {
                    Console.WriteLine($"User '{user.Username}' is already used.");
                    return;
                }

                if (!user.Email.IsValidEmail())
                {
                    Console.WriteLine($"Email '{user.Email}' is not valid.");
                    return;
                }

                if (_database.Users.HasAny(x => x.Email.Equals(user.Email, StringComparison.OrdinalIgnoreCase)))
                {
                    Console.WriteLine($"Email '{user.Email}' is already used.");
                    return;
                }

                if (!user.Password.Equals(passwordConfirmation))
                {
                    Console.WriteLine("Passwords doesn't match.");
                    return;
                }

                user.Password = MD5.GetMD5Hash(passwordSalt, user.Password);

                this._database.Users.Create(user);
                this._database.Complete();
                Console.WriteLine($"User '{user.Username}' created.");
            }
        }
Exemplo n.º 14
0
        public void OnExecute()
        {
            if (string.IsNullOrEmpty(DatabaseConfigurationFile))
            {
                DatabaseConfigurationFile = ConfigurationConstants.DatabasePath;
            }

            var dbConfig = ConfigurationHelper.Load <DatabaseConfiguration>(DatabaseConfigurationFile, ConfigurationConstants.DatabaseConfiguration);

            if (dbConfig is null)
            {
                Console.WriteLine("Couldn't load database configuration file during execution of user create command.");
                return;
            }

            var user = new DbUser();

            Console.Write("Username: "******"Email: ");
            user.Email = Console.ReadLine();

            Console.Write("Password: "******"Password confirmation: ");
            string passwordConfirmation = _consoleHelper.ReadPassword();

            Console.Write("Password salt: ");
            string passwordSalt = _consoleHelper.ReadStringOrDefault();

            Console.WriteLine("Authority: ");
            _consoleHelper.DisplayEnum <AuthorityType>();
            user.Authority = (int)_consoleHelper.ReadEnum <AuthorityType>();

            Console.WriteLine("--------------------------------");
            Console.WriteLine("User account information:");
            Console.WriteLine($"Username: {user.Username}");
            Console.WriteLine($"Email: {user.Email}");
            Console.WriteLine($"Authority: {(AuthorityType)user.Authority}");
            Console.WriteLine("--------------------------------");

            bool response = _consoleHelper.AskConfirmation("Create user?");

            if (response)
            {
                using IRhisisDatabase database = _databaseFactory.CreateDatabaseInstance(dbConfig);

                if (database.Users.Any(x => x.Username.Equals(user.Username, StringComparison.OrdinalIgnoreCase)))
                {
                    Console.WriteLine($"User '{user.Username}' is already used.");
                    return;
                }

                if (!user.Email.IsValidEmail())
                {
                    Console.WriteLine($"Email '{user.Email}' is not valid.");
                    return;
                }

                if (database.Users.Any(x => x.Email.Equals(user.Email, StringComparison.OrdinalIgnoreCase)))
                {
                    Console.WriteLine($"Email '{user.Email}' is already used.");
                    return;
                }

                if (!user.Password.Equals(passwordConfirmation))
                {
                    Console.WriteLine("Passwords doesn't match.");
                    return;
                }

                user.Password = MD5.GetMD5Hash(passwordSalt, user.Password);

                database.Users.Add(user);
                database.SaveChanges();

                Console.WriteLine($"User '{user.Username}' created.");
            }
        }