Angsuman's Authenticated WordPress Plugin

WordPress 自体を CUG 化する案件があった。社内での 一般的な IT 知識の共有のために使いたい ということだ。
この手のリクエストでは、なんらかの Blog システムをインストールして、Basic認証 をかける程度にする場合が多い。企業資産や企業秘密の漏洩に繋がる場合を除いて、ガチガチに認証をかけたいというケースはあまりない。コストパフォーマンスを考えると、リスクの程度 (確立 x 被害甚大度) によっては、必要充分であるという判断をするケースが多い。
「なんでもかんでも Basic認証というのもエレガントじゃないし、アクセス時の状態を確認してプリパースしてやればいいんじゃね?んなの 数行の PHP ですむでそ?」「WordPress ならプラグインでそんなのくらいありそーね」…調べてみた…Angsuman's Authenticated WordPress PluginWordPress の URI にアクセスしようとすると、現在ログイン中かどうかを判断して、ログイン中でなければ、ログイン画面に移行するということで、全く以ておあつらえ向きな感じ。ファイルをダウンロードして、例によって、wp-content/plugins に放り込んでやるのみ。ブツは、ac_authenticator.php という実質10数行のファイルひとつ。しかも、入れて、Dashboard > Plugins から有効にしてやるだけだと言う。ところが、ログアウトした後、WordPress にアクセスすると、ログイン画面が出るには出るのだが、正しい ID / Password を入力してやっても、先に進まない…対処方法を調べてみると…
[wp-testers] Angsuman’s Authenticated WordPress PluginAngsuman's Authenticated WordPress Plugin just keeps redirecting backto the dashboard, alternately the Authenticate plugin seems to workcorrectly.
ってーのがあったので、スレッドをたどってみる…
Re: [wp-testers] Angsuman’s Authenticated WordPress PluginI'm glad you mentioned this. I was planning on bringing this up myselfsometime soon. I also use Angsuman's plugin. The reason it doesn'twork in 2.5 is that there's a new system for generating cookies andthat plugin implicitly relies on the old system.I work around the issue by modifying the plugin. I replaced this bit:if ( (!empty($_COOKIE[USER_COOKIE]) &&!wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true)) ||(empty($_COOKIE[USER_COOKIE])) )with this bit:global $user_ID;if (!$user_ID) {and it _seems_ to work exactly as it should. But it seems too simple.If it was really that easy, why didn't it work like that to beginwith? I wonder, is there something I'm missing? Is checking for avalue to $user_ID less secure than verifying cookie contents?
Re: [wp-testers] Angsuman’s Authenticated WordPress Pluginis_user_logged_in() would be better. That's been around for many releases.Ryan
どうやら、ログインしているかどうかを確認するのに使用する cookie のジェネレートシステムが、WordPress2.5以前と以降では異なる ということが原因のよう。ということで、ac_authenticator.php を修正する。※ こういうとき、Coda は、直接サーバ内のファイルを修正している感じで便利!

《修正前》

[php highlight="14,15,16"]<?php/*Plugin Name: Angsuman's Authenticated WordPress PluginPlugin URI: http://blog.taragana.com/index.php/archive/angsumans-authenticated-wordpress-plugin-password-protection-for-your-wordpress-blog/Description: This plugin allows you to make your WordPress site accessible to logged in users only. In other words to view your site they have to create / have an account in your site and be logged in. No configuration necessary. Simply activating the plugin is all that is required from you.Author: Angsuman Chakraborty, TaraganaVersion: 1.0Author URI: http://blog.taragana.com/License: Free to use non-commercially.Warranties: None.*/function ac_auth_redirect() { // Checks if a user is logged in, if not redirects them to the login page if ( (!empty($_COOKIE[USER_COOKIE]) && !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true)) || (empty($_COOKIE[USER_COOKIE])) ) { nocache_headers(); header("HTTP/1.1 302 Moved Temporarily"); header('Location: ' . get_settings('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI'])); header("Status: 302 Moved Temporarily"); exit(); }}if('wp-login.php' != $pagenow && 'wp-register.php' != $pagenow) add_action('template_redirect', 'ac_auth_redirect');?>[/php]

《修正後》

[php highlight="14"]<?php/*Plugin Name: Angsuman's Authenticated WordPress PluginPlugin URI: http://blog.taragana.com/index.php/archive/angsumans-authenticated-wordpress-plugin-password-protection-for-your-wordpress-blog/Description: This plugin allows you to make your WordPress site accessible to logged in users only. In other words to view your site they have to create / have an account in your site and be logged in. No configuration necessary. Simply activating the plugin is all that is required from you.Author: Angsuman Chakraborty, TaraganaVersion: 1.0Author URI: http://blog.taragana.com/License: Free to use non-commercially.Warranties: None.*/function ac_auth_redirect() { // Checks if a user is logged in, if not redirects them to the login page if ( !is_user_logged_in() ){ nocache_headers(); header("HTTP/1.1 302 Moved Temporarily"); header('Location: ' . get_settings('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI'])); header("Status: 302 Moved Temporarily"); exit(); }}if('wp-login.php' != $pagenow && 'wp-register.php' != $pagenow) add_action('template_redirect', 'ac_auth_redirect');?>[/php]思った動きになった。
つか…[php]auth_redirect()[/php]っていう便利な関数があるわけだから…Function Reference/auth redirect « WordPress Codex[php]<?phpif (!is_user_logged_in()) { auth_redirect();}?>[/php]とかを使用テーマの header.php に書くだけじゃだめ?…やってみた。できた。www

脳みその中身(Ownd 別館)

ICTコンサルタントの備忘録

0コメント

  • 1000 / 1000