Skip to content

Commit

Permalink
Merge pull request #36 from nextcloud/techdebt/noid/ocp-n-deprecation
Browse files Browse the repository at this point in the history
Deprecations and cleanup
  • Loading branch information
blizzz authored May 7, 2021
2 parents 500b55a + 9f409fe commit 55a3eb2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 63 deletions.
4 changes: 3 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@

class Application extends App {

public const APP_ID = 'globalsiteselector';

public function __construct(array $urlParams = array()) {
parent::__construct('globalsiteselector', $urlParams);
parent::__construct(self::APP_ID, $urlParams);

$container = $this->getContainer();

Expand Down
148 changes: 86 additions & 62 deletions lib/Slave.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@

namespace OCA\GlobalSiteSelector;


use OC\Accounts\AccountManager;
use OCP\Federation\ICloudIdManager;
use Exception;
use OCA\GlobalSiteSelector\AppInfo\Application;
use OCP\Accounts\IAccountManager;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserManager;
use Firebase\JWT\JWT;
use Psr\Log\LoggerInterface;

class Slave {

/** @var AccountManager */
/** @var IAccountManager */
private $accountManager;

/** @var IUserManager */
Expand All @@ -43,12 +43,9 @@ class Slave {
/** @var IClientService */
private $clientService;

/** @var ILogger */
/** @var LoggerInterface */
private $logger;

/** @var ICloudIdManager */
private $cloudIdManager;

/** @var string */
private $lookupServer;

Expand All @@ -71,30 +68,18 @@ class Slave {
/** @var IConfig */
private $config;

/**
* Slave constructor.
*
* @param AccountManager $accountManager
* @param IUserManager $userManager
* @param IClientService $clientService
* @param GlobalSiteSelector $gss
* @param ILogger $logger
* @param ICloudIdManager $cloudIdManager
* @param IConfig $config
*/
public function __construct(AccountManager $accountManager,
IUserManager $userManager,
IClientService $clientService,
GlobalSiteSelector $gss,
ILogger $logger,
ICloudIdManager $cloudIdManager,
IConfig $config
public function __construct(
IAccountManager $accountManager,
IUserManager $userManager,
IClientService $clientService,
GlobalSiteSelector $gss,
LoggerInterface $logger,
IConfig $config
) {
$this->accountManager = $accountManager;
$this->userManager = $userManager;
$this->clientService = $clientService;
$this->logger = $logger;
$this->cloudIdManager = $cloudIdManager;
$this->lookupServer = $gss->getLookupServerUrl();
$this->operationMode = $gss->getMode();
$this->authKey = $gss->getJwtKey();
Expand All @@ -104,14 +89,19 @@ public function __construct(AccountManager $accountManager,
$this->config = $config;
}

public function createUser(array $params) {
if ($this->checkConfiguration() === false) {
public function createUser(array $params): void {
if ($this->checkConfiguration() === false) {
return;
}

$uid = $params['uid'];

$this->logger->debug('Adding new user: ' . $uid);
$this->logger->debug('Adding new user: {uid}',
[
'app' => Application::APP_ID,
'uid' => $uid,
]
);

$user = $this->userManager->get($uid);
$userData = [];
Expand All @@ -126,18 +116,21 @@ public function createUser(array $params) {
*
* @param IUser $user
*/
public function updateUser(IUser $user) {
if ($this->checkConfiguration() === false) {
public function updateUser(IUser $user): void {
if ($this->checkConfiguration() === false) {
return;
}

$this->logger->debug('Updating user: ' . $user->getUID());
$this->logger->debug('Updating user: {uid}',
[
'app' => Application::APP_ID,
'uid' => $user->getUID(),
]
);

$userData = [];
if ($user !== null) {
$userData[$user->getCloudId()] = $this->getAccountData($user);
$this->addUsers($userData);
}
$userData[$user->getCloudId()] = $this->getAccountData($user);
$this->addUsers($userData);
}

/**
Expand All @@ -147,7 +140,7 @@ public function updateUser(IUser $user) {
*
* @param array $params
*/
public function preDeleteUser(array $params) {
public function preDeleteUser(array $params): void {
$uid = $params['uid'];
$user = $this->userManager->get($uid);
if ($user !== null) {
Expand All @@ -160,14 +153,19 @@ public function preDeleteUser(array $params) {
*
* @param array $params
*/
public function deleteUser(array $params) {
if ($this->checkConfiguration() === false) {
public function deleteUser(array $params): void {
if ($this->checkConfiguration() === false) {
return;
}

$uid = $params['uid'];

$this->logger->debug('Removing user: ' . $uid);
$this->logger->debug('Removing user: {uid}',
[
'app' => Application::APP_ID,
'uid' => $uid,
]
);

if (isset(self::$toRemove[$uid])) {
$this->removeUsers([self::$toRemove[$uid]]);
Expand All @@ -179,8 +177,8 @@ public function deleteUser(array $params) {
* update the lookup server with all known users on this instance. This
* is triggered by a cronjob
*/
public function batchUpdate() {
if ($this->checkConfiguration() === false) {
public function batchUpdate(): void {
if ($this->checkConfiguration() === false) {
return;
}

Expand Down Expand Up @@ -209,8 +207,8 @@ public function batchUpdate() {
* @param IUser $user
* @return array
*/
protected function getAccountData(IUser $user) {
$rawData = $this->accountManager->getUser($user);
protected function getAccountData(IUser $user): array {
$rawData = $this->accountManager->getAccount($user);
$data = [];
foreach ($rawData as $key => $value) {
if ($key === 'displayname') {
Expand All @@ -230,10 +228,15 @@ protected function getAccountData(IUser $user) {
*
* @param array $users
*/
protected function addUsers(array $users) {
protected function addUsers(array $users): void {
$dataBatch = ['authKey' => $this->authKey, 'users' => $users];

$this->logger->debug('Batch updating users: ' . json_encode($users));
$this->logger->debug('Batch updating users: {users}',
[
'app' => Application::APP_ID,
'users' => $users,
]
);

$httpClient = $this->clientService->newClient();
try {
Expand All @@ -244,8 +247,13 @@ protected function addUsers(array $users) {
'connect_timeout' => 3,
]
);
} catch (\Exception $e) {
$this->logger->logException($e, ['message' => 'Could not send user to lookup server', 'app' => 'globalsiteselector', 'level' => \OCP\Util::WARN]);
} catch (Exception $e) {
$this->logger->warning('Could not send user to lookup server',
[
'app' => Application::APP_ID,
'exception' => $e,
]
);
}
}

Expand All @@ -254,10 +262,15 @@ protected function addUsers(array $users) {
*
* @param array $users
*/
protected function removeUsers(array $users) {
protected function removeUsers(array $users): void {
$dataBatch = ['authKey' => $this->authKey, 'users' => $users];

$this->logger->debug('Batch deleting users: ' . json_encode($users));
$this->logger->debug('Batch deleting users: {users}',
[
'app' => Application::APP_ID,
'users' => $users,
]
);

$httpClient = $this->clientService->newClient();
try {
Expand All @@ -268,35 +281,43 @@ protected function removeUsers(array $users) {
'connect_timeout' => 3,
]
);
} catch (\Exception $e) {
$this->logger->logException($e, ['message' => 'Could not remove user from the lookup server', 'app' => 'globalsiteselector', 'level' => \OCP\Util::WARN]);
} catch (Exception $e) {
$this->logger->warning('Could not remove user from the lookup server',
[
'app' => Application::APP_ID,
'exception' => $e,
]
);
}
}

protected function checkConfiguration() {
protected function checkConfiguration(): bool {
if (empty($this->lookupServer)
|| empty($this->operationMode)
|| empty($this->authKey)
) {
$this->logger->error('global side selector app not configured correctly', ['app' => 'globalsiteselector']);
$this->logger->error('global site selector app not configured correctly',
[
'app' => Application::APP_ID,
]
);
return false;
}

return true;
}

/**
* Operation mode - slave or master
* @return string
*/
public function getOperationMode() {
public function getOperationMode(): string {
return $this->operationMode;
}

/**
* send user back to master
*/
public function handleLogoutRequest() {

$token = ['logout' => 'true',
'exp' => time() + 300, // expires after 5 minute
];
Expand All @@ -305,14 +326,17 @@ public function handleLogoutRequest() {
$location = $this->config->getSystemValue('gss.master.url', '');

if ($location === '') {
$this->logger->error('Can not redirect to master for logout, "gss.master.url" not set in config.php');
$this->logger->error('Can not redirect to master for logout, "gss.master.url" not set in config.php',
[
'app' => Application::APP_ID,
]
);
return;
}

$redirectUrl = $location . '/index.php/apps/globalsiteselector/autologout?jwt=' . $jwt;

header('Location: ' . $redirectUrl);
die();

}
}
}

0 comments on commit 55a3eb2

Please sign in to comment.