class UserManager in Social Post 8.2
Same name and namespace in other branches
- 3.x src/User/UserManager.php \Drupal\social_post\User\UserManager
Manages database related tasks.
Hierarchy
- class \Drupal\social_api\User\UserManager implements UserManagerInterface
- class \Drupal\social_post\User\UserManager uses StringTranslationTrait
Expanded class hierarchy of UserManager
1 string reference to 'UserManager'
1 service uses UserManager
File
- src/
User/ UserManager.php, line 17
Namespace
Drupal\social_post\UserView source
class UserManager extends SocialApiUserManager {
use StringTranslationTrait;
/**
* The current logged in Drupal user.
*
* @var \Drupal\Core\Session\AccountProxy
*/
protected $currentUser;
/**
* Constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Used for loading and creating Social Post entities.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* Used to display messages to user.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* Used for logging errors.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* Used to get current active user.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger, LoggerChannelFactoryInterface $logger_factory, AccountProxyInterface $current_user) {
parent::__construct('social_post', $entity_type_manager, $messenger, $logger_factory);
$this->currentUser = $current_user;
}
/**
* Add user record in Social Post Entity.
*
* @param string $name
* The user name in the provider.
* @param int $user_id
* The Drupal User ID associated to the record.
* @param int|string $provider_user_id
* Unique Social ID returned by social network.
* @param string $url
* The URL to the profile in the provider.
* @param string $token
* Token to be used for autoposting.
*
* @return bool
* True if User record was created or False otherwise
*/
public function addUserRecord($name, $user_id, $provider_user_id, $url, $token) {
if ($this
->getDrupalUserId($provider_user_id)) {
return FALSE;
}
$values = [
'user_id' => $user_id,
'plugin_id' => $this->pluginId,
'provider_user_id' => $provider_user_id,
'name' => $name,
'token' => $token,
];
// If URL to profile is provided.
if ($url) {
$values['link'] = [
'uri' => $url,
'title' => $name,
];
}
try {
$user_info = SocialPost::create($values);
// Save the entity.
$user_info
->save();
} catch (\Exception $ex) {
$this->loggerFactory
->get($this
->getPluginId())
->error('Failed to add user record in Social Auth entity.
Exception: @message', [
'@message' => $ex
->getMessage(),
]);
$this->messenger
->addError($this
->t('You could not be authenticated, please contact the administrator.'));
return FALSE;
}
return TRUE;
}
/**
* Gets the Social Post records associated with a user and a provider.
*
* @param string $plugin_id
* The plugin for which to get the accounts.
* @param string|null $user_id
* The Drupal user ID.
*
* @return \Drupal\social_post\Entity\SocialPost[]
* An array of Social Post records associated with the user.
*/
public function getAccounts($plugin_id, $user_id = NULL) {
$storage = $this->entityTypeManager
->getStorage($this->entityType);
if (!$user_id) {
$user_id = $this->currentUser
->id();
}
// Get the accounts associated to the user.
$accounts = $storage
->loadByProperties([
'user_id' => $user_id,
'plugin_id' => $plugin_id,
]);
return $accounts;
}
/**
* Update token of a particular record.
*
* @param string $plugin_id
* Type of social network.
* @param string $provider_user_id
* Unique Social ID returned by social network.
* @param string $token
* Token provided by social_network.
*
* @return bool
* True if updated
* False otherwise
*/
public function updateToken($plugin_id, $provider_user_id, $token) {
/** @var \Drupal\social_post\Entity\SocialPost|false $social_post_user */
$social_post_user = current($this->entityTypeManager
->getStorage($this->entityType)
->loadByProperties([
'plugin_id' => $plugin_id,
'provider_user_id' => $provider_user_id,
]));
if ($social_post_user === FALSE) {
return FALSE;
}
try {
$social_post_user
->setToken($token)
->save();
return TRUE;
} catch (EntityStorageException $e) {
$this->loggerFactory
->get($this
->getPluginId())
->error('Failed to save user with updated token. Error @error', [
'@error' => $e
->getMessage(),
]);
return FALSE;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
UserManager:: |
protected | property | The current logged in Drupal user. | |
UserManager:: |
protected | property | The entity type. | |
UserManager:: |
protected | property | The Drupal Entity Manager. | |
UserManager:: |
protected | property | The Drupal logger factory. | |
UserManager:: |
protected | property | The Messenger service. | |
UserManager:: |
protected | property | The implementer plugin id. | |
UserManager:: |
public | function | Add user record in Social Post Entity. | |
UserManager:: |
public | function | Gets the Social Post records associated with a user and a provider. | |
UserManager:: |
public | function |
Gets the Drupal user id based on the provider user id. Overrides UserManagerInterface:: |
|
UserManager:: |
public | function |
Gets the implementer plugin id. Overrides UserManagerInterface:: |
|
UserManager:: |
public | function |
Sets the implementer plugin id. Overrides UserManagerInterface:: |
|
UserManager:: |
public | function | Update token of a particular record. | |
UserManager:: |
public | function |
Constructor. Overrides UserManager:: |