2015-12-14 03:22:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories;
|
|
|
|
|
2016-01-13 04:43:33 +00:00
|
|
|
use Validator;
|
2015-12-14 03:22:16 +00:00
|
|
|
use Hash;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Pterodactyl\Services\UuidService;
|
|
|
|
|
2016-01-13 04:43:33 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
|
|
|
|
2015-12-14 03:22:16 +00:00
|
|
|
class UserRepository
|
|
|
|
{
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a user on the panel. Returns the created user's ID.
|
2015-12-15 20:08:41 +00:00
|
|
|
*
|
2015-12-14 03:22:16 +00:00
|
|
|
* @param string $email
|
|
|
|
* @param string $password An unhashed version of the user's password.
|
2016-01-02 20:08:33 +00:00
|
|
|
* @return bool|integer
|
2015-12-14 03:22:16 +00:00
|
|
|
*/
|
2016-01-13 04:43:33 +00:00
|
|
|
public function create($email, $password, $admin = false)
|
2015-12-14 03:22:16 +00:00
|
|
|
{
|
2016-01-13 04:43:33 +00:00
|
|
|
|
|
|
|
$validator = Validator::make([
|
|
|
|
'email' => $email,
|
|
|
|
'password' => $password,
|
|
|
|
'admin' => $admin
|
|
|
|
], [
|
|
|
|
'email' => 'required|email|unique:users,email',
|
|
|
|
'password' => 'required|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})',
|
|
|
|
'admin' => 'required|boolean'
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Run validator, throw catchable and displayable exception if it fails.
|
|
|
|
// Exception includes a JSON result of failed validation rules.
|
|
|
|
if ($validator->fails()) {
|
|
|
|
throw new DisplayValidationException($validator->errors());
|
|
|
|
}
|
|
|
|
|
2015-12-14 03:22:16 +00:00
|
|
|
$user = new User;
|
|
|
|
$uuid = new UuidService;
|
|
|
|
|
2015-12-15 20:08:41 +00:00
|
|
|
$user->uuid = $uuid->generate('users', 'uuid');
|
2015-12-14 03:22:16 +00:00
|
|
|
$user->email = $email;
|
|
|
|
$user->password = Hash::make($password);
|
2016-01-13 04:43:33 +00:00
|
|
|
$user->root_admin = ($admin) ? 1 : 0;
|
2015-12-14 03:22:16 +00:00
|
|
|
|
2016-01-13 04:43:33 +00:00
|
|
|
try {
|
|
|
|
$user->save();
|
|
|
|
return $user->id;
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2015-12-14 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
2016-01-02 05:25:41 +00:00
|
|
|
/**
|
2016-01-02 20:08:33 +00:00
|
|
|
* Updates a user on the panel.
|
2016-01-02 05:25:41 +00:00
|
|
|
*
|
2016-01-02 20:08:33 +00:00
|
|
|
* @param integer $id
|
|
|
|
* @param array $user An array of columns and their associated values to update for the user.
|
2016-01-02 05:25:41 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-01-02 20:08:33 +00:00
|
|
|
public function update($id, array $user)
|
2016-01-02 05:25:41 +00:00
|
|
|
{
|
|
|
|
if(array_key_exists('password', $user)) {
|
2016-01-02 20:08:33 +00:00
|
|
|
$user['password'] = Hash::make($user['password']);
|
|
|
|
}
|
2016-01-02 05:25:41 +00:00
|
|
|
|
2016-01-02 20:08:33 +00:00
|
|
|
return User::find($id)->update($user);
|
2016-01-02 05:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-02 20:08:33 +00:00
|
|
|
* Deletes a user on the panel, returns the number of records deleted.
|
2016-01-02 05:25:41 +00:00
|
|
|
*
|
2016-01-02 20:08:33 +00:00
|
|
|
* @param integer $id
|
|
|
|
* @return integer
|
2016-01-02 05:25:41 +00:00
|
|
|
*/
|
|
|
|
public function delete($id)
|
|
|
|
{
|
2016-01-13 04:49:56 +00:00
|
|
|
// @TODO cannot delete user with associated servers!
|
|
|
|
// clean up subusers!
|
2016-01-02 20:08:33 +00:00
|
|
|
return User::destroy($id);
|
2016-01-02 05:25:41 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 03:22:16 +00:00
|
|
|
}
|