简体   繁体   中英

Implementing custom user/login provider into Bolt cms

I am currently evaluating my options on changing the Bolt user provider and associated views to use some custom user/login services. So far, it seems i would have to modify some core elements of the Bolt source code, which obviously is a bad choice:

  • Replace Users service with custom class (Same interface)
  • Change/Extend Backend class as it is routing and rendering associated views itself

Any ideas on how to go at this with the smallest impact on the core source code?

A lot of this comes down to how much you want to change, but as you mention swapping out services in Silex is very easy all you need to do is this:

<?php
namespace Custom;
use Bolt\Users;
// src/Custom/UserService
class UserService extends Users {}

and then in your bootstrap do something like the following.

<?php
// public/index.php
require_once "../vendor/autoload.php";
$configuration = new Bolt\Configuration\Composer(dirname(__DIR__));
$app = new Bolt\Application(array('resources'=>$configuration));
$app['users'] = new Custom\UserService($app);
$app->initialize();
$app->run();

Now your custom user service can decide which of the core Bolt methods to override.

Since the Bolt services architecture currently isnt as clean as needed to override certian core functionalities and since it's currently being rewritten for Symfony 5 Flex, since path is not really feasible right now.

BUT! Since a couple month, there is a new loginAsUser method in Bolt's user service. You can now optionaly create your own user with Bolt's user reporistory and then login as this user.

Here are some sample code snipptes you can adapt and use inside an extension:

Note : This is incomplete sample code. Since modifying/bypassing the login system is riskay, be advised to fully read and understand Bolt's user and auth services. Also, this code is using internal methods that are not for use in extensions and might change. Do this at your own risk and use carefull error handling.

Create a user

/* @var Repository\UsersRepository $repository */
$repository = $this->app["storage"]->getRepository(Users::class);
$userEntity = new Users();
... set user attributes ...
$repository->save($userEntity);

Login as user

$event = new AccessControlEvent($request);
$this->login()->loginAsUser($user->getEmail(), $event);
$token = $this->session()->get('authentication');
$response = $this->setAuthenticationCookie($request, $response, (string) $token);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM