Advertisement
Guest User

Untitled

a guest
Mar 10th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace UserLogin
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Console.OutputEncoding = Encoding.UTF8;
  15.  
  16.             Console.Write("Enter username: ");
  17.             string username = Console.ReadLine();
  18.  
  19.             Console.Write("Enter password: ");
  20.             string password = Console.ReadLine();
  21.  
  22.             LoginValidation validation = new LoginValidation(username, password, ExceptionAction);
  23.  
  24.             User user = new User();
  25.  
  26.             if (validation.ValidateUserInput(user))
  27.             {
  28.                 Console.WriteLine();
  29.                 Console.WriteLine("-----Sucessfull login!-----");
  30.                 Console.WriteLine();
  31.  
  32.                 switch (LoginValidation.currentUserRole)
  33.                 {
  34.                     case UserRoles.ADMIN:
  35.                         Console.WriteLine("Welcome, ADMIN!");
  36.                         user.PrintInfo();
  37.  
  38.                         AdminActions();
  39.  
  40.                         break;
  41.                     case UserRoles.INSPECTOR:
  42.                         Console.WriteLine("Welcome, INSPECTOR!");
  43.                         user.PrintInfo();
  44.                         break;
  45.                     case UserRoles.PROFESSOR:
  46.                         Console.WriteLine("Welcome, PROFESSOR!");
  47.                         user.PrintInfo();
  48.                         break;
  49.                     case UserRoles.STUDENT:
  50.                         Console.WriteLine("Welcome, STUDENT!");
  51.                         user.PrintInfo();
  52.                         break;
  53.                 }
  54.  
  55.                 Console.WriteLine();
  56.             }
  57.             else
  58.             {
  59.                 //Console.WriteLine(validation.ErrorMessage);
  60.             }
  61.  
  62.             //Console.ReadLine();
  63.         }
  64.  
  65.         public static void AdminActions()
  66.         {
  67.             Console.WriteLine("----------------------------------");
  68.             Console.WriteLine("Изберете опция:");
  69.             Console.WriteLine("0: Изход");
  70.             Console.WriteLine("1: Промяна на роля на потребител");
  71.             Console.WriteLine("2: Промяна на активност на потребител");
  72.             Console.WriteLine("3: Списък на потребителите");
  73.             Console.WriteLine("4: Преглед на лог на актовност");
  74.             Console.WriteLine("5: Преглед текуща активност");
  75.             Console.Write("\nИзбор: ");
  76.  
  77.             int choice = int.Parse(Console.ReadLine());
  78.  
  79.             switch (choice)
  80.             {
  81.                 case 0:
  82.                     Environment.Exit(1);
  83.                     break;
  84.                 case 1:
  85.                     ChangeUserRole();
  86.                     break;
  87.                 case 2:
  88.                     ChangeUserActivity();
  89.                     break;
  90.                 case 3:
  91.                     ListAllUsers();
  92.                     break;
  93.                 case 4:
  94.                     ListLogActivity();
  95.                     break;
  96.                 case 5:
  97.                     GetCurrentSessionActivities();
  98.                     break;
  99.                 default:
  100.                     Console.WriteLine("Oops! Невалиден избор");
  101.                     AdminActions();
  102.                     break;
  103.             }
  104.  
  105.         }
  106.  
  107.         public static void GetCurrentSessionActivities()
  108.         {
  109.             Console.WriteLine("Въведете филтър за търсене: ");
  110.             string filter = Console.ReadLine();
  111.  
  112.             Logger.GetCurrentSessionActivities(filter);
  113.  
  114.             AdminActions();
  115.         }
  116.  
  117.         public static void ListLogActivity()
  118.         {
  119.             if (File.Exists("log.txt") == true)
  120.             {
  121.                 using (StreamReader Reader = new StreamReader("log.txt"))
  122.                 {
  123.                     StringBuilder Sb = new StringBuilder();
  124.                     Sb.Append(Reader.ReadToEnd());
  125.                     {
  126.                         Console.WriteLine("The Log File is read");
  127.                     }
  128.  
  129.                     Console.WriteLine(Sb);
  130.  
  131.                     Reader.Close();
  132.                 }
  133.             }
  134.  
  135.             AdminActions();
  136.         }
  137.  
  138.         public static void ListAllUsers()
  139.         {
  140.             Dictionary<string, int> allUsers = UserData.AllUsersUsernames();
  141.  
  142.             foreach (KeyValuePair<string, int> user in allUsers)
  143.             {
  144.                 Console.WriteLine(user.Value + ". " + user.Key);
  145.             }
  146.  
  147.             AdminActions();
  148.         }
  149.  
  150.         public static void ChangeUserRole()
  151.         {
  152.             Console.WriteLine();        
  153.             Console.Write("Потребителско име: ");
  154.             string userToEdit = Console.ReadLine();
  155.             Dictionary<string, int> allUsers = UserData.AllUsersUsernames();
  156.  
  157.             if (!allUsers.ContainsKey(userToEdit) || userToEdit.Equals(String.Empty))
  158.             {
  159.                 Console.WriteLine();
  160.                 Console.WriteLine("Несъществуващ потребител!");
  161.                 AdminActions();
  162.             }
  163.  
  164.             Console.WriteLine("1.ADMIN, 2.INSPECTOR, 3.PROFESSOR, 4.STUDENT");
  165.             Console.Write("Нова роля: ");
  166.             int role = int.Parse(Console.ReadLine());
  167.  
  168.             if (role != 1 && role != 2 && role !=3 && role != 4)
  169.             {
  170.                 Console.WriteLine("Oops! Невалидна роля!");
  171.                 ChangeUserRole();
  172.             }
  173.  
  174.             if (allUsers.ContainsKey(userToEdit))
  175.             {
  176.                 UserData.AssignUserRole(allUsers[userToEdit], (UserRoles)role);
  177.                 Console.WriteLine();
  178.                 Console.WriteLine("Успешно променена роля!");
  179.                 AdminActions();
  180.             }
  181.             else
  182.             {
  183.                 Console.WriteLine();
  184.                 Console.WriteLine("Невалиден потребител!");
  185.                 AdminActions();
  186.             }
  187.         }
  188.  
  189.         public static void ChangeUserActivity()
  190.         {
  191.             Console.WriteLine();
  192.             Console.Write("Потребителско име: ");
  193.             string userToEdit = Console.ReadLine();
  194.             Dictionary<string, int> allUsers = UserData.AllUsersUsernames();
  195.            
  196.             Console.Write("Нова дата на активност: ");
  197.             string date = Console.ReadLine();
  198.             DateTime validTo;
  199.  
  200.             if (DateTime.TryParse(date, out validTo))
  201.             {
  202.                 if (allUsers.ContainsKey(userToEdit))
  203.                 {
  204.                     UserData.SetUserActiveTo(allUsers[userToEdit], validTo);
  205.                     Console.WriteLine();
  206.                     Console.WriteLine("Успешно сменена дата!");
  207.                     AdminActions();
  208.                 }
  209.                 else
  210.                 {
  211.                     Console.WriteLine("Невалиден потребител!");
  212.                     AdminActions();
  213.                 }
  214.                            
  215.             }
  216.             else
  217.             {
  218.                 Console.WriteLine("Невалидна дата!");
  219.                 AdminActions();
  220.             }
  221.  
  222.         }
  223.  
  224.         public static void ExceptionAction(string s)
  225.         {
  226.             Console.WriteLine("Oops! " + s);
  227.         }
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement