Beispiel #1
0
        static private void ImitatingShell()
        {
            Authorization AutoUser     = new Authorization(null, null);
            string        InputCommand = "";

            while (InputCommand != "close")
            {
                logger.Trace("Новая интерация цикла, ждём ввода с консоли");
                Console.Write("\n@WntifDB>");//new interation application
                if (AutoUser.Name != null)
                {
                    Console.Write(AutoUser.Name + ">");
                }
                InputCommand = SecurityPassword.ReadCommandWithPassword();//request on obtain command from console
                string tempSW = "";
                try
                {
                    tempSW = InputCommand.Remove(InputCommand.IndexOf(" "));
                }
                catch (Exception e)
                {
                    tempSW = InputCommand;                                                                                               //if string is a one word
                }
                switch (tempSW)                                                                                                          //first word from command line
                {
                case "register":                                                                                                         //case for registration
                {
                    Regex reg = new Regex("[register] (Admin|User) [A-z]{1}[A-z||0-9]{0,10} [A-z||0-9]{1,20}", RegexOptions.IgnoreCase); //regular exspression for registration new people and switch him access

                    logger.Trace("Создание объекта класса регистрации");                                                                 //logger
                    if (reg.IsMatch(InputCommand))                                                                                       //check
                    {
                        string[] words = InputCommand.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);                  //obtain array words from string
                        words[3] = hashMP5.ConvertToHash(words[3]);                                                                      //obtain hash-code by input password
                        Registration NewUser = new Registration(words[1], words[2], words[3]);                                           //access,name,hash-password
                    }
                    else
                    {
                        Console.WriteLine("Try more, uncorrect command syntax\n" +
                                          "Examle: register UserOrAdmin YourName .YourPassword                 {0,50}", "//where '.' used for hidden password");//error-text
                    }
                }
                break;

                case "login":
                {
                    Regex reg = new Regex("[login] [A-z]{1}[A-z||0-9]{0,10} [A-z||0-9]{1,15}", RegexOptions.IgnoreCase);

                    logger.Trace("Создание объекта класса авторизации");
                    //будут вводиться три аргумента с командной строки 1
                    if (reg.IsMatch(InputCommand))
                    {
                        string[] words = InputCommand.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        words[2] = hashMP5.ConvertToHash(words[2]);
                        AutoUser = new Authorization(words[1], words[2]); // name, hash-code by password

                        AutoUser.LogIn();                                 //try log in
                    }
                    else
                    {
                        string txt  = "Examle: authorization YourName .YourPassword";
                        int    size = 100 - txt.Length;
                        Console.WriteLine("Try more, uncorrect command syntax\n" +
                                          "Examle: login YourName .YourPassword                  {0," + size + "}", "//where '.' used for hidden password");
                    }
                }
                break;

                case "StartJob":
                {
                    if (CheckAccessUser(AutoUser))
                    {
                        string tempStr = "";

                        interpret ob         = new interpret(AutoUser);
                        string    first_word = ob.command.Remove(ob.command.IndexOf(" "));
                        // Console.WriteLine("fw" + first_word);//
                        while (first_word != "back")
                        {
                            switch (first_word)
                            {
                            case "IN":
                            {
                                try
                                {
                                    Regex reg = new Regex("IN [A-z]");
                                    if (!reg.IsMatch(ob.command))
                                    {
                                        throw new Exception("Incorrect syntax\nExample: IN NameDatabase");
                                    }
                                    AutoUser.NameDB = ob.command.Substring(ob.command.IndexOf(" "));
                                    if (!File.Exists(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\" + AutoUser.NameDB + ".xml"))
                                    {
                                        AutoUser.NameDB = null;
                                        throw new Exception("Database is not exist");
                                    }
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e.Message);
                                }
                            }
                            break;

                            case "EXIT":
                            {
                                AutoUser.NameDB = null;
                            }
                            break;

                            case "CREATE":
                            {
                                try
                                {
                                    if (ob.command.IndexOf("CREATE TABLE") != -1)
                                    {
                                        if (AutoUser.NameDB == null)
                                        {
                                            throw new Exception("Sign in Database! Use command 'IN NameDatabase'");
                                        }
                                        Create.Table table = new Create.Table(ob.command, AutoUser.NameDB);
                                        table.Create();
                                    }
                                    else
                                    if (ob.command.IndexOf("CREATE DATABASE") != -1)
                                    {
                                        Create.Database db = new Create.Database(ob.command);
                                        db.Create();
                                    }
                                }
                                catch (Exception e)
                                {
                                    logger.Trace(e.Message);
                                }

                                ///
                                ///тут будет словать или свич
                                ///
                            }
                            break;
                            }
                            ob         = new interpret(AutoUser);
                            first_word = ob.command.Remove(ob.command.IndexOf(" "));
                        }
                        //Regex reg = new Regex("[CREATE] (DBase|Table) '[A-z||0-9]{1,15}'", RegexOptions.IgnoreCase);
                        //if (reg.IsMatch(InputCommand) == true)
                        //{
                        //    Console.WriteLine("");
                        //}
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Denied access!");
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                }
                break;

                case "close":
                {
                    InputCommand = "close";
                }
                break;
                }
            }
        }
Beispiel #2
0
        static private void ImitatingShell()
        {
            Authorization AutoUser     = new Authorization(null, null);
            string        InputCommand = "";

            while (InputCommand != "close")
            {
                logger.Trace("Новая интерация цикла, ждём ввода с консоли");
                Console.Write("\n@WntifDB>");//new interation application
                if (AutoUser.Name != null)
                {
                    Console.Write(AutoUser.Name + ">");
                }
                InputCommand = SecurityPassword.ReadCommandWithPassword();//request on obtain command from console
                string tempSW = "";
                try
                {
                    tempSW = InputCommand.Remove(InputCommand.IndexOf(" "));
                }
                catch (Exception e)
                {
                    tempSW = InputCommand;                                                                                               //if string is a one word
                }
                switch (tempSW)                                                                                                          //first word from command line
                {
                case "register":                                                                                                         //case for registration
                {
                    Regex reg = new Regex("[register] (Admin|User) [A-z]{1}[A-z||0-9]{0,10} [A-z||0-9]{1,20}", RegexOptions.IgnoreCase); //regular exspression for registration new people and switch him access

                    logger.Trace("Создание объекта класса регистрации");                                                                 //logger
                    if (reg.IsMatch(InputCommand))                                                                                       //check
                    {
                        string[] words = InputCommand.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);                  //obtain array words from string
                        words[3] = hashMP5.ConvertToHash(words[3]);                                                                      //obtain hash-code by input password
                        Registration NewUser = new Registration(words[1], words[2], words[3]);                                           //access,name,hash-password
                    }
                    else
                    {
                        Console.WriteLine("Try more, uncorrect command syntax\n" +
                                          "Examle: register UserOrAdmin YourName .YourPassword                 {0,50}", "//where '.' used for hidden password");//error-text
                    }
                }
                break;

                case "login":
                {
                    Regex reg = new Regex("[login] [A-z]{1}[A-z||0-9]{0,10} [A-z||0-9]{1,15}", RegexOptions.IgnoreCase);

                    logger.Trace("Создание объекта класса авторизации");
                    //будут вводиться три аргумента с командной строки 1
                    if (reg.IsMatch(InputCommand))
                    {
                        string[] words = InputCommand.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        words[2] = hashMP5.ConvertToHash(words[2]);
                        AutoUser = new Authorization(words[1], words[2]); // name, hash-code by password

                        AutoUser.LogIn();                                 //try log in
                    }
                    else
                    {
                        string txt  = "Examle: authorization YourName .YourPassword";
                        int    size = 100 - txt.Length;
                        Console.WriteLine("Try more, uncorrect command syntax\n" +
                                          "Examle: login YourName .YourPassword                  {0," + size + "}", "//where '.' used for hidden password");
                    }
                }
                break;

                case "CREATE":
                {
                    if (AutoUser.Access == "Admin")
                    {
                        Regex reg = new Regex("[CREATE] (DBase|Table) '[A-z||0-9]{1,15}'", RegexOptions.IgnoreCase);
                        if (reg.IsMatch(InputCommand) == true)
                        {
                            Console.WriteLine("");
                        }
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Denied access!");
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                }
                break;

                case "close":
                {
                    InputCommand = "close";
                }
                break;
                }
            }
        }
Beispiel #3
0
        private void RecordNewUser()
        {
            logger.Trace("Попытка записи новых данных");
            if (!File.Exists(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\InfoAboutUsers.xml"))
            {
                XDocument  xdoc         = new XDocument();
                XElement   user         = new XElement("User");
                XAttribute NameUser     = new XAttribute("name", name);
                XElement   AccessUser   = new XElement("access", access);
                XElement   PasswordUser = new XElement("password", password);
                user.Add(NameUser);
                user.Add(AccessUser);
                user.Add(PasswordUser);
                XElement users = new XElement("Users");
                users.Add(user);
                xdoc.Add(users);
                xdoc.Save(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\InfoAboutUsers.xml");
                string         FileName  = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\InfoAboutUsers.xml";
                FileAttributes attribute = File.GetAttributes(FileName);//make hidden file xml
                attribute |= FileAttributes.Hidden;
                File.SetAttributes(FileName, attribute);
                Console.WriteLine("Record data about you is completed, welcome to WntifDB, {0}", name);
                logger.Trace("Запись пользователя {0} завершена, уровень доступа {1}", name, access);
            }
            else
            {
                try
                {
                    XDocument xdoc  = XDocument.Load(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\InfoAboutUsers.xml");
                    XElement  users = xdoc.Element("Users");

                    IEnumerable <XElement> usersCheck =
                        (from el in xdoc.Elements("Users").Elements("User")
                         where (string)el.Attribute("name") == name
                         select el);
                    if (usersCheck.Count() > 0)
                    {
                        throw new Exception("Name already in use");
                    }
                    XElement user = new XElement("User");

                    IEnumerable <XElement> AdminCheck =
                        (from el in xdoc.Elements("Users").Elements("User").Elements("access")
                         where (string)el.Value == "Admin"
                         select el);
                    if (access == "Admin" && AdminCheck.Count() > 0)
                    {
                        Console.Write("Input name and password other admin\nName:");
                        string TempName = Console.ReadLine();
                        Console.Write("Password:"******"Users").Elements("User").Elements("password")
                             where (string)el.Value == TempPassword
                             select el);
                        if (PasswordCheck.Count() == 0)
                        {
                            throw new Exception("false input password or name administration");
                        }
                    }

                    //FileAttributes attribute = File.GetAttributes(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\InfoAboutUsers.xml");//make hidden file xml
                    //attribute |= FileAttributes.Hidden;
                    //File.SetAttributes(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\InfoAboutUsers.xml", attribute);

                    XAttribute NameUser     = new XAttribute("name", name);
                    XElement   AccessUser   = new XElement("access", access);
                    XElement   PasswordUser = new XElement("password", password);
                    user.Add(NameUser);
                    user.Add(AccessUser);
                    user.Add(PasswordUser);
                    users.Add(user);
                    xdoc.Save(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\InfoAboutUsers.xml");
                    Console.WriteLine("Record data about you is completed\n" +
                                      "Name {0} Access:{1}", name, access);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message + "\n" + e.StackTrace);
                    access   = "User";
                    name     = "Visitor";
                    password = "";
                }
            }
        }