Coder Social home page Coder Social logo

drupal8-wiki's People

Contributors

iamxwk avatar

Watchers

 avatar

drupal8-wiki's Issues

重载系统Service

举一个例子,现在需要修改用户登录验证用户名密码的逻辑,让drupal登录验证走自定义的实体里去查,这样实现一个UserLogin实体

UserLogin
  id
  user_login_id
  passwod
  source

1:首先找到 core/modules/user/src/Form/UserLoginForm.php

可以看到在`validateAuthentication`方法的最后面
`$uid = $this->userAuth->authenticate($form_state->getValue('name'), $password);`
是通过这样来验证用户名密码
再分别看 `create` 方法 和` __construct` 得知 `$this->userAuth` 是因为调用了 user.auth 这个服务得到的。所以我们要做的就是重载 user.auth 这个服务

2:在自定义模块下建一个 ModuleNameServiceProvider

如果模块名为 my_module_name 那么名字为 MyModuleNameServiceProvider
src/ModuleNameServiceProvider.php
	<?php
	
	namespace Drupal\como_user_login;
	
	
	use Drupal\Core\DependencyInjection\ContainerBuilder;
	use Drupal\Core\DependencyInjection\ServiceProviderBase;
	use Symfony\Component\DependencyInjection\Reference;
	
	/**
	 * Defines a class to build a listing of User Login entities.
	 *
	 * @ingroup como_user_login
	 */
	class ComoUserLoginServiceProvider extends ServiceProviderBase {
	
	  public function alter(ContainerBuilder $container) {
	    $definition = $container->getDefinition('user.auth');
	    $definition->setClass('Drupal\my_module_name\UserAuth')
	      ->addArgument(new Reference('entity_type.manager'), new Reference('password'));
	  }
	}
	?>

3:在自定义模块下建一个 UserAuth 继承 Drupal\user\UserAuth 重载 authenticate 方法就好了

4:如果怕被别的模块和你抢夺重载服务,那么可以在 my_module_name.install 里 hook_install 设置权重

	module_set_weight('my_module_name', 10);

不将密码框设置为password 防止chrome 自动填充

use Illuminate\Support\Arr;
/**
 * 不将密码框设置为password 防止chrome 自动填充
 * Implements hook_preprocess_input().
 */
function module_name_preprocess_input(array &$variables) {
  $field = $variables['element'];
  if ($field['#type'] == 'password') {
    $variables['attributes']['type'] = 'text';
    $variables['attributes']['autocomplete'] = 'new-password';
    $onclick = Arr::get($variables, 'attributes.onclick', '');
    if (!empty($onclick)) {
      $onclick .= ';';
    }
    $variables['attributes']['onclick'] = $onclick . 'this.type="password"';
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.