MicroFrameWork/kernel/modules/user/service/UserService.php

85 lines
1.9 KiB
PHP
Raw Normal View History

2024-07-24 14:07:45 +03:00
<?php
2024-09-06 16:53:20 +03:00
namespace kernel\modules\user\service;
2024-07-24 14:07:45 +03:00
use kernel\FormModel;
2024-09-11 17:38:46 +03:00
use kernel\modules\user\models\User;
2024-07-24 14:07:45 +03:00
class UserService
{
2024-07-24 17:22:59 +03:00
public function create(FormModel $form_model): false|User
2024-07-24 14:07:45 +03:00
{
$model = new User();
$model->username = $form_model->getItem('username');
$model->email = $form_model->getItem('email');
$model->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
2024-07-24 17:22:59 +03:00
if ($model->save()){
return $model;
}
return false;
2024-07-24 14:07:45 +03:00
}
2024-07-25 13:23:50 +03:00
public function update(FormModel $form_model, User $user): false|User
{
$user->username = $form_model->getItem('username');
$user->email = $form_model->getItem('email');
$user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
if ($user->save()){
return $user;
}
return false;
}
2024-10-22 11:09:35 +03:00
/**
* @param string $field
* @param string $value
* @return mixed
*/
2024-09-24 17:22:09 +03:00
public function getByField(string $field, string $value)
{
return User::where($field, $value)->first();
}
2024-07-26 16:09:06 +03:00
public static function createUsernameArr(): array
{
foreach (User::all()->toArray() as $user) {
2024-07-26 16:26:01 +03:00
$userArr[$user['id']] = $user['username'];
2024-07-26 16:09:06 +03:00
}
if (!empty($userArr)) {
return $userArr;
}
return [];
}
2024-09-24 17:22:09 +03:00
public static function getAuthUser()
{
if (isset($_COOKIE['user_id'])){
$user = User::where("id", $_COOKIE['user_id'])->first();
if ($user){
return $user;
}
}
return false;
}
public static function getAuthUsername(): string
{
$user = self::getAuthUser();
if ($user){
return $user->username;
}
return '';
}
2024-10-22 11:09:35 +03:00
public function getByAccessToken(string $token)
{
return $this->getByField("access_token", $token);
}
2024-07-24 14:07:45 +03:00
}