前回の続き

ドライバの実装が終わったので、呼び出し側のコード

<?php

class Controller_Base extends Controller_Template {

	public $template = 'common/template';

	public function before()
	{
		parent::before();
		$this->current_user = Auth::check() ? Model_User::find_by_username(Auth::get_screen_name()) : null;
		View::set_global('current_user', $this->current_user);
	}

}

これはoil generate admin コマンドで自動生成されるベースファイルをそのまま流用
テンプレートパスは’common/template’にしている

ログインを管理するsign.phpを作成

<?php
class Controller_Sign extends Controller_Base {

	public function before()
	{
		parent::before();
	}

	public function action_login()
	{
		if(Auth::check()) Response::redirect($this->get_groups_redirect_path());

		$val = Validation::forge();

		if (Input::method() == 'POST')
		{
			$val->add('email', 'Email or Username')
			->add_rule('required');
			$val->add('password', 'Password')
			->add_rule('required');

			if ($val->run())
			{
				if (Auth::instance()->login(Input::post('email'), Input::post('password')))
				{
					$current_user = Model_User::find_by_username(Auth::get_screen_name());
					Session::set_flash('success', e('Welcome, '.$current_user->username));

					Response::redirect($this->get_groups_redirect_path());
				}

				// login failure
				Session::set_flash('error', e('login error. please try agein'));
					
			}
		}

		$this->template->title = 'Login';
		$this->template->content = View::forge('admin/login')->set('val', $val, false);
	}

	public function action_logout()
	{
		if(!Auth::logout())
		{
			Session::set_flash('info', e('logout success'));
			Response::redirect('sign/login');
		}	
	}

	public function get_groups_redirect_path()
	{
		$groups = Auth::instance()->get_groups();
		$group = $groups[0][1];

		$group == Util_Const::GROUP_ADMINISTRATOR_KEY and $redirect_path = 'admin/index';
		$group == Util_Const::GROUP_USER_KEY and $redirect_path = 'user/index';

		return empty($redirect_path) ? '':$redirect_path;
	}
}

ログイン認証を通った場合のリダイレクト先についてはこちらの都合上、groupの権限でswitchしている。
groupのkey値をconstに指定。

<?php
class Util_Const
{
	/* ======= db ======= */
	
	const DB_DLT_OFF = 0;
	const DB_DLT_ON = 1;
	
	
	/* ======= group ======= */
	
	const GROUP_USER_KEY = 1;
	const GROUP_ADMINISTRATOR_KEY = 100;
	
}

ここまでで呼び出し完了のはず。今回はログインのみのためtb_usersテーブルにアカウントを作成しないと動かない。アカウント作成用のメソッドをauth/login/baseauth.phpに作成。

	public function create_user($username, $password, $email, $group = 1,$auth_type = 1,$dlt_flg = 0)
	{
		$password = trim($password);
		$email = filter_var(trim($email), FILTER_VALIDATE_EMAIL);

		if (empty($username) or empty($password) or empty($email))
			throw new Exception('Username, password or email address is not given, or email address is invalid');

		$user = new Model_User();
		$user->username = $username;
		$user->password = $this->hash_password((string) $password);
		$user->email = $email;
		$user->group = $group;
		$user->auth_type = $auth_type;
		$user->last_login = '';
		$user->salt = '';
		$user->dlt_flg = $dlt_flg;

		try{
			$user->save();
		}catch(Exception $e){
			throw new Exception('create user registry error');
		}
		return 1;
	}

それでは呼び出してadminアカウントを作成。ここではsignup画面は省略してoil consoleから作成。

$ oil console
>>> Auth::create_user('admin', 'password', 'test@test.co.jp', 100);
1
>>> exit

正直戻り値が成功で「1」っていうのは意味が分からないが、simpleauthに便乗している。trueにしたいところ。phpだからしかたないのかな。これでログインできれば成功かな。

今回はviewなどのコードをアップしていないので、分かりにくいところがあるので、一応githubにsignup画面込みでアップする予定。

その他おすすめの備忘録

Tagged with:
 

コメントを残す