class UloginController in uLogin (advanced version) 8
Controller routines for user routes.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\ulogin\Controller\UloginController
Expanded class hierarchy of UloginController
File
- src/
Controller/ UloginController.php, line 17
Namespace
Drupal\ulogin\ControllerView source
class UloginController extends ControllerBase {
/**
* Returns report.
*
* @return array
* Report table.
*/
public function uloginReport() {
$providers = UloginHelper::providersList();
$header = [
$this
->t('Authentication provider'),
$this
->t('Users count'),
];
$rows = [];
$query = \Drupal::database()
->select('ulogin_identity', 'ul_id');
$query
->addField('ul_id', 'network', 'network');
$query
->addExpression('COUNT(ulogin_uid)', 'count');
$query
->groupBy('network');
$results = $query
->execute()
->fetchAllAssoc('network', \PDO::FETCH_ASSOC);
foreach ($results as $result) {
$rows[] = [
$providers[$result['network']],
$result['count'],
];
}
$build = [];
$build['report'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
];
return $build;
}
/**
* Call back for login and registration.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* Redirect.
*/
public function uloginCallback() {
$get_token = \Drupal::request()->query
->get('token');
$post_token = \Drupal::request()->request
->get('token');
if (!empty($post_token) || !empty($get_token)) {
$token = !empty($post_token) ? $post_token : $get_token;
$data_raw = \Drupal::httpClient()
->get('http://ulogin.ru/token.php?token=' . $token . '&host=' . $_SERVER['HTTP_HOST']);
$data = $data_raw
->getBody()
->getContents();
if (!empty($data_raw
->getStatusCode() != 200)) {
\Drupal::logger('ulogin')
->warning($data);
drupal_set_message($data, 'error');
throw new AccessDeniedHttpException();
}
$data = json_decode($data, TRUE);
// Check for error.
if (!empty($data['error'])) {
\Drupal::logger('ulogin')
->warning($data['error']);
drupal_set_message($data['error'], 'error');
throw new AccessDeniedHttpException();
}
// Validate that returned data contains 'network' and 'uid' keys.
if (empty($data['network']) || empty($data['uid'])) {
\Drupal::logger('ulogin')
->warning('Empty data =>' . json_encode($data));
drupal_set_message('something is wrong, try again later', 'error');
throw new AccessDeniedHttpException();
}
// Remove 'access_token' property.
unset($data['access_token']);
}
else {
drupal_set_message('no token given', 'error');
throw new AccessDeniedHttpException();
}
$user = \Drupal::currentUser();
// User is already logged in, tries to add new identity.
if ($user
->isAuthenticated()) {
// Identity is already registered.
if ($identity = UloginHelper::identityLoad($data)) {
// Registered to this user.
if ($user
->id() == $identity['uid']) {
drupal_set_message($this
->t('You have already registered this identity.'));
}
else {
drupal_set_message($this
->t('This identity is registered to another user.'), 'error');
}
return new RedirectResponse(Url::fromRoute('user.page')
->toString());
}
else {
UloginHelper::identitySave($data);
drupal_set_message($this
->t('New identity added.'));
return new RedirectResponse(Url::fromRoute('user.page')
->toString());
}
}
$vars = \Drupal::config('ulogin.settings')
->getRawData();
if ($identity = UloginHelper::identityLoad($data)) {
// Check if user is blocked.
if (UloginHelper::isUserBlockedByUid($identity['uid'])) {
drupal_set_message($this
->t('Your account has not been activated or is blocked.'), 'error');
}
else {
$user = User::load($identity['uid']);
user_login_finalize($user);
}
}
elseif ((array_key_exists('duplicate_emails', $vars) ? $vars['duplicate_emails'] : 1) && !empty($data['email']) && ($account = user_load_by_mail($data['email']))) {
drupal_set_message($this
->t('You are trying to login with email address of another user.'), 'error');
$ulogin = \Drupal::service('user.data')
->get('ulogin', $account
->id());
if (!empty($ulogin)) {
$providers = UloginHelper::providersList();
drupal_set_message($this
->t('If you are completely sure it is your email address, try to login through %network.', [
'%network' => $providers[$ulogin['network']],
]), 'status');
}
else {
drupal_set_message($this
->t('If you are completely sure it is your email address, try to login using your username and password on this site. If you don\'t remember your password - <a href="@password">request new password</a>.', [
'@password' => Url::fromRoute('user.pass')
->toString(),
]));
}
}
else {
global $_ulogin_data;
$_ulogin_data = $data;
\Drupal::service('externalauth.externalauth')
->loginRegister(UloginHelper::makeUsername($data), 'ulogin');
UloginHelper::userSave($data);
}
return new RedirectResponse(Url::fromRoute('user.page')
->toString());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
40 |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UloginController:: |
public | function | Call back for login and registration. | |
UloginController:: |
public | function | Returns report. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |