LoginController::login PHP Method

login() public method

The login action, when you do login/login
public login ( )
    public function login()
    {
        // check if csrf token is valid
        if (!Csrf::isTokenValid()) {
            LoginModel::logout();
            Redirect::home();
            exit;
        }
        // perform the login method, put result (true or false) into $login_successful
        $login_successful = LoginModel::login(Request::post('user_name'), Request::post('user_password'), Request::post('set_remember_me_cookie'));
        // check login status: if true, then redirect user to user/index, if false, then to login form again
        if ($login_successful) {
            if (Request::post('redirect')) {
                Redirect::toPreviousViewedPageAfterLogin(ltrim(urldecode(Request::post('redirect')), '/'));
            } else {
                Redirect::to('user/index');
            }
        } else {
            if (Request::post('redirect')) {
                Redirect::to('login?redirect=' . ltrim(urlencode(Request::post('redirect')), '/'));
            } else {
                Redirect::to('login/index');
            }
        }
    }

Usage Example

コード例 #1
0
 function perform()
 {
     // get request params
     $company_id = $this->af->get('company_id');
     $login_id = $this->af->get('login_id');
     $password = $this->af->get('password');
     $app_name = $this->config->get('app_name');
     $company_cookei_key = $app_name . '_COMPANYCD';
     $loginid_cookei_key = $app_name . '_LOGINID';
     $expiretime = time() + 3600 * 24 * 30;
     // パスワードをハッシュ化
     $pwd_hash = md5($password);
     $output = array();
     try {
         $login = new LoginController();
         $params = array('company_id' => $company_id, 'login_id' => $login_id, 'password' => $pwd_hash);
         // ユーザ状態をチェック
         $code = $login->login($params);
         if ($code >= 1) {
             // 1 以上はエラー
             // IP、会社コード、ログインID、パスワードをログに記録
             $login_ip = getenv("REMOTE_ADDR");
             $authErrorMsg = $code . '/' . $login_ip . '/' . $company_id . '/' . $login_id . '/' . $password;
             $this->logger->log(LOG_INFO, 'Login failed: ' . $authErrorMsg);
             return array(401);
         }
         // パスワード強制変更
         // ログインのレスポンスとしては下記3項目
         $output = array('password_forced_change_flg' => '', 'password_forced_change_msg' => '', 'password_forced_change_location' => '');
         // 期限切れフラグチェック
         if ($this->session->get('pwd_kigengire') == 1) {
             // 期限切れなので、レスポンスに値を含める
             $output['password_forced_change_flg'] = 1;
             $output['password_forced_change_msg'] = Konst::ERR_MSG_LOGIN_PWD_FORCED_CHANGE_MSG;
             $output['password_forced_change_location'] = Konst::ERR_MSG_LOGIN_PWD_FORCED_CHANGE_LOCATION;
         } else {
             // 期限は切れていないので、フラグは0
             $output['password_forced_change_flg'] = 0;
         }
         $this->session->set('current_locale', $this->af->get('locale'));
         setcookie($company_cookei_key, $company_id, $expiretime);
         setcookie($loginid_cookei_key, $login_id, $expiretime);
         return array('json', $output);
     } catch (Exception $e) {
         // 致命的なエラーが発生
         $this->logger->log(LOG_DEBUG, $e->getTraceAsString());
         return array(500, $e->getMessage());
     }
     exit;
 }
All Usage Examples Of LoginController::login