User.php in Rocket.Chat 8.2
File
modules/rocket_chat_api/src/RocketChat/Element/User.php
View source
<?php
namespace Drupal\rocket_chat_api\RocketChat\Element;
use Drupal;
use Drupal\rocket_chat_api\RocketChat\ApiClient;
use Drupal\rocket_chat_api\RocketChat\Collection\Users;
use Drupal\rocket_chat_api\RocketChat\RocketChatStateinterface;
use Exception;
class User {
private $User = NULL;
protected $name = NULL;
protected $email = NULL;
protected $username = NULL;
protected $state;
protected $Logger;
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
return $this;
}
public function getEmail() {
return $this->email;
}
public function setEmail($email) {
$this->email = $email;
return $this;
}
public function getUsername() {
return $this->username;
}
public function setUsername($username) {
$this->username = $username;
return $this;
}
public function __construct(RocketChatStateinterface $state, $username = NULL, $email = NULL, $name = NULL) {
$this->state = $state;
$this->username = $username;
$this->email = $email;
$this->name = $name;
$this->Logger = Drupal::logger("Rocket Chat API: User");
}
private function isInList($list) {
return !is_null($this
->getFromList($list));
}
private function getFromList($list) {
foreach ($list as $user) {
$found = false;
if (!empty($this->username) && $user["username"] == $this->username) {
$found = TRUE;
}
if (!empty($this->email)) {
if (isset($user['emails'])) {
foreach ($user['emails'] as $email) {
if ($email['address'] == $this->email) {
$found = TRUE;
return $user;
}
}
}
else {
if (!(strcmp($user['type'], "bot") === 0)) {
$this->Logger
->warning("User is missing the email property |" . json_encode($user, JSON_PRETTY_PRINT));
}
}
}
if ($found) {
return $user;
}
}
return NULL;
}
public function getUserProxy(ApiClient $apiClient) {
if (empty($this->User)) {
$users = [];
if ($this
->isEmpty()) {
return NULL;
}
$usersState = new Users($this->state, $apiClient);
$users = $usersState
->getCache();
if (!$this
->isInList($users)) {
$options = [];
$options['json'] = [];
$options['json']['email'] = $this->email;
$options['json']['name'] = $this->name;
$options['json']['password'] = bin2hex(random_bytes(512));
$options['json']['username'] = $this->username;
$options['json']['verified'] = TRUE;
$ret = $apiClient
->postToRocketChat("users.create", $options);
}
else {
$found = 0;
$userCache = $this
->getFromList($users);
if (empty($userCache)) {
$ret = $apiClient
->getFromRocketChat("users.info", [
"query" => [
"username" => $this->username,
],
]);
}
else {
$ret = [];
$ret['body'] = [];
$ret['body']['user'] = $userCache;
}
}
$this->User = $ret['body']['user'];
return $this->User;
}
else {
return null;
}
}
public function isEmpty() {
$empty = TRUE;
$email = FALSE;
$username = FALSE;
$name = FALSE;
if (!empty($this->email)) {
$email = TRUE;
}
if (!empty($this->username)) {
$username = TRUE;
}
if (!empty($this->name)) {
$name = TRUE;
}
if ($email && $username && $name) {
$empty = FALSE;
}
return $empty;
}
public function getUser() {
return $this->User;
}
}