Commit 14ab6d1e authored by Open Source Developer's avatar Open Source Developer

cond

parent f2d6aa6f
<?php
namespace Drupal\oauthost\Controller;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\ControllerBase;
use OAuth;
use OAuthException;
use Drupal\user\Entity\User;
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
class OAuthLogin extends ControllerBase
{
protected $entity_query;
protected $entityTypeManager;
protected $logger;
protected $connection;
protected $consumer_key = 'tc97t89';
protected $consumer_secret = 'xr7tgt9AbK3';
protected $request_token_url;
protected $user_authorization_url;
protected $access_token_url;
protected $signature_method;
protected $api_url;
protected $callback_url;
protected $logout_url;
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
QueryFactory $entity_query,
Connection $connection,
LoggerChannelFactoryInterface $loggerChannel)
{
$this->entityTypeManager = $entityTypeManager;
$this->entity_query = $entity_query;
$this->connection = $connection;
$this->logger = $loggerChannel->get('oauthost');
}
public static function create(ContainerInterface $container)
{
return new static(
$container->get('entity.manager'),
$container->get('entity.query'),
$container->get('database'),
$container->get('logger.factory')
);
}
public function loginGo(Request $request)
{
$ostauthConfigs = $this->entityTypeManager->getStorage('oauthost_config')->loadByProperties(array('name' => 'oauthost_taxisnet_config'));
$ostauthConfig = reset($ostauthConfigs);
if ($ostauthConfig) {
$this->consumer_key = $ostauthConfig->consumer_key->value;
$this->consumer_secret = $ostauthConfig->consumer_secret->value;
$this->request_token_url = $ostauthConfig->request_token_url->value;
$this->user_authorization_url = $ostauthConfig->user_authorization_url->value;
$this->access_token_url = $ostauthConfig->access_token_url->value;
$this->signature_method = $ostauthConfig->signature_method->value;
$this->api_url = $ostauthConfig->api_url->value;
$this->callback_url = $ostauthConfig->callback_url->value;
$this->logout_url = $ostauthConfig->logout_url->value;
} else {
$response = new Response();
$response->setContent('forbidden');
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
$customUser = null;
$customUsers = null;
$userId = null;
$user = null;
try {
$oauth = new OAuth($this->consumer_key, $this->consumer_secret, OAUTH_SIG_METHOD_PLAINTEXT, OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();
$uniqid = uniqid('sid');
$requestToken = $oauth->getRequestToken($this->request_token_url, $this->callback_url . '?sid_ost=' . $uniqid);
// store auth token
$oauthostSession = $this->entityTypeManager()->getStorage('oauthost_session')->create(array(
// 'langcode' => $language_interface->getId(),
'langcode' => 'el',
'user_id' => \Drupal::currentUser()->id(),
'name' => $uniqid,
'request_token' => $requestToken['oauth_token'],
'request_token_secret' => $requestToken['oauth_token_secret'],
'status' => 1
));
$oauthostSession->save();
header('Location: '.$this->user_authorization_url.'?oauth_token='.$requestToken['oauth_token']);
$this->logger->warning('redirected to:'.$this->user_authorization_url.'?oauth_token='.$requestToken['oauth_token']);
exit;
} catch (OAuthException $e) {
$this->logger->warning($e->getMessage());
$response = new Response();
$response->setContent('forbidden');
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
}
<?php
namespace Drupal\oauthost\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Provides an interface for defining OAuthOST Config entities.
*
* @ingroup oauthost
*/
interface OAuthOSTConfigInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface {
// Add get/set methods for your configuration properties here.
/**
* Gets the OAuthOST Config name.
*
* @return string
* Name of the OAuthOST Config.
*/
public function getName();
/**
* Sets the OAuthOST Config name.
*
* @param string $name
* The OAuthOST Config name.
*
* @return \Drupal\oauthost\Entity\OAuthOSTConfigInterface
* The called OAuthOST Config entity.
*/
public function setName($name);
/**
* Gets the OAuthOST Config creation timestamp.
*
* @return int
* Creation timestamp of the OAuthOST Config.
*/
public function getCreatedTime();
/**
* Sets the OAuthOST Config creation timestamp.
*
* @param int $timestamp
* The OAuthOST Config creation timestamp.
*
* @return \Drupal\oauthost\Entity\OAuthOSTConfigInterface
* The called OAuthOST Config entity.
*/
public function setCreatedTime($timestamp);
/**
* Returns the OAuthOST Config published status indicator.
*
* Unpublished OAuthOST Config are only visible to restricted users.
*
* @return bool
* TRUE if the OAuthOST Config is published.
*/
public function isPublished();
/**
* Sets the published status of a OAuthOST Config.
*
* @param bool $published
* TRUE to set this OAuthOST Config to published, FALSE to set it to unpublished.
*
* @return \Drupal\oauthost\Entity\OAuthOSTConfigInterface
* The called OAuthOST Config entity.
*/
public function setPublished($published);
}
<?php
namespace Drupal\oauthost\Entity;
use Drupal\views\EntityViewsData;
/**
* Provides Views data for OAuthOST Config entities.
*/
class OAuthOSTConfigViewsData extends EntityViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
// Additional information for Views integration, such as table joins, can be
// put here.
return $data;
}
}
<?php
namespace Drupal\oauthost\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Provides an interface for defining OAuthOST Session entities.
*
* @ingroup oauthost
*/
interface OAuthOSTSessionInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface {
// Add get/set methods for your configuration properties here.
/**
* Gets the OAuthOST Session name.
*
* @return string
* Name of the OAuthOST Session.
*/
public function getName();
/**
* Sets the OAuthOST Session name.
*
* @param string $name
* The OAuthOST Session name.
*
* @return \Drupal\oauthost\Entity\OAuthOSTSessionInterface
* The called OAuthOST Session entity.
*/
public function setName($name);
/**
* Gets the OAuthOST Session creation timestamp.
*
* @return int
* Creation timestamp of the OAuthOST Session.
*/
public function getCreatedTime();
/**
* Sets the OAuthOST Session creation timestamp.
*
* @param int $timestamp
* The OAuthOST Session creation timestamp.
*
* @return \Drupal\oauthost\Entity\OAuthOSTSessionInterface
* The called OAuthOST Session entity.
*/
public function setCreatedTime($timestamp);
/**
* Returns the OAuthOST Session published status indicator.
*
* Unpublished OAuthOST Session are only visible to restricted users.
*
* @return bool
* TRUE if the OAuthOST Session is published.
*/
public function isPublished();
/**
* Sets the published status of a OAuthOST Session.
*
* @param bool $published
* TRUE to set this OAuthOST Session to published, FALSE to set it to unpublished.
*
* @return \Drupal\oauthost\Entity\OAuthOSTSessionInterface
* The called OAuthOST Session entity.
*/
public function setPublished($published);
}
<?php
namespace Drupal\oauthost\Entity;
use Drupal\views\EntityViewsData;
/**
* Provides Views data for OAuthOST Session entities.
*/
class OAuthOSTSessionViewsData extends EntityViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
// Additional information for Views integration, such as table joins, can be
// put here.
return $data;
}
}
<?php
namespace Drupal\oauthost\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
/**
* Defines the OAuthOST Session entity.
*
* @ingroup oauthost
*
* @ContentEntityType(
* id = "oauthost_session",
* label = @Translation("OAuthOST Session"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\oauthost\OAuthOSTSessionListBuilder",
* "views_data" = "Drupal\oauthost\Entity\OAuthOSTSessionViewsData",
*
* "form" = {
* "default" = "Drupal\oauthost\Form\OAuthOSTSessionForm",
* "add" = "Drupal\oauthost\Form\OAuthOSTSessionForm",
* "edit" = "Drupal\oauthost\Form\OAuthOSTSessionForm",
* "delete" = "Drupal\oauthost\Form\OAuthOSTSessionDeleteForm",
* },
* "access" = "Drupal\oauthost\OAuthOSTSessionAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\oauthost\OAuthOSTSessionHtmlRouteProvider",
* },
* },
* base_table = "oauthost_session",
* admin_permission = "administer oauthost session entities",
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "status" = "status",
* },
* links = {
* "canonical" = "/admin/structure/oauthost_session/{oauthost_session}",
* "add-form" = "/admin/structure/oauthost_session/add",
* "edit-form" = "/admin/structure/oauthost_session/{oauthost_session}/edit",
* "delete-form" = "/admin/structure/oauthost_session/{oauthost_session}/delete",
* "collection" = "/admin/structure/oauthost_session",
* },
* field_ui_base_route = "oauthost_session.settings"
* )
*/
class OAuthOSTSession extends ContentEntityBase implements OAuthOSTSessionInterface {
use EntityChangedTrait;
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += array(
'user_id' => \Drupal::currentUser()->id(),
);
}
/**
* {@inheritdoc}
*/
public function getName() {
return $this->get('name')->value;
}
/**
* {@inheritdoc}
*/
public function setName($name) {
$this->set('name', $name);
return $this;
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
$this->set('created', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('user_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('user_id')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('user_id', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('user_id', $account->id());
return $this;
}
/**
* {@inheritdoc}
*/
public function isPublished() {
return (bool) $this->getEntityKey('status');
}
/**
* {@inheritdoc}
*/
public function setPublished($published) {
$this->set('status', $published ? TRUE : FALSE);
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the OAuthOST Session entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
))
->setDisplayOptions('form', array(
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => array(
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
),
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('OAuthOST session id'))
->setDescription(t('The name of the OAuthOST Session entity.'))
->setSettings(array(
'max_length' => 200,
'text_processing' => 0,
))
->setDefaultValue('')
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -4,
))
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => -4,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['request_token'] = BaseFieldDefinition::create('string')
->setLabel(t('Request Token'))
->setDescription(t('The Request Token.'))
->setSettings(array(
'max_length' => 500,
'text_processing' => 0,
))
->setDefaultValue('')
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -4,
))
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => -4,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['request_token_secret'] = BaseFieldDefinition::create('string')
->setLabel(t('Request Token Secret'))
->setDescription(t('The Request Token Secret.'))
->setSettings(array(
'max_length' => 500,
'text_processing' => 0,
))
->setDefaultValue('')
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -4,
))
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => -4,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the OAuthOST Session is published.'))
->setDefaultValue(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
return $fields;
}
}
<?php
namespace Drupal\oauthost\Form;
use Drupal\Core\Entity\ContentEntityDeleteForm;
/**
* Provides a form for deleting OAuthOST Config entities.
*
* @ingroup oauthost
*/
class OAuthOSTConfigDeleteForm extends ContentEntityDeleteForm {
}
<?php
namespace Drupal\oauthost\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Form controller for OAuthOST Config edit forms.
*
* @ingroup oauthost
*/
class OAuthOSTConfigForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/* @var $entity \Drupal\oauthost\Entity\OAuthOSTConfig */
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = &$this->entity;
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
drupal_set_message($this->t('Created the %label OAuthOST Config.', [
'%label' => $entity->label(),
]));
break;
default:
drupal_set_message($this->t('Saved the %label OAuthOST Config.', [
'%label' => $entity->label(),
]));
}
$form_state->setRedirect('entity.oauthost_config.canonical', ['oauthost_config' => $entity->id()]);
}
}
<?php
namespace Drupal\oauthost\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class OAuthOSTConfigSettingsForm.
*
* @package Drupal\oauthost\Form
*
* @ingroup oauthost
*/
class OAuthOSTConfigSettingsForm extends FormBase {
/**