私は何か間違ったことをしているに違いない。Webのどこにも自分の問題に対する答えが見つからないようです。これは一般的に、解決策が非常に単純で、誰も答えを必要としないことを意味します。
セッションを保存するためにデータベースを使用しています。私はそれを私のブートストラップに次のように設定しました:
保護された関数_initDBSessions(){ $ resource = $ this-> getPluginResource('db'); // config.iniから? $ db = $ resource-> getOptions(); $ adapter = new Zend_Db_Adapter_Pdo_Mysql($ db ["params"]); Zend_Db_Table_Abstract :: setDefaultAdapter($ adapter); $ config = array('name' =>'sessions'、'primary' =>'id'、'modifiedColumn' =>'modified'、'dataColumn' =>'data'、'lifetimeColumn' =>'lifetime') ; $ options = array( "strict" => FALSE、 "name" => "eCubed"、 "use_cookies" => FALSE ); Zend_Session :: setOptions($ options); Zend_Session :: setSaveHandler(new Zend_Session_SaveHandler_DbTable($ config)); }
次のブートストラップはプラグインのセットアップです
保護された関数_initPlugins(){ Zend_Controller_Front :: getInstance()-> registerPlugin(new Acl_Acl()); }
私のAcl_Aclは次のようになります。
クラスAcl_AclはZend_Controller_Plugin_Abstract{を拡張します パブリック関数preDispatch(Zend_controller_request_abstract $ request){ $ acl = new Zend_Acl(); //役割を追加します $ acl-> addRole(new Zend_Acl_Role(Acl_Levels :: $ GUEST)); $ acl-> addRole(new Zend_Acl_Role(Acl_Levels :: $ BASIC)、Acl_Levels :: $ GUEST); $ acl-> addRole(new Zend_Acl_Role(Acl_Levels :: $ SHOP)、Acl_Levels :: $ BASIC); $ acl-> addRole(new Zend_Acl_Role(Acl_Levels :: $ OFFICE)、Acl_Levels :: $ SHOP); $ acl-> addRole(new Zend_Acl_Role(Acl_Levels :: $ EXECUTIVE)、Acl_Levels :: $ OFFICE); $ acl-> addRole(new Zend_Acl_Role(Acl_Levels :: $ OWNER)); $ acl-> addRole(new Zend_Acl_Role(Acl_Levels :: $ ADMIN)、Acl_Levels :: $ OWNER); //リソースを追加します $ acl-> addResource( "index"); $ acl-> addResource( "authenticate"); $ acl-> addResource( "error"); $ acl-> addResource( "employees"); $ acl-> addResource( "mold"); $ acl-> addResource( "search"); $ acl-> addResource( "shop"); $ acl-> addResource( "user"); //アクセスルール $ acl-> allow(null、array('index'、'error'、'authenticate')); //デフォルトのリソース //ゲストメンバーのアクセス $ acl-> allow(Acl_Levels :: $ GUEST、'mold'、array('index'、'list-molds')); $ acl-> allow(Acl_Levels :: $ GUEST、'user'、array('index'、'login'、'new-profile'、'my-profile')); //SHOPメンバーアクセス $ acl-> allow(Acl_Levels :: $ BASIC、'mold'、array('get-mold'、'get-part'、'get-order'、'get-orders'、'get-parts'、'print -mold-labels'、' print-part-labels')); $ acl-> allow(Acl_Levels :: $ BASIC、'user'、array('my-profile'、'profile')); //OFFICEメンバーアクセス //エグゼクティブメンバーアクセス //オーナーメンバーアクセス //ADMINメンバーアクセス //現在の使用者 if(Zend_Auth :: getInstance()-> hasIdentity()){ $ level = Zend_Auth :: getInstance()-> getIdentity()-> level; } そうしないと { $ level = Acl_Levels :: $ GUEST; } $ conroller = $ request-> controller; $ action = $ request-> action; 試す { if(!$ acl-> isAllowed($ level、$ conroller、$ action)){ $ request-> setControllerName('application-error'); $ request-> setActionName('not-authorized'); } } catch(Exception $ e){ $ request-> setControllerName( "application-error"); $ request-> setActionName( "error"); $ error = new Zend_Controller_Plugin_ErrorHandler(); $ error-> type = Zend_Controller_Plugin_ErrorHandler :: EXCEPTION_OTHER; $ error-> request = clone($ request); $ error-> exception = $ e; $ request-> setParam('error_handler'、$ error); } } }
私の認証コントローラーには次のアクションがあります。
パブリック関数loginAction(){ $ this-> _ helper-> viewRenderer-> setNoRender(TRUE); $ loginForm = new Form_Login(); $ form = $ loginForm-> getLoginForm(); $ form-> setAction( "/ authenticate / login"); if($ this-> getRequest()-> isPost()){ if($ form-> isValid($ _ POST)){ $ email = $ form-> getValue('email'); $ pass = $ form-> getValue('password'); $ authAdapter = $ this-> getAuthAdapter(); $ authAdapter-> setIdentity($ email) -> setCredential($ pass); $ result = Zend_Auth :: getInstance()-> authenticate($ authAdapter); if($ result-> isValid()){ $ omit = array('password'、'timestamp'、'temp_password'、'active'、'created'); $ ID = $ authAdapter-> getResultRowObject(NULL、$ omit); $ authStorage = Zend_Auth :: getInstance()-> getStorage(); $ authStorage-> write($ Identity); $ nickname = $ Identity->ニックネーム?$ ID->ニックネーム:$ Identity-> first_name; $ this-> _ helper-> flashMessenger( "Welcome back $ nickname"); // Zend_Debug :: dump($dentity); 出口; $ this-> _ redirect( "/"); } そうしないと { $ this-> _ helper-> flashMessenger( "ログインできません。もう一度やり直してください"); $ this-> _ redirect( "/"); } } } }
私のデータベース構造:id:int変更:intライフタイム:intデータ:テキスト
すべて順調ですよね?うーん、ダメ...
まず、ログインしていない人が更新したり、ページに移動したりするたびに、セッションが作成されます。これは許容できると思います...
私が抱えている問題は、最終的にログインすると、データベースにZend_Auth IDとFlashmessengerが完全に保存されていることがわかりますが、...
...また、ログインしていないユーザーがWebサイトをナビゲートしているように、データベースにファントム行を作成しています...。
たとえば、ユーザーが「プロファイル」ページにリダイレクトされると、Zendはデータをまったく含まないファントムセッションデータを参照しているため、これにより認証が不可能になります。
以下は、Zend_Sessionデータベーステーブルに保存されている情報です。
Zend_Auth | a:1:{s:7: "storage"; O:8: "stdClass":7:{s:2: "id"; s:1: "2"; s:5: "email"; s:17: "wes @ * *** .com"; s:10: "first_name"; s:6: "Wesley"; s:9: "last_name"; s:7: " * "; s:5 :"level"; s:5: "basic"; s:8: "nickname"; s:3: "Wes"; s:9: "lastlogin"; s:19: "2011-07-14 19:30 :36 ";}} __ ZF | a:1:{s:14:" FlashMessenger "; a:1:{s:4:" ENNH "; i:1;}} FlashMessenger | a:1:{s:7 :"default"; a:1:{i:0; s:16: "Welcome back Wes";}}
これは私を2日間運転してきました。Zend_Sessionは自動的に1つのセッションだけを使用してデータを保存しているという印象を受けていますが、これらの複数のエントリが私を怒らせています!!
私は誰かにうまくいくのに十分な情報を与えたことを望みます。