"Fossies" - the Fresh Open Source Software Archive

Member "grav/system/src/Grav/Common/Service/AccountsServiceProvider.php" (19 Mar 2020, 4294 Bytes) of package /linux/www/grav-v1.6.23.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AccountsServiceProvider.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 /**
    4  * @package    Grav\Common\Service
    5  *
    6  * @copyright  Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
    7  * @license    MIT License; see LICENSE file for details.
    8  */
    9 
   10 namespace Grav\Common\Service;
   11 
   12 use Grav\Common\Config\Config;
   13 use Grav\Common\Debugger;
   14 use Grav\Common\User\DataUser;
   15 use Grav\Common\User\FlexUser;
   16 use Grav\Common\User\User;
   17 use Grav\Framework\File\Formatter\YamlFormatter;
   18 use Grav\Framework\Flex\Flex;
   19 use Grav\Framework\Flex\FlexDirectory;
   20 use Pimple\Container;
   21 use Pimple\ServiceProviderInterface;
   22 use RocketTheme\Toolbox\Event\Event;
   23 use RocketTheme\Toolbox\Event\EventDispatcher;
   24 
   25 class AccountsServiceProvider implements ServiceProviderInterface
   26 {
   27     public function register(Container $container)
   28     {
   29         $container['accounts'] = function (Container $container) {
   30             $type = strtolower(defined('GRAV_USER_INSTANCE') ? GRAV_USER_INSTANCE : $container['config']->get('system.accounts.type', 'data'));
   31             if ($type === 'flex') {
   32                 /** @var Debugger $debugger */
   33                 $debugger = $container['debugger'];
   34                 $debugger->addMessage('User Accounts: Flex Directory');
   35                 return $this->flexAccounts($container);
   36             }
   37 
   38             return $this->dataAccounts($container);
   39         };
   40 
   41         $container['users'] = $container->factory(function (Container $container) {
   42             user_error('Grav::instance()[\'users\'] is deprecated since Grav 1.6, use Grav::instance()[\'accounts\'] instead', E_USER_DEPRECATED);
   43 
   44             return $container['accounts'];
   45         });
   46     }
   47 
   48     protected function dataAccounts(Container $container)
   49     {
   50         if (!defined('GRAV_USER_INSTANCE')) {
   51             define('GRAV_USER_INSTANCE', 'DATA');
   52         }
   53 
   54         // Use User class for backwards compatibility.
   55         return new DataUser\UserCollection(User::class);
   56     }
   57 
   58     protected function flexAccounts(Container $container)
   59     {
   60         if (!defined('GRAV_USER_INSTANCE')) {
   61             define('GRAV_USER_INSTANCE', 'FLEX');
   62         }
   63 
   64         /** @var Config $config */
   65         $config = $container['config'];
   66 
   67         $options = [
   68             'enabled' => true,
   69             'data' => [
   70                 'object' => User::class, // Use User class for backwards compatibility.
   71                 'collection' => FlexUser\UserCollection::class,
   72                 'index' => FlexUser\UserIndex::class,
   73                 'storage' => $this->getFlexStorage($config->get('system.accounts.storage', 'file')),
   74                 'search' => [
   75                     'options' => [
   76                         'contains' => 1
   77                     ],
   78                     'fields' => [
   79                         'key',
   80                         'email'
   81                     ]
   82                 ]
   83             ]
   84         ] + ($config->get('plugins.flex-objects.object') ?: []);
   85 
   86         $directory = new FlexDirectory('accounts', 'blueprints://user/accounts.yaml', $options);
   87 
   88         /** @var EventDispatcher $dispatcher */
   89         $dispatcher = $container['events'];
   90         $dispatcher->addListener('onFlexInit', function (Event $event) use ($directory) {
   91             /** @var Flex $flex */
   92             $flex = $event['flex'];
   93             $flex->addDirectory($directory);
   94         });
   95 
   96         return $directory->getIndex();
   97     }
   98 
   99     protected function getFlexStorage($config)
  100     {
  101         if (\is_array($config)) {
  102             return $config;
  103         }
  104 
  105         if ($config === 'folder') {
  106             return [
  107                 'class' => FlexUser\Storage\UserFolderStorage::class,
  108                 'options' => [
  109                     'formatter' => ['class' => YamlFormatter::class],
  110                     'folder' => 'account://',
  111                     'pattern' => '{FOLDER}/{KEY:2}/{KEY}/user.yaml',
  112                     'key' => 'username',
  113                     'indexed' => true
  114                 ],
  115             ];
  116         }
  117 
  118         return [
  119             'class' => FlexUser\Storage\UserFileStorage::class,
  120             'options' => [
  121                 'formatter' => ['class' => YamlFormatter::class],
  122                 'folder' => 'account://',
  123                 'pattern' => '{FOLDER}/{KEY}.yaml',
  124                 'key' => 'storage_key',
  125                 'indexed' => true
  126             ],
  127         ];
  128     }
  129 }