Drupal - 사용자 지정 로그인을 위한 API 리소스
누락된 csrf(
/session/token
에서 얻을 수 있음).Drush 명령을 사용하여 나머지 플러그인을 만듭니다.
$ drush generate plugin-rest-resource
또는 별칭 사용$ drush gen rest-resource
이것은
POST
리소스이므로 $ drush cr
를 실행합니다.Rest UI을 사용하여 리소스를 활성화하고 익명 역할에 대한 권한을 추가합니다.
<?php
namespace Drupal\custom_rest_api\Plugin\rest\resource;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\rest\ModifiedResourceResponse;
use Drupal\rest\Plugin\ResourceBase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Password\PasswordInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* Represents Custom login resource records as resources.
*
* @RestResource (
* id = "custom_rest_api_custom_login_resource",
* label = @Translation("Custom login resource"),
* uri_paths = {
* "create" = "/api/custom/login"
* }
* )
*
* @DCG
* This plugin exposes database records as REST resources. In order to enable it
* import the resource configuration into active configuration storage. You may
* find an example of such configuration in the following file:
* core/modules/rest/config/optional/rest.resource.entity.node.yml.
* Alternatively you can make use of REST UI module.
* @see https://www.drupal.org/project/restui
* For accessing Drupal entities through REST interface use
* \Drupal\rest\Plugin\rest\resource\EntityResource plugin.
*/
class CustomLoginResource extends ResourceBase {
/**
* A current user instance.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
protected $sessionManager;
protected $moduleHandler;
protected $password;
/**
* Constructs a new CustomLoginResource object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param array $serializer_formats
* The available serialization formats.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* A current user instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
array $serializer_formats,
LoggerInterface $logger,
AccountProxyInterface $current_user,
SessionManagerInterface $session_manager,
ModuleHandlerInterface $module_handler,
PasswordInterface $password) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->currentUser = $current_user;
$this->sessionManager = $session_manager;
$this->moduleHandler = $module_handler;
$this->password = $password;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->getParameter('serializer.formats'),
$container->get('logger.factory')->get('exp_fs'),
$container->get('current_user'),
$container->get('session_manager'),
$container->get('module_handler'),
$container->get('password')
);
}
/**
* Responds to POST requests.
*
* @return \Drupal\rest\ModifiedResourceResponse
* The HTTP response object.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws exception expected.
*/
public function post($data) {
$this->validate($data);
$pass_check = FALSE;
$name = $data['name'];
$pass = $data['pass'];
$account = user_load_by_name(trim($name));
if ($account) {
$pass_check = $this->password->check(trim($pass), $account->getPassword());
}
else {
$body = [
'error' => 'Wrong username and/or password.',
];
}
if ($pass_check == FALSE) {
$body = [
'error' => 'Wrong username and/or password..',
];
}
else {
$session = \Drupal::service('session');
$session->migrate();
$session->set('uid', $account->id());
$this->moduleHandler->invokeAll('user_login', [$account]);
user_login_finalize($account);
$sess_name = $this->sessionManager->getName();
$sess_id = $this->sessionManager->getId();
$body = [
'sess_name' => $sess_name,
'sess_id' => $sess_id,
'current_user' => [
'name' => $account->getAccountName(),
'uid' => $account->id(),
'roles' => $account->getRoles(),
],
];
}
return new ModifiedResourceResponse($body, 200);
}
/**
* Validates incoming record.
*
* @param mixed $record
* Data to validate.
*
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/
protected function validate($record) {
if (!is_array($record) || count($record) == 0) {
throw new BadRequestHttpException(t('No record content received'));
}
if (empty($record['name'])) {
throw new BadRequestHttpException(t('name id is required'));
}
if (empty($record['pass'])) {
throw new BadRequestHttpException(t('Password date is required'));
}
}
}
Reference
이 문제에 관하여(Drupal - 사용자 지정 로그인을 위한 API 리소스), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mahmoudsayed96/drupal-api-resources-for-custom-login-2da8텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)