public List<Employee> Export()
 {
     Facade facade = new Facade();
     Company comp = facade.GetCompanyRepository().Get(Thread.CurrentPrincipal.Identity.Name);
     Log newLog = new Log() { Company = comp, Active = true, Date = DateTime.Now, Import = false, Employees = comp.Employees };
     facade.GetLogRepository().Add(newLog);
     return comp.Employees;
 }
        //Checks wether the username and password mathces.
        protected override bool OnAuthorizeUser(string username, string password, HttpActionContext actionContext)
        {
            string uri = actionContext.Request.RequestUri.AbsolutePath;
            Facade facade = new Facade();

            if (facade.GetCompanyRepository().Get(username) == null ||
                     !facade.GetCompanyRepository().AuthenticateCompany(username, password))
            {
                if (uri.ToLower().Contains("import"))
                {
                    facade.GetLogRepository()
                        .Add(new Log() {Active = true, Date = DateTime.Now, Import = true, LogState = LogState.unknown});
                }
                else
                {
                    facade.GetLogRepository()
                        .Add(new Log() { Active = true, Date = DateTime.Now, Import = false, LogState = LogState.unknown });
                }
                return false;
            }

            else if (!facade.GetCompanyRepository().Get(username).Active)
            {
                if (uri.ToLower().Contains("import"))
                {
                    facade.GetLogRepository()
                        .Add(new Log()
                        {
                            Active = true,
                            Date = DateTime.Now,
                            Import = true,
                            LogState = LogState.unsuccessfull,
                            Company = facade.GetCompanyRepository().Get(username)
                        });
                }
                else
                {
                    facade.GetLogRepository()
                        .Add(new Log()
                        {
                            Active = true,
                            Date = DateTime.Now,
                            Import = false,
                            LogState = LogState.unsuccessfull,
                            Company = facade.GetCompanyRepository().Get(username)
                        });
                }
                return false;
            }

            return true;
        }
        /// <summary>
        /// Override to Web API filter method to handle Basic Auth check
        /// </summary>
        /// <param name="actionContext"></param>
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            if (Active)
            {
                var identity = ParseAuthorizationHeader(actionContext);
                if (identity == null)
                {
                    string uri = actionContext.Request.RequestUri.AbsolutePath;
                    Facade facade = new Facade();

                    if (uri.ToLower().Contains("import"))
                    {
                        facade.GetLogRepository()
                            .Add(new Log() { Active = true, Date = DateTime.Now, Import = true, LogState = LogState.unknown });
                    }
                    else
                    {
                        facade.GetLogRepository()
                            .Add(new Log() { Active = true, Date = DateTime.Now, Import = false, LogState = LogState.unknown });
                    }

                    Challenge(actionContext);
                    return;
                }


                if (!OnAuthorizeUser(identity.Name, identity.Password, actionContext))
                {
                    Challenge(actionContext);
                    return;
                }

                var principal = new GenericPrincipal(identity, null);

                Thread.CurrentPrincipal = principal;

                // inside of ASP.NET this is required
                //if (HttpContext.Current != null)
                //    HttpContext.Current.User = principal;

                base.OnAuthorization(actionContext);
            }
        }
        public bool Import(IEnumerable<Employee> Employees )
        {
            Facade facade = new Facade();

            if (Employees == null || !Employees.Any())
            {
                return false;
            }


            Company comp = facade.GetCompanyRepository().Get(Thread.CurrentPrincipal.Identity.Name);
            Log newLog = new Log() { Company = comp, Active = true, Date = DateTime.Now, Import = true, Employees = new List<Employee>() };

            //Runs through every employee from the import list and checks if they are already present in the system.
            bool changesWereMade = false;
            foreach (var emp in Employees)
            {
                //Checks if the employee is already in the system.
                if (!ContainsEmployee(comp, emp))
                {
                    //If it is not in the system the nessecary changes will be made.
                    changesWereMade = true;
                    emp.Company = comp;
                    Employee newEmp = facade.GetEmployeeRepository().Add(emp);
                    comp.Employees.Add(newEmp);
                    newLog.Employees.Add(newEmp);
                }

            }

            //If changes were made the company will be updated and the new log will be saved. Otherwise it will be discarded.
            if (changesWereMade)
            {
                facade.GetCompanyRepository().Update(comp);
                facade.GetLogRepository().Add(newLog);
            }

            return true;
        }