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