Skip to content

Instantly share code, notes, and snippets.

@zz
Created October 14, 2012 15:11
Show Gist options
  • Save zz/3888866 to your computer and use it in GitHub Desktop.
Save zz/3888866 to your computer and use it in GitHub Desktop.
Facbook App review codes
<?php
/**
* This is the model class for table "www_oauth".
*
* The followings are the available columns in table 'www_oauth':
* @property integer $id
* @property integer $user_id
* @property integer $service
* @property string $service_uid
*/
class Oauth extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Oauth the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'www_oauth';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('user_id, service, service_uid', 'required'),
array('user_id, service', 'numerical', 'integerOnly'=>true),
array('service_uid', 'length', 'max'=>64),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, user_id, service, service_uid', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'user_id' => 'User',
'service' => 'Service',
'service_uid' => 'Service Uid',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('service',$this->service);
$criteria->compare('service_uid',$this->service_uid,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public function linkedLogin($service, $service_uid)
{
if ($oauth = Oauth::model()->findByAttributes(array('service'=>$service, 'service_uid'=>$service_uid)))
{
if ($user = User::model()->findByPk($oauth->user_id))
{
User::model()->autoLogin($user->username ,$user->password);
return true;
}
}
return false;
}
public function linkAccout($user_id, $service, $service_uid)
{
$oauth = new Oauth;
$oauth->user_id = $user_id;
$oauth->service_uid = $service_uid;
$oauth->service = $service;
$oauth->save();
Yii::app()->amqp->exchange('social')->publish($service_uid, 'fb.link');
}
}
<?php
class OauthController extends Controller
{
/**
* FIX ME!!!
* If facebook email already have, it will 500 at FirstSetup Model.
* Please write new model function.
*/
public function actionFacebook()
{
if (!Yii::app()->user->isGuest)
$this->redirect(array('/settings/photos'));
Yii::import('application.extensions.oauth.facebook.Facebook');
$facebook = new Facebook(array(
'appId' => 'XXXX',
'secret' => 'XXXX',
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
else
$this->redirect(Yii::app()->homeUrl);
// Check account link in Model Oauth !!
if (Oauth::model()->linkedLogin(1, $user_profile['id']) )
$this->redirect('/settings/photos');
if ($user_profile)
{
$model = new FirstSetupForm;
$tmpUser = array();
$tmpUsername = str_replace('.', '', $user_profile['username']);
if (!$user=User::model()->findByAttributes(array('username'=>$tmpUsername)))
$tmpUser['username'] = $tmpUsername ;
else
$tmpUser['username'] = $tmpUsername.rand(0,9) ;
$tmpUser['password'] = CMyWeb::random();
$tmpUser['email'] = $user_profile['email'];
list($tmpUser['birthmonth'], $tmpUser['birthday'], $tmpUser['birthyear']) = explode('/', $user_profile['birthday']);
Yii::app()->user->setState('firstSetup', $tmpUser);
$model->gender = $user_profile['gender']=='female'?1:0;
$model->looking_for = $user_profile['gender']=='female'?0:1;
$location = Yii::app()->geoip->lookupLocation(Yii::app()->request->userHostAddress=='127.0.0.1'?'1.1.1.1':Yii::app()->request->userHostAddress);
$model->country = $location->countryCode;
$model->region = $location->region;
$model->city = $location->city;
$geo_location = Location::model()->findByAttributes(array('country'=>$location->countryCode, 'region'=>$location->region, 'city'=>$location->city), 'latitude LIKE :latitude', array(':latitude'=>$location->latitude));
$model->location=$geo_location->locId;
$model->profile_text = $user_profile['bio'];
$model->service_uid = $user_profile['id'];
$this->createAccount($model);
}
}
private function createAccount($model)
{
if($user_id = $model->createAccount())
{
// Linked Account Here
Oauth::model()->linkAccout($user_id, 1, $model->service_uid);
$this->redirect(array('settings/photos'));
exit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment