BaseClient.php 5.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\authclient;

use Yii;
11
use yii\base\Component;
12
use yii\base\NotSupportedException;
13
use yii\helpers\Inflector;
14 15 16
use yii\helpers\StringHelper;

/**
17
 * BaseClient is a base Auth Client class.
18 19 20
 *
 * @see ClientInterface
 *
21 22 23 24 25 26 27 28
 * @property string $id auth service id.
 * @property string $name auth service name.
 * @property string $title auth service title.
 * @property array $userAttributes authenticated user attributes.
 * @property array $normalizeUserAttributeMap map used to normalize user attributes fetched from
 * external auth service in format: rawAttributeName => normalizedAttributeName.
 * @property array $viewOptions view options in format: optionName => optionValue.
 *
29 30 31
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
32
abstract class BaseClient extends Component implements ClientInterface
33 34
{
	/**
35
	 * @var string auth service id.
36 37 38 39
	 * This value mainly used as HTTP request parameter.
	 */
	private $_id;
	/**
40
	 * @var string auth service name.
41 42 43 44
	 * This value may be used in database records, CSS files and so on.
	 */
	private $_name;
	/**
45
	 * @var string auth service title to display in views.
46 47 48 49 50 51
	 */
	private $_title;
	/**
	 * @var array authenticated user attributes.
	 */
	private $_userAttributes;
52 53 54 55 56
	/**
	 * @var array map used to normalize user attributes fetched from external auth service
	 * in format: rawAttributeName => normalizedAttributeName
	 */
	private $_normalizeUserAttributeMap;
57 58 59 60
	/**
	 * @var array view options in format: optionName => optionValue
	 */
	private $_viewOptions;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

	/**
	 * @param string $id service id.
	 */
	public function setId($id)
	{
		$this->_id = $id;
	}

	/**
	 * @return string service id
	 */
	public function getId()
	{
		if (empty($this->_id)) {
			$this->_id = $this->getName();
		}
		return $this->_id;
	}

81 82 83 84 85 86 87 88
	/**
	 * @param string $name service name.
	 */
	public function setName($name)
	{
		$this->_name = $name;
	}

89 90 91 92 93 94 95 96 97 98 99 100
	/**
	 * @return string service name.
	 */
	public function getName()
	{
		if ($this->_name === null) {
			$this->_name = $this->defaultName();
		}
		return $this->_name;
	}

	/**
101
	 * @param string $title service title.
102
	 */
103
	public function setTitle($title)
104
	{
105
		$this->_title = $title;
106 107 108 109 110 111 112 113 114 115 116 117 118 119
	}

	/**
	 * @return string service title.
	 */
	public function getTitle()
	{
		if ($this->_title === null) {
			$this->_title = $this->defaultTitle();
		}
		return $this->_title;
	}

	/**
120
	 * @param array $userAttributes list of user attributes
121
	 */
122
	public function setUserAttributes($userAttributes)
123
	{
124
		$this->_userAttributes = $this->normalizeUserAttributes($userAttributes);
125 126 127 128 129 130 131 132
	}

	/**
	 * @return array list of user attributes
	 */
	public function getUserAttributes()
	{
		if ($this->_userAttributes === null) {
133
			$this->_userAttributes = $this->normalizeUserAttributes($this->initUserAttributes());
134 135 136 137 138
		}
		return $this->_userAttributes;
	}

	/**
139
	 * @param array $normalizeUserAttributeMap normalize user attribute map.
140
	 */
141
	public function setNormalizeUserAttributeMap($normalizeUserAttributeMap)
142
	{
143 144 145 146 147 148 149 150 151 152 153 154
		$this->_normalizeUserAttributeMap = $normalizeUserAttributeMap;
	}

	/**
	 * @return array normalize user attribute map.
	 */
	public function getNormalizeUserAttributeMap()
	{
		if ($this->_normalizeUserAttributeMap === null) {
			$this->_normalizeUserAttributeMap = $this->defaultNormalizeUserAttributeMap();
		}
		return $this->_normalizeUserAttributeMap;
155 156
	}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	/**
	 * @param array $viewOptions view options in format: optionName => optionValue
	 */
	public function setViewOptions($viewOptions)
	{
		$this->_viewOptions = $viewOptions;
	}

	/**
	 * @return array view options in format: optionName => optionValue
	 */
	public function getViewOptions()
	{
		if ($this->_viewOptions === null) {
			$this->_viewOptions = $this->defaultViewOptions();
		}
		return $this->_viewOptions;
	}

176 177 178 179 180 181
	/**
	 * Generates service name.
	 * @return string service name.
	 */
	protected function defaultName()
	{
182
		return Inflector::camel2id(StringHelper::basename(get_class($this)));
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	}

	/**
	 * Generates service title.
	 * @return string service title.
	 */
	protected function defaultTitle()
	{
		return StringHelper::basename(get_class($this));
	}

	/**
	 * Initializes authenticated user attributes.
	 * @return array auth user attributes.
	 */
	protected function initUserAttributes()
	{
		throw new NotSupportedException('Method "' . get_class($this) . '::' . __FUNCTION__ . '" not implemented.');
	}
202

203 204 205 206 207
	/**
	 * Returns the default [[normalizeUserAttributeMap]] value.
	 * Particular client may override this method in order to provide specific default map.
	 * @return array normalize attribute map.
	 */
208
	protected function defaultNormalizeUserAttributeMap()
209 210 211 212
	{
		return [];
	}

213 214 215 216 217 218 219 220 221
	/**
	 * Returns the default [[viewOptions]] value.
	 * Particular client may override this method in order to provide specific default view options.
	 * @return array list of default [[viewOptions]]
	 */
	protected function defaultViewOptions()
	{
		return [];
	}
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

	/**
	 * Normalize given user attributes according to {@link normalizeUserAttributeMap}.
	 * @param array $attributes raw attributes.
	 * @return array normalized attributes.
	 */
	protected function normalizeUserAttributes($attributes)
	{
		foreach ($this->getNormalizeUserAttributeMap() as $normalizedName => $actualName) {
			if (array_key_exists($actualName, $attributes)) {
				$attributes[$normalizedName] = $attributes[$actualName];
			}
		}
		return $attributes;
	}
237
}