Skip to content

Commit b02df8e

Browse files
authored
Implement base notifications support (pterodactyl#77)
* initial implementation of notifications * typehint UUID returns. Fixes that notifications bug
1 parent b3ca8a3 commit b02df8e

File tree

11 files changed

+176
-28
lines changed

11 files changed

+176
-28
lines changed

app/Http/Controllers/Server/ServerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function getDownloadFile(Request $request, $uuid, $file)
176176

177177
$download = new Models\Download;
178178

179-
$download->token = Uuid::generate(4);
179+
$download->token = (string) Uuid::generate(4);
180180
$download->server = $server->uuid;
181181
$download->path = str_replace('../', '', $file);
182182

app/Models/User.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,4 @@ public function sendPasswordResetNotification($token)
137137
$this->notify(new ResetPasswordNotification($token));
138138
}
139139

140-
141140
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Notifications;
25+
26+
use Illuminate\Bus\Queueable;
27+
use Illuminate\Notifications\Notification;
28+
use Illuminate\Contracts\Queue\ShouldQueue;
29+
use Illuminate\Notifications\Messages\MailMessage;
30+
31+
class AccountCreated extends Notification implements ShouldQueue
32+
{
33+
use Queueable;
34+
35+
/**
36+
* The password reset token to send.
37+
*
38+
* @var string
39+
*/
40+
public $token;
41+
42+
/**
43+
* Create a new notification instance.
44+
*
45+
* @return void
46+
*/
47+
public function __construct($token)
48+
{
49+
$this->token = $token;
50+
}
51+
52+
/**
53+
* Get the notification's delivery channels.
54+
*
55+
* @param mixed $notifiable
56+
* @return array
57+
*/
58+
public function via($notifiable)
59+
{
60+
return ['mail', 'database'];
61+
}
62+
63+
/**
64+
* Get the mail representation of the notification.
65+
*
66+
* @param mixed $notifiable
67+
* @return \Illuminate\Notifications\Messages\MailMessage
68+
*/
69+
public function toMail($notifiable)
70+
{
71+
return (new MailMessage)
72+
->line('You are recieving this email because an account has been created for you on Pterodactyl Panel.')
73+
->line('Email: ' . $notifiable->email)
74+
->action('Setup Your Account', url('/auth/password/reset/' . $this->token . '?email=' . $notifiable->email));
75+
}
76+
77+
/**
78+
* Get the array representation of the notification.
79+
*
80+
* @param mixed $notifiable
81+
* @return array
82+
*/
83+
public function toArray($notifiable)
84+
{
85+
return [
86+
'email' => $notifiable->email,
87+
'token' => $this->token
88+
];
89+
}
90+
}

app/Notifications/SendPasswordReset.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
<?php
2-
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
324
namespace Pterodactyl\Notifications;
425

526
use Illuminate\Bus\Queueable;
627
use Illuminate\Notifications\Notification;
728
use Illuminate\Contracts\Queue\ShouldQueue;
829
use Illuminate\Notifications\Messages\MailMessage;
930

10-
class SendPasswordReset extends Notification
31+
class SendPasswordReset extends Notification implements ShouldQueue
1132
{
1233
use Queueable;
1334

app/Repositories/UserRepository.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
use Hash;
3030
use Validator;
3131
use Mail;
32+
use Carbon;
3233

3334
use Pterodactyl\Models;
3435
use Pterodactyl\Services\UuidService;
36+
use Pterodactyl\Notifications\AccountCreated;
3537

3638
use Pterodactyl\Exceptions\DisplayValidationException;
3739
use Pterodactyl\Exceptions\DisplayException;
@@ -48,18 +50,18 @@ public function __construct()
4850
* Creates a user on the panel. Returns the created user's ID.
4951
*
5052
* @param string $email
51-
* @param string $password An unhashed version of the user's password.
53+
* @param string|null $password An unhashed version of the user's password.
5254
* @return bool|integer
5355
*/
54-
public function create($email, $password, $admin = false)
56+
public function create($email, $password = null, $admin = false)
5557
{
5658
$validator = Validator::make([
5759
'email' => $email,
5860
'password' => $password,
5961
'root_admin' => $admin
6062
], [
6163
'email' => 'required|email|unique:users,email',
62-
'password' => 'required|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})',
64+
'password' => 'nullable|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})',
6365
'root_admin' => 'required|boolean'
6466
]);
6567

@@ -77,25 +79,26 @@ public function create($email, $password, $admin = false)
7779

7880
$user->uuid = $uuid->generate('users', 'uuid');
7981
$user->email = $email;
80-
$user->password = Hash::make($password);
82+
$user->password = Hash::make((is_null($password)) ? str_random(30) : $password);
8183
$user->language = 'en';
8284
$user->root_admin = ($admin) ? 1 : 0;
8385
$user->save();
8486

85-
Mail::queue('emails.new-account', [
87+
// Setup a Password Reset to use when they set a password.
88+
$token = str_random(32);
89+
DB::table('password_resets')->insert([
8690
'email' => $user->email,
87-
'forgot' => route('auth.password'),
88-
'login' => route('auth.login')
89-
], function ($message) use ($email) {
90-
$message->to($email);
91-
$message->from(Settings::get('email_from', env('MAIL_FROM')), Settings::get('email_sender_name', env('MAIL_FROM_NAME', 'Pterodactyl Panel')));
92-
$message->subject(Settings::get('company') . ' - New Account');
93-
});
91+
'token' => $token,
92+
'created_at' => Carbon::now()->toDateTimeString()
93+
]);
94+
95+
$user->notify((new AccountCreated($token)));
96+
9497
DB::commit();
9598
return $user->id;
9699
} catch (\Exception $ex) {
97100
DB::rollBack();
98-
throw $e;
101+
throw $ex;
99102
}
100103
}
101104

app/Services/UuidService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function generate($table = 'users', $field = 'uuid', $type = 4)
5959

6060
} while (!$return);
6161

62-
return $return;
62+
return (string) $return;
6363

6464
}
6565

@@ -85,7 +85,7 @@ public function generateShort($table = 'servers', $field = 'uuidShort', $attache
8585

8686
} while (!$return);
8787

88-
return $return;
88+
return (string) $return;
8989

9090
}
9191

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=5.6.4",
20-
"laravel/framework": "5.3.*",
20+
"laravel/framework": "5.3.6",
2121
"barryvdh/laravel-debugbar": "^2.2.3",
2222
"doctrine/dbal": "^2.5.4",
2323
"guzzlehttp/guzzle": "^6.2.1",
@@ -35,7 +35,8 @@
3535
"mockery/mockery": "0.9.*",
3636
"phpunit/phpunit": "~5.0",
3737
"symfony/css-selector": "3.1.*",
38-
"symfony/dom-crawler": "3.1.*"
38+
"symfony/dom-crawler": "3.1.*",
39+
"laravel/homestead": "3.0.*"
3940
},
4041
"autoload": {
4142
"classmap": [

config/queue.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
'driver' => 'database',
4040
'table' => 'jobs',
4141
'queue' => 'default',
42-
'expire' => 60,
42+
'retry_after' => 60,
4343
],
4444

4545
'beanstalkd' => [
4646
'driver' => 'beanstalkd',
4747
'host' => 'localhost',
4848
'queue' => 'default',
49-
'ttr' => 60,
49+
'retry_after' => 60,
5050
],
5151

5252
'sqs' => [
@@ -70,7 +70,7 @@
7070
'driver' => 'redis',
7171
'connection' => 'default',
7272
'queue' => 'default',
73-
'expire' => 60,
73+
'retry_after' => 60,
7474
],
7575

7676
],
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateNotificationsTable extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('notifications', function (Blueprint $table) {
16+
$table->string('id')->primary();
17+
$table->string('type');
18+
$table->morphs('notifiable');
19+
$table->text('data');
20+
$table->timestamp('read_at')->nullable();
21+
$table->timestamps();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::drop('notifications');
33+
}
34+
}

resources/views/vendor/notifications/email-plain.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
}
2020
2121
echo 'Regards,', "\n";
22-
echo config('app.name'), "\n";
22+
echo Settings::get('company'), "\n";

0 commit comments

Comments
 (0)