Model fixing, moving things around to improve code.
Adds unique UUID generator, moves functions into repositories for adding servers and users, cleans up code, adding more comments.
This commit is contained in:
parent
01eaeaf178
commit
22b0bbf6ce
8 changed files with 333 additions and 250 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Pterodactyl\Models;
|
||||
|
||||
use Hash;
|
||||
use Google2FA;
|
||||
use Pterodactyl\Exceptions\AccountNotFoundException;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
|
@ -47,51 +48,21 @@ class User extends Model implements AuthenticatableContract,
|
|||
return $this->hasMany(Permission::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the TOTP secret for an account.
|
||||
*
|
||||
* @param int $id Account ID for which we want to generate a TOTP secret
|
||||
* @return string
|
||||
*/
|
||||
public static function setTotpSecret($id)
|
||||
{
|
||||
|
||||
$totpSecretKey = Google2FA::generateSecretKey();
|
||||
|
||||
$user = User::find($id);
|
||||
|
||||
if (!$user) {
|
||||
throw new AccountNotFoundException('An account with that ID (' . $id . ') does not exist in the system.');
|
||||
}
|
||||
|
||||
$user->totp_secret = $totpSecretKey;
|
||||
$user->save();
|
||||
|
||||
return $totpSecretKey;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables TOTP on an account if the token is valid.
|
||||
*
|
||||
* @param int $id Account ID for which we want to generate a TOTP secret
|
||||
* @param int $token The token that we want to verify.
|
||||
* @return boolean
|
||||
*/
|
||||
public static function toggleTotp($id, $token)
|
||||
public function toggleTotp($token)
|
||||
{
|
||||
|
||||
$user = User::find($id);
|
||||
|
||||
if (!$user) {
|
||||
throw new AccountNotFoundException('An account with that ID (' . $id . ') does not exist in the system.');
|
||||
}
|
||||
|
||||
if (!Google2FA::verifyKey($user->totp_secret, $token)) {
|
||||
if (!Google2FA::verifyKey($this->totp_secret, $token)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$user->use_totp = ($user->use_totp === 1) ? 0 : 1;
|
||||
$user->save();
|
||||
$this->use_totp = !$this->use_totp;
|
||||
$this->save();
|
||||
|
||||
return true;
|
||||
|
||||
|
@ -104,51 +75,19 @@ class User extends Model implements AuthenticatableContract,
|
|||
* - at least one lowercase character
|
||||
* - at least one number
|
||||
*
|
||||
* @param int $id The ID of the account to update the password on.
|
||||
* @param string $password The raw password to set the account password to.
|
||||
* @param string $regex The regex to use when validating the password. Defaults to '((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})'.
|
||||
* @return void
|
||||
*/
|
||||
public static function setPassword($id, $password, $regex = '((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})')
|
||||
public function setPassword($password, $regex = '((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})')
|
||||
{
|
||||
|
||||
$user = User::find($id);
|
||||
if (!$user) {
|
||||
throw new AccountNotFoundException('An account with that ID (' . $id . ') does not exist in the system.');
|
||||
}
|
||||
|
||||
if (!preg_match($regex, $password)) {
|
||||
throw new DisplayException('The password passed did not meet the minimum password requirements.');
|
||||
}
|
||||
|
||||
$user->password = password_hash($password, PASSWORD_BCRYPT);
|
||||
$user->save();
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the email address for an account.
|
||||
*
|
||||
* @param int $id
|
||||
* @param string $email
|
||||
* @return void
|
||||
*/
|
||||
public static function setEmail($id, $email)
|
||||
{
|
||||
|
||||
$user = User::find($id);
|
||||
if (!$user) {
|
||||
throw new AccountNotFoundException('An account with that ID (' . $id . ') does not exist in the system.');
|
||||
}
|
||||
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
throw new DisplayException('The email provided (' . $email . ') was not valid.');
|
||||
}
|
||||
|
||||
$user->email = $email;
|
||||
$user->save();
|
||||
$this->password = Hash::make($password);
|
||||
$this->save();
|
||||
|
||||
return;
|
||||
|
||||
|
|
Reference in a new issue