オレオレsecurityバンドル作っちゃいました
DESCRIPTION
TRANSCRIPT
![Page 1: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/1.jpg)
オレオレSecurityBundle作っちゃいました
クロコス株式会社 小川 雄大
![Page 2: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/2.jpg)
現状のSecurityBundle
設定しにくい
拡張しにくい
理解しにくい
社内でクレーム続出
![Page 3: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/3.jpg)
そんなわけで....
作ってみました
CrocosSecurityBundlehttps://github.com/crocos/CrocosSecurityBundle
![Page 4: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/4.jpg)
CrocosSecurityBundle
アノテーションのみで設定
認証状態の保持と、アクション呼び出しの制御のみを行う
![Page 5: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/5.jpg)
class MyController{ /** * @Secure */ public function doAction() { }}
![Page 6: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/6.jpg)
/** * @Secure */class MyController{ public function doAction() { }}
![Page 7: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/7.jpg)
Secure アノテーションを付与したアクションがログイン必須に
クラスにつけるとすべてのアクションでログインが必須に
(*)アノテーションはインポートが必要
![Page 8: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/8.jpg)
/** * @Secure */class MyController{ /** * @Secure(disabled=true) */ public function doAction() { }}
![Page 9: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/9.jpg)
Secure アノテーションに disabled オプションをつけると無効に
![Page 10: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/10.jpg)
/** * @Secure */class AppController {}
class FooController extends AppController {}
class BarController extends AppController {}
![Page 11: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/11.jpg)
親クラスを共通化して、親クラスにSecure アノテーションを付与すれば全体でログインを必須に
![Page 12: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/12.jpg)
/** * @Secure * @SecureConfig( forward=“Acme:My:login”) */class MyController{ public function loginAction() { }}
![Page 13: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/13.jpg)
ログインが必須なアクションにアクセスしたら、SecureConfigアノテーションのforward属性に指定したコントローラが呼び出される
forwardは必須
![Page 14: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/14.jpg)
/** * @Secure * @SecureConfig(domain=“default”) */class UserController {}
/** * @Secure * @SecureConfig(domain=“admin”) */class AdminController {}
![Page 15: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/15.jpg)
SecureConfigアノテーションのdomain
属性ごとに認証状態を管理できる
defaultドメインで認証されていても、adminドメインでは認証されていない
![Page 16: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/16.jpg)
/** * @Secure * @SecureConfig(auth=“facebook”) */class FacebookController {}
![Page 17: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/17.jpg)
SecureConfigアノテーションにauth属性を指定すると認証状態の管理方法を切り替えることができる
デフォルトはsession
facebookにするとFacebookのPHP SDK
に認証状態の管理を委譲
![Page 18: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/18.jpg)
SecurityContext
認証状態の管理はSecurityContextオブジェクトが行っている
crocos_security.context(DIコンテナ)
![Page 19: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/19.jpg)
$context = $this->get( ‘crocos_security.context’);
$user = $userRepo->find(1);
$context->login($user);
$context->logout();
![Page 20: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/20.jpg)
login()メソッドにユーザを識別できる値を渡してログインする
ユーザオブジェクトでも、IDでも
logout()メソッドでログアウト
![Page 21: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/21.jpg)
if ($context->isAuthenticated()) { $user = $context->getUser();}
![Page 22: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/22.jpg)
isAuthenticated()メソッドで判定
getUser()メソッドでユーザの取得
![Page 23: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/23.jpg)
今後
基本的に欲しい機能は実装してあるので、あまり大きなアップデートはしないと思います。
![Page 24: オレオレSecurityバンドル作っちゃいました](https://reader034.vdocuments.pub/reader034/viewer/2022050920/54b739e84a7959be4c8b46dd/html5/thumbnails/24.jpg)
GitHubで公開中
https://github.com/crocos/CrocosSecurityBundleREADMEに日本語で使い方書いてます