Browse Source

Merge pull request #25902 from nextcloud/techdept/psalm/loggerinterface/part2

LoggerInterface for provisioning API Controllers
tags/v22.0.0beta1
Roeland Jago Douma 3 years ago
parent
commit
b87e54bb1c
No account linked to committer's email address

+ 3
- 3
apps/provisioning_api/lib/Controller/GroupsController.php View File

use OCP\IConfig; use OCP\IConfig;
use OCP\IGroup; use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\L10N\IFactory; use OCP\L10N\IFactory;
use Psr\Log\LoggerInterface;


class GroupsController extends AUserData { class GroupsController extends AUserData {


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


public function __construct(string $appName, public function __construct(string $appName,
IUserSession $userSession, IUserSession $userSession,
AccountManager $accountManager, AccountManager $accountManager,
IFactory $l10nFactory, IFactory $l10nFactory,
ILogger $logger) {
LoggerInterface $logger) {
parent::__construct($appName, parent::__construct($appName,
$request, $request,
$userManager, $userManager,

+ 33
- 28
apps/provisioning_api/lib/Controller/UsersController.php View File

use OCP\IConfig; use OCP\IConfig;
use OCP\IGroup; use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUser; use OCP\IUser;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
use OCP\Security\Events\GenerateSecurePasswordEvent; use OCP\Security\Events\GenerateSecurePasswordEvent;
use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;
use Psr\Log\LoggerInterface;


class UsersController extends AUserData { class UsersController extends AUserData {


private $appManager; private $appManager;
/** @var IURLGenerator */ /** @var IURLGenerator */
protected $urlGenerator; protected $urlGenerator;
/** @var ILogger */
/** @var LoggerInterface */
private $logger; private $logger;
/** @var IFactory */ /** @var IFactory */
protected $l10nFactory; protected $l10nFactory;
IUserSession $userSession, IUserSession $userSession,
AccountManager $accountManager, AccountManager $accountManager,
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
ILogger $logger,
LoggerInterface $logger,
IFactory $l10nFactory, IFactory $l10nFactory,
NewUserMailHelper $newUserMailHelper, NewUserMailHelper $newUserMailHelper,
FederatedShareProviderFactory $federatedShareProviderFactory, FederatedShareProviderFactory $federatedShareProviderFactory,
} catch (\Exception $e) { } catch (\Exception $e) {
// Mail could be failing hard or just be plain not configured // Mail could be failing hard or just be plain not configured
// Logging error as it is the hardest of the two // Logging error as it is the hardest of the two
$this->logger->logException($e, [
'message' => "Unable to send the invitation mail to $email",
'level' => ILogger::ERROR,
'app' => 'ocs_api',
]);
$this->logger->error("Unable to send the invitation mail to $email",
[
'app' => 'ocs_api',
'exception' => $e,
]
);
} }
} }
} }


return new DataResponse(['id' => $userid]); return new DataResponse(['id' => $userid]);
} catch (HintException $e) { } catch (HintException $e) {
$this->logger->logException($e, [
'message' => 'Failed addUser attempt with hint exception.',
'level' => ILogger::WARN,
'app' => 'ocs_api',
]);
$this->logger->warning('Failed addUser attempt with hint exception.',
[
'app' => 'ocs_api',
'exception' => $e,
]
);
throw new OCSException($e->getHint(), 107); throw new OCSException($e->getHint(), 107);
} catch (OCSException $e) { } catch (OCSException $e) {
$this->logger->logException($e, [
'message' => 'Failed addUser attempt with ocs exeption.',
'level' => ILogger::ERROR,
'app' => 'ocs_api',
]);
$this->logger->warning('Failed addUser attempt with ocs exeption.',
[
'app' => 'ocs_api',
'exception' => $e,
]
);
throw $e; throw $e;
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->logException($e, [
'message' => 'Failed addUser attempt with exception.',
'level' => ILogger::ERROR,
'app' => 'ocs_api',
]);
$this->logger->error('Failed addUser attempt with exception.',
[
'app' => 'ocs_api',
'exception' => $e
]
);
throw new OCSException('Bad request', 101); throw new OCSException('Bad request', 101);
} }
} }
$emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
$this->newUserMailHelper->sendMail($targetUser, $emailTemplate); $this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->logException($e, [
'message' => "Can't send new user mail to $email",
'level' => ILogger::ERROR,
'app' => 'settings',
]);
$this->logger->error("Can't send new user mail to $email",
[
'app' => 'settings',
'exception' => $e,
]
);
throw new OCSException('Sending email failed', 102); throw new OCSException('Sending email failed', 102);
} }



+ 3
- 3
apps/provisioning_api/tests/Controller/GroupsControllerTest.php View File

use OCA\Provisioning_API\Controller\GroupsController; use OCA\Provisioning_API\Controller\GroupsController;
use OCP\Accounts\IAccountManager; use OCP\Accounts\IAccountManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\L10N\IFactory; use OCP\L10N\IFactory;
use OCP\UserInterface; use OCP\UserInterface;
use Psr\Log\LoggerInterface;


class GroupsControllerTest extends \Test\TestCase { class GroupsControllerTest extends \Test\TestCase {


protected $userSession; protected $userSession;
/** @var AccountManager|\PHPUnit\Framework\MockObject\MockObject */ /** @var AccountManager|\PHPUnit\Framework\MockObject\MockObject */
protected $accountManager; protected $accountManager;
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
protected $logger; protected $logger;
/** @var SubAdmin|\PHPUnit\Framework\MockObject\MockObject */ /** @var SubAdmin|\PHPUnit\Framework\MockObject\MockObject */
protected $subAdminManager; protected $subAdminManager;
$this->userSession = $this->createMock(IUserSession::class); $this->userSession = $this->createMock(IUserSession::class);
$this->accountManager = $this->createMock(AccountManager::class); $this->accountManager = $this->createMock(AccountManager::class);
$this->l10nFactory = $this->createMock(IFactory::class); $this->l10nFactory = $this->createMock(IFactory::class);
$this->logger = $this->createMock(ILogger::class);
$this->logger = $this->createMock(LoggerInterface::class);


$this->subAdminManager = $this->createMock(SubAdmin::class); $this->subAdminManager = $this->createMock(SubAdmin::class);



+ 10
- 9
apps/provisioning_api/tests/Controller/UsersControllerTest.php View File

use OCP\IConfig; use OCP\IConfig;
use OCP\IGroup; use OCP\IGroup;
use OCP\IL10N; use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUser; use OCP\IUser;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
use OCP\UserInterface; use OCP\UserInterface;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase; use Test\TestCase;


class UsersControllerTest extends TestCase { class UsersControllerTest extends TestCase {
protected $groupManager; protected $groupManager;
/** @var IUserSession|MockObject */ /** @var IUserSession|MockObject */
protected $userSession; protected $userSession;
/** @var ILogger|MockObject */
/** @var LoggerInterface|MockObject */
protected $logger; protected $logger;
/** @var UsersController|MockObject */ /** @var UsersController|MockObject */
protected $api; protected $api;
$this->appManager = $this->createMock(IAppManager::class); $this->appManager = $this->createMock(IAppManager::class);
$this->groupManager = $this->createMock(Manager::class); $this->groupManager = $this->createMock(Manager::class);
$this->userSession = $this->createMock(IUserSession::class); $this->userSession = $this->createMock(IUserSession::class);
$this->logger = $this->createMock(ILogger::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->request = $this->createMock(IRequest::class); $this->request = $this->createMock(IRequest::class);
$this->accountManager = $this->createMock(AccountManager::class); $this->accountManager = $this->createMock(AccountManager::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
->will($this->throwException($exception)); ->will($this->throwException($exception));
$this->logger $this->logger
->expects($this->once()) ->expects($this->once())
->method('logException')
->with($exception, [
'message' => 'Failed addUser attempt with exception.',
'level' => ILogger::ERROR,
'app' => 'ocs_api',
]);
->method('error')
->with('Failed addUser attempt with exception.',
[
'app' => 'ocs_api',
'exception' => $exception
]
);
$loggedInUser = $this->getMockBuilder(IUser::class) $loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();

Loading…
Cancel
Save