1
0
mirror of https://github.com/chylex/Lightning-Tracker.git synced 2025-04-10 02:15:43 +02:00

Add unique key to role ordering

This commit is contained in:
chylex 2020-09-12 03:29:45 +02:00
parent 7b80d54a73
commit 9051da555c
8 changed files with 63 additions and 29 deletions

View File

@ -6,6 +6,7 @@ CREATE TABLE IF NOT EXISTS `project_roles` (
`ordering` SMALLINT NOT NULL,
PRIMARY KEY (`project_id`, `role_id`),
UNIQUE KEY (`project_id`, `title`),
UNIQUE KEY (`project_id`, `type`, `ordering`),
KEY (`role_id`, `project_id`), # Needed for role-project pair checks.
CONSTRAINT fk__project_role__project FOREIGN KEY (`project_id`)
REFERENCES `projects` (`id`)

View File

@ -4,7 +4,8 @@ CREATE TABLE IF NOT EXISTS `system_roles` (
`title` VARCHAR(32) NOT NULL,
`ordering` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`title`)
UNIQUE KEY (`title`),
UNIQUE KEY (`type`, `ordering`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE utf8mb4_general_ci

View File

@ -64,14 +64,31 @@ SQL;
}
public function swapRolesIfNormal(int $ordering1, int $ordering2): void{
$sql = <<<SQL
$this->db->beginTransaction();
try{
$sql = <<<SQL
UPDATE project_roles pr1 INNER JOIN project_roles pr2 ON pr1.ordering = ? AND pr2.ordering = ? AND pr1.project_id = pr2.project_id
SET pr1.ordering = pr2.ordering,
pr2.ordering = pr1.ordering
SET pr1.ordering = -pr2.ordering,
pr2.ordering = -pr1.ordering
WHERE pr1.project_id = ? AND pr1.type = 'normal' AND pr2.type = 'normal'
SQL;
$this->execute($sql, 'III', [$ordering1, $ordering2, $this->getProjectId()]);
$this->execute($sql, 'III', [$ordering1, $ordering2, $this->getProjectId()]);
$sql = <<<SQL
UPDATE project_roles
SET ordering = -ordering
WHERE project_id = ? AND ordering IN (-?, -?) AND type = 'normal'
SQL;
$this->execute($sql, 'III', [$this->getProjectId(), $ordering1, $ordering2]);
$this->db->commit();
}catch(PDOException $e){
$this->db->rollBack();
throw $e;
}
}
public function findMaxOrdering(): ?int{
@ -161,12 +178,12 @@ SQL;
$this->execute('UPDATE project_members SET role_id = NULL WHERE role_id = ? AND project_id = ?',
'II', [$id, $project]);
$this->execute('UPDATE project_roles SET ordering = ordering - 1 WHERE ordering > ? AND project_id = ? AND type = \'normal\'',
'II', [$ordering, $project]);
$this->execute('DELETE FROM project_roles WHERE role_id = ? AND project_id = ? AND type = \'normal\'',
'II', [$id, $project]);
$this->execute('UPDATE project_roles SET ordering = ordering - 1 WHERE ordering > ? AND project_id = ? AND type = \'normal\'',
'II', [$ordering, $project]);
$this->db->commit();
}catch(PDOException $e){
$this->db->rollBack();

View File

@ -52,14 +52,31 @@ SQL;
}
public function swapRolesIfNormal(int $ordering1, int $ordering2): void{
$sql = <<<SQL
$this->db->beginTransaction();
try{
$sql = <<<SQL
UPDATE system_roles sr1 INNER JOIN system_roles sr2 ON sr1.ordering = ? AND sr2.ordering = ?
SET sr1.ordering = sr2.ordering,
sr2.ordering = sr1.ordering
SET sr1.ordering = -sr2.ordering,
sr2.ordering = -sr1.ordering
WHERE sr1.type = 'normal' AND sr2.type = 'normal'
SQL;
$this->execute($sql, 'II', [$ordering1, $ordering2]);
$this->execute($sql, 'II', [$ordering1, $ordering2]);
$sql = <<<SQL
UPDATE system_roles
SET ordering = -ordering
WHERE ordering IN (-?, -?) AND type = 'normal'
SQL;
$this->execute($sql, 'II', [$ordering1, $ordering2]);
$this->db->commit();
}catch(PDOException $e){
$this->db->rollBack();
throw $e;
}
}
public function findMaxOrdering(): ?int{
@ -140,13 +157,13 @@ SQL;
$this->db->rollBack();
return;
}
$this->execute('DELETE FROM system_roles WHERE id = ? AND type = \'normal\'',
'I', [$id]);
$this->execute('UPDATE system_roles SET ordering = ordering - 1 WHERE ordering > ? AND type = \'normal\'',
'I', [$ordering]);
$this->execute('DELETE FROM system_roles WHERE id = ? AND type = \'normal\'',
'I', [$id]);
$this->db->commit();
}catch(PDOException $e){
$this->db->rollBack();

View File

@ -14,7 +14,10 @@ final class Migration9 extends AbstractMigrationProcess{
self::sql('ALTER TABLE project_roles ADD type ENUM (\'normal\', \'owner\') NOT NULL DEFAULT \'normal\' AFTER role_id'),
self::sql('UPDATE project_roles SET type = \'owner\' WHERE special = TRUE'),
self::sql('ALTER TABLE project_roles DROP COLUMN special')
self::sql('ALTER TABLE project_roles DROP COLUMN special'),
self::sql('ALTER TABLE system_roles ADD UNIQUE KEY (`type`, `ordering`)'),
self::sql('ALTER TABLE project_roles ADD UNIQUE KEY (`project_id`, `type`, `ordering`)'),
];
}
}

View File

@ -18,13 +18,11 @@ class T013_SystemSettingsRolesSpecial_Cest{
public function createAdminRoles(AcceptanceTester $I): void{
$db = Acceptance::getDB();
$db->exec('INSERT INTO system_roles (type, title, ordering) VALUES (\'admin\', \'Special1\', 0)');
$db->exec('INSERT INTO system_roles (type, title, ordering) VALUES (\'admin\', \'Special2\', 0)');
$db->exec('INSERT INTO system_roles (type, title, ordering) VALUES (\'admin\', \'Admin\', 0)');
$I->amOnPage('/settings/roles');
$I->seeTableRowOrder(['Special1',
'Special2',
$I->seeTableRowOrder(['Admin',
'Moderator',
'ManageUsers1',
'ManageUsers2',
@ -37,8 +35,7 @@ class T013_SystemSettingsRolesSpecial_Cest{
public function cannotMoveRolesAboveAdminRoles(AcceptanceTester $I): void{
$I->click('#Move-1 button[value="Up"]');
$I->seeTableRowOrder(['Special1',
'Special2',
$I->seeTableRowOrder(['Admin',
'Moderator',
'ManageUsers1',
'ManageUsers2',
@ -58,8 +55,7 @@ class T013_SystemSettingsRolesSpecial_Cest{
$I->fillField('#Delete-1 input[type="hidden"][name="Role"]', $id);
$I->click('#Delete-1 button[type="submit"]');
$I->seeTableRowOrder(['Special1',
'Special2',
$I->seeTableRowOrder(['Admin',
'Moderator',
'ManageUsers1',
'ManageUsers2',

View File

@ -41,7 +41,7 @@ class T015_UserManageability_Cest{
$db = Acceptance::getDB();
$moderator = $db->query('SELECT id FROM system_roles WHERE title = \'Moderator\'')->fetchColumn();
$special = $db->query('SELECT id FROM system_roles WHERE title = \'Special1\'')->fetchColumn();
$special = $db->query('SELECT id FROM system_roles WHERE title = \'Admin\'')->fetchColumn();
$db->exec(<<<SQL
INSERT INTO users (id, name, email, password, role_id, admin, date_registered)

View File

@ -13,8 +13,7 @@ class T016_UserEditing_Cest{
'ManageUsers1',
'ManageUsers2',
'User',
'Special1',
'Special2'
'Admin'
];
private function startEditingAs(AcceptanceTester $I, string $editor, string $user): void{