{"id":102,"date":"2019-11-12T14:21:24","date_gmt":"2019-11-12T05:21:24","guid":{"rendered":"https:\/\/ta539.com\/bokumemo\/?p=102"},"modified":"2019-11-18T12:31:10","modified_gmt":"2019-11-18T03:31:10","slug":"cakephp3-%e3%83%ad%e3%82%b0%e3%82%a4%e3%83%b3%e5%87%a6%e7%90%86","status":"publish","type":"post","link":"https:\/\/ta539.com\/bokumemo\/2019\/11\/12\/cakephp3-%e3%83%ad%e3%82%b0%e3%82%a4%e3%83%b3%e5%87%a6%e7%90%86\/","title":{"rendered":"cakephp3 \u30ed\u30b0\u30a4\u30f3\u51e6\u7406"},"content":{"rendered":"<p>\u25a0AppController\u306einitialize()\u306b<\/p>\n<p>$this-&gt;loadComponent(&#8216;Auth&#8217;,[<br \/>\n\u00a0 \u00a0 &#8216;authorize&#8217; =&gt; [&#8216;Controller&#8217;],<br \/>\n\u00a0 \u00a0 &#8216;authenticate&#8217; =&gt; [<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 &#8216;Form&#8217; =&gt; [<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8216;fields&#8217; =&gt; [<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8216;username&#8217; =&gt; &#8216;username&#8217;,<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8216;password&#8217; =&gt; &#8216;password&#8217;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ]<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 ]<br \/>\n\u00a0 \u00a0 ],<br \/>\n\u00a0 \u00a0 &#8216;loginRedirect&#8217; =&gt; [<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 &#8216;controller&#8217; =&gt; &#8216;Users&#8217;,<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 &#8216;action&#8217; =&gt; &#8216;index&#8217;<br \/>\n\u00a0 \u00a0 ],<br \/>\n\u00a0 \u00a0 &#8216;logoutRedirect&#8217; =&gt; [<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 &#8216;controller&#8217; =&gt; &#8216;Users&#8217;,<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 &#8216;action&#8217; =&gt; &#8216;login&#8217;,<br \/>\n\u00a0 \u00a0 ],<br \/>\n\u00a0 \u00a0 &#8216;loginAction&#8217; =&gt; [<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 &#8216;controller&#8217; =&gt; &#8216;Users&#8217;,<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 &#8216;action&#8217; =&gt; &#8216;login&#8217;<br \/>\n\u00a0 \u00a0 ],<br \/>\n\u00a0 \u00a0 &#8216;authError&#8217; =&gt; false,<br \/>\n]);<\/p>\n<p>\u25a0UsersController\u306b<\/p>\n<p>public function isAuthorized($user = null){<br \/>\n\u00a0 \u00a0 if(in_array($this-&gt;request-&gt;params[&#8216;action&#8217;], [&#8216;index&#8217;, &#8216;delete&#8217;])){<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 if(in_array($user[&#8216;role&#8217;], [&#8216;admin&#8217;, &#8216;super&#8217;])) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return true;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 if(in_array($this-&gt;request-&gt;params[&#8216;action&#8217;], [&#8216;edit&#8217;])){<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 if(in_array($user[&#8216;role&#8217;], [&#8216;admin&#8217;, &#8216;super&#8217;, &#8216;user&#8217;, &#8216;sp&#8217;])) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return true;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 }<\/p>\n<p>\u00a0 \u00a0 return false;<br \/>\n}<\/p>\n<p>public function beforeFilter(\\Cake\\Event\\Event $event) {<br \/>\n\u00a0 \u00a0 parent::beforeFilter($event);<br \/>\n\u00a0 \u00a0 $this-&gt;Auth-&gt;allow([&#8216;add&#8217;, &#8216;confirm&#8217;, &#8216;logout&#8217;]);<br \/>\n}<\/p>\n<p>public function login()<br \/>\n{<br \/>\n\u00a0 \u00a0 $this-&gt;viewBuilder()-&gt;setLayout(&#8216;nohead&#8217;);<\/p>\n<p>\u00a0 \u00a0 if($this-&gt;request-&gt;is(&#8216;post&#8217;)){<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 $user = $this-&gt;Auth-&gt;identify();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 if($user){<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 $this-&gt;Auth-&gt;setUser($user);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return $this-&gt;redirect($this-&gt;Auth-&gt;redirectUrl());<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 $this-&gt;Flash-&gt;error(&#8216;\u30e6\u30fc\u30b6\u30fc\u540d\u304b\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u9593\u9055\u3048\u3066\u3044\u307e\u3059\u3002&#8217;);<br \/>\n\u00a0 \u00a0 }<br \/>\n}<\/p>\n<p>public function logout()<br \/>\n{<br \/>\n\u00a0 \u00a0 $this-&gt;Flash-&gt;success(&#8216;\u30ed\u30b0\u30a2\u30a6\u30c8\u3057\u307e\u3057\u305f&#8217;);<br \/>\n\u00a0 \u00a0 return $this-&gt;redirect($this-&gt;Auth-&gt;logout());<br \/>\n}<\/p>\n<p>\u25a0login.ctp<\/p>\n<p>&lt;div class=&#8221;users form columns content&#8221;&gt;<br \/>\n\u00a0 \u00a0 &lt;p style=&#8221;text-align:center;border-bottom:1px solid #555;&#8221;&gt;Login&lt;\/p&gt;<br \/>\n\u00a0 \u00a0 &lt;?= $this-&gt;Form-&gt;create() ?&gt;<\/p>\n<p>\u00a0 \u00a0 &lt;?= $this-&gt;Form-&gt;input(&#8216;username&#8217;) ?&gt;<br \/>\n\u00a0 \u00a0 &lt;?= $this-&gt;Form-&gt;input(&#8216;password&#8217;) ?&gt;<br \/>\n\u00a0 \u00a0 &lt;?= $this-&gt;Form-&gt;button(&#8216;Login&#8217;) ?&gt;<\/p>\n<p>\u00a0 \u00a0 &lt;p style=&#8221;clear:right;float:right;&#8221;&gt;&lt;?= $this-&gt;Html-&gt;link(&#8220;\u30e6\u30fc\u30b6\u30fc\u767b\u9332\u306f\u3053\u3061\u3089\u304b\u3089&#8221;, [&#8216;action&#8217; =&gt; &#8216;add&#8217;]) ?&gt;&lt;\/p&gt;<\/p>\n<p>\u00a0 \u00a0 &lt;?= $this-&gt;Form-&gt;end() ?&gt;<br \/>\n&lt;\/div&gt;<\/p>\n<p>\u25a0Model User<\/p>\n<p>use Cake\\Auth\\DefaultPasswordHasher;<\/p>\n<p>protected $_hidden = [<br \/>\n\u00a0 \u00a0 &#8216;password&#8217;<br \/>\n];<\/p>\n<p>protected function _setPassword($password)<br \/>\n{<br \/>\n\u00a0 \u00a0 if (strlen($password) &gt; 0) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 return (new DefaultPasswordHasher)-&gt;hash($password);<br \/>\n\u00a0 \u00a0 }<br \/>\n}<\/p>\n<p>\u25a0SQL<\/p>\n<p>CREATE TABLE `users` (<br \/>\n`id` int(10) unsigned NOT NULL AUTO_INCREMENT,<br \/>\n`username` varchar(50) NOT NULL,<br \/>\n`password` varchar(255) NOT NULL,<br \/>\n`role` varchar(20) DEFAULT NULL,<br \/>\n`created` datetime DEFAULT NULL,<br \/>\n`modified` datetime DEFAULT NULL,<br \/>\nPRIMARY KEY (`id`)<br \/>\n)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u25a0AppController\u306einitialize()\u306b $this-&gt;loadComponent(&#8216;Auth&#8217;,[ \u00a0 \u00a0 &#8216;authorize&#8217; =&gt; [&#8216;Controller&#8217;], \u00a0 \u00a0 &#8216;authenticate&#8217; =&gt; [ \u00a0 \u00a0 \u00a0 \u00a0 &#8216;Form&#8217; =&gt; [ \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8216;fields&#8217; =&gt; [ \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8216;username&#8217; =&gt; &#8216;username&#8217;, \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8216;password&#8217; =&gt; &#8216;password&#8217; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ] \u00a0 \u00a0 \u00a0 \u00a0 ] \u00a0 \u00a0 ], \u00a0 \u00a0 &#8216;loginRedirect&#8217; =&gt; [ \u00a0 \u00a0 \u00a0 \u00a0 &#8216;controller&#8217; =&gt; &#8216;Users&#8217;, \u00a0 \u00a0 \u00a0 \u00a0 &#8216;action&#8217; =&gt; &#8216;index&#8217; \u00a0 \u00a0 ], \u00a0 \u00a0 &#8216;logoutRedirect&#8217; =&gt; [ \u00a0 \u00a0 \u00a0 \u00a0 &#8216;controller&#8217; =&gt; &#8216;Users&#8217;, \u00a0 \u00a0 \u00a0 \u00a0 &#8216;action&#8217; =&gt; &#8216;login&#8217;, \u00a0 \u00a0 ], \u00a0 \u00a0 &#8216;loginAction&#8217; =&gt; [ \u00a0 \u00a0 \u00a0 \u00a0 &#8216;controller&#8217; =&gt; &#8216;Users&#8217;, \u00a0 \u00a0 \u00a0 \u00a0 &#8216;action&#8217; =&gt; &#8216;login&#8217; \u00a0 \u00a0 ], \u00a0 \u00a0 &#8216;authError&#8217; =&gt; false, ]); \u25a0UsersController\u306b public function isAuthorized($us<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[12],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-11","tag-cakephp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ta539.com\/bokumemo\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ta539.com\/bokumemo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ta539.com\/bokumemo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ta539.com\/bokumemo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ta539.com\/bokumemo\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":0,"href":"https:\/\/ta539.com\/bokumemo\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"wp:attachment":[{"href":"https:\/\/ta539.com\/bokumemo\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ta539.com\/bokumemo\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ta539.com\/bokumemo\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}