Permalink
Browse files

Optional logins, logout handler

  • Loading branch information...
1 parent 5708601 commit 84ed804949bbf2561eb9772322d5591e159eab8c @brendonh committed Sep 11, 2011
@@ -13,14 +13,17 @@ public function init() {
parent::init();
}
- public function getUser() {
+ public function getUser($login=true) {
+
$sessionID = Yii::app()->session->sessionID;
$session = RedisSession::ensure($sessionID);
if ($session->userID) {
- return $session->getUser();
+ return $session->getUser();
}
+ if (!$login) return null;
+
$fbUser = $this->conn->getUser();
if (!$fbUser) {
@@ -61,4 +64,12 @@ public function doLogin() {
$request->redirect($url);
}
+ public function doLogout() {
+ $app = Yii::app();
+ $request = $app->getRequest();
+ $cb = $request->getBaseUrl() . $app->createUrl("yiidis/facebook/afterLogout");
+ $url = $this->conn->getLoginUrl(array('redirect_uri'=>$cb));
+ $request->redirect($url);
+ }
+
}
@@ -2,27 +2,39 @@
class YiidisController extends CController {
- public $user;
+ public $requireLogin = true;
+ public $user;
+
+ public function beforeAction() {
+ return $this->getUser();
+ }
- public function beforeAction() {
- try {
- $this->user = Yii::app()->facebook->getUser();
- } catch (FacebookNeedsLogin $e) {
- Yii::app()->facebook->doLogin();
- return false;
- }
-
- return true;
- }
-
- public function afterAction() {
- $count = Yii::app()->redis->conn->count;
- Yii::log("Queries: $count", "info");
- }
-
- public function getSession() {
- $sessionID = Yii::app()->session->sessionID;
- return RedisSession::ensure($sessionID);
+ public function getUser($login = null) {
+ if ($login === null) $login = $this->requireLogin;
+ try {
+ $this->user = Yii::app()->facebook->getUser($login);
+ } catch (FacebookNeedsLogin $e) {
+ Yii::app()->facebook->doLogin();
+ return false;
}
+
+ return true;
+ }
+
+ public function getUserOrRedirect($to=array('site/index')) {
+ if ($this->getUser(true)) return true;
+ $this->redirect($to);
+ return false;
+ }
+ public function afterAction() {
+ $count = Yii::app()->redis->conn->count;
+ Yii::log("Queries: $count", "info");
+ }
+
+ public function getSession() {
+ $sessionID = Yii::app()->session->sessionID;
+ return RedisSession::ensure($sessionID);
+ }
+
}
@@ -11,4 +11,9 @@ public function actionAfterLogin() {
Yii::app()->request->redirect("/");
}
+ public function actionAfterLogout() {
+ Yii::log("Facebook logout successful", "info");
+ Yii::app()->request->redirect("/");
+ }
+
}

0 comments on commit 84ed804

Please sign in to comment.