コード例 #1
0
ファイル: Auth.php プロジェクト: ravikathaitarm01/fluenz1
 protected function _login()
 {
     if ($this->input->is_ajax_request()) {
         $login_as = false;
         $u = null;
         if (UserSession::get('user.type') === 'admin') {
             $u = (new User($this->input->post('id')))->get();
             $this->_set_picture($u);
             $login_as = true;
         } else {
             if (UserSession::get('user.type') === 'partner') {
                 $partner = new Partner(UserSession::get('user._id'));
                 if ($partner->valid_brand($this->input->post('id'))) {
                     $u = (new User($this->input->post('id')))->get();
                     $this->_set_picture($u);
                     $login_as = true;
                 }
             } else {
                 try {
                     $data = $this->_get_login_data();
                     $user = new User(null);
                     if ($u = $user->authenticate($data['username'], Secure::password($data['password'], $data['username']))) {
                         $this->_set_picture($u);
                         if (isset($u['social'])) {
                             unset($u['social']);
                             // Unset unnecessary social data
                         }
                         if ($u['type'] === 'extra') {
                             $t = $u;
                             $u = (new User($t['account']))->get();
                             $u['manager'] = $t;
                         }
                     }
                 } catch (\Exception $e) {
                     Json::error($e->getMessage());
                 }
             }
         }
         if ($u) {
             $data = array('user' => $u);
             if ($login_as) {
                 // Set the main user, if an existing doesn't exist
                 // Only the first user set is main user
                 $data['main_user'] = UserSession::get('main_user') ?: UserSession::get('user');
             }
             UserSession::set(null, $data);
             Json::success('Login successful! Redirecting to home...', Url::base(''));
         }
         Json::error('Invalid credentials or user not active');
     }
 }