2020年11月18日水曜日

laravel user password reset際、登録済みemailのチェック条件

 laravelデフォルトuser table softdelete以外のemailですでに登録済みユーザーかどうか判断する。追加条件で登録済みユーザーを判断したい場面もでてくる。その際、sendResetLinkEmail functionをoverwriteすれば良い。


下記のように、app\Http\Controllers\Auth\ForgotPasswordController.php はtrait SendsPasswordResetEmails(vendor\laravel\framework\src\Illuminate\Foundation\Auth\SendsPasswordResetEmails.php)を使用している。


use SendsPasswordResetEmails


登録済みのemailかどうかについて、sendResetLinkEmail functionの中で行っている。


/**

 * overwrite SendsPasswordResetEmails function in ForgotPasswordController.php

 */

public function sendResetLinkEmail(Request $request)

    {

        $this->validateEmail($request);

        $request->merge(['type' => 0]); // 条件追加:type 0のユーザーのみ登録済みかどうか判断

       

        $response = $this->broker()->sendResetLink(

            $request->only('email', 'type') // ここでemail以外、type値も追加

        );

        return $response == Password::RESET_LINK_SENT

                    ? $this->sendResetLinkResponse($request, $response)

                    : $this->sendResetLinkFailedResponse($request, $response);

    }



2020年10月28日水曜日

git remote

remote(github)すでにある、localでもgit initで作った。remoteとlocalを連結させるため
git remote add origin https://github.com/xxx/yyy.git

localですでにgitあり、remote(gitlab)まだない、local gitをremoteで反映させる
git remote add origin https://アカウント名@gitlab.com/アカウント名/プロジェクト名.git

削除
git remote rm origin // 関連性を削除
git remote remove origin // remote repository削除

2020年8月24日月曜日

laravel projectにてhtpasswd でbase認証をかける

1)base認証用password fileを作成

mkdir /etc/htpasswd (必要に応じて)

htpasswd -c /etc/htpasswd/.htpasswd username(コマンドを実行すると、PW入力を要求される)


2)/var/www/html/site/public/.htaccess を変更

一番上に、以下を追加


AuthType Basic

AuthName "Authentication Required"

AuthUserFile "/etc/htpasswd/.htpasswd"

Require valid-user

サクラVPSにSSLを導入

 1)certbotインストール

yum install certbot python2-certbot-apache


2)apache confファイルの最後に下記VirtualHost追加


NameVirtualHost *:80


<VirtualHost *:80>

ServerAdmin root@xxx.com

DocumentRoot /var/www/html/site/public

ServerName xxx.com


3)certbotコマンドを実行して証明書をインストール

certbot --apache -d xxx.com

※実施中、httpをhttpsへredirectするか聞いてくる際、redirectするようにします。


初期ドメイン→独自ドメイン リダイレクト

以下の内容をapache confファイルに追加

<VirtualHost *:80>

ServerAdmin root@xxx.com

DocumentRoot /var/www/html/site/public

ServerName os1-234-56789.vs.sakura.ne.jp

RewriteEngine on

RewriteCond %{SERVER_NAME} =os1-234-56789.vs.sakura.ne.jp

RewriteRule ^ https://xxx.com%{REQUEST_URI} [END,NE,R=permanent]


4)証明書を更新

certbot renew

2019年7月5日金曜日

ssh転送でremote mysqlをlocal開発に利用


ssh -f -N -L 13306:localhost:3306 username@10.10.10.10 -p 22

2019年6月3日月曜日

laravel type hintでobjとれない件

以下のrouting定義がある。
Route::resource('tokus', 'TokuController')

controllerで下記のtype hintがあるが、dd($toku)であるはずのobjがとれない。
public function show(Toku $toku)
    {
        return view('tokus.show', compact('toku'));
    }

route:listで確認したところ、show actionのURIは、tokes/{tokes}になっている。
type hintの引数は{tokes}と一致しないといけないため、public function show(Toku $tokus)に変更したら解決できた。それが嫌なら、引数を指定できる。

Route::resource('tokus', 'TokuController')->parameters([
    'tokus' => 'toku'
]);

参考:https://laracasts.com/discuss/channels/laravel/controller-method-with-type-hinting-give-empty-eloquent-object

2019年5月30日木曜日

laravel middleware

Controllers also allow you to register middleware using a Closure. This provides a convenient way to define a middleware for a single controller without defining an entire middleware class:

$this->middleware(function ($request, $next) {
    // ...

    return $next($request);
});