Example #1
0
 /**
  * Get current application user
  *
  * @return User
  */
 public static function currentUser()
 {
     if (Common::checkAuthorization()) {
         $user = User::find_by_id($_SESSION['user']);
         return $user;
     }
     return null;
 }
Example #2
0
 /**
  * Perform user authorisation.
  *
  * @param string $login
  * @param string $password
  * @return bool
  */
 public static function doAuthorisation($login = '', $password = '')
 {
     $user = User::find_by_login_and_password($login, Common::getPasswordHash($password, $login));
     if ($user) {
         $_SESSION['user'] = $user->id;
         $_SESSION['login'] = $user->login;
         $session = Session::find_by_user_and_agent($user->id, $_SERVER['HTTP_USER_AGENT']);
         if (!$session) {
             $session = Session::create(array('user' => $user->id, 'agent' => $_SERVER['HTTP_USER_AGENT']));
         }
         $session->save();
         $_SESSION['session'] = $session->id;
         setcookie("u", $user->id, time() + 3600 * 24 * 14);
         setcookie("s", md5($session->id), time() + 3600 * 24 * 14);
         return true;
     }
     return false;
 }
Example #3
0
 /**
  * DELETE method: Delete user
  *
  * @param Request $request
  * @throws Exception
  * @return mixed
  */
 public function delete($request)
 {
     switch (count($request->url_elements)) {
         case 2:
             // Don't have permissions - throw Exception
             if (!Application::isAdmin()) {
                 throw new Exception("You don't have required permissions to update this user.", 403);
             }
             // Like success - delete user & return
             $user = User::find_by_id($request->url_elements[1]);
             if ($user) {
                 $user->delete();
                 return json_decode($user->to_json(array('except' => 'password')));
             } else {
                 throw new Exception("User not found.", 404);
             }
         default:
             throw new Exception("Unknown request.", 500);
     }
 }