Skip to content

Commit

Permalink
fixs
Browse files Browse the repository at this point in the history
  • Loading branch information
docjyJ committed Oct 5, 2024
1 parent 76e0387 commit bc00121
Show file tree
Hide file tree
Showing 20 changed files with 692 additions and 656 deletions.
131 changes: 68 additions & 63 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

namespace OCA\Stalwart\Controller;

use OCA\Stalwart\Db\AccountManager;
use OCA\Stalwart\Db\ConfigManager;
use OCA\Stalwart\Db\EmailManager;
use OCA\Stalwart\Models\AccountEntity;
use OCA\Stalwart\Models\AccountsType;
use OCA\Stalwart\Models\EmailEntity;
use OCA\Stalwart\Models\EmailType;
use OCA\Stalwart\ResponseDefinitions;
use OCA\Stalwart\Services\ConfigService;
use OCA\Stalwart\Services\UsersService;
use OCA\Stalwart\Settings\Admin;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
Expand All @@ -18,6 +23,7 @@
use OCP\AppFramework\OCSController;
use OCP\DB\Exception;
use OCP\IRequest;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -27,19 +33,21 @@
*/
class ApiController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private readonly ConfigService $configService,
private readonly UsersService $usersService,
private readonly LoggerInterface $logger,
string $appName,
IRequest $request,
private readonly ConfigManager $configManager,
private readonly AccountManager $accountManager,
private readonly EmailManager $emailManager,
private readonly IUserManager $userManager,
private readonly LoggerInterface $logger,
) {
parent::__construct($appName, $request);
}


/**
* List all available servers
* @return DataResponse<Http::STATUS_OK, list<StalwartServerConfig>, array{}>
* @return DataResponse<Http::STATUS_OK, StalwartServerConfig[], array{}>
* @throws OCSException If an error occurs
*
* 200: Returns the list of available servers
Expand All @@ -49,37 +57,32 @@ public function __construct(
#[ApiRoute(verb: 'GET', url: '/config')]
public function list(): DataResponse {
try {
$result = $this->configService->findMany();
$result = $this->configManager->list();
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new DataResponse($result);
return new DataResponse(array_map(static fn ($c) => $c->toData(), $result));
}

/**
* Add a new server
* @param string $endpoint The server endpoint (e.g. `https://mail.example.com:443/api`)
* @param string $username The username to authenticate with
* @param string $password The password to authenticate with
* @return DataResponse<Http::STATUS_OK, StalwartServerConfig, array{}>
* @return DataResponse<Http::STATUS_OK, int, array{}>
*
* 200: Returns the new server configuration
* @throws OCSException
*/
#[AuthorizedAdminSetting(Admin::class)]
#[OpenAPI(scope: OpenAPI::SCOPE_ADMINISTRATION)]
#[ApiRoute(verb: 'POST', url: '/config')]
public function post(string $endpoint, string $username, string $password): DataResponse {
public function post(): DataResponse {
try {
$result = $this->configService->create($endpoint, $username, $password);
$id = $this->configManager->create();
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
}
unset($result['password']);
unset($result['health_expires']);
return new DataResponse($result);
return new DataResponse($id);
}

/**
Expand All @@ -96,17 +99,15 @@ public function post(string $endpoint, string $username, string $password): Data
#[ApiRoute(verb: 'GET', url: '/config/{id}')]
public function get(int $id): DataResponse {
try {
$result = $this->configService->findId($id);
$result = $this->configManager->find($id);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
}
if ($result === null) {
throw new OCSNotFoundException();
}
unset($result['password']);
unset($result['health_expires']);
return new DataResponse($result);
return new DataResponse($result->toData());
}

/**
Expand All @@ -126,24 +127,29 @@ public function get(int $id): DataResponse {
#[ApiRoute(verb: 'PUT', url: '/config/{id}')]
public function put(int $id, string $endpoint, string $username, string $password): DataResponse {
try {
$result = $this->configService->updateCredentials($id, $endpoint, $username, $password);
$entity = $this->configManager->find($id);
if ($entity !== null) {
$entity->endpoint = $endpoint;
$entity->username = $username;
if ($password !== '') {
$entity->password = $password;
}
$this->configManager->update($entity);
}
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
}
if ($result === null) {
if ($entity === null) {
throw new OCSNotFoundException();
}
unset($result['password']);
unset($result['health_expires']);
return new DataResponse($result);
return new DataResponse($entity->toData());
}

/**
* Delete the configuration of a server number `id`
* @param int $id The server number
* @return DataResponse<Http::STATUS_OK, StalwartServerConfig, array{}>
* @throws OCSNotFoundException If the server number `id` does not exist
* @return DataResponse<Http::STATUS_OK, null, array{}>
* @throws OCSException if an error occurs
*
* 200: The server configuration has been deleted
Expand All @@ -153,24 +159,19 @@ public function put(int $id, string $endpoint, string $username, string $passwor
#[ApiRoute(verb: 'DELETE', url: '/config/{id}')]
public function delete(int $id): DataResponse {
try {
$result = $this->configService->delete($id);
$this->configManager->delete($id);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
}
if ($result === null) {
throw new OCSNotFoundException();
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
}
unset($result['password']);
unset($result['health_expires']);
return new DataResponse($result);
return new DataResponse(null);

}

/**
* Get the users of the server number `id`
* @param int $id The server number
* @return DataResponse<Http::STATUS_OK, list<StalwartServerUser>, array{}>
* @return DataResponse<Http::STATUS_OK, StalwartServerUser[], array{}>
* @throws OCSException if an error occurs
*
* 200: Returns the list of users
Expand All @@ -180,12 +181,12 @@ public function delete(int $id): DataResponse {
#[ApiRoute(verb: 'GET', url: '/config/{id}/users')]
public function getUsers(int $id): DataResponse {
try {
$result = $this->usersService->findMany($id);
$result = $this->accountManager->listConfig($id);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new DataResponse($result);
return new DataResponse(array_map(static fn ($u) => ['uid' => $u->uid, 'displayName' => $u->displayName, 'email' => null], $result));
}

/**
Expand All @@ -203,22 +204,29 @@ public function getUsers(int $id): DataResponse {
#[ApiRoute(verb: 'POST', url: '/config/{id}/users')]
public function postUsers(int $id, string $uid): DataResponse {
try {
$user = $this->usersService->addUser($id, $uid);
$user = $this->userManager->get($uid);
if ($user !== null) {
$this->accountManager->create(new AccountEntity($id, $uid, AccountsType::Individual, $user->getDisplayName(), $user->getPasswordHash() ?? '', 0));
$email = $user->getEMailAddress();
if ($email !== null) {
$this->emailManager->create(new EmailEntity($id, $uid, $email, EmailType::Primary));
}
}
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
}
if ($user === null) {
throw new OCSNotFoundException();
}
return new DataResponse($user);
return new DataResponse(['uid' => $uid,'displayName' => $user->getDisplayName(),'email' => $user->getEMailAddress()]);
}

/**
* Remove a user from the server number `id`
* @param int $id The server number
* @param string $uid The user ID
* @return DataResponse<Http::STATUS_OK, StalwartServerUser, array{}>
* @return DataResponse<Http::STATUS_OK, null, array{}>
* @throws OCSNotFoundException If the user does not exist
* @throws OCSException if an error occurs
*
Expand All @@ -229,14 +237,11 @@ public function postUsers(int $id, string $uid): DataResponse {
#[ApiRoute(verb: 'DELETE', url: '/config/{id}/users')]
public function deleteUsers(int $id, string $uid): DataResponse {
try {
$user = $this->usersService->removeUser($id, $uid);
$this->accountManager->delete($id, $uid);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
}
if ($user === null) {
throw new OCSNotFoundException();
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new DataResponse($user);
return new DataResponse(null);
}
}
12 changes: 9 additions & 3 deletions lib/Cron/CheckTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace OCA\Stalwart\Cron;

use OCA\Stalwart\Services\ConfigService;
use DateTime;
use OCA\Stalwart\Db\ConfigManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\DB\Exception;
Expand All @@ -11,15 +12,20 @@
class CheckTask extends TimedJob {
public function __construct(
ITimeFactory $time,
private readonly ConfigService $configService,
private readonly ConfigManager $configService,
) {
parent::__construct($time);
$this->setInterval(3600);
}

/** @throws Exception */
protected function run(mixed $argument): void {
$this->configService->updateExpiredHealth();
$now = new DateTime();
foreach ($this->configService->list() as $item) {
if ($item->expires < $now) {
$this->configService->update($item);
}
}
}

}
Loading

0 comments on commit bc00121

Please sign in to comment.