Kohana 3.3 Twitter和Auth orm集成

时间:2013-08-30 13:15:23

标签: php authentication twitter orm kohana-3.3

HI我是Kohana的新手我正在尝试使用twitter登录模块,我可以访问twitter信息,但在注册时我正在使用Auth orm注册(我使用Model_Auth_User扩展了我的用户模型,否则它会产生问题而登录),但注册时,由于twitter不提供电子邮件(这是我认为的问题)验证失败,我需要知道是否可以删除所需电子邮件字段的验证

我正在添加我的代码以供参考

if(!empty($_GET['oauth_verifier']) && Session::instance()->get('oauth_token') && Session::instance()->get('oauth_token_secret'))
        {
            $twitteroauth = new TwitterOAuth(Kohana::$config->load('apiKey.twitter.TWITTER_CONSUMER_KEY'),Kohana::$config->load('apiKey.twitter.TWITTER_CONSUMER_SECRET'),Session::instance()->get('oauth_token'),Session::instance()->get('oauth_token_secret'));
            $access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);

            $user_info = $twitteroauth->get('account/verify_credentials');

            if(isset($user_info->error))
            {
                show_error("Some error has occured.");
            }
            else
            {
                $model = ORM::factory('user');

                $result = $model->where('twitter_id','=', $user_info->id)->find();

                if(!$result->id)
                {
                    $name = explode(' ',$user_info->name);
                    $model = ORM::factory('user');
                    $data['twitter_id'] = $user_info->id;
                    $data['first_name'] = $name[0];
                    $data['last_name'] = $name[1];
                    $data['username'] = $user_info->screen_name;                
                    $data['password'] = Auth::instance()->hash_password($user_info->screen_name);               
                    $model->values($data);
                    $result = $model->save();
                    $role = ORM::factory('role')->where('name', '=', 'login')->find();
                    $model->add('roles', $role);
                }


                if($result->id)
                {
                    //creating new ORM instance to use the result id 

                    $model = ORM::factory('user');
                    $user =  $model->find($result->id);
                    Auth::instance()->login($user->username,Auth::instance()->hash_password($user->username));
                    //Session::instance()->set('id', $model->id);
                    HTTP::redirect('user/dashboard');
                }
                else
                {
                    HTTP::redirect('/');
                }
            }
        }
        else
        {
            $twitteroauth = new TwitterOAuth(Kohana::$config->load('apiKey.twitter.TWITTER_CONSUMER_KEY'),Kohana::$config->load('apiKey.twitter.TWITTER_CONSUMER_SECRET'));

            $request_token = $twitteroauth->getRequestToken(URL::site());
            $url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);

            Session::instance()->set('oauth_token',$request_token['oauth_token']);
            Session::instance()->set('oauth_token_secret', $request_token['oauth_token_secret']);


            HTTP::redirect($url);
        }

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我不得不在Model_user

中的Model_auth_user下覆盖规则函数

以下是我的MOdel_user

<?php defined('SYSPATH') or die('No direct script access.');

class Model_User extends Model_Auth_User {


    public function rules()
    {
        return array(
            'username' => array(
                array('not_empty'),
                array('max_length', array(':value', 32)),
                array(array($this, 'unique'), array('username', ':value')),
            ),
            'password' => array(
                array('not_empty'),
            ),
            'email' => array(
                array('email'),
                array(array($this, 'unique'), array('email', ':value')),
            ),
        );
    }


}