Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nextcloud 26 support #44

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ Features:
- Attach Nextcloud / ownCloud files to email.

Supported Versions:
- Nextcloud: 9, 10, 11, 12, 13
- ownCloud: 9, 9.1, 10
- Nextcloud: 24, 25, 26

## Install

Expand Down
55 changes: 0 additions & 55 deletions nextcloud-app/appinfo/database.xml

This file was deleted.

6 changes: 3 additions & 3 deletions nextcloud-app/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</description>
<licence>AGPL</licence>
<author>ZeXtras</author>
<version>0.8.23</version>
<version>0.8.26</version>
<namespace>ZimbraDrive</namespace>
<category>auth</category>
<category>integration</category>
Expand All @@ -41,8 +41,8 @@
<screenshot>zimbra.png</screenshot>

<dependencies>
<owncloud min-version="9.0" max-version="10.0"/>
<nextcloud min-version="9" max-version="15"/>
<owncloud min-version="9.0" max-version="10.3"/>
<nextcloud min-version="24" max-version="26"/>
</dependencies>
<types>
<authentication/>
Expand Down
58 changes: 58 additions & 0 deletions nextcloud-app/lib/Migration/Version000826Date20230504093000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace OCA\ZimbraDrive\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version000826Date20230504093000 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

if (!$schema->hasTable('zimbradrive_users')) {
$table = $schema->createTable('zimbradrive_users');
$table->addColumn('uid', 'string', [
'notnull' => true,
'length' => 256,
'default' => '',
]);
$table->addColumn('display_name', 'string', [
'notnull' => false,
'length' => 256,
]);
$table->setPrimaryKey(['uid']);
}
return $schema;
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}
}
104 changes: 58 additions & 46 deletions nextcloud-app/lib/auth/abstractzimbrausersbackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
use OCA\ZimbraDrive\Settings\AppSettings;
use OC\User\User;
use OCP\IServerContainer;
use Psr\Log\LoggerInterface;
use OCP\Security\VerificationToken\IVerificationToken;
use OCP\Defaults;
use OCP\L10N\IFactory;
use OCP\IURLGenerator;
use OC\Accounts\Account;

abstract class AbstractZimbraUsersBackend extends RetroCompatibleBackend
{
Expand Down Expand Up @@ -57,7 +63,7 @@ public function __construct($server = null, $zimbraAuthenticationBackend = null)
$this->zimbraAuthenticationBackend = $zimbraAuthenticationBackend;
}

$this->logger = $server->getLogger();
$this->logger = $server->get(LoggerInterface::class);
$this->config = $server->getConfig();
$this->userManager = $server->getUserManager();
$this->groupManager = $server->getGroupManager();
Expand All @@ -66,8 +72,16 @@ public function __construct($server = null, $zimbraAuthenticationBackend = null)
{
$this->accountManager = new AccountManager(
$server->getDatabaseConnection(),
$this->config, // Nextcloud >= 21
$server->getEventDispatcher(),
$server->getJobList() //Nextcloud >= 12.0.1
$server->getJobList(), //Nextcloud >= 12.0.1
$this->logger, // Nexcloud >= 18.0.0
$server->get(IVerificationToken::class),
$server->getMailer(),
$server->get(Defaults::class),
$server->get(IFactory::class),
$server->get(IURLGenerator::class),
$server->getCrypto()
);
}

Expand Down Expand Up @@ -99,7 +113,9 @@ public function checkPassword($uid, $password)
{
$this->createUser($zimbraUser->getUid(), $zimbraUser->getDisplayName());
}
$this->setDefaultUserAttributes($zimbraUser);
$user = $this->userManager->get($zimbraUser->getUid());
$account = $this->accountManager->getAccount($user);
$this->setDefaultUserAttributes($zimbraUser, $account);

return $zimbraUser->getUid();
} catch (\Exception $ignore)
Expand All @@ -110,24 +126,32 @@ public function checkPassword($uid, $password)

/**
* @param ZimbraUser $zimbraUser
* @param Account $account
*/
private function setDefaultUserAttributes($zimbraUser){
$user = $this->userManager->get($zimbraUser->getUid());
$this->restoreUserEmailIfChanged($user, $zimbraUser->getEmail());
$this->restoreUserDisplayNameIfChanged($user, $zimbraUser->getDisplayName());
$this->setDefaultGroups($user);
private function setDefaultUserAttributes($zimbraUser, $account){
$this->restoreUserEmailIfChanged($account, $zimbraUser->getEmail());
$this->restoreUserDisplayNameIfChanged($account, $zimbraUser->getDisplayName());
$this->setDefaultGroups($account);
}

/**
* @param $user User
* @param $account Account
*/
private function setDefaultGroups($user)
private function setDefaultGroups($account)
{
$user = $account->getUser();

$accountEmail = '';
if(!is_null($account->getProperty(AccountManager::PROPERTY_EMAIL)))
{
$accountEmail = $account->getProperty(AccountManager::PROPERTY_EMAIL)->getValue();
}

if ($this->setZimbraGroupToUsers)
{
$this->insertUserInGroup($user, self::ZIMBRA_GROUP);
}
$this->insertUserInGroup($user, $this->getEmailDomain($user->getEMailAddress()));
$this->insertUserInGroup($user, $this->getEmailDomain($accountEmail));
}

private function getEmailDomain($email)
Expand All @@ -144,54 +168,42 @@ private function getEmailDomain($email)
protected abstract function createUser($userId, $userDisplayName);

/**
* @param $user User
* @param $account Account
* @param $userEmail string
*/
private function restoreUserEmailIfChanged(User $user, $userEmail)
private function restoreUserEmailIfChanged(Account $account, $userEmail)
{
if( $this->getUserEmailAddress($user) !== $userEmail)
$accountEmail = '';
if(!is_null($account->getProperty(AccountManager::PROPERTY_EMAIL)))
{
$this->setUserEmailAddress($user, $userEmail);
$accountEmail = $account->getProperty(AccountManager::PROPERTY_EMAIL)->getValue();
}
}

private function getUserEmailAddress(User $user){
if(!is_null($this->accountManager)) //Nextcloud 11
{
$userData = $this->accountManager->getUser($user);
$userEmailAddress = $userData[AccountManager::PROPERTY_EMAIL]['value'];
} else
if( $accountEmail !== $userEmail)
{
$userEmailAddress = $user->getEMailAddress();
$this->setUserEmailAddress($account, $userEmail);
}
return $userEmailAddress;
}

private function setUserEmailAddress(User $user, $userEmail){
if(!is_null($this->accountManager)) //Nextcloud 11
{
$userData = $this->accountManager->getUser($user);
$userData[AccountManager::PROPERTY_EMAIL]['value'] = $userEmail;
$this->accountManager->updateUser($user, $userData);
} else
{
$user->setEMailAddress($userEmail);
}
private function setUserEmailAddress(Account $account, $userEmail){
$account->setProperty(AccountManager::PROPERTY_EMAIL, $userEmail, AccountManager::SCOPE_LOCAL, AccountManager::NOT_VERIFIED);
$this->accountManager->updateAccount($account);
}

private function restoreUserDisplayNameIfChanged(User $user, $userDisplayName)
private function setUserDisplayName(Account $account, $userDisplayName){
$account->setProperty(AccountManager::PROPERTY_DISPLAYNAME, $userDisplayName, AccountManager::SCOPE_LOCAL, AccountManager::NOT_VERIFIED);
$this->accountManager->updateAccount($account);
}

private function restoreUserDisplayNameIfChanged(Account $account, $userDisplayName)
{
if($user->getDisplayName() !== $userDisplayName)
$accountDisplayName = '';
if(!is_null($account->getProperty(AccountManager::PROPERTY_DISPLAYNAME)))
{
if(!is_null($this->accountManager)) //Nextcloud 11
{
$userData = $this->accountManager->getUser($user);
$userData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $userDisplayName;
$this->accountManager->updateUser($user, $userData);
} else
{
$user->setDisplayName($userDisplayName);
}
$accountDisplayName = $account->getProperty(AccountManager::PROPERTY_DISPLAYNAME)->getValue();
}
if( $accountDisplayName !== $userDisplayName)
{
$this->setUserDisplayName($account, $userDisplayName);
}
}

Expand Down