yumコマンド調査 その2
Linuxサーバでパッケージをインストールする際にyumコマンドを利用することが多いですが、
正直ちゃんとリポジトリパッケージの仕組みを理解していないまま使っていました。
このタイミングでリポジトリパッケージの基礎から調査して、その過程で残しておきたい情報を記載していきます。
調査環境
AWS ec2(os:Red Hat Enterprise Linux 7.5 (HVM))サーバ
$ cat /proc/version
Linux version 4.14.62-70.117.amzn2.x86_64 (mockbuild@ip-10-0-1-79) (gcc version 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC)) #1 SMP Fri Aug 10 20:14:53 UTC 2018
リボリト追加
//追加前 $ cd /etc/yum.repos.d/ $ ll 合計 8 -rw-r--r-- 1 root root 982 6月 26 00:04 amzn2-core.repo -rw-r--r-- 1 root root 763 8月 11 02:03 amzn2-extras.repo
epel(Extra Packages for Enterprise Linux)リポジトリ追加
$ sudo wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm $ ll -rw-r--r-- 1 root root 982 6月 26 00:04 amzn2-core.repo -rw-r--r-- 1 root root 763 8月 11 02:03 amzn2-extras.repo -rw-r--r-- 1 root root 15080 10月 2 2017 epel-release-latest-7.noarch.rpm $sudo rpm -ivh epel-release-latest-7.noarch.rpm $ ll -rw-r--r-- 1 root root 982 6月 26 00:04 amzn2-core.repo -rw-r--r-- 1 root root 763 8月 11 02:03 amzn2-extras.repo -rw-r--r-- 1 root root 15080 10月 2 2017 epel-release-latest-7.noarch.rpm -rw-r--r-- 1 root root 1050 10月 2 2017 epel-testing.repo -rw-r--r-- 1 root root 951 10月 2 2017 epel.repo $ yum repolist 読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd 147 packages excluded due to repository priority protections リポジトリー ID リポジトリー名 状態 !amzn2-core/2/x86_64 Amazon Linux 2 core repository 9,269 amzn2extra-docker/2/x86_64 Amazon Extras repo for docker 5 epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 12,515+147 repolist: 21,789
remiリポジトリ追加
$ sudo wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm $ ll -rw-r--r-- 1 root root 982 6月 26 00:04 amzn2-core.repo -rw-r--r-- 1 root root 763 8月 11 02:03 amzn2-extras.repo -rw-r--r-- 1 root root 15080 10月 2 2017 epel-release-latest-7.noarch.rpm -rw-r--r-- 1 root root 1050 10月 2 2017 epel-testing.repo -rw-r--r-- 1 root root 951 10月 2 2017 epel.repo -rw-r--r-- 1 root root 15384 6月 19 08:29 remi-release-7.rpm $ sudo rpm -ivh remi-release-7.rpm $ll -rw-r--r-- 1 root root 982 6月 26 00:04 amzn2-core.repo -rw-r--r-- 1 root root 763 8月 11 02:03 amzn2-extras.repo -rw-r--r-- 1 root root 15080 10月 2 2017 epel-release-latest-7.noarch.rpm -rw-r--r-- 1 root root 1050 10月 2 2017 epel-testing.repo -rw-r--r-- 1 root root 951 10月 2 2017 epel.repo -rw-r--r-- 1 root root 446 6月 19 08:28 remi-glpi91.repo -rw-r--r-- 1 root root 446 6月 19 08:28 remi-glpi92.repo -rw-r--r-- 1 root root 446 6月 19 08:28 remi-glpi93.repo -rw-r--r-- 1 root root 456 6月 19 08:28 remi-php54.repo -rw-r--r-- 1 root root 1314 6月 19 08:28 remi-php70.repo -rw-r--r-- 1 root root 1314 6月 19 08:28 remi-php71.repo -rw-r--r-- 1 root root 1314 6月 19 08:28 remi-php72.repo -rw-r--r-- 1 root root 1314 6月 19 08:28 remi-php73.repo -rw-r--r-- 1 root root 15384 6月 19 08:29 remi-release-7.rpm -rw-r--r-- 1 root root 750 6月 19 08:28 remi-safe.repo -rw-r--r-- 1 root root 2605 6月 19 08:28 remi.repo $yum repolist リポジトリー ID リポジトリー名 状態 amzn2-core/2/x86_64 Amazon Linux 2 core repository 9,269 amzn2extra-docker/2/x86_64 Amazon Extras repo for docker 5 epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 12,515+147 remi-safe Safe Remi's RPM repository for Enterprise Linux 7 - x86_64 3,014+1 repolist: 24,803
yumコマンド調査 その1
Linuxサーバでパッケージをインストールする際にyumコマンドを利用することが多いですが、
正直ちゃんとyumの仕組みを理解していないまま使っていました。
このタイミングでyumの基礎から調査して、その過程で残しておきたい情報を記載していきます。
調査環境
AWS ec2(os:Red Hat Enterprise Linux 7.5 (HVM))サーバ
yumレポジトリ一覧コマンド
$ yum repolist all 読み込んだプラグイン:amazon-id, rhui-lb, search-disabled-repos リポジトリー ID リポジトリー名 状態 rhui-REGION-client-config-server-7/x86_64 Red Hat Update Infrastructure 2.0 Client Configuration Server 7 有効: 0 rhui-REGION-rhel-server-debug-extras/7Server/x86_64 Red Hat Enterprise Linux Server 7 Extra Debug (Debug RPMs) 無効 rhui-REGION-rhel-server-debug-optional/7Server/x86_64 Red Hat Enterprise Linux Server 7 Optional Debug (Debug RPMs) 無効 rhui-REGION-rhel-server-debug-rh-common/7Server/x86_64 Red Hat Enterprise Linux Server 7 RH Common Debug (Debug RPMs) 無効 rhui-REGION-rhel-server-debug-rhscl/7Server/x86_64 Red Hat Enterprise Linux Server 7 RHSCL Debug (Debug RPMs) 無効 rhui-REGION-rhel-server-debug-supplementary/7Server/x86_64 Red Hat Enterprise Linux Server 7 Supplementary Debug (Debug RP 無効 rhui-REGION-rhel-server-extras/7Server/x86_64 Red Hat Enterprise Linux Server 7 Extra(RPMs) 無効 rhui-REGION-rhel-server-optional/7Server/x86_64 Red Hat Enterprise Linux Server 7 Optional (RPMs) 無効 rhui-REGION-rhel-server-releases/7Server/x86_64 Red Hat Enterprise Linux Server 7 (RPMs) 有効: 20,908 rhui-REGION-rhel-server-releases-debug/7Server/x86_64 Red Hat Enterprise Linux Server 7 Debug (Debug RPMs) 無効 rhui-REGION-rhel-server-releases-source/7Server/x86_64 Red Hat Enterprise Linux Server 7 (SRPMs) 無効 rhui-REGION-rhel-server-rh-common/7Server/x86_64 Red Hat Enterprise Linux Server 7 RH Common (RPMs) 有効: 233 rhui-REGION-rhel-server-rhscl/7Server/x86_64 Red Hat Enterprise Linux Server 7 RHSCL (RPMs) 無効 rhui-REGION-rhel-server-source-extras/7Server/x86_64 Red Hat Enterprise Linux Server 7 Extra (SRPMs) 無効 rhui-REGION-rhel-server-source-optional/7Server/x86_64 Red Hat Enterprise Linux Server 7 Optional (SRPMs) 無効 rhui-REGION-rhel-server-source-rh-common/7Server/x86_64 Red Hat Enterprise Linux Server 7 RH Common (SRPMs) 無効 rhui-REGION-rhel-server-source-rhscl/7Server/x86_64 Red Hat Enterprise Linux Server 7 RHSCL (SRPMs) 無効 rhui-REGION-rhel-server-source-supplementary/7Server/x86_64 Red Hat Enterprise Linux Server 7 Supplementary (SRPMs) 無効 rhui-REGION-rhel-server-supplementary/7Server/x86_64 Red Hat Enterprise Linux Server 7 Supplementary (RPMs) 無効 repolist: 21,141
コマンドの結果がずらりと並んでいますが、実際に状態が有効なリポジトリは3つ。
状態の"有効"と"無効"は各リポジトリの設定ファイルにて記載されています。
一般的には拡張子が.repoのものがリポジトリの設定ファイルとなります。
$ cd /etc/yum.repos.d/ $ ll 合計 20 -rw-r--r--. 1 root root 607 8月 25 14:16 redhat-rhui-client-config.repo -rw-r--r--. 1 root root 8679 8月 25 14:27 redhat-rhui.repo -rw-r--r--. 1 root root 90 8月 25 14:16 rhui-load-balancers.conf $ll
redhat-rhui-client-config.repoとredhat-rhui.repo内で"enabled=1"になっているリポジトリが"有効"になっているものと一致します。
rhui-REGION-rhel-server-releases/7Server/x86_64を無効にしてみます。
vi redhat-rhui.repo [rhui-REGION-rhel-server-releases] name=Red Hat Enterprise Linux Server 7 (RPMs) mirrorlist=https://rhui2-cds01.REGION.aws.ce.redhat.com/pulp/mirror/content/dist/rhel/rhui/server/7/$releasever/$basearch/os enabled=1 ← enabled=0に変更して保存 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release sslverify=1 sslclientkey=/etc/pki/rhui/content-rhel7.key sslclientcert=/etc/pki/rhui/product/content-rhel7.crt sslcacert=/etc/pki/rhui/cdn.redhat.com-chain.crt
再度yum repolist allコマンドでrhui-REGION-rhel-server-releases/7Server/x86_64が無効になっていることを確認
$ yum repolist all 読み込んだプラグイン:amazon-id, rhui-lb, search-disabled-repos リポジトリー ID リポジトリー名 状態 rhui-REGION-client-config-server-7/x86_64 Red Hat Update Infrastructure 2.0 Client Configuration Server 7 有効: 0 rhui-REGION-rhel-server-debug-extras/7Server/x86_64 Red Hat Enterprise Linux Server 7 Extra Debug (Debug RPMs) 無効 rhui-REGION-rhel-server-debug-optional/7Server/x86_64 Red Hat Enterprise Linux Server 7 Optional Debug (Debug RPMs) 無効 rhui-REGION-rhel-server-debug-rh-common/7Server/x86_64 Red Hat Enterprise Linux Server 7 RH Common Debug (Debug RPMs) 無効 rhui-REGION-rhel-server-debug-rhscl/7Server/x86_64 Red Hat Enterprise Linux Server 7 RHSCL Debug (Debug RPMs) 無効 rhui-REGION-rhel-server-debug-supplementary/7Server/x86_64 Red Hat Enterprise Linux Server 7 Supplementary Debug (Debug RPMs) 無効 rhui-REGION-rhel-server-extras/7Server/x86_64 Red Hat Enterprise Linux Server 7 Extra(RPMs) 無効 rhui-REGION-rhel-server-optional/7Server/x86_64 Red Hat Enterprise Linux Server 7 Optional (RPMs) 無効 rhui-REGION-rhel-server-releases/7Server/x86_64 Red Hat Enterprise Linux Server 7 (RPMs) 無効 rhui-REGION-rhel-server-releases-debug/7Server/x86_64 Red Hat Enterprise Linux Server 7 Debug (Debug RPMs) 無効 rhui-REGION-rhel-server-releases-source/7Server/x86_64 Red Hat Enterprise Linux Server 7 (SRPMs) 無効 rhui-REGION-rhel-server-rh-common/7Server/x86_64 Red Hat Enterprise Linux Server 7 RH Common (RPMs) 有効: 233 rhui-REGION-rhel-server-rhscl/7Server/x86_64 Red Hat Enterprise Linux Server 7 RHSCL (RPMs) 無効 rhui-REGION-rhel-server-source-extras/7Server/x86_64 Red Hat Enterprise Linux Server 7 Extra (SRPMs) 無効 rhui-REGION-rhel-server-source-optional/7Server/x86_64 Red Hat Enterprise Linux Server 7 Optional (SRPMs) 無効 rhui-REGION-rhel-server-source-rh-common/7Server/x86_64 Red Hat Enterprise Linux Server 7 RH Common (SRPMs) 無効 rhui-REGION-rhel-server-source-rhscl/7Server/x86_64 Red Hat Enterprise Linux Server 7 RHSCL (SRPMs) 無効 rhui-REGION-rhel-server-source-supplementary/7Server/x86_64 Red Hat Enterprise Linux Server 7 Supplementary (SRPMs) 無効 rhui-REGION-rhel-server-supplementary/7Server/x86_64 Red Hat Enterprise Linux Server 7 Supplementary (RPMs) 無効 repolist: 233
有効と無効の変更はyumコマンドでも可能です。
//有効 --enable $ yum-config-manager --enable rhui-REGION-rhel-server-releases //無効 --disable $ yum-config-manager --disable rhui-REGION-rhel-server-releases
上記を実行すると.repoファイルのenabled=1 or enabled=0 に変更されます。
Phalcon PHP インストール (Amazon Linux2)
AWSのec2インスタンス上にPhalconフレームワークをインストールします。
githubからソースをクローンしてコンパイルする方法でやっていきます。
ちなみにインスタンスタイプはt2.smallを使っています。t2.micro(メモリ1g)はビルド時にメモリが足りなくてエラーになりました。
インスタンスイメージはAmazon Linux 2 AMIを使います。
Apacheインストール
$ yum install httpd
PHP7.2インストール
phpはAmazon Linux Extras Libraryからインストールします。
$ amazon-linux-extras install php7.2 $ php -v PHP 7.2.5 (cli) (built: May 29 2018 19:08:12) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
その他コンパイルに必須なパッケージをインストール
$ yum install php-devel $ yum install gcc make $ yum install git
githubからphalconのソースをインストール&ビルド
$ cd /tmp/ $ git clone git://github.com/phalcon/cphalcon.git $ cd cphalcon/build $ sudo ./install ・ ・ ・ Thanks for compiling Phalcon! Build succeed: Please restart your web server to complete the installation
PHP.ini変更
/etc/php.d/phalcon.ini を新規作成し下記を記述します。
$ vi /etc/php.d/phalcon.ini //下記を追加して保存 extension=phalcon.so
Phalcon DevToolsインンストール
$ cd /tmp/ $ git clone git://github.com/phalcon/phalcon-devtools.git $ cd phalcon-devtools/ $ . ./phalcon.sh $ phalcon Phalcon DevTools (3.4.0) Available commands: info (alias of: i) commands (alias of: list, enumerate) controller (alias of: create-controller) module (alias of: create-module) model (alias of: create-model) all-models (alias of: create-all-models) project (alias of: create-project) scaffold (alias of: create-scaffold) migration (alias of: create-migration) webtools (alias of: create-webtools) serve (alias of: server) console (alias of: shell, psysh)
サンプルプロジェクトを作ります
$d /usr/local/src $phalcon create-project test Phalcon DevTools (3.4.0) Success: Controller "index" was successfully created. /usr/local/src/store/app/controllers/IndexController.php Success: Project 'test' was successfully created. Please choose a password and username to use Database connection.Used default:'root' without password.
Apache設定変更
$vi /etc/httpd/conf/httpd.conf #Document #DocumentRootを"/usr/local/src/test/public"に変更 #Directoryの設定を追加 $service httpd restart
ブラウザでアクセス
Apache2.4系 アクセス許可設定
PHPのPhalconフレームワークをインストールした時に、
サーバへアクセスしても403が返却されてしまった。
Apacheの設定を見直しても改善されずに、いろいろと調べたら
Apache2.4系からアクセス許可の記述方法に変更があった。
単純に参考にしていた記事のApacheバージョンが2.2系??
だった。。。
変更前
<Directory "/usr/local/src/phalcon-sample/public"> AllowOverride All Allow from all </Directory>
変更後
<Directory "/usr/local/src/phalcon-sample/public"> AllowOverride All Require all granted </Directory>
AmazonLinux2 amazon-linux-extrasでPHPをインストール
Amazon マシンイメージ (AMI):Amazon Linux 2 AMI (HVM), SSD Volume Type - ami-e99f4896
extras-libraryでインストール可能なリストは下記コマンドで確認できます。
# amazon-linux-extras 0 ansible2 available [ =2.4.2 ] 1 emacs available [ =25.3 ] 2 memcached1.5 available [ =1.5.1 ] 3 nginx1.12 available [ =1.12.2 ] 4 postgresql9.6 available [ =9.6.6 =9.6.8 ] 5 postgresql10 available [ =10 ] 6 python3 available [ =3.6.2 ] 7 redis4.0 available [ =4.0.5 =4.0.10 ] 8 R3.4 available [ =3.4.3 ] 9 rust1 available \ [ =1.22.1 =1.26.0 =1.26.1 ] 10 vim available [ =8.0 ] 11 golang1.9 available [ =1.9.2 ] 12 ruby2.4 available [ =2.4.2 =2.4.4 ] 13 nano available [ =2.9.1 ] 14 php7.2 available [ =7.2.0 =7.2.4 =7.2.5 ] 15 lamp-mariadb10.2-php7.2 available \ [ =10.2.10_7.2.0 =10.2.10_7.2.4 =10.2.10_7.2.5 ] 16 libreoffice available [ =5.0.6.2_15 ] 17 gimp available [ =2.8.22 ] 18 docker=latest enabled [ =17.12.1 =18.03.1 ] 19 mate-desktop1.x available [ =1.19.0 =1.20.0 ] 20 GraphicsMagick1.3 available [ =1.3.29 ] 21 tomcat8.5 available [ =8.5.31 ]
php7.2があることが確認できます。
Extras Library ではソフトウェアをトピックと呼ぶらしいです。
php7.2に紐づくトピックを下記コマンドで確認します。
# amazon-linux-extras info php7.2 php7.2 recommends php-cli # yum install php-cli php7.2 recommends php-pdo # yum install php-pdo php7.2 recommends php-fpm # yum install php-fpm php7.2 recommends php-json # yum install php-json php7.2 recommends php-mysqlnd # yum install php-mysqlnd
トピックのインストールは下記コマンドで実行可能です
amazon-linux-extras install php7.2 インストール: php-cli.x86_64 0:7.2.5-3.amzn2.0.2 php-fpm.x86_64 0:7.2.5-3.amzn2.0.2 php-json.x86_64 0:7.2.5-3.amzn2.0.2 php-mysqlnd.x86_64 0:7.2.5-3.amzn2.0.2 php-pdo.x86_64 0:7.2.5-3.amzn2.0.2 依存性関連をインストールしました: php-common.x86_64 0:7.2.5-3.amzn2.0.2 完了しました! 0 ansible2 available [ =2.4.2 ] 1 emacs available [ =25.3 ] 2 memcached1.5 available [ =1.5.1 ] 3 nginx1.12 available [ =1.12.2 ] 4 postgresql9.6 available [ =9.6.6 =9.6.8 ] 5 postgresql10 available [ =10 ] 6 python3 available [ =3.6.2 ] 7 redis4.0 available [ =4.0.5 =4.0.10 ] 8 R3.4 available [ =3.4.3 ] 9 rust1 available \ [ =1.22.1 =1.26.0 =1.26.1 ] 10 vim available [ =8.0 ] 11 golang1.9 available [ =1.9.2 ] 12 ruby2.4 available [ =2.4.2 =2.4.4 ] 13 nano available [ =2.9.1 ] 14 php7.2=latest enabled [ =7.2.0 =7.2.4 =7.2.5 ] 15 lamp-mariadb10.2-php7.2 available \ [ =10.2.10_7.2.0 =10.2.10_7.2.4 =10.2.10_7.2.5 ] 16 libreoffice available [ =5.0.6.2_15 ] 17 gimp available [ =2.8.22 ] 18 docker=latest enabled [ =17.12.1 =18.03.1 ] 19 mate-desktop1.x available [ =1.19.0 =1.20.0 ] 20 GraphicsMagick1.3 available [ =1.3.29 ] 21 tomcat8.5 available [ =8.5.31 ]
インストールされたphpのバージョンを確認
php -v PHP 7.2.5 (cli) (built: May 29 2018 19:08:12) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
AmazonLinuxでデフォルトリポジトリ調査
EC2マシンイメージ (AMI):
Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type - ami-9c9443e3
#yum repolist all 読み込んだプラグイン:priorities, update-motd, upgrade-helper リポジトリー ID リポジトリー名 状態 amzn-main/latest amzn-main-Base 有効: 5,934 amzn-main-debuginfo/latest amzn-main-debuginfo 無効 amzn-main-source/latest amzn-main-source 無効 amzn-nosrc/latest amzn-nosrc-Base 無効 amzn-preview/latest amzn-preview-Base 無効 amzn-preview-debuginfo/latest amzn-preview-debuginfo 無効 amzn-preview-source/latest amzn-preview-source 無効 amzn-updates/latest amzn-updates-Base 有効: 474 amzn-updates-debuginfo/latest amzn-updates-debuginfo 無効 amzn-updates-source/latest amzn-updates-source 無効 epel/x86_64 Extra Packages for Enterprise Linux 6 - x86_64 無効 epel-debuginfo/x86_64 Extra Packages for Enterprise Linux 6 - x86_64 - Debug 無効 epel-source/x86_64 Extra Packages for Enterprise Linux 6 - x86_64 - Source 無効 epel-testing/x86_64 Extra Packages for Enterprise Linux 6 - Testing - x86_64 無効 epel-testing-debuginfo/x86_64 Extra Packages for Enterprise Linux 6 - Testing - x86_64 - Debug 無効 epel-testing-source/x86_64 Extra Packages for Enterprise Linux 6 - Testing - x86_64 - Source 無効 repolist: 6,408
EC2マシンイメージ (AMI):
Amazon Linux 2 AMI (HVM), SSD Volume Type - ami-e99f4896
# yum repolist all 読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd リポジトリー ID リポジトリー名 状態 amzn2-core/2/x86_64 Amazon Linux 2 core repository 有効: 9,144 amzn2-core-debuginfo/2/x86_64 Amazon Linux 2 core repository - debuginfo packages 無効 amzn2-core-source/2 Amazon Linux 2 core repository - source packages 無効 amzn2extra-docker/2/x86_64 Amazon Extras repo for docker 有効: 5 amzn2extra-docker-debuginfo/2/x86_64 Amazon Extras debuginfo repo for docker 無効 amzn2extra-docker-source/2 Amazon Extras source repo for docker 無効 repolist: 9,149
centos7.2 + Apache2.4 + php7 + cakephp3 + mysql5.6 で管理画面を作ります。 その5
centos7.2 + Apache2.4 + php7 + cakephp3 + mysql5.6 で管理画面を作ります。 その5
シンプルな認証機能を作る。メモとして書きます。
基本的はcakephpのAuthComponentを使うとほとんどやることをないです。
公式ドキュメントを参考にします。
シンプルな認証と認可のアプリケーション - 3.6
まずはMysqlにusersのTabelを作成します。
usersテーブルのにはログイン時のusernameとパスワードを保持させます。
CREATE TABLE users ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50), password VARCHAR(255), role VARCHAR(20), created DATETIME DEFAULT NULL, modified DATETIME DEFAULT NULL );
usersテーブルができたら、次はプログラムを作成します。
基本ベースはbakeコマンドでuserテーブルを指定して作成されたプログラムを修正していくイメージです。
bakeコマンドでとりあえずAllで全部ありで作ります。
bin/cake bake All Users --------------------------------------------------------------- Baking table class for Users... Bake All complete. 成功!!!!
次はAuthComponentを適用するためにsrc/Controller/AppController.phpに下記に修正します。
今回はログイン後のリダイレクト先コントローラーとアクションの指定と
ログアウト後のリダイレクト先の指定となります。
ログイン認証時に利用するカラム名がデフォルトのusernameとpasswordの場合は、記述の省略が可能です。ログインアカウント登録画面は認証が不要なので、beforeFilterで'add'を認証除外にしています。
public function initialize() { parent::initialize(); $this->loadComponent('RequestHandler', ['enableBeforeRedirect' => false]); $this->loadComponent('Flash'); // AuthComponentようの設定 $this->loadComponent('Auth', [ 'loginRedirect' => [ 'controller' => 'Index', 'action' => 'index' ], 'logoutRedirect' => [ 'controller' => 'Users', 'action' => 'login', 'home' ] ]); } public function beforeFilter(Event $event) { $this->Auth->allow(['add']); }
次にアカウント登録処理を作成します。
先ほどのbakeコマンド実行でコンントローラーのaddアクション処理とテンンプレート(/Template/Users/add.ctp)、モデルのベースは作成済みです。ありがたいーーーーー。
パスワードの登録データは平文ではなく、ハッッシュ化されたものをUsersテーブルに登録するため、修正します。
_setPasswordで入力された平文パスワードをハッシュ化します。
/Model/Entity/User.php
//追加 use Cake\Auth\DefaultPasswordHasher; class User extends Entity { //追加 protected function _setPassword($password) { if (strlen($password) > 0) { return (new DefaultPasswordHasher)->hash($password); } } }
次はログイン画面を作ります。
bakeコマンドではログイン関連は作成されないため、Usersコントローラにloginアクションとlogoutアクションの追加とlogin.ctpテンンプレートを作成します。
/Controller/UsersController.php
//追加 public function login() { if ($this->request->is('post')) { $user = $this->Auth->identify(); if ($user) { $this->Auth->setUser($user); return $this->redirect($this->Auth->redirectUrl()); } $this->Flash->error(__('Invalid username or password, try again')); } } //追加 public function logout() { return $this->redirect($this->Auth->logout()); }
/Template/Users/login.ctp
<!-- File: src/Template/Users/login.ctp --> <div class="users form"> <?= $this->Flash->render() ?> <?= $this->Form->create() ?> <fieldset> <legend><?= __('Please enter your username and password') ?></legend> <?= $this->Form->control('username') ?> <?= $this->Form->control('password') ?> </fieldset> <?= $this->Form->button(__('Login')); ?> <?= $this->Form->end() ?> </div>
/users/addにブラウザでアクセスするとアカウント追加画面が表示される。
usernameとパスワードと権限)(1)を入力して登録するとusersテーブルにデータが登録される。
login画面でusernameとパスワードを入力すればログインができる。