chg: [user:updateToAdvancedAuthKeys] Functionality accessible via the CLI

pull/7578/head
mokaddem 2021-07-16 15:13:55 +02:00
parent 4e1a6c5221
commit e7fd73e50e
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
3 changed files with 38 additions and 24 deletions

View File

@ -716,4 +716,12 @@ class AdminShell extends AppShell
$this->out('Dropping default galaxy clusters. This process might take some time...');
$this->GalaxyCluster->wipe_default();
}
public function updateToAdvancedAuthKeys()
{
$this->loadModel('User');
$updated = $this->User->updateToAdvancedAuthKeys();
$message = __('The upgrade process is complete, %s authkey(s) generated.', $updated);
$this->out($message);
}
}

View File

@ -2730,30 +2730,7 @@ class UsersController extends AppController
if (!$this->request->is('post')) {
throw new MethodNotAllowedException(__('This endpoint can only be triggered via POST requests.'));
}
$users = $this->User->find('all', [
'recursive' => -1,
'contain' => ['AuthKey'],
'fields' => ['id', 'authkey']
]);
$updated = 0;
foreach ($users as $user) {
if (!empty($user['AuthKey'])) {
$currentKeyStart = substr($user['User']['authkey'], 0, 4);
$currentKeyEnd = substr($user['User']['authkey'], -4);
foreach ($user['AuthKey'] as $authkey) {
if ($authkey['authkey_start'] === $currentKeyStart && $authkey['authkey_end'] === $currentKeyEnd) {
continue 2;
}
}
}
$this->User->AuthKey->create();
$this->User->AuthKey->save([
'authkey' => $user['User']['authkey'],
'expiration' => 0,
'user_id' => $user['User']['id']
]);
$updated += 1;
}
$updated = $this->User->updateToAdvancedAuthKeys();
$message = __('The upgrade process is complete, %s authkey(s) generated.', $updated);
if ($this->_isRest()) {
return $this->RestResponse->saveSuccessResponse('User', 'acceptRegistrations', false, $this->response->type(), $message);

View File

@ -1398,4 +1398,33 @@ class User extends AppModel
return null;
}
}
public function updateToAdvancedAuthKeys()
{
$users = $this->find('all', [
'recursive' => -1,
'contain' => ['AuthKey'],
'fields' => ['id', 'authkey']
]);
$updated = 0;
foreach ($users as $user) {
if (!empty($user['AuthKey'])) {
$currentKeyStart = substr($user['User']['authkey'], 0, 4);
$currentKeyEnd = substr($user['User']['authkey'], -4);
foreach ($user['AuthKey'] as $authkey) {
if ($authkey['authkey_start'] === $currentKeyStart && $authkey['authkey_end'] === $currentKeyEnd) {
continue 2;
}
}
}
$this->AuthKey->create();
$this->AuthKey->save([
'authkey' => $user['User']['authkey'],
'expiration' => 0,
'user_id' => $user['User']['id']
]);
$updated += 1;
}
return $updated;
}
}