Adopt DB and Manager to personal settings structure

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2017-05-19 12:11:07 +02:00
parent d56e86cfde
commit 7f48b6f14f
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
5 changed files with 229 additions and 41 deletions

View File

@ -2135,6 +2135,127 @@
</declaration>
</table>
<table>
<!-- Extra personal settings sections -->
<name>*dbprefix*personal_sections</name>
<declaration>
<field>
<name>id</name>
<type>text</type>
<default></default>
<notnull>false</notnull>
<length>64</length>
</field>
<field>
<name>class</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>255</length>
</field>
<field>
<name>priority</name>
<type>integer</type>
<default></default>
<notnull>true</notnull>
<length>1</length>
</field>
<index>
<name>personal_sections_id_index</name>
<primary>true</primary>
<field>
<name>id</name>
<sorting>ascending</sorting>
</field>
</index>
<index>
<name>personal_sections_class</name>
<unique>true</unique>
<field>
<name>class</name>
<sorting>ascending</sorting>
</field>
</index>
</declaration>
</table>
<table>
<!-- Extra personal settings -->
<name>*dbprefix*personal_settings</name>
<declaration>
<field>
<name>id</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<autoincrement>1</autoincrement>
<length>4</length>
</field>
<field>
<name>class</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>255</length>
</field>
<!-- id of the section, foreign key: admin_sections.id -->
<field>
<name>section</name>
<type>text</type>
<default></default>
<notnull>false</notnull>
<length>64</length>
</field>
<field>
<name>priority</name>
<type>integer</type>
<default></default>
<notnull>true</notnull>
<length>1</length>
</field>
<index>
<name>personal_settings_id_index</name>
<primary>true</primary>
<field>
<name>id</name>
<sorting>ascending</sorting>
</field>
</index>
<index>
<name>personal_settings_class</name>
<unique>true</unique>
<field>
<name>class</name>
<sorting>ascending</sorting>
</field>
</index>
<index>
<name>personal_settings_section</name>
<unique>false</unique>
<field>
<name>section</name>
<sorting>ascending</sorting>
</field>
</index>
</declaration>
</table>
<table>
<name>*dbprefix*accounts</name>

View File

@ -41,9 +41,6 @@ use OCP\Settings\IManager;
use OCP\Settings\ISection;
class Manager implements IManager {
const TABLE_ADMIN_SETTINGS = 'admin_settings';
const TABLE_ADMIN_SECTIONS = 'admin_sections';
/** @var ILogger */
private $log;
/** @var IDBConnection */
@ -87,6 +84,7 @@ class Manager implements IManager {
* @param AccountManager $accountManager
* @param IGroupManager $groupManager
* @param IFactory $l10nFactory
* @param \OC_Defaults $defaults
*/
public function __construct(
ILogger $log,
@ -125,10 +123,17 @@ class Manager implements IManager {
*/
public function setupSettings(array $settings) {
if (isset($settings[IManager::KEY_ADMIN_SECTION])) {
$this->setupAdminSection($settings[IManager::KEY_ADMIN_SECTION]);
$this->setupSectionEntry($settings[IManager::KEY_ADMIN_SECTION], 'admin');
}
if (isset($settings[IManager::KEY_ADMIN_SETTINGS])) {
$this->setupAdminSettings($settings[IManager::KEY_ADMIN_SETTINGS]);
$this->setupSettingsEntry($settings[IManager::KEY_ADMIN_SETTINGS], 'admin');
}
if (isset($settings[IManager::KEY_PERSONAL_SECTION])) {
$this->setupSectionEntry($settings[IManager::KEY_PERSONAL_SECTION], 'personal');
}
if (isset($settings[IManager::KEY_PERSONAL_SETTINGS])) {
$this->setupSettingsEntry($settings[IManager::KEY_PERSONAL_SETTINGS], 'personal');
}
}
@ -144,15 +149,22 @@ class Manager implements IManager {
$appInfo = \OC_App::getAppInfo($appId); // hello static legacy
if (isset($appInfo['settings'][IManager::KEY_ADMIN_SECTION])) {
$this->mapper->remove(self::TABLE_ADMIN_SECTIONS, trim($appInfo['settings'][IManager::KEY_ADMIN_SECTION], '\\'));
$this->mapper->remove(Mapper::TABLE_ADMIN_SECTIONS, trim($appInfo['settings'][IManager::KEY_ADMIN_SECTION], '\\'));
}
if (isset($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS])) {
$this->mapper->remove(self::TABLE_ADMIN_SETTINGS, trim($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS], '\\'));
$this->mapper->remove(Mapper::TABLE_ADMIN_SETTINGS, trim($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS], '\\'));
}
if (isset($appInfo['settings'][IManager::KEY_PERSONAL_SECTION])) {
$this->mapper->remove(Mapper::TABLE_PERSONAL_SECTIONS, trim($appInfo['settings'][IManager::KEY_PERSONAL_SECTION], '\\'));
}
if (isset($appInfo['settings'][IManager::KEY_PERSONAL_SETTINGS])) {
$this->mapper->remove(Mapper::TABLE_PERSONAL_SETTINGS, trim($appInfo['settings'][IManager::KEY_PERSONAL_SETTINGS], '\\'));
}
}
public function checkForOrphanedClassNames() {
$tables = [self::TABLE_ADMIN_SECTIONS, self::TABLE_ADMIN_SETTINGS];
$tables = [Mapper::TABLE_ADMIN_SECTIONS, Mapper::TABLE_ADMIN_SETTINGS, Mapper::TABLE_PERSONAL_SECTIONS, Mapper::TABLE_PERSONAL_SETTINGS];
foreach ($tables as $table) {
$classes = $this->mapper->getClasses($table);
foreach ($classes as $className) {
@ -167,10 +179,11 @@ class Manager implements IManager {
/**
* @param string $sectionClassName
* @param string $type either 'admin' or 'personal'
*/
private function setupAdminSection($sectionClassName) {
private function setupSectionEntry($sectionClassName, $type) {
if (!class_exists($sectionClassName)) {
$this->log->debug('Could not find admin section class ' . $sectionClassName);
$this->log->debug('Could not find ' . ucfirst($type) . ' section class ' . $sectionClassName);
return;
}
try {
@ -182,37 +195,38 @@ class Manager implements IManager {
if (!$section instanceof ISection) {
$this->log->error(
'Admin section instance must implement \OCP\ISection. Invalid class: {class}',
ucfirst($type) .' section instance must implement \OCP\ISection. Invalid class: {class}',
['class' => $sectionClassName]
);
return;
}
if (!$this->hasAdminSection(get_class($section))) {
$this->addAdminSection($section);
$table = $this->getSectionTableForType($type);
if(!$this->hasSection(get_class($section), $table)) {
$this->addSection($section, $table);
} else {
$this->updateAdminSection($section);
$this->updateSection($section, $table);
}
}
private function addAdminSection(ISection $section) {
$this->mapper->add(self::TABLE_ADMIN_SECTIONS, [
private function addSection(ISection $section, $table) {
$this->mapper->add($table, [
'id' => $section->getID(),
'class' => get_class($section),
'priority' => $section->getPriority(),
]);
}
private function addAdminSettings(ISettings $settings) {
$this->mapper->add(self::TABLE_ADMIN_SETTINGS, [
private function addSettings(ISettings $settings, $table) {
$this->mapper->add($table, [
'class' => get_class($settings),
'section' => $settings->getSection(),
'priority' => $settings->getPriority(),
]);
}
private function updateAdminSettings(ISettings $settings) {
private function updateSettings(ISettings $settings, $table) {
$this->mapper->update(
self::TABLE_ADMIN_SETTINGS,
$table,
'class',
get_class($settings),
[
@ -222,9 +236,9 @@ class Manager implements IManager {
);
}
private function updateAdminSection(ISection $section) {
private function updateSection(ISection $section, $table) {
$this->mapper->update(
self::TABLE_ADMIN_SECTIONS,
$table,
'class',
get_class($section),
[
@ -236,23 +250,24 @@ class Manager implements IManager {
/**
* @param string $className
* @param string $table
* @return bool
*/
private function hasAdminSection($className) {
return $this->mapper->has(self::TABLE_ADMIN_SECTIONS, $className);
private function hasSection($className, $table) {
return $this->mapper->has($table, $className);
}
/**
* @param string $className
* @return bool
*/
private function hasAdminSettings($className) {
return $this->mapper->has(self::TABLE_ADMIN_SETTINGS, $className);
private function hasSettings($className, $table) {
return $this->mapper->has($table, $className);
}
private function setupAdminSettings($settingsClassName) {
private function setupSettingsEntry($settingsClassName, $type) {
if (!class_exists($settingsClassName)) {
$this->log->debug('Could not find admin section class ' . $settingsClassName);
$this->log->debug('Could not find ' . $type . ' section class ' . $settingsClassName);
return;
}
@ -266,18 +281,37 @@ class Manager implements IManager {
if (!$settings instanceof ISettings) {
$this->log->error(
'Admin section instance must implement \OCP\Settings\ISection. Invalid class: {class}',
ucfirst($type) . ' section instance must implement \OCP\Settings\ISettings. Invalid class: {class}',
['class' => $settingsClassName]
);
return;
}
if (!$this->hasAdminSettings(get_class($settings))) {
$this->addAdminSettings($settings);
$table = $this->getSettingsTableForType($type);
if (!$this->hasSettings(get_class($settings), $table)) {
$this->addSettings($settings, $table);
} else {
$this->updateAdminSettings($settings);
$this->updateSettings($settings, $table);
}
}
private function getSectionTableForType($type) {
if($type === 'admin') {
return Mapper::TABLE_ADMIN_SECTIONS;
} else if($type === 'personal') {
return Mapper::TABLE_PERSONAL_SECTIONS;
}
throw new \InvalidArgumentException('"admin" or "personal" expected');
}
private function getSettingsTableForType($type) {
if($type === 'admin') {
return Mapper::TABLE_ADMIN_SETTINGS;
} else if($type === 'personal') {
return Mapper::TABLE_PERSONAL_SETTINGS;
}
throw new \InvalidArgumentException('"admin" or "personal" expected');
}
private function query($className) {
try {
return \OC::$server->query($className);
@ -423,7 +457,7 @@ class Manager implements IManager {
5 => [new Section('sessions', $this->l->t('Sessions'), 0, $this->url->imagePath('settings', 'admin.svg'))],
10 => [new Section('app-passwords', $this->l->t('App passwords'), 0, $this->url->imagePath('settings', 'password.svg'))],
15 => [new Section('sync-clients', $this->l->t('Sync clients'), 0, $this->url->imagePath('settings', 'change.svg'))],
98 => [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))],
];
return $sections;
}

View File

@ -28,6 +28,8 @@ use OCP\IDBConnection;
class Mapper {
const TABLE_ADMIN_SETTINGS = 'admin_settings';
const TABLE_ADMIN_SECTIONS = 'admin_sections';
const TABLE_PERSONAL_SETTINGS = 'personal_settings';
const TABLE_PERSONAL_SECTIONS = 'personal_sections';
/** @var IDBConnection */
private $dbc;
@ -46,9 +48,30 @@ class Mapper {
* @return array[] [['class' => string, 'priority' => int], ...]
*/
public function getAdminSettingsFromDB($section) {
return $this->getSettingsFromDB(self::TABLE_ADMIN_SETTINGS, $section);
}
/**
* Get the configured personal settings from the database for the provided section
*
* @param string $section
* @return array[] [['class' => string, 'priority' => int], ...]
*/
public function getPersonalSettingsFromDB($section) {
return $this->getSettingsFromDB(self::TABLE_PERSONAL_SETTINGS, $section);
}
/**
* Get the configured settings from the database for the provided table and section
*
* @param $table
* @param $section
* @return array
*/
private function getSettingsFromDB($table, $section) {
$query = $this->dbc->getQueryBuilder();
$query->select(['class', 'priority'])
->from(self::TABLE_ADMIN_SETTINGS)
->from($table)
->where($query->expr()->eq('section', $this->dbc->getQueryBuilder()->createParameter('section')))
->setParameter('section', $section);
@ -76,7 +99,7 @@ class Mapper {
}
/**
* @param string $table Mapper::TABLE_ADMIN_SECTIONS or Mapper::TABLE_ADMIN_SETTINGS
* @param string $table one of the Mapper::TABLE_* constants
* @param array $values
*/
public function add($table, array $values) {
@ -91,7 +114,7 @@ class Mapper {
/**
* returns the registered classes in the given table
*
* @param $table Mapper::TABLE_ADMIN_SECTIONS or Mapper::TABLE_ADMIN_SETTINGS
* @param string $table one of the Mapper::TABLE_* constants
* @return string[]
*/
public function getClasses($table) {
@ -110,7 +133,7 @@ class Mapper {
/**
* Check if a class is configured in the database
*
* @param string $table Mapper::TABLE_ADMIN_SECTIONS or Mapper::TABLE_ADMIN_SETTINGS
* @param string $table one of the Mapper::TABLE_* constants
* @param string $className
* @return bool
*/
@ -131,8 +154,8 @@ class Mapper {
/**
* deletes an settings or admin entry from the given table
*
* @param $table Mapper::TABLE_ADMIN_SECTIONS or Mapper::TABLE_ADMIN_SETTINGS
* @param $className
* @param string $table one of the Mapper::TABLE_* constants
* @param string $className
*/
public function remove($table, $className) {
$query = $this->dbc->getQueryBuilder();
@ -143,7 +166,7 @@ class Mapper {
}
/**
* @param string $table Mapper::TABLE_ADMIN_SECTIONS or Mapper::TABLE_ADMIN_SETTINGS
* @param string $table one of the Mapper::TABLE_* constants
* @param string $idCol
* @param string $id
* @param array $values

View File

@ -37,6 +37,16 @@ interface IManager {
*/
const KEY_ADMIN_SECTION = 'admin-section';
/**
* @since 12.0.0
*/
const KEY_PERSONAL_SETTINGS = 'personal';
/**
* @since 12.0.0
*/
const KEY_PERSONAL_SECTION = 'personal-section';
/**
* sets up settings according to data specified by an apps info.xml, within
* the <settings> element.

View File

@ -26,7 +26,7 @@
// between betas, final and RCs. This is _not_ the public version number. Reset minor/patchlevel
// when updating major/minor version number.
$OC_Version = array(13, 0, 0, 0);
$OC_Version = array(13, 0, 0, 1);
// The human readable string
$OC_VersionString = '13.0.0 alpha';