Рефакторинг. В АПИ добавлены методы в изменения: username, email, password

This commit is contained in:
iIronside
2023-10-20 15:02:59 +03:00
parent b02d0b3ddf
commit 672d0833c5
10 changed files with 385 additions and 77 deletions

View File

@ -0,0 +1,168 @@
<?php
namespace frontend\modules\api\services;
use common\models\Manager;
use common\models\ManagerEmployee;
use common\models\Position;
use common\models\UserCard;
use common\models\UserCardPortfolioProjects;
use frontend\modules\api\models\profile\ProfileSearchForm;
use Yii;
use yii\web\ServerErrorHttpException;
class ProfileService
{
public static function getPortfolioProjects($card_id)
{
/** @var UserCardPortfolioProjects[] $portfolioProjects */
$portfolioProjects = UserCardPortfolioProjects::find()
->where(['card_id' => $card_id])
->all();
$array = [];
if (!empty($portfolioProjects)) {
foreach ($portfolioProjects as $project) {
array_push(
$array,
[
'id' => $project->id,
'title' => $project->title,
'description' => $project->description,
'main_stack' => $project->skill->name,
'additional_stack' => $project->additional_stack,
'link' => $project->link
]
);
}
}
return $array;
}
/**
* @throws ServerErrorHttpException
*/
public static function getMainData($user_id): array
{
$userCard = UserCard::findOne(['id_user' => $user_id]);
if (empty($userCard)) {
throw new ServerErrorHttpException('Profile not found!');
}
return array('fio' => $userCard->fio,
'photo' => $userCard->photo,
'gender' => $userCard->gender,
'level' => $userCard->level,
'years_of_exp' => $userCard->years_of_exp,
'specification' => $userCard->specification,
'position_name' => $userCard->position->name);
}
public static function getProfile($id, $request): ?array
{
$searchModel = new ProfileSearchForm();
$searchModel->attributes = $request;
if ($id) {
return $searchModel->byId();
}
return $searchModel->byParams();
}
public static function getProfileById($id): ?array
{
$searchModel = new ProfileSearchForm();
$searchModel->id = $id;
return $searchModel->byId();
}
/**
* @throws ServerErrorHttpException
*/
public static function getProfileWithReportPermission($user_card_id): ?array
{
if (UserCard::find()->where(['id' => $user_card_id])->exists()) {
$searchModel = new ProfileSearchForm();
$searchModel->id = $user_card_id;
$profile = $searchModel->byId();
self::addPermission($profile, $user_card_id);
return $profile;
}
throw new ServerErrorHttpException('There is no user with this id');
}
/**
* @return array|\yii\db\ActiveRecord[]
*/
public static function getPositionsList()
{
return Position::find()->all();
}
private static function addPermission(&$profile, $user_card_id)
{
$searcherCardID = self::getSearcherCardID(Yii::$app->user->getId());
if (self::checkReportPermission($user_card_id, $searcherCardID)) {
$profile += ['report_permission' => '1'];
} else {
$profile += ['report_permission' => '0'];
}
}
private static function getSearcherCardID($user_id): int
{
return UserCard::findOne(['id_user' => $user_id])->id;
}
private static function checkReportPermission($user_card_id, $searcherCardID): bool
{
if (self::isMyProfile($user_card_id, $searcherCardID)
or self::isMyEmployee($user_card_id, $searcherCardID)) {
return true;
}
return false;
}
private static function isMyProfile($user_card_id, $searcherCardID): bool
{
if ($user_card_id == $searcherCardID) {
return true;
}
return false;
}
private static function isMyEmployee($user_card_id, $searcherCardID): bool
{
if (!self::amIManager($searcherCardID)) {
return false;
}
if (self::isMyEmployer($user_card_id, $searcherCardID)) {
return true;
}
return false;
}
private static function amIManager($searcherCardID): bool
{
if (Manager::find()->where(['user_card_id' => $searcherCardID])->exists()) {
return true;
}
return false;
}
private static function isMyEmployer($user_card_id, $searcherCardID): bool
{
$manager = Manager::find()->where(['user_card_id' => $searcherCardID])->one();
$exist = ManagerEmployee::find()
->where(['manager_id' => $manager->id, 'user_card_id' => $user_card_id])
->exists();
if ($exist) {
return true;
}
return false;
}
}

View File

@ -0,0 +1,102 @@
<?php
namespace frontend\modules\api\services;
use Exception;
use frontend\modules\api\models\LoginForm;
use frontend\modules\api\models\profile\ProfileChangeEmailForm;
use frontend\modules\api\models\profile\ProfileChangePasswordForm;
use frontend\modules\api\models\profile\ProfileChangePersonalDataForm;
use frontend\modules\api\models\profile\User;
use Yii;
use yii\web\BadRequestHttpException;
class UserService
{
public function login(array $params)
{
$model = new LoginForm();
$model->load($params, '');
if ($model->load($params, '') && $model->login()) {
/** @var User $user */
$user = $model->getUser();
return [
'access_token' => $model->login(),
'access_token_expired_at' => $model->getUser()->getTokenExpiredAt(),
'id' => $user->id,
'status' => $user->userCard->status ?? null,
'card_id' => $user->userCard->id ?? null,
];
} else {
throw new BadRequestHttpException(json_encode($model->errors));
}
}
public function findCurrentUser(): User
{
$user = User::findOne(Yii::$app->user->id);
if (!$user){
throw new BadRequestHttpException("User not found");
}
return $user;
}
/**
* @throws Exception
*/
public function changeChangePersonalData(array $params)
{
$form = new ProfileChangePersonalDataForm();
$form->load($params);
if (!$form->validate()){
return $form;
}
$user = User::findOne(['id' => Yii::$app->user->identity->getId()]);;
$user->username = $form->newUsername;
if (!$user->save()) {
throw new Exception('User dont save');
}
return ['status' => 'success'];
}
public function changeEmail(array $params)
{
$form = new ProfileChangeEmailForm();
$form->load($params);
if (!$form->validate()) {
return $form;
}
$user = User::findOne(Yii::$app->user->identity->getId());
$user->email = $form->newEmail;
$user->save();
return ['status' => 'success'];
}
public function changePassword(array $params)
{
$form = new ProfileChangePasswordForm();
$form->load($params);
if (!$form->validate()){
return $form;
}
$user = User::findOne(Yii::$app->user->identity->getId());
if ($user->validatePassword($form->password)) {
$user->password_hash = Yii::$app->security->generatePasswordHash($form->newPassword);
$user->save();
return ['status' => 'success'];
}
return ['error' => 'Wrong password!'];
}
}