Skip to content

Commit

Permalink
feat: Move email from usr_mails table directly to column email in use…
Browse files Browse the repository at this point in the history
…r table
  • Loading branch information
KminekMatej committed Sep 15, 2024
1 parent a216bac commit 917df2f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 44 deletions.
30 changes: 30 additions & 0 deletions app/module/admin/migrations/2024-09-15T17-16-04.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

-- SQL Migration file
-- Author: Matěj Kmínek <[email protected]>
-- Created: 15.09.2024 17:16:04


/*
Comment for this migration: (not neccessary, but can be handy)
*/


-- UP:
-- commands that updates database shall be written here:

ALTER TABLE `user` ADD `email` VARCHAR(1023) NULL DEFAULT NULL AFTER `password`;
UPDATE `user` SET `email` = (SELECT `email` FROM `usr_mails` WHERE `user_id` = `user`.`id` LIMIT 1);
DROP TABLE `usr_mails`;
DROP VIEW IF EXISTS `v_users`;


-- DOWN:
-- commands that reverts updates from UP section shall be written here:

CALL 'Migrations DOWN are for security reasons temporarily DISABLED. Impossible to migrate down from 2024-09-15T17-16-04, in file 2024-09-15T17-16-04.sql';

-- DOWN migrations are disabled for security reasons.
-- These migrations might often include removal of some existent database columns, which is very dangerous to do on production servers.
-- Whenever we would migrate DOWN, then it would be impossible to migrate UP again, without loss of data.
1 change: 0 additions & 1 deletion app/module/core/factory/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ public function createUserConfigForm(Closure $onSuccess, ?User $user): Form
User::STATUS_PLAYER => $this->translator->translate("team.PLAYER", 1),
User::STATUS_MEMBER => $this->translator->translate("team.MEMBER", 1),
User::STATUS_SICK => $this->translator->translate("team.SICK", 1),
User::STATUS_DELETED => $this->translator->translate("team.DELETED", 1),
];

$rolesList = [
Expand Down
48 changes: 6 additions & 42 deletions app/module/user/manager/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function playersOnly(array $users): array
public function getSimpleUser(int $userId): ?SimpleUser
{
if (empty($this->simpleUserCache) || !array_key_exists($userId, $this->simpleUserCache)) {
$allRows = $this->database->table(User::VIEW)->fetchAll();
$allRows = $this->database->table(User::TABLE)->fetchAll();
foreach ($allRows as $userRow) {
$this->simpleUserCache[$userRow->id] = new SimpleUser($userRow->id, $userRow->user_name, $userRow->call_name, $this->getPictureUrl($userRow->id), (strtoupper($userRow->sex) == "FEMALE" ? "FEMALE" : "MALE"), $userRow->status, $userRow->email);
}
Expand All @@ -101,7 +101,7 @@ public function getSimpleUser(int $userId): ?SimpleUser
*/
public function getSimpleUsers(?array $userIds = null): array
{
$selector = $this->database->table(User::VIEW);
$selector = $this->database->table(User::TABLE);
if ($userIds) {
$selector->where("id", $userIds);
}
Expand Down Expand Up @@ -157,35 +157,7 @@ public function createByArray(array $array): ActiveRow
$array["roles"] = implode(",", $array["roles"]);
}

$createdRow = parent::createByArray($array);

$this->saveEmail($createdRow->id, $array["email"]);

return $createdRow;
}

/**
* Function to save email for given user.
* @param string $type Default DEF
* @throws AbortException
*/
private function saveEmail(int $userId, string $email, string $type = "DEF"): void
{
$updated = $this->database->table(User::TABLE_MAILS)->where("user_id", $userId)->where("type", $type)->update(["email" => $email]);

if ($updated === 0) {
$created = $this->database->table(User::TABLE_MAILS)->insert(
[
"user_id" => $userId,
"type" => $type,
"email" => $email,
]
);

if (!$created) {
$this->responder->E4009_CREATE_FAILED(User::MODULE);
}
}
return parent::createByArray($array);
}

/**
Expand All @@ -212,10 +184,6 @@ public function updateByArray(int $id, array $array): int
}
}

if (array_key_exists("email", $array) && !empty($array["email"]) && $array["email"] !== $userModel->getEmail()) {
$this->saveEmail($id, $array["email"]);
}

if (array_key_exists("roles", $array) && is_array($array["roles"])) {
$array["roles"] = implode(",", $array["roles"]);
}
Expand Down Expand Up @@ -251,10 +219,6 @@ public function map(?IRow $row, bool $force = false): ?BaseModel
$user->setPictureUrl($this->getPictureUrl($row->id));
$user->setIsNew($user->getCreatedAt() > new DateTime("- 14 days"));

$emailRow = $row->related(User::TABLE_MAILS, "user_id")->where("type", "DEF")->fetch();

$user->setEmail($emailRow ? $emailRow["email"] : null);

$user->setWebName($user->getId() . "-" . Strings::webalize($user->getDisplayName()));

$this->addWarnings($user);
Expand Down Expand Up @@ -334,7 +298,7 @@ public function getExistingLoginsExcept(?string $exceptLogin = null): array
*/
public function getExistingEmails(): array
{
return $this->database->table(User::VIEW)->fetchPairs(null, "email");
return $this->database->table(User::TABLE)->fetchPairs(null, "email");
}

/**
Expand Down Expand Up @@ -369,8 +333,8 @@ public function checkCredentials(Team $team, string $username, string $password)
*/
public function getIdByEmail(string $email): ?int
{
$row = $this->database->table(User::TABLE_MAILS)->where("email", $email)->fetch();
return $row !== null ? $row->user_id : null;
$row = $this->database->table(User::TABLE)->where("email", $email)->fetch();
return $row !== null ? $row->id : null;
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/module/user/mapper/UserMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static function scheme(): array
return [
Field::int()->withPropertyAndColumn("id", false, false),
Field::string(20)->withColumn("user_name", true)->setProperty("login"),
Field::string(1023)->withPropertyAndColumn("email"),
Field::int()->withColumn("can_login")->setProperty("canLogin"),
Field::int()->withColumn("editable_call_name")->setProperty("canEditCallName"),
Field::datetime()->withColumn("created_at", false, true)->setProperty("createdAt"),
Expand Down
1 change: 0 additions & 1 deletion app/module/user/model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class User extends BaseModel
public const TABLE = "user";
public const TABLE_MAILS = "usr_mails";
public const TABLE_PWD_RESET = "pwd_reset";
public const VIEW = "v_user";
public const MODULE = "user";
public const ROLE_SUPER = "SUPER";
public const ROLE_USER = "USR";
Expand Down

0 comments on commit 917df2f

Please sign in to comment.