PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/View/Helper/UserHelper.php

http://github.com/CakeDC/users
PHP | 268 lines | 160 code | 29 blank | 79 comment | 12 complexity | b86072e77b34661e11485a662118d4a6 MD5 | raw file
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com)
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. */
  12. namespace CakeDC\Users\View\Helper;
  13. use Cake\Core\Configure;
  14. use Cake\Utility\Hash;
  15. use Cake\Utility\Inflector;
  16. use Cake\View\Helper;
  17. use CakeDC\Users\Utility\UsersUrl;
  18. /**
  19. * User helper
  20. *
  21. * @property \CakeDC\Users\View\Helper\AuthLinkHelper $AuthLink
  22. * @property \Cake\View\Helper\HtmlHelper $Html
  23. * @property \Cake\View\Helper\FormHelper $Form
  24. */
  25. class UserHelper extends Helper
  26. {
  27. public $helpers = ['Html', 'Form', 'CakeDC/Users.AuthLink'];
  28. /**
  29. * Default configuration.
  30. *
  31. * @var array
  32. */
  33. protected $_defaultConfig = [];
  34. /**
  35. * Social login link
  36. *
  37. * @param string $name name
  38. * @param array $options options
  39. * @return string
  40. */
  41. public function socialLogin($name, $options = [])
  42. {
  43. if (empty($options['label'])) {
  44. $options['label'] = __d('cake_d_c/users', 'Sign in with');
  45. }
  46. $icon = $this->Html->tag('i', '', [
  47. 'class' => 'fa fa-' . strtolower($name),
  48. ]);
  49. if (isset($options['title'])) {
  50. $providerTitle = $options['title'];
  51. } else {
  52. $providerTitle = ($options['label'] ?? '') . ' ' . Inflector::camelize($name);
  53. }
  54. $providerClass = 'btn btn-social btn-' . strtolower($name);
  55. $optionClass = $options['class'] ?? null;
  56. if ($optionClass) {
  57. $providerClass .= " $optionClass";
  58. }
  59. return $this->Html->link($icon . $providerTitle, "/auth/$name", [
  60. 'escape' => false, 'class' => $providerClass,
  61. ]);
  62. }
  63. /**
  64. * All available Social Login Icons
  65. *
  66. * @param array $providerOptions Provider link options.
  67. * @return array Links to Social Login Urls
  68. */
  69. public function socialLoginList(array $providerOptions = [])
  70. {
  71. if (!Configure::read('Users.Social.login')) {
  72. return [];
  73. }
  74. $outProviders = [];
  75. $providers = Configure::read('OAuth.providers');
  76. foreach ($providers as $provider => $options) {
  77. if (
  78. !empty($options['options']['redirectUri']) &&
  79. !empty($options['options']['clientId']) &&
  80. !empty($options['options']['clientSecret'])
  81. ) {
  82. if (isset($providerOptions[$provider])) {
  83. $options['options'] = Hash::merge($options['options'], $providerOptions[$provider]);
  84. }
  85. $outProviders[] = $this->socialLogin($provider, $options['options']);
  86. }
  87. }
  88. return $outProviders;
  89. }
  90. /**
  91. * Logout link
  92. *
  93. * @param null $message logout message info.
  94. * @param array $options Array with option data.
  95. * @return string
  96. */
  97. public function logout($message = null, $options = [])
  98. {
  99. $url = UsersUrl::actionUrl('logout');
  100. $title = empty($message) ? __d('cake_d_c/users', 'Logout') : $message;
  101. return $this->AuthLink->link($title, $url, $options);
  102. }
  103. /**
  104. * Welcome display
  105. *
  106. * @return string|null
  107. */
  108. public function welcome()
  109. {
  110. $identity = $this->getView()->getRequest()->getAttribute('identity');
  111. if (!$identity) {
  112. return null;
  113. }
  114. $profileUrl = Configure::read('Users.Profile.route');
  115. $title = $identity['first_name'] ?? null;
  116. $title = $title ?: ($identity['username'] ?? null);
  117. $title = is_array($title) ? '-' : (string)$title;
  118. $label = __d(
  119. 'cake_d_c/users',
  120. 'Welcome, {0}',
  121. $this->AuthLink->link($title, $profileUrl)
  122. );
  123. return $this->Html->tag('span', $label, ['class' => 'welcome']);
  124. }
  125. /**
  126. * Add reCaptcha script
  127. *
  128. * @return void
  129. */
  130. public function addReCaptchaScript()
  131. {
  132. $this->Html->script('https://www.google.com/recaptcha/api.js', [
  133. 'block' => 'script',
  134. ]);
  135. }
  136. /**
  137. * Add reCaptcha to the form
  138. *
  139. * @return mixed
  140. */
  141. public function addReCaptcha()
  142. {
  143. if (!Configure::read('Users.reCaptcha.key')) {
  144. return $this->Html->tag(
  145. 'p',
  146. __d(
  147. 'cake_d_c/users',
  148. 'reCaptcha is not configured! Please configure Users.reCaptcha.key'
  149. )
  150. );
  151. }
  152. $this->addReCaptchaScript();
  153. try {
  154. $this->Form->unlockField('g-recaptcha-response');
  155. } catch (\Exception $e) {
  156. }
  157. return $this->Html->tag('div', '', [
  158. 'class' => 'g-recaptcha',
  159. 'data-sitekey' => Configure::read('Users.reCaptcha.key'),
  160. 'data-theme' => Configure::read('Users.reCaptcha.theme') ?: 'light',
  161. 'data-size' => Configure::read('Users.reCaptcha.size') ?: 'normal',
  162. 'data-tabindex' => Configure::read('Users.reCaptcha.tabindex') ?: '3',
  163. ]);
  164. }
  165. /**
  166. * Generate a link if the target url is authorized for the logged in user
  167. *
  168. * @deprecated Since 3.2.1. Use AuthLinkHelper::link() instead
  169. * @param string $title link's title.
  170. * @param string|array|null $url url that the user is making request.
  171. * @param array $options Array with option data.
  172. * @return string
  173. */
  174. public function link($title, $url = null, array $options = [])
  175. {
  176. trigger_error(
  177. 'UserHelper::link() deprecated since 3.2.1. Use AuthLinkHelper::link() instead',
  178. E_USER_DEPRECATED
  179. );
  180. return $this->AuthLink->link($title, $url, $options);
  181. }
  182. /**
  183. * Create links for all social providers enabled social link (connect)
  184. *
  185. * @param string $name Provider name in lowercase
  186. * @param array $provider Provider configuration
  187. * @param bool $isConnected User is connected with this provider
  188. * @return string
  189. */
  190. public function socialConnectLink($name, $provider, $isConnected = false)
  191. {
  192. $optionClass = $provider['options']['class'] ?? null;
  193. $linkClass = 'btn btn-social btn-' . strtolower($name) . ($optionClass ? ' ' . $optionClass : '');
  194. if ($isConnected) {
  195. $title = __d('cake_d_c/users', 'Connected with {0}', Inflector::camelize($name));
  196. return "<a class=\"$linkClass disabled\"><span class=\"fa fa-$name\"></span> $title</a>";
  197. }
  198. $title = __d('cake_d_c/users', 'Connect with {0}', Inflector::camelize($name));
  199. return $this->Html->link(
  200. "<span class=\"fa fa-$name\"></span> $title",
  201. "/link-social/$name",
  202. [
  203. 'escape' => false,
  204. 'class' => $linkClass,
  205. ]
  206. );
  207. }
  208. /**
  209. * Create links for all social providers enabled social link (connect)
  210. *
  211. * @param array $socialAccounts All social accounts connected by a user.
  212. * @return string
  213. */
  214. public function socialConnectLinkList($socialAccounts = [])
  215. {
  216. if (!Configure::read('Users.Social.login')) {
  217. return '';
  218. }
  219. $html = '';
  220. $connectedProviders = array_map(
  221. function ($item) {
  222. return strtolower($item->provider);
  223. },
  224. (array)$socialAccounts
  225. );
  226. $providers = Configure::read('OAuth.providers');
  227. foreach ($providers as $name => $provider) {
  228. if (
  229. !empty($provider['options']['callbackLinkSocialUri']) &&
  230. !empty($provider['options']['linkSocialUri']) &&
  231. !empty($provider['options']['clientId']) &&
  232. !empty($provider['options']['clientSecret'])
  233. ) {
  234. $html .= $this->socialConnectLink($name, $provider, in_array($name, $connectedProviders));
  235. }
  236. }
  237. return $html;
  238. }
  239. }