Example #1
0
 /**
  * Returns singleton class instance.
  *
  * @return SwiftRiver_Client
  */
 public static function instance()
 {
     if (!self::$_instance) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
Example #2
0
 /**
  * Perfomr login using user credentials.
  *
  * @param   string   username
  * @param   string   password
  * @param   boolean  remember enable autologin
  * @return  boolean
  */
 protected function _login($username, $password, $remember)
 {
     try {
         $token_params = array('username' => $username, 'password' => $password);
         $auth = SwiftRiver_Client::instance()->get_access_token('password', $token_params);
         $this->complete_login($auth);
     } catch (SwiftRiver_API_Exception_Authorization $e) {
         Kohana::$log->add(Log::ERROR, $e->getMessage());
         return FALSE;
     }
     return TRUE;
 }
Example #3
0
 /**
  * Call any path, DELETE method
  * Example: $api->delete('/v1/rivers/1')
  *
  * @param  string   $path The resource path
  * @return bool
  */
 protected function delete($path)
 {
     return $this->client->delete($path);
 }
Example #4
0
 /**
  * The before() method is called before main controller action.
  * In our template controller we override this method so that we can
  * set up default values. These variables are then available to our
  * controllers if they need to be modified.
  *
  * @return	void
  */
 public function before()
 {
     // Execute parent::before first
     parent::before();
     try {
         $this->session = Session::instance();
     } catch (ErrorException $e) {
         session_destroy();
     }
     // Load the default Cache engine
     $this->cache = Cache::instance();
     // Open session
     $this->session = Session::instance();
     // SwiftRiver API
     $this->api = SwiftRiver_Client::instance();
     // Services
     $this->account_service = new Service_Account($this->api);
     $this->river_service = new Service_River($this->api);
     $this->bucket_service = new Service_Bucket($this->api);
     $this->form_service = new Service_Form($this->api);
     // Initialize the dashboard url
     $this->dashboard_url = URL::site(FALSE, TRUE);
     if (Auth::instance()->logged_in()) {
         try {
             $auth = Auth::instance()->get_user();
             $this->api->set_access_token($auth['access_token']);
             $this->user = $this->account_service->get_logged_in_account();
             if ($this->user['owner']['username'] == 'public') {
                 if (strtolower($this->request->controller()) == 'login' or !(bool) Swiftriver::get_setting('anonymous_access_enabled')) {
                     Auth::instance()->logout();
                 }
             }
         } catch (Swiftriver_API_Exception_Authorization $e) {
             Auth::instance()->logout();
         } catch (Swiftriver_API_Exception_Forbidden $e) {
             Auth::instance()->logout();
         }
     }
     // If we're not logged in, gives us chance to auto login
     $supports_auto_login = new ReflectionClass(get_class(Auth::instance()));
     $supports_auto_login = $supports_auto_login->hasMethod('auto_login');
     if (!Auth::instance()->logged_in() and $supports_auto_login) {
         // Controller exempt from auth check
         $exempt_controllers = Kohana::$config->load('auth.ignore_controllers');
         Auth::instance()->auto_login();
         if (!Auth::instance()->get_user() and !in_array(strtolower($this->request->controller()), $exempt_controllers)) {
             $this->login_required();
         }
     }
     if ($this->auth_required !== FALSE and Auth::instance()->logged_in($this->auth_required) === FALSE) {
         if (Auth::instance()->logged_in()) {
             // User is logged in but not on the secure_actions list
             $this->access_required();
         } else {
             $this->login_required();
         }
     }
     if ($this->user) {
         // Is anonymous logged in?
         if ($this->user['owner']['username'] == 'public') {
             $this->anonymous = TRUE;
         }
         // Is this user an admin?
         $this->admin = FALSE;
         // FIXME:$this->user->is_admin();
         if (strtolower(Kohana::$config->load('auth.driver')) == 'riverid' and !in_array($this->user->username, Kohana::$config->load('auth.exempt'))) {
             $this->riverid_auth = TRUE;
         }
         // Logged in user's dashboard url
         if ($this->anonymous) {
             $this->dashboard_url = URL::site('welcome');
         } else {
             $this->dashboard_url = URL::site($this->user['account_path'], TRUE);
         }
         // Build the base URL
         $visited_account_path = $this->request->param('account');
         if ($visited_account_path and $visited_account_path != $this->user['account_path']) {
             $this->base_url = URL::site($visited_account_path . '/' . strtolower($this->request->controller()));
             try {
                 $this->visited_account = $this->account_service->get_account_by_name($visited_account_path);
             } catch (Swiftriver_API_Exception $e) {
                 // Visited account doesn't exist?
                 $this->redirect($this->dashboard_url, 302);
             }
         } else {
             $this->base_url = URL::site($this->user['account_path'] . '/' . strtolower($this->request->controller()));
             $this->visited_account = $this->user;
         }
     }
     // Load Header & Footer & variables
     if ($this->auto_render) {
         $this->template->header = View::factory('template/header')->bind('user', $this->user)->bind('site_name', $site_name)->bind('dashboard_url', $this->dashboard_url);
         $this->template->header->js = '';
         // Dynamic Javascript
         $this->template->header->css = '';
         // Dynamic CSS
         $this->template->header->meta = '';
         $this->template->header->show_nav = TRUE;
         $site_name = Swiftriver::get_setting('site_name');
         // System messages
         $this->template->header->messages = json_encode($this->session->get_once('messages'));
         // Header Nav
         $this->template->header->nav_header = View::factory('template/nav/header')->bind('user', $this->user)->bind('admin', $this->admin)->bind('account', $this->account)->bind('anonymous', $this->anonymous)->bind('dashboard_url', $this->dashboard_url)->bind('show_search_bar', $show_search_bar)->bind('controller', $controller);
         $controller = strtolower($this->request->controller());
         if ($this->user) {
             $this->template->header->nav_header->num_notifications = 0;
             if (!($buckets = Cache::instance()->get('user_buckets_' . $this->user['id'], FALSE))) {
                 $buckets = json_encode($this->account_service->get_buckets($this->user, $this->user));
                 //Cache::instance()->set('user_buckets_'.$this->user->id, $buckets, 3600 + rand(0,3600));
             }
             $this->template->header->bucket_list = $buckets;
             if (!($rivers = Cache::instance()->get('user_rivers_' . $this->user['id'], FALSE))) {
                 $rivers = json_encode($this->account_service->get_rivers($this->user, $this->user));
                 //Cache::instance()->set('user_rivers_'.$this->user->id, $rivers, 3600 + rand(0,3600));
             }
             $this->template->header->river_list = $rivers;
             if (!($forms = Cache::instance()->get('user_forms_' . $this->user['id'], FALSE))) {
                 $forms = json_encode($this->account_service->get_forms($this->user, $this->user));
                 //Cache::instance()->set('user_forms_'.$this->user->id, $rivers, 3600 + rand(0,3600));
             }
             $this->template->header->form_list = $forms;
         }
         $this->template->content = '';
         $this->template->footer = View::factory('template/footer');
     }
 }