Blame view

common/components/nodge/eauth/src/Widget.php 2.52 KB
b0f143c3   Yarik   first commit
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
  <?php
  /**
   * Widget class file.
   *
   * @author Maxim Zemskov <nodge@yandex.ru>
   * @link http://github.com/Nodge/yii2-eauth/
   * @license http://www.opensource.org/licenses/bsd-license.php
   */
  
  namespace common\modules\nodge\eauth\src\eauth;
  
  use Yii;
  
  /**
   * The EAuthWidget widget prints buttons to authenticate user with OpenID and OAuth providers.
   *
   * @package application.extensions.eauth
   */
  class Widget extends \yii\base\Widget
  {
  
  	/**
  	 * @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;
  
  	/**
  	 * @var boolean include the CSS file. Default is true.
  	 * If this is set false, you are responsible to explicitly include the necessary CSS file in your page.
  	 */
  	public $assetBundle = 'nodge\\eauth\\assets\\WidgetAssetBundle';
  
  	/**
  	 * 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
  		/** @var $component \nodge\eauth\EAuth */
  		$component = Yii::$app->get($this->component);
  
  		// Some default properties from component configuration
  		if (!isset($this->services)) {
  			$this->services = $component->getServices();
  		}
  
  		if (is_array($this->predefinedServices)) {
  			$_services = [];
  			foreach ($this->predefinedServices as $_serviceName) {
  				if (isset($this->services[$_serviceName])) {
  					$_services[$_serviceName] = $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->requestedRoute;
  		}
  	}
  
  	/**
  	 * Executes the widget.
  	 * This method is called by {@link CBaseController::endWidget}.
  	 */
  	public function run()
  	{
  		parent::run();
  		echo $this->render('widget', [
  			'id' => $this->getId(),
  			'services' => $this->services,
  			'action' => $this->action,
  			'popup' => $this->popup,
  			'assetBundle' => $this->assetBundle,
  		]);
  	}
  }