actually update values in the active user object when updating things

This commit is contained in:
flash 2016-08-07 16:32:14 +02:00
parent 5a8a80641e
commit ba18a6bdcc

View file

@ -533,6 +533,8 @@ class User
// Save to the database // Save to the database
foreach ($ranks as $rank) { foreach ($ranks as $rank) {
$this->ranks[$rank] = Rank::construct($rank);
DB::table('user_ranks') DB::table('user_ranks')
->insert([ ->insert([
'rank_id' => $rank, 'rank_id' => $rank,
@ -552,6 +554,8 @@ class User
// Iterate over the ranks // Iterate over the ranks
foreach ($remove as $rank) { foreach ($remove as $rank) {
unset($this->ranks[$rank]);
DB::table('user_ranks') DB::table('user_ranks')
->where('user_id', $this->id) ->where('user_id', $this->id)
->where('rank_id', $rank) ->where('rank_id', $rank)
@ -562,19 +566,17 @@ class User
/** /**
* Change the main rank of a user. * Change the main rank of a user.
* @param int $rank * @param int $rank
* @return bool
*/ */
public function setMainRank($rank) public function setMainRank($rank)
{ {
// If it does exist update their row $this->mainRankId = $rank;
$this->mainRank = $this->ranks[$rank];
DB::table('users') DB::table('users')
->where('user_id', $this->id) ->where('user_id', $this->id)
->update([ ->update([
'rank_main' => $rank, 'rank_main' => $this->mainRankId,
]); ]);
// Return true if everything was successful
return true;
} }
/** /**
@ -816,11 +818,11 @@ class User
// Check if there's already a record of premium for this user in the database // Check if there's already a record of premium for this user in the database
$getUser = DB::table('premium') $getUser = DB::table('premium')
->where('user_id', $this->id) ->where('user_id', $this->id)
->get(); ->first();
// Calculate the (new) start and expiration timestamp // Calculate the (new) start and expiration timestamp
$start = $getUser ? $getUser[0]->premium_start : time(); $start = $getUser ? $getUser->premium_start : time();
$expire = $getUser ? $getUser[0]->premium_expire + $seconds : time() + $seconds; $expire = $getUser ? $getUser->premium_expire + $seconds : time() + $seconds;
// If the user already exists do an update call, otherwise an insert call // If the user already exists do an update call, otherwise an insert call
if ($getUser) { if ($getUser) {
@ -856,14 +858,12 @@ class User
$expire = $this->premiumInfo()->expire; $expire = $this->premiumInfo()->expire;
// Check if the user has static premium // Check if the user has static premium
if (!$expire if (!$expire && $this->permission(Site::STATIC_PREMIUM)) {
&& $this->permission(Site::STATIC_PREMIUM)) {
$expire = time() + 1; $expire = time() + 1;
} }
// Check if the user has premium and isn't in the premium rank // Check if the user has premium and isn't in the premium rank
if ($expire if ($expire && !$this->hasRanks([$premiumRank])) {
&& !$this->hasRanks([$premiumRank])) {
// Add the premium rank // Add the premium rank
$this->addRanks([$premiumRank]); $this->addRanks([$premiumRank]);
@ -871,8 +871,7 @@ class User
if ($this->mainRankId == $defaultRank) { if ($this->mainRankId == $defaultRank) {
$this->setMainRank($premiumRank); $this->setMainRank($premiumRank);
} }
} elseif (!$expire } elseif (!$expire && $this->hasRanks([$premiumRank])) {
&& $this->hasRanks([$premiumRank])) {
$this->removeRanks([$premiumRank]); $this->removeRanks([$premiumRank]);
if ($this->mainRankId == $premiumRank) { if ($this->mainRankId == $premiumRank) {
@ -893,12 +892,12 @@ class User
$check = DB::table('premium') $check = DB::table('premium')
->where('user_id', $this->id) ->where('user_id', $this->id)
->where('premium_expire', '>', time()) ->where('premium_expire', '>', time())
->get(); ->first();
$return = new stdClass; $return = new stdClass;
$return->start = $check ? $check[0]->premium_start : 0; $return->start = $check->premium_start ?? 0;
$return->expire = $check ? $check[0]->premium_expire : 0; $return->expire = $check->premium_expire ?? 0;
return $return; return $return;
} }
@ -936,11 +935,11 @@ class User
/** /**
* Alter the user's username. * Alter the user's username.
* @param string $username * @param string $username
* @param string $username_clean
*/ */
public function setUsername($username, $username_clean) public function setUsername($username)
{ {
// Insert into username_history table $username_clean = clean_string($username, true);
DB::table('username_history') DB::table('username_history')
->insert([ ->insert([
'change_time' => time(), 'change_time' => time(),
@ -951,12 +950,14 @@ class User
'username_old_clean' => $this->usernameClean, 'username_old_clean' => $this->usernameClean,
]); ]);
// Update userrow $this->username = $username;
$this->usernameClean = $username_clean;
DB::table('users') DB::table('users')
->where('user_id', $this->id) ->where('user_id', $this->id)
->update([ ->update([
'username' => $username, 'username' => $this->username,
'username_clean' => $username_clean, 'username_clean' => $this->usernameClean,
]); ]);
} }
@ -966,11 +967,12 @@ class User
*/ */
public function setMail($email) public function setMail($email)
{ {
// Update userrow $this->email = $email;
DB::table('users') DB::table('users')
->where('user_id', $this->id) ->where('user_id', $this->id)
->update([ ->update([
'email' => $email, 'email' => $this->email,
]); ]);
} }
@ -981,14 +983,15 @@ class User
public function setPassword($password) public function setPassword($password)
{ {
// Create hash // Create hash
$password = password_hash($password, PASSWORD_BCRYPT); $this->password = password_hash($password, PASSWORD_BCRYPT);
$this->passwordChan = time();
// Update userrow // Update userrow
DB::table('users') DB::table('users')
->where('user_id', $this->id) ->where('user_id', $this->id)
->update([ ->update([
'password' => $password, 'password' => $this->password,
'password_chan' => time(), 'password_chan' => $this->passwordChan,
]); ]);
} }