forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserRepository.php
More file actions
90 lines (74 loc) · 2.19 KB
/
UserRepository.php
File metadata and controls
90 lines (74 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace Pterodactyl\Repositories;
use Validator;
use Hash;
use Pterodactyl\Models\User;
use Pterodactyl\Services\UuidService;
use Pterodactyl\Exceptions\DisplayValidationException;
class UserRepository
{
public function __construct()
{
//
}
/**
* Creates a user on the panel. Returns the created user's ID.
*
* @param string $email
* @param string $password An unhashed version of the user's password.
* @return bool|integer
*/
public function create($email, $password, $admin = false)
{
$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());
}
$user = new User;
$uuid = new UuidService;
$user->uuid = $uuid->generate('users', 'uuid');
$user->email = $email;
$user->password = Hash::make($password);
$user->root_admin = ($admin) ? 1 : 0;
try {
$user->save();
return $user->id;
} catch (\Exception $ex) {
throw $e;
}
}
/**
* Updates a user on the panel.
*
* @param integer $id
* @param array $user An array of columns and their associated values to update for the user.
* @return boolean
*/
public function update($id, array $user)
{
if(array_key_exists('password', $user)) {
$user['password'] = Hash::make($user['password']);
}
return User::find($id)->update($user);
}
/**
* Deletes a user on the panel, returns the number of records deleted.
*
* @param integer $id
* @return integer
*/
public function delete($id)
{
return User::destroy($id);
}
}