2021-09-17 23:55:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2021-12-02 13:15:53 +01:00
|
|
|
use App\Models\TwoFAccount;
|
2021-10-15 23:46:21 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-09-17 23:55:09 +02:00
|
|
|
|
|
|
|
class TwoFAccountService
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2021-09-21 22:46:35 +02:00
|
|
|
* Withdraw one or more twofaccounts from their group
|
2021-09-17 23:55:09 +02:00
|
|
|
*
|
2021-10-08 23:21:07 +02:00
|
|
|
* @param int|array|string $ids twofaccount ids to free
|
2021-09-17 23:55:09 +02:00
|
|
|
*/
|
2022-07-30 11:38:20 +02:00
|
|
|
public static function withdraw($ids) : void
|
2021-09-17 23:55:09 +02:00
|
|
|
{
|
2021-10-08 23:21:07 +02:00
|
|
|
// $ids as string could be a comma-separated list of ids
|
|
|
|
// so in this case we explode the string to an array
|
2022-07-30 11:38:20 +02:00
|
|
|
$ids = self::commaSeparatedToArray($ids);
|
2021-09-17 23:55:09 +02:00
|
|
|
|
2021-10-08 23:21:07 +02:00
|
|
|
// whereIn() expects an array
|
|
|
|
$ids = is_array($ids) ? $ids : func_get_args();
|
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
TwoFAccount::whereIn('id', $ids)
|
|
|
|
->update(
|
|
|
|
['group_id' => NULL]
|
|
|
|
);
|
|
|
|
|
|
|
|
Log::info(sprintf('TwoFAccounts #%s withdrawn', implode(',#', $ids)));
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-18 12:31:24 +02:00
|
|
|
/**
|
2021-09-21 22:46:35 +02:00
|
|
|
* Delete one or more twofaccounts
|
|
|
|
*
|
2021-10-08 23:21:07 +02:00
|
|
|
* @param int|array|string $ids twofaccount ids to delete
|
2021-09-21 22:46:35 +02:00
|
|
|
*
|
|
|
|
* @return int The number of deleted
|
2021-09-18 12:31:24 +02:00
|
|
|
*/
|
2022-07-30 11:38:20 +02:00
|
|
|
public static function delete($ids) : int
|
2021-09-18 12:31:24 +02:00
|
|
|
{
|
2021-10-08 23:21:07 +02:00
|
|
|
// $ids as string could be a comma-separated list of ids
|
|
|
|
// so in this case we explode the string to an array
|
2022-07-30 11:38:20 +02:00
|
|
|
$ids = self::commaSeparatedToArray($ids);
|
2022-06-01 00:10:29 +02:00
|
|
|
Log::info(sprintf('Deletion of TwoFAccounts #%s requested', is_array($ids) ? implode(',#', $ids) : $ids ));
|
2021-09-21 22:46:35 +02:00
|
|
|
$deleted = TwoFAccount::destroy($ids);
|
|
|
|
|
|
|
|
return $deleted;
|
2021-09-18 12:31:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-17 23:55:09 +02:00
|
|
|
|
2021-10-08 23:21:07 +02:00
|
|
|
/**
|
2022-09-07 17:54:27 +02:00
|
|
|
* Explode a comma separated list of IDs to an array of IDs
|
2021-10-08 23:21:07 +02:00
|
|
|
*
|
2022-09-07 17:54:27 +02:00
|
|
|
* @param int|array|string $ids
|
2021-10-08 23:21:07 +02:00
|
|
|
*/
|
2022-09-07 17:54:27 +02:00
|
|
|
private static function commaSeparatedToArray($ids) : mixed
|
2021-10-08 23:21:07 +02:00
|
|
|
{
|
2021-11-30 17:39:33 +01:00
|
|
|
if(is_string($ids))
|
|
|
|
{
|
|
|
|
$regex = "/^\d+(,{1}\d+)*$/";
|
|
|
|
if (preg_match($regex, $ids)) {
|
|
|
|
$ids = explode(',', $ids);
|
|
|
|
}
|
2021-10-08 23:21:07 +02:00
|
|
|
}
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2021-10-08 23:21:07 +02:00
|
|
|
return $ids;
|
|
|
|
}
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|