Groups: Fix naming and update IDs

This commit is contained in:
Igor Scheller 2019-07-28 15:33:01 +02:00
parent 968f0141cb
commit 4d9f4694ae
9 changed files with 136 additions and 22 deletions

View File

@ -0,0 +1,80 @@
<?php
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
class FixOldGroupsTableIdAndName extends Migration
{
/** @var string[] */
protected array $naming = [
'1-Gast' => 'Guest',
'2-Engel' => 'Angel',
'Shirt-Manager' => 'Shirt Manager',
'3-Shift Coordinator' => 'Shift Coordinator',
'4-Team Coordinator' => 'Team Coordinator',
'5-Bürokrat' => 'Bureaucrat',
'6-Developer' => 'Developer',
];
/** @var int[] */
protected array $ids = [
-25 => -30,
-26 => -35,
-30 => -50,
-40 => -60,
-50 => -65,
-60 => -80,
-65 => -85,
-70 => -90,
];
/**
* Run the migration
*/
public function up()
{
$this->migrate($this->naming, $this->ids);
}
/**
* Reverse the migration
*/
public function down()
{
$this->migrate(array_flip($this->naming), array_flip($this->ids));
}
/**
* @param string[] $naming
* @param int[] $ids
*/
protected function migrate(array $naming, array $ids)
{
if (!$this->schema->hasTable('Groups')) {
return;
}
$connection = $this->schema->getConnection();
foreach ($connection->table('Groups')->orderByDesc('UID')->get() as $data) {
if (isset($naming[$data->Name])) {
$data->Name = $naming[$data->Name];
}
$data->oldId = $data->UID;
if (isset($ids[$data->oldId])) {
$data->UID = $ids[$data->oldId];
} elseif (isset($ids[$data->oldId * -1])) {
$data->UID = $ids[$data->oldId * -1] * -1;
}
$connection
->table('Groups')
->where('UID', $data->oldId)
->update([
'UID' => $data->UID * -1,
'Name' => $data->Name,
]);
}
}
}

View File

@ -59,8 +59,8 @@ function admin_groups()
} else { } else {
switch ($request->input('action')) { switch ($request->input('action')) {
case 'edit': case 'edit':
if ($request->has('id') && preg_match('/^-\d{1,11}$/', $request->input('id'))) { if ($request->has('id')) {
$group_id = $request->input('id'); $group_id = (int)$request->input('id');
} else { } else {
return error('Incomplete call, missing Groups ID.', true); return error('Incomplete call, missing Groups ID.', true);
} }
@ -103,10 +103,9 @@ function admin_groups()
case 'save': case 'save':
if ( if (
$request->has('id') $request->has('id')
&& preg_match('/^-\d{1,11}$/', $request->input('id'))
&& $request->hasPostData('submit') && $request->hasPostData('submit')
) { ) {
$group_id = $request->input('id'); $group_id = (int)$request->input('id');
} else { } else {
return error('Incomplete call, missing Groups ID.', true); return error('Incomplete call, missing Groups ID.', true);
} }
@ -117,7 +116,8 @@ function admin_groups()
Db::delete('DELETE FROM `GroupPrivileges` WHERE `group_id`=?', [$group_id]); Db::delete('DELETE FROM `GroupPrivileges` WHERE `group_id`=?', [$group_id]);
$privilege_names = []; $privilege_names = [];
foreach ($privileges as $privilege) { foreach ($privileges as $privilege) {
if (preg_match('/^\d+$/', $privilege)) { $privilege = (int)$privilege;
if ($privilege) {
$group_privileges_source = Db::selectOne( $group_privileges_source = Db::selectOne(
'SELECT `name` FROM `Privileges` WHERE `id`=? LIMIT 1', 'SELECT `name` FROM `Privileges` WHERE `id`=? LIMIT 1',
[$privilege] [$privilege]

View File

@ -127,24 +127,24 @@ function admin_user()
$html .= '<hr />'; $html .= '<hr />';
$my_highest_group = DB::selectOne( $my_highest_group = DB::selectOne(
'SELECT group_id FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` LIMIT 1', 'SELECT group_id FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` DESC LIMIT 1',
[$user->id] [$user->id]
); );
if (!empty($my_highest_group)) { if (!empty($my_highest_group)) {
$my_highest_group = $my_highest_group['group_id']; $my_highest_group = $my_highest_group['group_id'];
} }
$his_highest_group = DB::selectOne( $angel_highest_group = DB::selectOne(
'SELECT `group_id` FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` LIMIT 1', 'SELECT `group_id` FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` LIMIT 1',
[$user_id] [$user_id]
); );
if (!empty($his_highest_group)) { if (!empty($angel_highest_group)) {
$his_highest_group = $his_highest_group['group_id']; $angel_highest_group = $angel_highest_group['group_id'];
} }
if ( if (
($user_id != $user->id || auth()->can('admin_groups')) ($user_id != $user->id || auth()->can('admin_groups'))
&& ($my_highest_group <= $his_highest_group || is_null($his_highest_group)) && ($my_highest_group >= $angel_highest_group || is_null($angel_highest_group))
) { ) {
$html .= 'Hier kannst Du die Benutzergruppen des Engels festlegen:<form action="' $html .= 'Hier kannst Du die Benutzergruppen des Engels festlegen:<form action="'
. page_link_to('admin_user', ['action' => 'save_groups', 'id' => $user_id]) . page_link_to('admin_user', ['action' => 'save_groups', 'id' => $user_id])
@ -160,7 +160,7 @@ function admin_user()
`UserGroups`.`group_id` = `Groups`.`UID` `UserGroups`.`group_id` = `Groups`.`UID`
AND `UserGroups`.`uid` = ? AND `UserGroups`.`uid` = ?
) )
WHERE `Groups`.`UID` >= ? WHERE `Groups`.`UID` <= ?
ORDER BY `Groups`.`Name` ORDER BY `Groups`.`Name`
', ',
[ [
@ -192,19 +192,19 @@ function admin_user()
case 'save_groups': case 'save_groups':
if ($user_id != $user->id || auth()->can('admin_groups')) { if ($user_id != $user->id || auth()->can('admin_groups')) {
$my_highest_group = DB::selectOne( $my_highest_group = DB::selectOne(
'SELECT * FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id`', 'SELECT * FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` DESC LIMIT 1',
[$user->id] [$user->id]
); );
$his_highest_group = DB::selectOne( $angel_highest_group = DB::selectOne(
'SELECT * FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id`', 'SELECT * FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` DESC LIMIT 1',
[$user_id] [$user_id]
); );
if ( if (
count($my_highest_group) > 0 $my_highest_group
&& ( && (
empty($his_highest_group) empty($angel_highest_group)
|| ($my_highest_group['group_id'] <= $his_highest_group['group_id']) || ($my_highest_group['group_id'] >= $angel_highest_group['group_id'])
) )
) { ) {
$groups_source = DB::select( $groups_source = DB::select(
@ -215,7 +215,7 @@ function admin_user()
`UserGroups`.`group_id` = `Groups`.`UID` `UserGroups`.`group_id` = `Groups`.`UID`
AND `UserGroups`.`uid` = ? AND `UserGroups`.`uid` = ?
) )
WHERE `Groups`.`UID` >= ? WHERE `Groups`.`UID` <= ?
ORDER BY `Groups`.`Name` ORDER BY `Groups`.`Name`
', ',
[ [

View File

@ -869,8 +869,7 @@ function User_groups_render($user_groups)
{ {
$output = []; $output = [];
foreach ($user_groups as $group) { foreach ($user_groups as $group) {
$groupName = preg_replace('/(^\d+-)/', '', $group['Name']); $output[] = __($group['Name']);
$output[] = __($groupName);
} }
return div('col-md-2', [ return div('col-md-2', [

View File

@ -28,6 +28,9 @@ class Authenticator
/** @var int|string|null */ /** @var int|string|null */
protected $passwordAlgorithm = PASSWORD_DEFAULT; protected $passwordAlgorithm = PASSWORD_DEFAULT;
/** @var int */
protected $guestRole = 10;
/** /**
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @param Session $session * @param Session $session
@ -119,7 +122,7 @@ class Authenticator
} }
if (empty($this->permissions)) { if (empty($this->permissions)) {
$this->permissions = $this->getPermissionsByGroup(-10); $this->permissions = $this->getPermissionsByGroup($this->guestRole);
} }
} }
@ -200,6 +203,22 @@ class Authenticator
$this->passwordAlgorithm = $passwordAlgorithm; $this->passwordAlgorithm = $passwordAlgorithm;
} }
/**
* @return int
*/
public function getGuestRole()
{
return $this->guestRole;
}
/**
* @param int $guestRole
*/
public function setGuestRole(int $guestRole)
{
$this->guestRole = $guestRole;
}
/** /**
* @param User $user * @param User $user
* @return array * @return array

View File

@ -14,6 +14,7 @@ class AuthenticatorServiceProvider extends ServiceProvider
/** @var Authenticator $authenticator */ /** @var Authenticator $authenticator */
$authenticator = $this->app->make(Authenticator::class); $authenticator = $this->app->make(Authenticator::class);
$authenticator->setPasswordAlgorithm($config->get('password_algorithm')); $authenticator->setPasswordAlgorithm($config->get('password_algorithm'));
$authenticator->setGuestRole($config->get('auth_guest_role', $authenticator->getGuestRole()));
$this->app->instance(Authenticator::class, $authenticator); $this->app->instance(Authenticator::class, $authenticator);
$this->app->instance('authenticator', $authenticator); $this->app->instance('authenticator', $authenticator);

View File

@ -58,6 +58,7 @@ trait HasDatabase
['migration' => '2021_12_30_000000_remove_admin_news_html_privilege'], ['migration' => '2021_12_30_000000_remove_admin_news_html_privilege'],
['migration' => '2022_06_02_000000_create_voucher_edit_permission'], ['migration' => '2022_06_02_000000_create_voucher_edit_permission'],
['migration' => '2022_06_03_000000_shifts_add_transaction_id'], ['migration' => '2022_06_03_000000_shifts_add_transaction_id'],
['migration' => '2022_07_21_000000_fix_old_groups_table_id_and_name'],
['migration' => '2022_10_21_000000_add_hide_register_to_angeltypes'], ['migration' => '2022_10_21_000000_add_hide_register_to_angeltypes'],
] ]
); );

View File

@ -22,6 +22,7 @@ class AuthenticatorServiceProviderTest extends ServiceProviderTest
$config = new Config(); $config = new Config();
$config->set('password_algorithm', PASSWORD_DEFAULT); $config->set('password_algorithm', PASSWORD_DEFAULT);
$config->set('auth_guest_role', 42);
$app->instance('config', $config); $app->instance('config', $config);
$serviceProvider = new AuthenticatorServiceProvider($app); $serviceProvider = new AuthenticatorServiceProvider($app);
@ -34,5 +35,6 @@ class AuthenticatorServiceProviderTest extends ServiceProviderTest
/** @var Authenticator $auth */ /** @var Authenticator $auth */
$auth = $app->get(Authenticator::class); $auth = $app->get(Authenticator::class);
$this->assertEquals(PASSWORD_DEFAULT, $auth->getPasswordAlgorithm()); $this->assertEquals(PASSWORD_DEFAULT, $auth->getPasswordAlgorithm());
$this->assertEquals(42, $auth->getGuestRole());
} }
} }

View File

@ -132,7 +132,7 @@ class AuthenticatorTest extends ServiceProviderTest
->getMock(); ->getMock();
$auth->expects($this->exactly(1)) $auth->expects($this->exactly(1))
->method('getPermissionsByGroup') ->method('getPermissionsByGroup')
->with(-10) ->with(10)
->willReturn([]); ->willReturn([]);
$auth->expects($this->exactly(1)) $auth->expects($this->exactly(1))
->method('getPermissionsByUser') ->method('getPermissionsByUser')
@ -245,6 +245,18 @@ class AuthenticatorTest extends ServiceProviderTest
$this->assertEquals(PASSWORD_ARGON2I, $auth->getPasswordAlgorithm()); $this->assertEquals(PASSWORD_ARGON2I, $auth->getPasswordAlgorithm());
} }
/**
* @covers \Engelsystem\Helpers\Authenticator::setGuestRole
* @covers \Engelsystem\Helpers\Authenticator::getGuestRole
*/
public function testGuestRole()
{
$auth = $this->getAuthenticator();
$auth->setGuestRole(42);
$this->assertEquals(42, $auth->getGuestRole());
}
/** /**
* @return Authenticator * @return Authenticator
*/ */