示例#1
0
 protected function _checkLogin()
 {
     if (RequestMethods::post("action") == "logmein") {
         $username = RequestMethods::post("username");
         $password = RequestMethods::post("password");
         $user = User::first(array("username = ?" => $username, "live = ?" => true));
         if (!$user) {
             return array("error" => "Invalid username/password");
         }
         if (!Markup::checkHash($password, $user->password)) {
             return array("error" => "Invalid username/password");
         }
         $session = Registry::get("session");
         $this->setUser($user);
         if ($user->admin) {
             self::redirect("/admin");
         }
         $headers = getallheaders();
         $scholar = Scholar::first(array("user_id = ?" => $user->id));
         if ($scholar) {
             $session->set('scholar', $scholar);
             $organization = Organization::first(array("id = ?" => $scholar->organization_id));
             $session->set('organization', $organization);
             if (isset($headers["X-Student-App"])) {
                 $meta = $this->_meta($user, "student");
                 return array("success" => true, "meta" => $meta, "scholar" => $scholar);
             } else {
                 self::redirect("/student");
             }
         }
         $organization = Organization::first(array("user_id = ?" => $user->id));
         if ($organization) {
             $session->set('organization', $organization);
             self::redirect("/school");
         }
         $educator = Educator::first(array("user_id = ?" => $user->id));
         if ($educator) {
             $session->set('educator', $educator);
             $organization = Organization::first(array("id = ?" => $educator->organization_id));
             $session->set('organization', $organization);
             if (isset($headers["X-Teacher-App"])) {
                 $meta = $this->_meta($user, "teacher");
                 return array("success" => true, "meta" => $meta, "educator" => $educator);
             } else {
                 self::redirect("/teacher");
             }
         }
         return array("error" => "Something went wrong please try again later");
     } else {
         return array("error" => "Invalid Request");
     }
 }