まず、アダプタパターンを使用することをお勧めします。特定のタイプの認証を実装するクラスを作成するよりも、インターフェイス(パブリックメソッド)を定義する抽象クラスを用意する必要があります。たとえば、DBベース認証用のクラス/アダプタ、LADP認証用のクラス/アダプタなどを作成できます。
車輪を改造するのではなく、準備が整ったソリューションを使用することをお勧めします。私はZend_Authを使用しました。これはアダプタパターンを実装しています。 Zend_Authを使うことで、良いOOP実践、アダプタパターン、インタフェースと抽象クラスの使い方を理解することができます。
ここでは、私がZend_Authをイントラネットでどのように使用したかを見ることができます。
protected function Authentication() {
$strEmail = trim($this->txtEmail->Text);
$this->txtPassword->Text = trim($this->txtPassword->Text);
// do the process of authentication, AD and then DB
// Get a reference to the singleton instance of QAuth
$auth = QAuth::getInstance();
// Set up the authentication adapter
$authAdapter = new QAuth_Adapter_WebService(__LOGIN_WS_URL__,
$strEmail, $this->txtPassword->Text
);
// Attempt authentication, saving the result
$result = $auth->authenticate($authAdapter);
if ($result->isValid()) {
$objUser = User::LoadByEmail($strEmail);
// if there is not a user's record create one
if(!$objUser) {
$this->User_Create($strEmail);
$objUser = User::LoadByEmail($strEmail);
}
$crypt = new Encryption();
$encr = $crypt->encrypt(__KEY__, $objUser->UserID);
$_SESSION['user_id'] = $encr;
setcookie('user_id', $_SESSION['user_id'], time()+(3600*24*365*20));
$this->Intranet1Integration($objUser->UserID);
QApplication::Redirect('http://'.__URL__.'/index.php');
}
else {
QApplication::DisplayAlert(
'Log on failed. You must provide a Company email and a correct password.'
);
}
}
非常に良い点、私の特定のプロジェクトに最も関連しています。 – Mathew
外部依存関係が存在しない場合は、具体的な実装に対するプログラミングに問題はありません。つまり、クラスが同じパッケージ内でのみ使用されている場合(パッケージは非常に密着しています)、インターフェースを持たないという障害はありません。 –