Skip to content

Commit

Permalink
Fix support with ownCloud 10 and Nextcloud 12.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZxUgoPadoan committed Jul 11, 2017
1 parent ba584a9 commit f0e363d
Show file tree
Hide file tree
Showing 10 changed files with 757 additions and 181 deletions.
1 change: 1 addition & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion nextcloud-app/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(array $urlParams=array()){

$app = new Application();

if(!interface_exists(ISettings::class)) // ISettings not supported in OwnCloud 9.1.4
if(!interface_exists('OCP\Settings\ISettings')) // ISettings not supported in OwnCloud 9.1.4
{
\OCP\App::registerAdmin(Application::APP_NAME, 'admin');
}
Expand Down
231 changes: 231 additions & 0 deletions nextcloud-app/lib/auth/abstractzimbrausersbackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
<?php
/**
* Copyright 2017 Zextras Srl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OCA\ZimbraDrive\Auth;

use \OCA\ZimbraDrive\Settings\AppSettings;

abstract class AbstractZimbraUsersBackend extends \OC_User_Backend
{
const ZIMBRA_GROUP = "zimbra";
protected $logger;
protected $config;
protected $zimbra_url;
protected $zimbra_port;
protected $use_ssl;
protected $trust_invalid_certs;
protected $url;
protected $userManager;
protected $groupManager;
protected $allow_zimbra_users_login;

public function __construct()
{
$server = \OC::$server;

$this->logger = $server->getLogger();
$this->config = $server->getConfig();
$this->userManager = $server->getUserManager();
$this->groupManager = $server->getGroupManager();

$appSettings = new AppSettings($this->config);

$this->zimbra_url =$appSettings->getServerUrl();
$this->zimbra_port = $appSettings->getServerPort();
$this->use_ssl = $appSettings->useSSLDuringZimbraAuthentication();
$this->trust_invalid_certs = $appSettings->trustInvalidCertificatesDuringZimbraAuthentication();
$this->allow_zimbra_users_login = $appSettings->allowZimbraUsersLogin();

$this->url = sprintf(
"%s://%s:%s/service/extension/ZimbraDrive_NcUserZimbraBackend",
"http" . ($this->use_ssl ? "s" : ""),
$this->zimbra_url,
$this->zimbra_port
);
}

/**
* Check if the password is correct
* @param string $uid The username
* @param string $password The password
* @return string
*
* Check if the password is correct without logging in the user
* returns the user id or false
*/
public function checkPassword($uid, $password)
{
if(!$this->allow_zimbra_users_login)
{
return false;
}

$httpRequestResponse = $this->doZimbraAuthenticationRequest($uid, $password);

if ($httpRequestResponse->getHttpCode() === 200) {
$response = json_decode($httpRequestResponse->getRawResponse());
$userId = $response->{'accountId'};
$userDisplayName = $response->{'displayName'};
$userEmail = $response->{'email'};
if(!$this->userManager->userExists($userId))
{
$this->createUser($userId, $userDisplayName);
$this->setDefaultUserAttributes($userId, $userEmail);
} else
{
$this->restoreUserAttributes($userId, $userEmail);
$this->restoreUserEmailIfChanged($userId, $userEmail);
}

return $userId;
} else {
return false;
}
}

/**
* @param $userId string
* @param $userEmail string
*/
private function restoreUserAttributes($userId, $userEmail){
$user = $this->userManager->get($userId);
$this->restoreUserEmailIfChanged($userId, $userEmail);
$this->setDefaultGroups($user);
}

/**
* @param $userId string
* @param $userEmail string
*/
private function setDefaultUserAttributes($userId, $userEmail){
$user = $this->userManager->get($userId);
$user->setEMailAddress($userEmail);
$this->setDefaultGroups($user);
}

/**
* @param $user \OC\User\User
*/
private function setDefaultGroups($user)
{
$this->insertUserInGroup($user, self::ZIMBRA_GROUP);
$this->insertUserInGroup($user, $this->zimbra_url);
}

/**
* @param $userId string
* @param $userDisplayName string
*/
protected abstract function createUser($userId, $userDisplayName);

/**
* @param $userId string
* @param $userEmail string
*/
private function restoreUserEmailIfChanged($userId, $userEmail)
{
$user = $this->userManager->get($userId);
if( $user->getEMailAddress() !== $userEmail)
{
$user->setEMailAddress($userEmail);
}
}

/**
* @param $user \OC\User\User
* @param $group
*/
protected function insertUserInGroup($user, $group)
{
if(isset($user))
{
if(!$this->groupManager->groupExists($group))
{
$this->groupManager->createGroup($group);
}

$zimbraGroup = $this->groupManager->get($group);
$zimbraGroup->addUser($user);
}
}

/**
* Change the display name of a user
*
* @param string $uid The username
* @param string $display_name The new display name
*
* @return true/false
*/
public abstract function setDisplayName($uid, $display_name);

/**
* @param $uid
* @param $password
* @return string
*/
private function buildPostField($uid, $password)
{
$fields = array(
"username" => $uid,
"password" => $password
);

//url-ify the data for the POST
$fields_string = "";
foreach ($fields as $key => $value) {
$fields_string .= $key . "=" . $value . "&";
}
$fields_string = rtrim($fields_string, "&");
return $fields_string;
}

/**
* @param $uid
* @param $password
* @return HttpRequestResponse
*/
private function doZimbraAuthenticationRequest($uid, $password)
{
$fields_string = $this->buildPostField($uid, $password);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if ($this->trust_invalid_certs) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
} else {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2);
}

//execute post
$raw_response = curl_exec($ch);
$response_info = curl_getinfo($ch);
curl_close($ch);
$http_code = $response_info["http_code"];
$httpRequestResponse = new HttpRequestResponse($raw_response, $http_code);
return $httpRequestResponse;
}
}

51 changes: 51 additions & 0 deletions nextcloud-app/lib/auth/httprequestresponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Copyright 2017 Zextras Srl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OCA\ZimbraDrive\Auth;

class HttpRequestResponse
{
private $rawResponse;
private $httpCode;

/**
* @return mixed
*/
public function getRawResponse()
{
return $this->rawResponse;
}

/**
* @return integer
*/
public function getHttpCode()
{
return $this->httpCode;
}

/**
* HttpRequestResponse constructor.
* @param $rawResponse
* @param $httpCode
*/
public function __construct($rawResponse, $httpCode)
{
$this->rawResponse = $rawResponse;
$this->httpCode = $httpCode;
}
}
Loading

0 comments on commit f0e363d

Please sign in to comment.