Commit 4e1f4d4e authored by Akhil's avatar Akhil 😶
Browse files

Removed app.php

parent a5ac727f
<?php
/**
* @copyright Copyright (c) 2020 Florent VINCENT <e.foundation>
*
* @author Florent VINCENT <diroots@e.email>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
use OCA\EcloudAccounts\AppInfo\Application;
$app = \OC::$server->query(Application::class);
$app->register();
\ No newline at end of file
......@@ -26,26 +26,24 @@ declare(strict_types=1);
namespace OCA\EcloudAccounts\AppInfo;
use OCA\EcloudAccounts\Events\UserDeletedListener;
use OCP\AppFramework\App;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\User\Events\UserDeletedEvent;
use OCA\EcloudAccounts\Events\UserDeletedListener;
class Application extends App
{
const APP_NAME = 'ecloud-accounts';
const APP_ID = 'ecloud-accounts';
public function __construct()
public function __construct(array $urlParams = [])
{
parent::__construct(self::APP_NAME);
parent::__construct(self::APP_ID, $urlParams);
}
public function register()
public function register(IRegistrationContext $context): void
{
/* @var IEventDispatcher $eventDispatcher */
$eventDispatcher = $this->getContainer()->query(IEventDispatcher::class);
$eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
}
}
......@@ -41,9 +41,17 @@ class UserController extends ApiController
return $response;
}
$errors = [];
$this->userService->setEmail($uid, $email);
$this->userService->setQuota($uid, $quota);
$createdFolder = $this->userService->createUserFolder($uid);
if(!$createdFolder){
$errors[] = 'error_creating_user_folder';
}
$response->setData(['errors' => $errors]);
return $response;
}
......
......@@ -3,32 +3,100 @@
declare(strict_types=1);
namespace OCA\EcloudAccounts\Service;
use OCP\IUserManager;
use OCP\IConfig;
use OCP\Files\Storage\IStorage;
class UserService
{
class UserService {
/** @var IUserManager */
private $userManager;
public function __construct(IUserManager $userManager)
/** @var IConfig */
private $config;
private $appName;
private $storage;
public function __construct($appName, IUserManager $userManager, IConfig $config, IStorage $storage)
{
$this->userManager = $userManager;
$this->config = $config;
$this->appName = $appName;
$this->storage = $storage;
}
public function userExists(string $uid) : bool {
private function isShardingEnabled() : bool {
$shardingEnabled = $this->config->getAppValue($this->appName, 'user_folder_sharding');
return $shardingEnabled;
}
public function userExists(string $uid): bool
{
return $this->userManager->userExists($uid);
}
public function setEmail(string $uid, string $email) : void {
public function setEmail(string $uid, string $email): void
{
$user = $this->userManager->get($uid);
if (is_null($user)) return;
$user->setEMailAddress($email);
}
public function setQuota(string $uid, string $quota) : void {
public function setQuota(string $uid, string $quota): void
{
$user = $this->userManager->get($uid);
if (is_null($user)) return;
$user->setQuota($quota);
}
}
\ No newline at end of file
public function createUserFolder(string $uid): bool
{
// return true as creation can be handled at login if sharding disabled
if (!$this->isShardingEnabled()) {
return true;
}
$realDataDir = $this->config->getAppValue($this->appName, 'realdatadirectory');
$ncDataDir = $this->config->getSystemValue('datadirectory');
$ncUserFolder = $ncDataDir . '/' . $uid;
// return false if no realDataDir specified and sharding is enabled
// As user data directory can't be created in correct location
if (!$realDataDir) {
return false;
}
// Randomly assign a directory for the new user
$directories = glob($realDataDir . '/*', GLOB_ONLYDIR);
$folderIndex = random_int(0, count($directories) - 1);
$folder = $directories[$folderIndex];
$realUserFolder = $folder . '/' . $uid;
// Folder already exists
if ($this->storage->file_exists($realUserFolder)) {
return true;
}
try {
$created = $this->storage->mkdir($realUserFolder);
if(!$created) {
$this->logger->error('Error while creating user folder for user: ' . $uid);
return false;
}
$linked = symlink($realUserFolder, $ncUserFolder);
if(!$linked) {
$this->logger->error('Error while linking user folder for user: '. $uid);
return false;
}
return true;
} catch (Exception $e) {
$this->logger->error("Error while creating user folder and linking for user: " . $uid);
return false;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment