Example #1
0
 public void UpdateUserDetails(LoginToken <Customer> token, Customer customer)
 {
     if (token != null)
     {
         try
         {
             UpdateUserDetails(token, customer.User);
             _customerDAO.Update(customer);
             log.Info($"Customer {token.User.Id} updated his details");
         }
         catch (Exception ex)
         {
             log.Error($"Could not change customer {token.User.Id} details: {ex.Message}");
             throw new WrongCredentialsException($"Could not change customer {token.User.Id} details: {ex.Message}");
         }
     }
     else
     {
         log.Error("An unknown user tried to update details");
         throw new WasntActivatedByCustomerException("An unknown user tried to update details");
     }
 }
Example #2
0
 public long CreateNewCustomer(LoginToken <Administrator> token, Customer customer)
 {
     if (token != null)
     {
         Administrator administrator = token.User.Id != 0 ? _adminDAO.Get(token.User.Id) : LoginService.mainAdmin;
         if (administrator.Level != 1)
         {
             try
             {
                 long userId = _userDAO.Add(customer.User);
                 customer.User_Id = userId;
                 customer.User.Id = userId;
                 long id = _customerDAO.Add(customer);
                 log.Info($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} added new customer: {customer.First_Name} {customer.Last_Name}");
                 return(id);
             }
             catch (Exception ex)
             {
                 if (customer.User_Id != 0)
                 {
                     _userDAO.Remove(customer.User);
                 }
                 log.Error($"Could not create new customer: {ex.Message}");
                 throw new DuplicateDetailsException($"Could not create new customer: {ex.Message}");
             }
         }
         else
         {
             log.Error($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} did not have sanction to create new customer");
             throw new AdministratorDoesntHaveSanctionException($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} did not have sanction to create new customer");
         }
     }
     else
     {
         log.Error("An unknown user tried to create new customer");
         throw new WasntActivatedByAdministratorException("An unknown user tried to create new customer");
     }
 }
Example #3
0
 public long CreateNewAirline(LoginToken <Administrator> token, AirlineCompany airline)
 {
     if (token != null)
     {
         Administrator administrator = token.User.Id != 0 ? _adminDAO.Get(token.User.Id) : LoginService.mainAdmin;
         if (administrator.Level != 1)
         {
             try
             {
                 long userId = _userDAO.Add(airline.User);
                 airline.User.Id = userId;
                 airline.User_Id = userId;
                 long id = _airlineDAO.Add(airline);
                 log.Info($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} added new airline: {airline.Name}");
                 return(id);
             }
             catch (Exception ex)
             {
                 if (airline.User_Id != 0)
                 {
                     _userDAO.Remove(airline.User);
                 }
                 log.Error($"Could not Add New airline company: {ex.Message}");
                 throw new DuplicateDetailsException($"Could not Add New airline company: {ex.Message}");
             }
         }
         else
         {
             log.Error($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} did not have sanction to create new airline in the Db");
             throw new AdministratorDoesntHaveSanctionException($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} did not have sanction to create new airline in the Db");
         }
     }
     else
     {
         log.Error("An unknown user tried to create new airline");
         throw new WasntActivatedByAdministratorException("An unknown user tried to create new airline");
     }
 }
Example #4
0
 public void UpdateCustomerDetails(LoginToken <Administrator> token, Customer customer)
 {
     if (token != null)
     {
         Administrator administrator = token.User.Id != 0 ? _adminDAO.Get(token.User.Id) : LoginService.mainAdmin;
         try
         {
             UpdateUserDetails(token, customer.User);
             _customerDAO.Update(customer);
             log.Info($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} Updated customer {customer.First_Name} {customer.Last_Name} details");
         }
         catch (Exception ex)
         {
             log.Error($"Could not change user {administrator.User.Id} details: {ex.Message}");
             throw new WrongCredentialsException($"Could not change user {administrator.User.Id} details: {ex.Message}");
         }
     }
     else
     {
         log.Error("An unknown user tried to update a customer details");
         throw new WasntActivatedByAdministratorException("An unknown user tried to update a customer details");
     }
 }
Example #5
0
 public void MofidyAirlineDetails(LoginToken <AirlineCompany> token, AirlineCompany airline)
 {
     if (token != null)
     {
         try
         {
             AirlineCompany airlineCompany = _airlineDAO.Get(token.User.Id);
             UpdateUserDetails(token, airline);
             _airlineDAO.Update(airline);
             log.Info($"Airline {airlineCompany.Name} updated their details");
         }
         catch (Exception ex)
         {
             log.Error($"Could not change Airkine {token.User.Id} details: {ex.Message}");
             throw new WrongCredentialsException($"Could not change Airkine {token.User.Id} details: {ex.Message}");
         }
     }
     else
     {
         log.Error("An unknown user tried to update airline company details");
         throw new WasntActivatedByAirlineException("An unknown user tried to update airline company details");
     }
 }
Example #6
0
 public void RemoveAirline(LoginToken <Administrator> token, AirlineCompany airline)
 {
     if (token != null)
     {
         Administrator administrator = token.User.Id != 0 ? _adminDAO.Get(token.User.Id) : LoginService.mainAdmin;
         if (administrator.Level != 1)
         {
             IList <Flight> flights = _flightDAO.GetAll();
             foreach (Flight flight in flights)
             {
                 if (flight.Airline_Company_Id == airline.Id)
                 {
                     IList <Ticket> tickets = _ticketDAO.GetTicketsByFlight(flight);
                     foreach (Ticket ticket in tickets)
                     {
                         _ticketDAO.Remove(ticket);
                     }
                     _flightDAO.Remove(flight);
                 }
             }
             User user = airline.User;
             _airlineDAO.Remove(airline);
             _userDAO.Remove(user);
             log.Info($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} removed airline {airline.Name} from the system");
         }
         else
         {
             log.Error($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} did not have sanction to remove airline {airline.Name} from the system");
             throw new AdministratorDoesntHaveSanctionException($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} did not have sanction to remove airline {airline.Name} from the system");
         }
     }
     else
     {
         log.Error("An unknown user tried to remove an airline from the system");
         throw new WasntActivatedByAdministratorException("An unknown user tried to remove an airline from the system");
     }
 }
Example #7
0
 public void ModifyMyAdminUser(LoginToken <Administrator> token, Administrator admin)
 {
     if (token != null)
     {
         Administrator administrator = token.User.Id != 0 ? _adminDAO.Get(token.User.Id) : LoginService.mainAdmin;
         User          user          = administrator.User;
         try
         {
             UpdateUserDetails(token, admin.User);
             _adminDAO.Update(admin);
             log.Info($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} Updated it's details");
         }
         catch (Exception ex)
         {
             log.Error($"Could not change user {user.Id} details: {ex.Message}");
             throw new WrongCredentialsException($"Could not change user {user.Id} details: {ex.Message}");
         }
     }
     else
     {
         log.Error("An unknown user tried to update an admin details");
         throw new WasntActivatedByAdministratorException("An unknown user tried to update an admin details");
     }
 }
Example #8
0
        private void UpdateUserDetails(LoginToken <AirlineCompany> token, AirlineCompany airline)
        {
            AirlineCompany airlineCompany = _airlineDAO.Get(token.User.Id);
            User           user           = _userDAO.Get(airlineCompany.User_Id);

            if (token != null)
            {
                try
                {
                    _userDAO.Update(airline.User);
                    log.Info($"User {user.Id} updated his details");
                }
                catch (Exception ex)
                {
                    log.Error($"Could not change user {user.Id} details: {ex.Message}");
                    throw new WrongCredentialsException($"Could not change user {user.Id} details: {ex.Message}");
                }
            }
            else
            {
                log.Error("An unknown user tried to update details");
                throw new WasntActivatedByAirlineException("An unknown user tried to update details");
            }
        }
Example #9
0
 public IList <Ticket> GetAllTickets(LoginToken <AirlineCompany> token)
 {
     if (token != null)
     {
         AirlineCompany airlineCompany = _airlineDAO.Get(token.User.Id);
         List <Ticket>  tickets        = new List <Ticket>();
         IList <Flight> flights        = GetAllFlights(token);
         IList <Ticket> tickets_list   = _ticketDAO.GetAll();
         foreach (Ticket ticket in tickets_list)
         {
             if (flights.Contains(ticket.Flight))
             {
                 tickets.Add(ticket);
             }
         }
         log.Info($"Airline {airlineCompany.Name} Got all bought tickets from the system");
         return(tickets);
     }
     else
     {
         log.Error("An unknown user tried to get all bought tickets of a airline company from the system");
         throw new WasntActivatedByAirlineException("An unknown user tried to get all bought tickets of a airline company from the system");
     }
 }
Example #10
0
 public long CreateAdmin(LoginToken <Administrator> token, Administrator admin)
 {
     if (token != null)
     {
         Administrator administrator = token.User.Id != 0 ? _adminDAO.Get(token.User.Id) : LoginService.mainAdmin;
         if (administrator.Level > admin.Level && administrator.Level == 3)
         {
             try
             {
                 long userId = _userDAO.Add(admin.User);
                 admin.User.Id = userId;
                 admin.User_Id = userId;
                 long id = _adminDAO.Add(admin);
                 log.Info($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} added new administrator: {admin.First_Name} {admin.Last_Name}");
                 return(id);
             }
             catch (Exception ex)
             {
                 if (admin.User_Id != 0)
                 {
                     _userDAO.Remove(admin.User);
                 }
                 log.Error($"faild to add user: {ex.Message}");
                 throw new DuplicateDetailsException($"faild to add user: {ex.Message}");
             }
         }
         else
         {
             if (administrator.Level == 3)
             {
                 if (administrator.First_Name == "Main" && administrator.Last_Name == "admin")
                 {
                     try
                     {
                         long userId = _userDAO.Add(admin.User);
                         admin.User.Id = userId;
                         admin.User_Id = userId;
                         long id = _adminDAO.Add(admin);
                         log.Info($"Main admin added new administrator: {admin.First_Name} {admin.Last_Name}");
                         return(id);
                     }
                     catch (Exception ex)
                     {
                         if (admin.User_Id != 0)
                         {
                             _userDAO.Remove(admin.User);
                         }
                         log.Error($"faild to add user: {ex.Message}");
                         throw new DuplicateDetailsException($"faild to add user: {ex.Message}");
                     }
                 }
                 else
                 {
                     log.Error($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} did not have sanction to add new administrator");
                     throw new AdministratorDoesntHaveSanctionException($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} did not have sanction to add new administrator");
                 }
             }
             else
             {
                 log.Error($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} did not have sanction to add new administrator");
                 throw new AdministratorDoesntHaveSanctionException($"{token.User.Id} {administrator.First_Name} {administrator.Last_Name} did not have sanction to add new administrator");
             }
         }
     }
     else
     {
         log.Error("An unknown user tried to add administrator");
         throw new WasntActivatedByAdministratorException("An unknown user tried to add administrator");
     }
 }
        public bool TryLogin(out FacadeBase facade, out ILoginToken loginToken, string userName, string password)
        {
            if (userName == "admin")
            {
                if (password == "99999")
                {
                    facade     = new LoggedInAdministratorFacade();
                    loginToken = new LoginToken <Administrator>()
                    {
                        User = mainAdmin
                    };
                    log.Info("Main Administrator has logged in to the system");
                    return(true);
                }
                else
                {
                    log.Error("One or more of the super admin details are wrong");
                    throw new WrongCredentialsException("One or more of the super admin details are wrong");
                }
            }
            User user;

            try
            {
                user = _userDAO.GetUserByUserName(userName);
            }
            catch (Exception ex)
            {
                log.Error($"Could not find user: {ex.Message}");
                throw new WrongCredentialsException($"One or more of the details are wrong: {ex.Message}");
            }
            if (user.Password == password)
            {
                switch (user.User_Role)
                {
                case 1:
                {
                    facade = new LoggedInAdministratorFacade();
                    Administrator admin = _adminDAO.GetAdminByUserId(user.Id);
                    loginToken = new LoginToken <Administrator>()
                    {
                        User = admin
                    };
                    break;
                }

                case 2:
                {
                    facade = new LoggedInAirlineFacade();
                    AirlineCompany airlineCompany = _airlineDAO.GetAirlineByUserId(user.Id);
                    loginToken = new LoginToken <AirlineCompany>()
                    {
                        User = airlineCompany
                    };
                    break;
                }

                case 3:
                {
                    facade = new LoggedInCustomerFacade();
                    Customer customer = _customerDAO.GetCustomerByUserId(user.Id);
                    loginToken = new LoginToken <Customer>()
                    {
                        User = customer
                    };
                    break;
                }

                default:
                {
                    facade     = new AnonymousUserFacade();
                    loginToken = null;
                    break;
                }
                }
                log.Info($"User {userName} has logged in to the system");
                return(true);
            }
            else
            {
                log.Error($"One or more of the user {userName} details are wrong");
                throw new WrongCredentialsException($"One or more of the user {userName} details are wrong");
            }
        }