Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
EAuth.php
1 <?php
2 /**
3  * EAuth 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 EAuth class provides simple authentication via OpenID and OAuth providers.
12  * @package application.extensions.eauth
13  */
14 class EAuth extends CApplicationComponent {
15 
16  /**
17  * @var array Authorization services and their settings.
18  */
19  public $services = array();
20 
21  /**
22  * @var boolean Whether to use popup window for the authorization dialog.
23  */
24  public $popup = true;
25 
26  /**
27  * Returns services settings declared in the authorization classes.
28  * For perfomance reasons it uses Yii::app()->cache to store settings array.
29  * @return array services settings.
30  */
31  public function getServices() {
32  if (Yii::app()->hasComponent('cache'))
33  $services = Yii::app()->cache->get('EAuth.services');
34  if (!isset($services) || !is_array($services)) {
35  $services = array();
36  foreach ($this->services as $service => $options) {
37  if ((!isset($options['class']) || $options['class']=='')) {
38  continue;
39  }
40  $class = $this->getIdentity($service);
41  $services[$service] = (object) array(
42  'id' => $class->getServiceName(),
43  'title' => $class->getServiceTitle(),
44  'type' => $class->getServiceType(),
45  'jsArguments' => $class->getJsArguments(),
46  );
47  }
48  if (Yii::app()->hasComponent('cache'))
49  Yii::app()->cache->set('EAuth.services', $services);
50  }
51  return $services;
52  }
53 
54  /**
55  * Returns the settings of the service.
56  * @param string $service the service name.
57  * @return array the service settings.
58  */
59  protected function getService($service) {
60  $service = strtolower($service);
61  $services = $this->getServices();
62  if (!isset($services[$service]))
63  throw new EAuthException(Yii::t('eauth', 'Undefined service name: {service}.', array('{service}' => $service)), 500);
64  return $services[$service];
65  }
66 
67  /**
68  * Returns the type of the service.
69  * @param string $service the service name.
70  * @return string the service type.
71  */
72  public function getServiceType($service) {
73  $service = $this->getService($service);
74  return $service->type;
75  }
76 
77  /**
78  * Returns the service identity class.
79  * @param string $service the service name.
80  * @return IAuthService the identity class.
81  */
82  public function getIdentity($service) {
83  $service = strtolower($service);
84  if (!isset($this->services[$service]))
85  throw new EAuthException(Yii::t('eauth', 'Undefined service name: {service}.', array('{service}' => $service)), 500);
86  $service = $this->services[$service];
87 
88  $class = $service['class'];
89  $point = strrpos($class, '.');
90  // if it is yii path alias
91  if ($point > 0) {
92  Yii::import($class);
93  $class = substr($class, $point + 1);
94  }
95  unset($service['class']);
96  $identity = new $class();
97  $identity->init($this, $service);
98  return $identity;
99  }
100 
101  /**
102  * Redirects to url. If the authorization dialog opened in the popup window,
103  * it will be closed instead of redirect. Set $jsRedirect=true if you want
104  * to redirect anyway.
105  * @param mixed $url url to redirect. Can be route or normal url. See {@link CHtml::normalizeUrl}.
106  * @param boolean $jsRedirect whether to use redirect while popup window is used. Defaults to true.
107  */
108  public function redirect($url, $jsRedirect = true) {
109  require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'EAuthRedirectWidget.php';
110  $widget = Yii::app()->getWidgetFactory()->createWidget($this, 'EAuthRedirectWidget', array(
111  'url' => CHtml::normalizeUrl($url),
112  'redirect' => $jsRedirect,
113  ));
114  $widget->init();
115  $widget->run();
116  }
117 
118  /**
119  * Simple wrapper for {@link CController::widget} function for render the {@link EAuthWidget} widget.
120  * @param array $properties the widget properties.
121  * @deprecated use CComponent->widget('ext.eauth.EAuthWidget', $properties) instead.
122  */
123  public function renderWidget($properties = array()) {
124  require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'EAuthWidget.php';
125  $widget = Yii::app()->getWidgetFactory()->createWidget($this, 'EAuthWidget', $properties);
126  $widget->init();
127  $widget->run();
128  }
129 
130  /**
131  * Serialize the identity class.
132  * @param EAuthServiceBase $identity the class instance.
133  * @return string serialized value.
134  */
135  public function toString($identity) {
136  return serialize($identity);
137  }
138 
139  /**
140  * Serialize the identity class.
141  * @param string $identity serialized value.
142  * @return EAuthServiceBase the class instance.
143  */
144  public function fromString($identity) {
145  return unserialize($identity);
146  }
147 }
148 
149 /**
150  * The EAuthException exception class.
151  *
152  * @author Maxim Zemskov <nodge@yandex.ru>
153  * @package application.extensions.auth
154  * @version 1.0
155  */
156 class EAuthException extends CException {}