Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
EAuthWidget.php
1 <?php
2 /**
3  * EAuthWidget class file.
4  *
5  * @author Maxim Zemskov <nodge@yandex.ru>
6  * @link http://code.google.com/p/yii-eauth/
7  * @license http://www.opensource.org/licenses/bsd-license.php
8  */
9 
10 /**
11  * The EAuthWidget widget prints buttons to authenticate user with OpenID and OAuth providers.
12  * @package application.extensions.eauth
13  */
14 class EAuthWidget extends CWidget {
15 
16  /**
17  * @var string EAuth component name.
18  */
19  public $component = 'eauth';
20 
21  /**
22  * @var array the services.
23  * @see EAuth::getServices()
24  */
25  public $services = null;
26 
27  /**
28  * @var boolean whether to use popup window for authorization dialog. Javascript required.
29  */
30  public $popup = null;
31 
32  /**
33  * @var string the action to use for dialog destination. Default: the current route.
34  */
35  public $action = null;
36 
37  /**
38  * Initializes the widget.
39  * This method is called by {@link CBaseController::createWidget}
40  * and {@link CBaseController::beginWidget} after the widget's
41  * properties have been initialized.
42  */
43  public function init() {
44  parent::init();
45 
46  // EAuth component
47  $component = Yii::app()->{$this->component};
48 
49  // Some default properties from component configuration
50  if (!isset($this->services))
51  $this->services = $component->getServices();
52  if (!isset($this->popup))
53  $this->popup = $component->popup;
54 
55  // Set the current route, if it is not set.
56  if (!isset($this->action))
57  $this->action = Yii::app()->urlManager->parseUrl(Yii::app()->request);
58  }
59 
60  /**
61  * Executes the widget.
62  * This method is called by {@link CBaseController::endWidget}.
63  */
64  public function run() {
65  parent::run();
66 
67  $this->registerAssets();
68  $this->render('auth', array(
69  'id' => $this->getId(),
70  'services' => $this->services,
71  'action' => $this->action,
72  ));
73  }
74 
75  /**
76  * Register CSS and JS files.
77  */
78  protected function registerAssets() {
79  $cs = Yii::app()->clientScript;
80  $cs->registerCoreScript('jquery');
81 
82  $assets_path = dirname(__FILE__).DIRECTORY_SEPARATOR.'assets';
83  $url = Yii::app()->assetManager->publish($assets_path, false, -1, YII_DEBUG);
84  $cs->registerCssFile($url.'/css/auth.css');
85 
86  // Open the authorization dilalog in popup window.
87  if ($this->popup) {
88  $cs->registerScriptFile($url.'/js/auth.js', CClientScript::POS_HEAD);
89  $js = '';
90  foreach ($this->services as $name => $service) {
91  $args = $service->jsArguments;
92  $args['id'] = $service->id;
93  $js .= '$(".auth-service.'.$service->id.' a").eauth('.json_encode($args).');'."\n";
94  }
95  $cs->registerScript('eauth-services', $js, CClientScript::POS_READY);
96  }
97  }
98 }