teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

追記

2021/01/18 10:45

投稿

meruchaaan
meruchaaan

スコア18

title CHANGED
File without changes
body CHANGED
@@ -1,9 +1,22 @@
1
+ >
2
+ $authenticationService->loadIdentifier('Authentication.Password', [
1
- ```Applicationphp
3
+ 'resolver'=>[
4
+ 'className' => 'Authentication.Orm',
5
+ 'userModel' => 'Contacts'
6
+ ],
7
+ 'fields' => [
8
+ 'username' => 'mail',
9
+ 'password' => 'password',
10
+ ]
11
+ ]);
2
12
 
3
- /////上省略・・・・・・・・・・・・・・・・・・・・・・・・・
13
+ の'fields'の中の'username' => 'mail'と私は載したのですが、ほとんどのサイトではmailではなく、'email'と記載しております。これはテーブルのカラムのことを指しているのでしょうか???
4
14
 
5
15
 
16
+ ```php
17
+ <?php
18
+
6
- public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
19
+ public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
7
20
  {
8
21
  $authenticationService = new AuthenticationService([
9
22
  'unauthenticatedRedirect' => '/sample/contacts/login', //未ログイン時にログイン画面にリダイレクトするように指定
@@ -39,17 +52,135 @@
39
52
 
40
53
  ```
41
54
 
42
- ここの
55
+ ```php
43
- >
56
+ <?php
44
- $authenticationService->loadIdentifier('Authentication.Password', [
45
- 'resolver'=>[
57
+ declare(strict_types=1);
46
- 'className' => 'Authentication.Orm',
47
- 'userModel' => 'Contacts'
48
- ],
49
- 'fields' => [
50
- 'username' => 'mail',
51
- 'password' => 'password',
52
- ]
53
- ]);
54
58
 
59
+ namespace App\Model\Table;
60
+
61
+ use Cake\ORM\Query;
62
+ use Cake\ORM\RulesChecker;
63
+ use Cake\ORM\Table;
64
+ use Cake\Validation\Validator;
65
+
66
+ /**
67
+ * Contacts Model
68
+ *
69
+ * @method \App\Model\Entity\Contact newEmptyEntity()
70
+ * @method \App\Model\Entity\Contact newEntity(array $data, array $options = [])
71
+ * @method \App\Model\Entity\Contact[] newEntities(array $data, array $options = [])
72
+ * @method \App\Model\Entity\Contact get($primaryKey, $options = [])
73
+ * @method \App\Model\Entity\Contact findOrCreate($search, ?callable $callback = null, $options = [])
74
+ * @method \App\Model\Entity\Contact patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
75
+ * @method \App\Model\Entity\Contact[] patchEntities(iterable $entities, array $data, array $options = [])
76
+ * @method \App\Model\Entity\Contact|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
77
+ * @method \App\Model\Entity\Contact saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
78
+ * @method \App\Model\Entity\Contact[]|\Cake\Datasource\ResultSetInterface|false saveMany(iterable $entities, $options = [])
79
+ * @method \App\Model\Entity\Contact[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable $entities, $options = [])
55
- の'fields'の中の'username' => 'mail'と私は記載したのですが、ほとんどのサイトではmailではなく、'email'と記載しております。これはテーブルのカラムのことを指しているのでしょうか???
80
+ * @method \App\Model\Entity\Contact[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable $entities, $options = [])
81
+ * @method \App\Model\Entity\Contact[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable $entities, $options = [])
82
+ *
83
+ * @mixin \Cake\ORM\Behavior\TimestampBehavior
84
+ */
85
+ class ContactsTable extends Table
86
+ {
87
+ /**
88
+ * Initialize method
89
+ *
90
+ * @param array $config The configuration for the Table.
91
+ * @return void
92
+ */
93
+ public function initialize(array $config): void
94
+ {
95
+ parent::initialize($config);
96
+
97
+ $this->setTable('contacts');
98
+ $this->setDisplayField('name');
99
+ $this->setPrimaryKey('id');
100
+
101
+ $this->addBehavior('Timestamp');
102
+ }
103
+
104
+ /**
105
+ * Default validation rules.
106
+ *
107
+ * @param \Cake\Validation\Validator $validator Validator instance.
108
+ * @return \Cake\Validation\Validator
109
+ */
110
+ public function validationDefault(Validator $validator): Validator
111
+ {
112
+ $validator
113
+ ->integer('id')
114
+ ->allowEmptyString('id', null, 'create');
115
+
116
+ $validator
117
+ ->scalar('name')
118
+ ->maxLength('name', 10)
119
+ ->requirePresence('name', 'create')
120
+ ->notEmptyString('name');
121
+
122
+ $validator
123
+ ->scalar('katakana')
124
+ ->maxLength('katakana', 10)
125
+ ->requirePresence('katakana', 'create')
126
+ ->notEmptyString('katakana');
127
+
128
+ $validator
129
+ ->integer('tel')
130
+ ->requirePresence('tel', 'create')
131
+ ->notEmptyString('tel');
132
+
133
+ $validator
134
+ ->scalar('mail')
135
+ ->maxLength('mail', 255)
136
+ ->requirePresence('mail', 'create')
137
+ ->notEmptyString('mail');
138
+
139
+ $validator
140
+ ->scalar('textarea')
141
+ ->maxLength('textarea', 255)
142
+ ->requirePresence('textarea', 'create')
143
+ ->notEmptyString('textarea');
144
+
145
+ $validator
146
+ ->scalar('password')
147
+ ->maxLength('password', 255)
148
+ ->requirePresence('password', 'create')
149
+ ->notEmptyString('password');
150
+
151
+ $validator
152
+ ->scalar('role')
153
+ ->maxLength('role', 20)
154
+ ->requirePresence('role', 'create')
155
+ ->notEmptyString('role');
156
+
157
+ return $validator;
158
+ }
159
+ }
160
+ ```
161
+
162
+ ```php
163
+ <?php
164
+
165
+ protected $_accessible = [
166
+ 'id' => false,
167
+ 'name' => true,
168
+ 'katakana' => true,
169
+ 'tel' => true,
170
+ 'mail' => true,
171
+ 'textarea' => true,
172
+ 'password' => true,
173
+ 'role' => true,
174
+ 'created' => true,
175
+ 'modified' => true,
176
+ ];
177
+
178
+ protected function _setPassword($password)
179
+ {
180
+ if (strlen($password) > 0) {
181
+ return (new DefaultPasswordHasher)->hash($password);
182
+ }
183
+ }
184
+ }
185
+
186
+ ```