How to differentiate oauth social media plugin in YII2 - facebook

I have a website which using twitter, facebook or google to login to system.
I use oAuth and here is my code.
config
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'clientId' => 'asdsad',
'clientSecret' => 'xzxas',
],
'twitter' => [
'class' => 'yii\authclient\clients\Twitter',
'consumerKey' => 'sadsd',
'consumerSecret' => 'dasdasd',
],
],
],
controller
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'oAuthSuccess'],
],
];
}
public function oAuthSuccess($client) {
// get user data from client
$userAttributes = $client->getUserAttributes();
echo '<pre>';
print_r($userAttributes);
die;
The question is how do i know that which one of social media the user use to log to system?

To differentiate your oauth client you can put some instance condition as:--
public function oAuthSuccess($client) {
$reponse = $client->getUserAttributes();
$session = Yii::$app->session;
$token = $client->accessToken->params['access_token'];
$session->set('token' ,$token);
$id = ArrayHelper::getValue($reponse , 'id');
$session->set('auth_id', $id);
//Facebook Oauth
if($client instanceof \yii\authclient\clients\Facebook){
//Do Facebook Login
}
//Google Oauth
elseif($client instanceof \yii\authclient\clients\GoogleOAuth){
//Do Google Login Condition
}
}

public function oAuthSuccess($client) {
// get user data from client
$userAttributes = $client->getUserAttributes();
if($client->getName() == 'twitter'){
........
}else if($client->getName() == 'facebook'){
.........
}

Related

Yii2 redirecting postman request to a different route in the api rest

I am accessing a route by the postman in an api made in Yii2, but the code that I insert in the action that corresponds to that route is not working. Follow the request print:
postman-request-print
The request return was not meant to be the one in the image, because the code I put in the 'create' action was this:
<?php
namespace app\modules\api\controllers;
use Yii;
use app\modules\pesquisa_mercado\models\PontoDaPesquisa;
class PesquisaPontoController extends \yii\rest\ActiveController
{
public $modelClass = 'app\modules\pesquisa_mercado\models\PesquisaPonto';
public function behaviors()
{
$behaviors = parent::behaviors();
return $behaviors + [
[
'class' => \yii\filters\auth\HttpBearerAuth::className(),
'except' => ['options'],
],
'verbs' => [
'class' => \app\modules\api\behaviors\Verbcheck::className(),
'actions' => [
'index' => ['GET'],
'create' => ['POST'],
'update' => ['PUT'],
'view' => ['GET'],
'delete' => ['DELETE'],
'finalizar-pesquisa' => ['POST'],
],
],
];
}
public function actions()
{
$actions = parent::actions();
unset($actions['update']);
return $actions;
}
public function actionCreate()
{
die("Test"); // test inserted here
}
}
That is, the return was to be 'Test'. For some reason I don't know, the route is being redirected to another place.
I also discovered that the request goes through the getLinks () method present in the PesquisaPonto model:
<?php
namespace app\modules\pesquisa_mercado\models;
class PesquisaPonto extends \yii\db\ActiveRecord implements \yii\web\Linkable
{
/**
* #inheritdoc
*/
public static function tableName()
{
return '{{%pesquisa_ponto}}';
}
/**
* #inheritdoc
*/
public function getLinks() // the requisition also passes through here!
{
return [
Link::REL_SELF => Url::to(['pesquisa-ponto/view', 'id' => $this->id], true),
'index' => Url::to(['pesquisa-ponto'], true)
];
}
}
Also follow the configuration of urlManager:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => true,
'rules' => [
// Pontos de Pesquisa
// api/pesquisa-ponto
[
'class' => 'yii\rest\UrlRule',
'controller' => [
'api/pesquisa-ponto'
],
'pluralize' => false,
],
],
]
I still haven't found the reason why Yii2 is redirecting the route and not allowing the postman to access the 'create' action...
The actions() method in yii\rest\ActiveController looks like this
public function actions()
{
return [
// ...
'create' => [
'class' => 'yii\rest\CreateAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'scenario' => $this->createScenario,
],
// ...
];
}
In your implementation of actions() method you are only removing the configuration for update action but the configuration for create action is left untouched. That means that the action is run from the yii\rest\CreateAction not the actionCreate() method of the controller.
To run the action from the PesquisaPontoController::actionCreate() you have to unset the configuration for the create action as well.
public function actions()
{
$actions = parent::actions();
unset($actions['update'], $actions['create']);
return $actions;
}

ZEND 2 framework how to async navigation menu

I want to optimize page loading to asynchronously load navigation menu through Ajax.
Now it is working the standard way in layout.phtml using:
echo $this->navigation('CatalogNavigation')->menu()->setPartial('catalog_menu');
That line does not work in controller:
$view = $this->navigation('CatalogNavigation')->menu()->setPartial('catalog_menu'); //this line is not working
return new JsonModel(array('view' => $view()));
Here is what worked, the additional ->get('navigation'):
$navigation = $this->getServiceLocator()->get('viewHelperManager')->get('navigation');
$catalog_navigation = $navigation('CatalogNavigation');
$view = $catalog_navigation->menu()->setPartial('catalog_menu');
return new JsonModel(array( 'view' => (string)$view ));
Try code below:
//factory
class NavigationFactory extends DefaultNavigationFactory
{
protected function getName()
{
return 'navigation-example';
}
}
//module.config.php
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'factories' => array(
'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
'navigation-example' => 'Application\Factory\NavigationFactory'
),
),
// global.php:
return array(
'navigation' => [
'navigation-example' => [
[
'label' => 'Google',
'uri' => 'https://www.google.com',
'target' => '_blank'
],
[
'label' => 'Home',
'route' => 'home'
]
]
]
);
// in controller
$viewHelperManager = $this->getServiceLocator()->get('viewHelperManager');
$navigation = $viewHelperManager->get('navigation');
$catalog_navigation = $navigation('navigation-example');
$menu = (string) $catalog_navigation->menu();
return new JsonModel(compact('menu'));

Queue is created but never executed

I'm trying to trigger push notifications and emails in the background of my Lumen API. I created the WarningUser class that creates both queues:
<?php
namespace App\Utils;
use App\Jobs\ProcessNotification;
use App\Mail\AllMail;
use Illuminate\Support\Facades\Mail;
class WarningUser
{
public static function send($user, $message, $url, $data = [])
{
$url = env('APP_URL_FRONT') . $url;
dispatch(new ProcessNotification($user, $data, $message, $url));
$emailData = EmailTexts::texts('pt', $data)[$message];
Mail::to($user->email)->queue(new AllMail($emailData['title'], $emailData['content']));
return true;
}
}
First we have the ProcessNotification job, which connects to Firebase and sends the notification:
<?php
namespace App\Jobs;
use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\CloudMessage;
class ProcessNotification extends Job
{
protected $user = null;
protected $data = null;
protected $message = null;
protected $url = null;
public function __construct($user, $data, $message, $url)
{
$this->user = $user;
$this->data = $data;
$this->message = $message;
$this->url = $url;
}
public function handle()
{
if (\is_string($this->user->token) and $this->user->token !== '') {
$messaging = (new Factory())
->withServiceAccount(__DIR__.'/../../private-key.json')
->createMessaging();
$notificationData = NotificationTexts::texts('pt', $this->data)[$this->message];
$messageAlert = CloudMessage::withTarget('token', $this->user->token)
->withNotification(Notification::create($notificationData['title'], $notificationData['content']))
->withData([ 'url' => $this->url ]);
$messaging->send($messageAlert);
}
}
}
And finally AllMail that sends a simple email:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class AllMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
protected $title;
protected $body;
public function __construct($title, $body)
{
$this->title = $title;
$this->body = $body;
}
public function build()
{
return $this
->subject($this->title)
->view('email')
->with([
'title' => $this->title,
'body' => $this->body
]);
}
}
I've tested both codes without using queues, and they work, but when I put the queue it stops working. The processes are recorded in my database (mongodb):
But the queue is never processed, I tried to execute php artisan queue: work andphp artisan queue: listen, but neither case works.
The queue is never attempted to be processed, nor does it go to the failed_jobs table
My config / queue.php is as follows. And my .env is with QUEUE_CONNECTION = database
<?php
return [
'default' => env('QUEUE_CONNECTION', 'database'),
'connections' => [
'sync' => [
'driver' => 'sync',
],
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
],
'beanstalkd' => [
'driver' => 'beanstalkd',
'host' => 'localhost',
'queue' => 'default',
'retry_after' => 90,
'block_for' => 0,
],
'sqs' => [
'driver' => 'sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
'queue' => env('SQS_QUEUE', 'your-queue-name'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 90,
'block_for' => null,
],
],
'failed' => [
'driver' => env('QUEUE_FAILED_DRIVER', 'database'),
'database' => env('DB_CONNECTION', 'mongodb'),
'table' => 'failed_jobs',
],
];
Can someone help me? I no longer know what the error may be.
PS: At no time is a browser or console error displayed
I managed to solve, for Mongo we need to use some more settings:
The connection in queue.php must be:
'connections' => [
'database' => [
'driver' => 'mongodb',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
],
...
And we need to register the package provider that works with mongo:
Jenssegers\Mongodb\MongodbQueueServiceProvider::class,

How get user data with Yii2 bearer authentication?

I need to get the user data to use for the access control. I bearer authentication works fine, the user session is disabled.
public static function findIdentityByAccessToken ($token, $type = null) {
$user = static::findOne(['access_token' => $token, 'status' => self::STATUS_ACTIVE]);
if ( $user ) {
if ( $user->isAccessTokenValid() ) {
$user->last_request = time();
$user->save(false);
return $user;
}
}
return null;
}
The Yii::$app->user->id and Yii::$app->user->getId() return null because the session is disabled.
I need to use it in:
$behaviors['access'] = ['class' => AccessControl::class,
'only' => ['view'],
'rules' => [
[
'actions' => ['view'],
'allow' => false,
'roles' => User::isUserAdmin(Yii::$app->user->getId()),
],
[
'actions' => ['view'],
'allow' => true,
'roles' => User::isUserManager(Yii::$app->user->getId()),
],
],
];
I tried to save the user data or the token in public static variable in the user class, but the variable always null.

Magento 2 Plugins / Interceptors accessing and modifying $this object

I have a plugin that i want to modify functionality of a method within specific class in Magento 2 however am not quite sure on how to access the original object and return the modified data.
Original Method
protected function _initTotals()
{
$source = $this->getSource();
$this->_totals = [];
$this->_totals['subtotal'] = new \Magento\Framework\DataObject(
['code' => 'subtotal', 'value' => $source->getSubtotal(), 'label' => __('Subtotal')]
);
/**
* Add shipping
*/
if (!$source->getIsVirtual() && ((double)$source->getShippingAmount() || $source->getShippingDescription())) {
$this->_totals['shipping'] = new \Magento\Framework\DataObject(
[
'code' => 'shipping',
'field' => 'shipping_amount',
'value' => $this->getSource()->getShippingAmount(),
'label' => __('Shipping & Handling'),
]
);
}
/**
* Add discount
*/
if ((double)$this->getSource()->getDiscountAmount()) {
if ($this->getSource()->getDiscountDescription()) {
$discountLabel = __('Discount (%1)', $source->getDiscountDescription());
} else {
$discountLabel = __('Discount');
}
$this->_totals['discount'] = new \Magento\Framework\DataObject(
[
'code' => 'discount',
'field' => 'discount_amount',
'value' => $source->getDiscountAmount(),
'label' => $discountLabel,
]
);
}
$this->_totals['grand_total'] = new \Magento\Framework\DataObject(
[
'code' => 'grand_total',
'field' => 'grand_total',
'strong' => true,
'value' => $source->getGrandTotal(),
'label' => __('Grand Total'),
]
);
/**
* Base grandtotal
*/
if ($this->getOrder()->isCurrencyDifferent()) {
$this->_totals['base_grandtotal'] = new \Magento\Framework\DataObject(
[
'code' => 'base_grandtotal',
'value' => $this->getOrder()->formatBasePrice($source->getBaseGrandTotal()),
'label' => __('Grand Total to be Charged'),
'is_formated' => true,
]
);
}
return $this;
}
This i have set to have a plugin to modify functionality of method above with di.xml:
<type name="Magento\Sales\Block\Order\Totals">
<plugin disabled="false" name="Harrigo_EverDiscountLabel_Plugin_Magento_Sales_Block_Order_Totals" sortOrder="10" type="Harrigo\EverDiscountLabel\Plugin\Magento\Sales\Block\Order\Totals"/>
</type>
Plugin
class Totals
{
public function after_initTotals(
\Magento\Sales\Block\Order\Totals $subject,
$result
) {
if ((double)$subject->getSource()->getDiscountAmount() != 0 OR $subject->getSource()->getDiscountDescription() != null) {
if ($subject->getSource()->getDiscountDescription()) {
$discountLabel = __('Offer (%1)', $source->getDiscountDescription());
} else {
$discountLabel = __('Offer');
}
$subject->_totals['discount'] = new \Magento\Framework\DataObject(
[
'code' => 'discount',
'field' => 'discount_amount',
'value' => $source->getDiscountAmount(),
'label' => $discountLabel,
]
);
}
return $subject;
}
}
Have used $subject instead of $this within the plugin, this does not work for me however. How do I access the $this object within the plugin to add / overwrite $this->_totals['discount'] and return the updated $this object from within the plugin. I have it working fine with a standard preference but would rather use a plugin if possible.
I think you should check this before implementing above code.
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html
As per devdocs for Magento2 protected functions can not be intercepted so We can not use plugins for that.
May be that is causing issue in your case.
Hope this helps!