2021-09-17 23:55:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2022-10-07 18:58:48 +02:00
|
|
|
use App\Factories\MigratorFactoryInterface;
|
2023-02-22 20:21:36 +01:00
|
|
|
use App\Helpers\Helpers;
|
2023-02-25 21:12:10 +01:00
|
|
|
use App\Models\TwoFAccount;
|
2023-04-19 08:38:48 +02:00
|
|
|
use App\Models\User;
|
2022-10-07 18:58:48 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2023-03-22 15:39:51 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-09-17 23:55:09 +02:00
|
|
|
|
|
|
|
class TwoFAccountService
|
|
|
|
{
|
2022-10-07 18:58:48 +02:00
|
|
|
/**
|
2022-11-22 15:15:52 +01:00
|
|
|
* @var MigratorFactoryInterface The Migration service
|
2022-10-07 18:58:48 +02:00
|
|
|
*/
|
|
|
|
protected $migratorFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public function __construct(MigratorFactoryInterface $migratorFactory)
|
|
|
|
{
|
|
|
|
$this->migratorFactory = $migratorFactory;
|
|
|
|
}
|
|
|
|
|
2021-09-17 23:55:09 +02:00
|
|
|
/**
|
2021-09-21 22:46:35 +02:00
|
|
|
* Withdraw one or more twofaccounts from their group
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2024-04-20 19:03:44 +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
|
2023-02-22 20:21:36 +01:00
|
|
|
$ids = Helpers::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)
|
2023-12-20 16:55:58 +01:00
|
|
|
->update(
|
|
|
|
['group_id' => null]
|
|
|
|
);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-03-08 09:41:18 +01:00
|
|
|
Log::info(sprintf('TwoFAccounts IDs #%s withdrawn', implode(',', $ids)));
|
2021-09-17 23:55:09 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 18:58:48 +02:00
|
|
|
/**
|
|
|
|
* Convert a migration payload to a set of TwoFAccount objects
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2024-04-20 19:03:44 +02:00
|
|
|
* @param string $migrationPayload Migration payload from 2FA apps export feature
|
2022-11-21 11:16:43 +01:00
|
|
|
* @return \Illuminate\Support\Collection<int|string, TwoFAccount> The converted accounts
|
2022-10-07 18:58:48 +02:00
|
|
|
*/
|
2022-10-12 11:13:13 +02:00
|
|
|
public function migrate(string $migrationPayload) : Collection
|
2022-10-07 18:58:48 +02:00
|
|
|
{
|
2022-11-22 15:15:52 +01:00
|
|
|
$migrator = $this->migratorFactory->create($migrationPayload);
|
2022-10-07 18:58:48 +02:00
|
|
|
$twofaccounts = $migrator->migrate($migrationPayload);
|
|
|
|
|
|
|
|
return self::markAsDuplicate($twofaccounts);
|
|
|
|
}
|
|
|
|
|
2022-12-13 15:57:33 +01:00
|
|
|
/**
|
|
|
|
* Export one or more twofaccounts
|
|
|
|
*
|
2024-04-20 19:03:44 +02:00
|
|
|
* @param int|array|string $ids twofaccount ids to delete
|
2022-12-13 15:57:33 +01:00
|
|
|
* @return \Illuminate\Support\Collection<int, TwoFAccount> The converted accounts
|
|
|
|
*/
|
|
|
|
public static function export($ids) : Collection
|
|
|
|
{
|
2023-03-10 22:59:46 +01:00
|
|
|
$ids = Helpers::commaSeparatedToArray($ids);
|
2023-03-08 09:41:57 +01:00
|
|
|
$ids = is_array($ids) ? $ids : func_get_args();
|
2023-03-10 22:59:46 +01:00
|
|
|
|
2022-12-13 15:57:33 +01:00
|
|
|
$twofaccounts = TwoFAccount::whereIn('id', $ids)->get();
|
|
|
|
|
|
|
|
return $twofaccounts;
|
|
|
|
}
|
|
|
|
|
2021-09-18 12:31:24 +02:00
|
|
|
/**
|
2021-09-21 22:46:35 +02:00
|
|
|
* Delete one or more twofaccounts
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2024-04-20 19:03:44 +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
|
2023-02-22 20:21:36 +01:00
|
|
|
$ids = Helpers::commaSeparatedToArray($ids);
|
2022-11-22 15:15:52 +01: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
|
|
|
}
|
|
|
|
|
2023-04-19 08:38:48 +02:00
|
|
|
/**
|
|
|
|
* Set owner of given twofaccounts
|
2023-12-20 16:55:58 +01:00
|
|
|
*
|
2023-04-19 08:38:48 +02:00
|
|
|
* @param \Illuminate\Support\Collection<int, TwoFAccount> $twofaccounts
|
|
|
|
*/
|
|
|
|
public static function setUser(Collection $twofaccounts, User $user) : void
|
|
|
|
{
|
|
|
|
$twofaccounts->each(function ($twofaccount, $key) use ($user) {
|
|
|
|
$twofaccount->user_id = $user->id;
|
|
|
|
$twofaccount->save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:58:48 +02:00
|
|
|
/**
|
2023-03-22 15:39:51 +01:00
|
|
|
* Return the given collection with items marked as Duplicates (using id=-1) if similar records exist
|
|
|
|
* in the authenticated user accounts
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
|
|
|
* @param \Illuminate\Support\Collection<int|string, TwoFAccount> $twofaccounts
|
2022-11-21 11:16:43 +01:00
|
|
|
* @return \Illuminate\Support\Collection<int|string, TwoFAccount>
|
2022-10-07 18:58:48 +02:00
|
|
|
*/
|
|
|
|
private static function markAsDuplicate(Collection $twofaccounts) : Collection
|
|
|
|
{
|
2023-03-22 15:39:51 +01:00
|
|
|
$userTwofaccounts = Auth::user()->twofaccounts;
|
2022-10-07 18:58:48 +02:00
|
|
|
|
2023-03-22 15:39:51 +01:00
|
|
|
$twofaccounts = $twofaccounts->map(function ($twofaccount, $key) use ($userTwofaccounts) {
|
|
|
|
if ($userTwofaccounts->contains(function ($value, $key) use ($twofaccount) {
|
2022-10-07 18:58:48 +02:00
|
|
|
return $value->secret == $twofaccount->secret
|
|
|
|
&& $value->service == $twofaccount->service
|
|
|
|
&& $value->account == $twofaccount->account
|
|
|
|
&& $value->otp_type == $twofaccount->otp_type
|
|
|
|
&& $value->digits == $twofaccount->digits
|
|
|
|
&& $value->algorithm == $twofaccount->algorithm;
|
|
|
|
})) {
|
2022-10-10 13:44:12 +02:00
|
|
|
$twofaccount->id = TwoFAccount::DUPLICATE_ID;
|
2022-10-07 18:58:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $twofaccount;
|
|
|
|
});
|
|
|
|
|
|
|
|
return $twofaccounts;
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|