2024-03-01 20:34:36 +01:00
|
|
|
<?php
|
|
|
|
|
2024-05-13 20:06:05 +02:00
|
|
|
namespace App\Repositories;
|
2024-03-01 20:34:36 +01:00
|
|
|
|
2024-05-13 21:36:06 +02:00
|
|
|
use App\Services\Assets\DocService;
|
2024-05-13 20:06:05 +02:00
|
|
|
use App\Interfaces\MemberRepositoryInterface;
|
2024-03-06 16:50:09 +01:00
|
|
|
use ReallySimpleJWT\Token;
|
2024-05-13 21:36:06 +02:00
|
|
|
use Carbon\Carbon;
|
2024-03-06 16:50:09 +01:00
|
|
|
|
2024-03-01 20:34:36 +01:00
|
|
|
use function _\find;
|
2024-05-13 21:36:06 +02:00
|
|
|
use function _\findIndex;
|
2024-03-01 20:34:36 +01:00
|
|
|
|
2024-05-13 20:06:05 +02:00
|
|
|
class MemberRepository implements MemberRepositoryInterface
|
2024-03-01 20:34:36 +01:00
|
|
|
{
|
2024-05-13 20:06:05 +02:00
|
|
|
protected $folks;
|
2024-05-13 21:36:06 +02:00
|
|
|
protected $docs;
|
2024-03-01 20:34:36 +01:00
|
|
|
|
2024-05-13 21:36:06 +02:00
|
|
|
public function __construct(DocService $docService)
|
2024-03-01 20:34:36 +01:00
|
|
|
{
|
2024-05-13 21:36:06 +02:00
|
|
|
$this->docs = $docService;
|
2024-05-13 20:06:05 +02:00
|
|
|
if (file_exists(env('FOLKS_PATH'))) {
|
|
|
|
$this->folks = json_decode(file_get_contents(env('FOLKS_PATH')), true);
|
|
|
|
} else {
|
|
|
|
$this->folks = json_decode(file_get_contents('../content/init/folks-template.json'), true);
|
|
|
|
}
|
2024-03-01 20:34:36 +01:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:06:05 +02:00
|
|
|
public function getAll()
|
2024-03-01 20:34:36 +01:00
|
|
|
{
|
2024-05-13 20:06:05 +02:00
|
|
|
return $this->$folks;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getById($id)
|
|
|
|
{
|
|
|
|
$member = find($this->folks, ['id' => $id]);
|
|
|
|
return $member;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getByHandle($handle)
|
|
|
|
{
|
|
|
|
$member = find($this->folks, ['handle' => $handle]);
|
|
|
|
return $member;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
//delete member stuff
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create($member)
|
|
|
|
{
|
|
|
|
//make new member
|
|
|
|
}
|
|
|
|
|
2024-05-13 21:36:06 +02:00
|
|
|
public function update($member)
|
2024-05-13 20:06:05 +02:00
|
|
|
{
|
2024-05-13 21:36:06 +02:00
|
|
|
$index = findIndex($this->folks, ['id' => $member->id]);
|
|
|
|
$this->folks[$index]['handle'] = $member->handle;
|
|
|
|
$this->folks[$index]['email'] = $member->email;
|
2024-05-31 20:56:38 +02:00
|
|
|
$this->folks[$index]['avatar'] = $member->avatar;
|
2024-05-13 21:36:06 +02:00
|
|
|
$this->folks[$index]['updated'] = Carbon::now();
|
|
|
|
//save new folks file
|
|
|
|
$this->docs::writeSettings($this->folks, env('FOLKS_PATH'));
|
|
|
|
//update session
|
|
|
|
session()->put('member', $this->folks[$index]);
|
2024-05-13 20:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function auth($request)
|
|
|
|
{
|
|
|
|
//suth stuff
|
|
|
|
$folks = $this->folks;
|
|
|
|
$found = $this->getByHandle($request->handle);
|
2024-03-01 20:34:36 +01:00
|
|
|
if ($found) {
|
2024-03-03 20:48:22 +01:00
|
|
|
if (password_verify($request->password, $found['password'])) {
|
2024-03-06 16:50:09 +01:00
|
|
|
$member = [
|
2024-05-13 21:36:06 +02:00
|
|
|
'id' => $found['id'],
|
2024-03-06 16:50:09 +01:00
|
|
|
'handle' => $found['handle'],
|
|
|
|
'email' => $found['email'],
|
|
|
|
'role' => $found['role'],
|
2024-05-31 20:56:38 +02:00
|
|
|
'avatar' => $found['avatar'],
|
2024-03-06 16:50:09 +01:00
|
|
|
'key' => $found['key'],
|
|
|
|
'secret' => $found['secret'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$token = Token::create(
|
|
|
|
$found['key'],
|
|
|
|
$found['secret'],
|
|
|
|
time() + 3600,
|
|
|
|
'localhost'
|
|
|
|
); //expires in an hour
|
|
|
|
$form_token = md5(uniqid(microtime(), true));
|
|
|
|
$request->session()->put('member', $member);
|
|
|
|
$request->session()->put('token', $token);
|
|
|
|
$request->session()->put('form_token', $form_token);
|
2024-03-03 20:48:22 +01:00
|
|
|
return ['status' => true, 'message' => 'HEY WELCOME BACK'];
|
2024-03-01 20:34:36 +01:00
|
|
|
//DO SESSION STUFF
|
|
|
|
} else {
|
2024-03-03 20:48:22 +01:00
|
|
|
return ['status' => false, 'message' => 'CHECK THAT PASSWORD'];
|
2024-03-01 20:34:36 +01:00
|
|
|
//RETURN ERROR
|
|
|
|
}
|
|
|
|
} else {
|
2024-03-03 20:48:22 +01:00
|
|
|
return ['status' => false, 'message' => 'CHECK THAT HANDLE'];
|
2024-03-01 20:34:36 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-06 16:50:09 +01:00
|
|
|
|
|
|
|
public static function status()
|
|
|
|
{
|
|
|
|
if (session('member') !== null) {
|
|
|
|
if (
|
|
|
|
Token::validate(session('token'), session('member')['secret']) &&
|
|
|
|
Token::validateExpiration(session('token'), session('member')['secret'])
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-03-01 20:34:36 +01:00
|
|
|
}
|