Skip to content

Commit

Permalink
implement group entity and group entity mapper (+ new table migration…
Browse files Browse the repository at this point in the history
… and unit tests)
  • Loading branch information
mrow4a committed Sep 28, 2017
1 parent e744c8c commit 84340ed
Show file tree
Hide file tree
Showing 8 changed files with 563 additions and 53 deletions.
57 changes: 57 additions & 0 deletions core/Migrations/Version20170927145820.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace OC\Migrations;

use Doctrine\DBAL\Schema\Schema;
use OCP\Migration\ISchemaMigration;
use Doctrine\DBAL\Types\Type;

class Version20170927145820 implements ISchemaMigration {

public function changeSchema(Schema $schema, array $options) {
$prefix = $options['tablePrefix'];

// Backend Groups Table
$table = $schema->createTable("{$prefix}backend_groups");
$table->addColumn('id', Type::INTEGER, [
'autoincrement' => true,
'unsigned' => true,
'notnull' => true,
]);
$table->addColumn('group_id', Type::STRING, [
'notnull' => true,
'length' => 255,
]);
$table->addColumn('display_name', Type::STRING, [
'notnull' => false,
'length' => 255,
]);
$table->addColumn('backend', Type::STRING, [
'notnull' => true,
'length' => 64,
]);
$table->setPrimaryKey(['id']);

// Group Memberships Table
$table = $schema->createTable("{$prefix}memberships");
$table->addColumn('backend_group_id', Type::INTEGER, [
'notnull' => true,
'unsigned' => true,
]);
$table->addColumn('account_id', Type::BIGINT, [
'notnull' => true,
'unsigned' => true,
]);
$table->addColumn('membership_type', Type::SMALLINT, [
'notnull' => true,
'comment' => '0: GroupUser, 1: GroupAdmin'
]);

// This set of values has to be unique
$table->addUniqueIndex(['backend_group_id', 'account_id', 'membership_type'], 'group_account_membership_index');

//TODO: Do it later since it requires changes here for Oracle https://github.com/owncloud/core/blob/master/lib/private/DB/OracleMigrator.php#L158-L160
// Add foreign keys on backend_group and accounts tables
//$table->addForeignKeyConstraint("{$prefix}backend_groups",array('backend_group_id'), array('id'));
//$table->addForeignKeyConstraint("{$prefix}accounts", array('account_id'), array('id'));
}
}
62 changes: 62 additions & 0 deletions lib/private/Group/BackendGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @author Piotr Mrowczynski <piotr@owncloud.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Group;

use OCP\AppFramework\Db\Entity;
use OCP\GroupInterface;

/**
* Class BackendGroup
*
* @method int getGroupId()
* @method string getDisplayName()
* @method void setDisplayName(string $displayName)
* @method string getBackend()
* @method void setBackend(string $backEnd)
*
* @package OC\Group
*/
class BackendGroup extends Entity {

protected $groupId;
protected $displayName;
protected $backend;

public function __construct() { }

public function setGroupId($gid) {
parent::setter('groupId', [$gid]);
}

/**
* @return GroupInterface
*/
public function getBackendInstance() {
$backendClass = $this->getBackend();
if (empty($backendClass)) {
return null;
}

return \OC::$server->getGroupManager()->getBackend($backendClass);
}

}
75 changes: 75 additions & 0 deletions lib/private/Group/GroupMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* @author Piotr Mrowczynski <piotr@owncloud.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Group;

use OC\Group\BackendGroup;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\Entity;
use OCP\IConfig;
use OCP\IDBConnection;

class GroupMapper extends Mapper {

public function __construct(IDBConnection $db) {
parent::__construct($db, 'backend_groups', BackendGroup::class);
}

/**
* @param BackendGroup $entity
* @return Entity the saved entity with the set id
*/
public function insert(Entity $entity) {
// run the normal entity insert operation to get an id
return parent::insert($entity);
}

/**
* @param string $uid
* @return BackendGroup
*/
public function getGroup($gid) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('group_id', $qb->createNamedParameter($gid)));

return $this->findEntity($qb->getSQL(), $qb->getParameters());
}

/**
* @param string $fieldName
* @param string $pattern
* @param integer $limit
* @param integer $offset
* @return BackendGroup[]
*/
public function search($fieldName, $pattern, $limit, $offset) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->iLike($fieldName, $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')))
->orderBy($fieldName);

return $this->findEntities($qb->getSQL(), $qb->getParameters(), $limit, $offset);
}

}
42 changes: 28 additions & 14 deletions lib/private/Group/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Vincent Petry <pvince81@owncloud.com>
* @author voxsim <Simon Vocella>
* @authod Piotr Mrowczynski <piotr@owncloud.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @license AGPL-3.0
Expand All @@ -38,6 +39,8 @@
use OC\Hooks\PublicEmitter;
use OCP\GroupInterface;
use OCP\IGroupManager;
use OC\Group\GroupMapper;
use OC\Group\Database;

/**
* Class Manager
Expand All @@ -55,34 +58,30 @@
* @package OC\Group
*/
class Manager extends PublicEmitter implements IGroupManager {
/**
* @var GroupInterface[] $backends
*/
/** @var GroupInterface[] $backends */
private $backends = [];

/**
* @var \OC\User\Manager $userManager
*/
/** @var \OC\User\Manager $userManager */
private $userManager;

/**
* @var \OC\Group\Group[]
*/
/** @var \OC\Group\Group[] */
private $cachedGroups = [];

/**
* @var \OC\Group\Group[]
*/
/** @var \OC\Group\Group[] */
private $cachedUserGroups = [];

/** @var \OC\SubAdmin */
private $subAdmin = null;

/** @var \OC\Group\GroupMapper */
private $groupMapper;

/**
* @param \OC\User\Manager $userManager
*/
public function __construct(\OC\User\Manager $userManager) {
public function __construct(\OC\User\Manager $userManager, \OC\Group\GroupMapper $groupMapper) {
$this->userManager = $userManager;
$this->groupMapper = $groupMapper;
$cachedGroups = & $this->cachedGroups;
$cachedUserGroups = & $this->cachedUserGroups;
$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
Expand Down Expand Up @@ -136,7 +135,7 @@ public function clearBackends() {
$this->backends = [];
$this->clearCaches();
}

protected function clearCaches() {
$this->cachedGroups = [];
$this->cachedUserGroups = [];
Expand Down Expand Up @@ -212,6 +211,21 @@ public function createGroup($gid) {
}
}


/**
* @param string $gid
* @param string $backendClass
* @return BackendGroup|\OCP\AppFramework\Db\Entity
*/
private function createBackendGroup($gid, $backendClass) {
$account = new BackendGroup();
$account->setGroupId($gid);
$account->setDisplayName($gid);
$account->setBackend($backendClass);
$account = $this->groupMapper->insert($account);
return $account;
}

/**
* @param string $search search string
* @param int|null $limit limit
Expand Down
13 changes: 12 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
use OC\Theme\ThemeService;
use OC\User\AccountMapper;
use OC\User\AccountTermMapper;
use OC\Group\GroupMapper;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IServerContainer;
Expand Down Expand Up @@ -232,13 +233,16 @@ public function __construct($webRoot, \OC\Config $config) {
$this->registerService('AccountMapper', function(Server $c) {
return new AccountMapper($c->getConfig(), $c->getDatabaseConnection(), new AccountTermMapper($c->getDatabaseConnection()));
});
$this->registerService('GroupMapper', function(Server $c) {
return new GroupMapper($c->getDatabaseConnection());
});
$this->registerService('UserManager', function (Server $c) {
$config = $c->getConfig();
$logger = $c->getLogger();
return new \OC\User\Manager($config, $logger, $c->getAccountMapper());
});
$this->registerService('GroupManager', function (Server $c) {
$groupManager = new \OC\Group\Manager($this->getUserManager());
$groupManager = new \OC\Group\Manager($this->getUserManager(), $c->getGroupMapper());
$groupManager->listen('\OC\Group', 'preCreate', function ($gid) {
\OC_Hook::emit('OC_Group', 'pre_createGroup', ['run' => true, 'gid' => $gid]);
});
Expand Down Expand Up @@ -975,6 +979,13 @@ public function getAccountMapper() {
return $this->query('AccountMapper');
}

/**
* @return \OC\Group\GroupMapper
*/
public function getGroupMapper() {
return $this->query('GroupMapper');
}

/**
* @return \OC\Group\Manager
*/
Expand Down
Loading

0 comments on commit 84340ed

Please sign in to comment.