$authenticationService->loadIdentifier('Authentication.Password', [ 'resolver'=>[ 'className' => 'Authentication.Orm', 'userModel' => 'Contacts' ], 'fields' => [ 'username' => 'mail', 'password' => 'password', ] ]);
の'fields'の中の'username' => 'mail'と私は記載したのですが、ほとんどのサイトではmailではなく、'email'と記載しております。これはテーブルのカラムのことを指しているのでしょうか???
php
1<?php 2 3 public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface 4{ 5 $authenticationService = new AuthenticationService([ 6 'unauthenticatedRedirect' => '/sample/contacts/login', //未ログイン時にログイン画面にリダイレクトするように指定 7 'queryParam' => 'redirect', 8 ]); 9 10 // メールアドレスとパスワードを認証情報とする 11 $authenticationService->loadIdentifier('Authentication.Password', [ 12 'resolver'=>[ 13 'className' => 'Authentication.Orm', 14 'userModel' => 'Contacts' 15 ], 16 'fields' => [ 17 'username' => 'mail', 18 'password' => 'password', 19 ] 20 ]); 21 22 // 認証子をロードするには、最初にセッションを実行する必要があります 23 $authenticationService->loadAuthenticator('Authentication.Session'); 24// メールアドレスとパスワードを認証情報としてチェックする設定 25 $authenticationService->loadAuthenticator('Authentication.Form', [ 26 'fields' => [ 27 'username' => 'mail', 28 'password' => 'password', 29 ], 30 'loginUrl' => '/sample/contacts/login', 31 ]); 32 33 return $authenticationService; 34} 35} 36
php
1<?php 2declare(strict_types=1); 3 4namespace App\Model\Table; 5 6use Cake\ORM\Query; 7use Cake\ORM\RulesChecker; 8use Cake\ORM\Table; 9use Cake\Validation\Validator; 10 11/** 12 * Contacts Model 13 * 14 * @method \App\Model\Entity\Contact newEmptyEntity() 15 * @method \App\Model\Entity\Contact newEntity(array $data, array $options = []) 16 * @method \App\Model\Entity\Contact[] newEntities(array $data, array $options = []) 17 * @method \App\Model\Entity\Contact get($primaryKey, $options = []) 18 * @method \App\Model\Entity\Contact findOrCreate($search, ?callable $callback = null, $options = []) 19 * @method \App\Model\Entity\Contact patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) 20 * @method \App\Model\Entity\Contact[] patchEntities(iterable $entities, array $data, array $options = []) 21 * @method \App\Model\Entity\Contact|false save(\Cake\Datasource\EntityInterface $entity, $options = []) 22 * @method \App\Model\Entity\Contact saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = []) 23 * @method \App\Model\Entity\Contact[]|\Cake\Datasource\ResultSetInterface|false saveMany(iterable $entities, $options = []) 24 * @method \App\Model\Entity\Contact[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable $entities, $options = []) 25 * @method \App\Model\Entity\Contact[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable $entities, $options = []) 26 * @method \App\Model\Entity\Contact[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable $entities, $options = []) 27 * 28 * @mixin \Cake\ORM\Behavior\TimestampBehavior 29 */ 30class ContactsTable extends Table 31{ 32 /** 33 * Initialize method 34 * 35 * @param array $config The configuration for the Table. 36 * @return void 37 */ 38 public function initialize(array $config): void 39 { 40 parent::initialize($config); 41 42 $this->setTable('contacts'); 43 $this->setDisplayField('name'); 44 $this->setPrimaryKey('id'); 45 46 $this->addBehavior('Timestamp'); 47 } 48 49 /** 50 * Default validation rules. 51 * 52 * @param \Cake\Validation\Validator $validator Validator instance. 53 * @return \Cake\Validation\Validator 54 */ 55 public function validationDefault(Validator $validator): Validator 56 { 57 $validator 58 ->integer('id') 59 ->allowEmptyString('id', null, 'create'); 60 61 $validator 62 ->scalar('name') 63 ->maxLength('name', 10) 64 ->requirePresence('name', 'create') 65 ->notEmptyString('name'); 66 67 $validator 68 ->scalar('katakana') 69 ->maxLength('katakana', 10) 70 ->requirePresence('katakana', 'create') 71 ->notEmptyString('katakana'); 72 73 $validator 74 ->integer('tel') 75 ->requirePresence('tel', 'create') 76 ->notEmptyString('tel'); 77 78 $validator 79 ->scalar('mail') 80 ->maxLength('mail', 255) 81 ->requirePresence('mail', 'create') 82 ->notEmptyString('mail'); 83 84 $validator 85 ->scalar('textarea') 86 ->maxLength('textarea', 255) 87 ->requirePresence('textarea', 'create') 88 ->notEmptyString('textarea'); 89 90 $validator 91 ->scalar('password') 92 ->maxLength('password', 255) 93 ->requirePresence('password', 'create') 94 ->notEmptyString('password'); 95 96 $validator 97 ->scalar('role') 98 ->maxLength('role', 20) 99 ->requirePresence('role', 'create') 100 ->notEmptyString('role'); 101 102 return $validator; 103 } 104}
php
1<?php 2 3 protected $_accessible = [ 4 'id' => false, 5 'name' => true, 6 'katakana' => true, 7 'tel' => true, 8 'mail' => true, 9 'textarea' => true, 10 'password' => true, 11 'role' => true, 12 'created' => true, 13 'modified' => true, 14 ]; 15 16 protected function _setPassword($password) 17 { 18 if (strlen($password) > 0) { 19 return (new DefaultPasswordHasher)->hash($password); 20 } 21 } 22} 23
あなたの回答
tips
プレビュー