public function doLogin(LoginRequest $request)
 {
     $username = Input::get('username');
     $password = Input::get('password');
     // check if I have a user with user and laravel hashed password
     $user = User::where('username', '=', $username)->first();
     // i have a user corresponding to the login
     if ($user && Hash::check($password, $user->password)) {
         // if the user has never logged in before, he must go through the start page
         if (!$user->last_login) {
             // if password is safe keep it so the form doesn't ask for it later
             // safe also the id of the matching user
             Session::set('start_session', ['safe_enough' => $this->safeEnough($password), 'user_id' => $user->id]);
             // redirect to start page
             return redirect()->route('login.start');
         } else {
             // actual login
             Auth::attempt(array('username' => $username, 'password' => $password), true);
             // double check if the user has an active contact, if not, setup one
             if (is_null(Auth::user()->active_contact_id)) {
                 $user = Auth::user();
                 $contact = CompanyPerson::where('person_id', Auth::user()->person_id)->first();
                 $user->active_contact_id = $contact->id;
                 $user->last_login = Carbon::now();
                 $user->save();
             }
             return redirect()->intended(route('tickets.index'))->with('successes', ['Accessed successfully']);
         }
     } else {
         // check if exists user and md5 hashed password
         $user = User::where('username', '=', $username)->where('password', '=', md5($password))->first();
         // if it exists
         if ($user) {
             // safe password in laravel hashed password and redo the login
             return $this->MD5ToHASHLaravelConversion($request, $user, $password);
         } else {
             return redirect()->route('login.index')->withErrors(['The username or the password are incorrect']);
         }
     }
 }
 public function edit($id)
 {
     if (Auth::user()->can('update-company')) {
         $data['company'] = Company::find($id);
         $selected_account_manager = CompanyAccountManager::where('company_id', '=', $id)->first();
         $data['company']->account_manager_id = isset($selected_account_manager) ? $selected_account_manager->account_manager_id : null;
         $selected_main_contact = CompanyMainContact::where('company_id', '=', $id)->first();
         $data['company']->main_contact_id = isset($selected_main_contact) ? $selected_main_contact->main_contact_id : null;
         $data['account_managers'] = CompanyPersonController::API()->all(["where" => ["company_person.company_id|=|" . ELETTRIC80_COMPANY_ID, "company_person.title_id|=|" . ACCOUNT_MANAGER_TITLE_ID], "order" => ["people.last_name|ASC", "people.first_name|ASC"], "paginate" => "false"]);
         $data['main_contacts'] = CompanyPerson::where('company_person.company_id', '=', $id)->get();
         $data['support_types'] = SupportType::orderBy("name")->get();
         $data['connection_types'] = ConnectionType::orderBy("name")->get();
         $data['escalation_profiles'] = EscalationProfile::orderBy("name")->get();
         $data['title'] = "Edit " . $data['company']->name;
         return view('companies/edit', $data);
     } else {
         return redirect()->back()->withErrors(['Access denied to companies edit page']);
     }
 }