Skip to content

Commit f6be061

Browse files
committed
fix user controller; closes pterodactyl#58, closes pterodactyl#59
1 parent 48b9bc0 commit f6be061

File tree

13 files changed

+343
-360
lines changed

13 files changed

+343
-360
lines changed

app/Http/Controllers/Admin/AccountsController.php

Lines changed: 0 additions & 145 deletions
This file was deleted.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
* Some Modifications (c) 2015 Dylan Seidt <dylan.seidt@gmail.com>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
namespace Pterodactyl\Http\Controllers\Admin;
26+
27+
use Alert;
28+
use Settings;
29+
use Mail;
30+
use Log;
31+
use Pterodactyl\Models\User;
32+
use Pterodactyl\Repositories\UserRepository;
33+
use Pterodactyl\Models\Server;
34+
35+
use Pterodactyl\Exceptions\DisplayException;
36+
use Pterodactyl\Exceptions\DisplayValidationException;
37+
38+
use Pterodactyl\Http\Controllers\Controller;
39+
use Illuminate\Http\Request;
40+
41+
class UserController extends Controller
42+
{
43+
44+
/**
45+
* Controller Constructor
46+
*/
47+
public function __construct()
48+
{
49+
//
50+
}
51+
52+
public function getIndex(Request $request)
53+
{
54+
return view('admin.users.index', [
55+
'users' => User::paginate(20)
56+
]);
57+
}
58+
59+
public function getNew(Request $request)
60+
{
61+
return view('admin.users.new');
62+
}
63+
64+
public function getView(Request $request, $id)
65+
{
66+
return view('admin.users.view', [
67+
'user' => User::findOrFail($id),
68+
'servers' => Server::select('servers.*', 'nodes.name as nodeName', 'locations.long as location')
69+
->join('nodes', 'servers.node', '=', 'nodes.id')
70+
->join('locations', 'nodes.location', '=', 'locations.id')
71+
->where('owner', $id)
72+
->where('active', 1)
73+
->get(),
74+
]);
75+
}
76+
77+
public function deleteUser(Request $request, $id)
78+
{
79+
try {
80+
$repo = new UserRepository;
81+
$repo->delete($id);
82+
Alert::success('Successfully deleted user from system.')->flash();
83+
return redirect()->route('admin.users');
84+
} catch(DisplayException $ex) {
85+
Alert::danger($ex->getMessage())->flash();
86+
} catch (\Exception $ex) {
87+
Log::error($ex);
88+
Alert::danger('An exception was encountered while attempting to delete this user.')->flash();
89+
}
90+
return redirect()->route('admin.users.view', $id);
91+
}
92+
93+
public function postNew(Request $request)
94+
{
95+
try {
96+
$user = new UserRepository;
97+
$userid = $user->create($request->input('email'), $request->input('password'));
98+
Alert::success('Account has been successfully created.')->flash();
99+
return redirect()->route('admin.users.view', $userid);
100+
} catch (DisplayValidationException $ex) {
101+
return redirect()->route('admin.users.new')->withErrors(json_decode($ex->getMessage()))->withInput();
102+
} catch (\Exception $ex) {
103+
Log::error($ex);
104+
Alert::danger('An error occured while attempting to add a new user.')->flash();
105+
return redirect()->route('admin.users.new');
106+
}
107+
}
108+
109+
public function updateUser(Request $request, $user)
110+
{
111+
$data = [
112+
'email' => $request->input('email'),
113+
'root_admin' => $request->input('root_admin'),
114+
'password_confirmation' => $request->input('password_confirmation'),
115+
];
116+
117+
if ($request->input('password')) {
118+
$data['password'] = $request->input('password');
119+
}
120+
121+
try {
122+
$repo = new UserRepository;
123+
$repo->update($user, $data);
124+
Alert::success('User account was successfully updated.')->flash();
125+
} catch (DisplayValidationException $ex) {
126+
return redirect()->route('admin.users.view', $user)->withErrors(json_decode($ex->getMessage()));
127+
} catch (\Exception $e) {
128+
Log::error($e);
129+
Alert::danger('An error occured while attempting to update this user.')->flash();
130+
}
131+
return redirect()->route('admin.users.view', $user);
132+
}
133+
134+
}

app/Http/Routes/AdminRoutes.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function map(Router $router) {
5959
});
6060

6161
$router->group([
62-
'prefix' => 'admin/accounts',
62+
'prefix' => 'admin/users',
6363
'middleware' => [
6464
'auth',
6565
'admin',
@@ -69,35 +69,35 @@ public function map(Router $router) {
6969

7070
// View All Accounts on System
7171
$router->get('/', [
72-
'as' => 'admin.accounts',
73-
'uses' => 'Admin\AccountsController@getIndex'
72+
'as' => 'admin.users',
73+
'uses' => 'Admin\UserController@getIndex'
7474
]);
7575

7676
// View Specific Account
7777
$router->get('/view/{id}', [
78-
'as' => 'admin.accounts.view',
79-
'uses' => 'Admin\AccountsController@getView'
78+
'as' => 'admin.users.view',
79+
'uses' => 'Admin\UserController@getView'
8080
]);
8181

82-
// Show Create Account Page
83-
$router->get('/new', [
84-
'as' => 'admin.accounts.new',
85-
'uses' => 'Admin\AccountsController@getNew'
82+
// View Specific Account
83+
$router->post('/view/{id}', [
84+
'uses' => 'Admin\UserController@updateUser'
8685
]);
8786

88-
// Handle Creating New Account
89-
$router->post('/new', [
90-
'uses' => 'Admin\AccountsController@postNew'
87+
// Delete an Account Matching an ID
88+
$router->delete('/view/{id}', [
89+
'uses' => 'Admin\UserController@deleteUser'
9190
]);
9291

93-
// Update A Specific Account
94-
$router->post('/update', [
95-
'uses' => 'Admin\AccountsController@postUpdate'
92+
// Show Create Account Page
93+
$router->get('/new', [
94+
'as' => 'admin.users.new',
95+
'uses' => 'Admin\UserController@getNew'
9696
]);
9797

98-
// Delete an Account Matching an ID
99-
$router->delete('/view/{id}', [
100-
'uses' => 'Admin\AccountsController@deleteView'
98+
// Handle Creating New Account
99+
$router->post('/new', [
100+
'uses' => 'Admin\UserController@postNew'
101101
]);
102102

103103
});

app/Repositories/UserRepository.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,15 @@ public function create($email, $password, $admin = false)
108108
*/
109109
public function update($id, array $data)
110110
{
111+
$user = Models\User::findOrFail($id);
112+
111113
$validator = Validator::make($data, [
112-
'email' => 'email|unique:users,email,' . $id,
113-
'password' => 'regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})',
114-
'root_admin' => 'boolean',
115-
'language' => 'string|min:1|max:5',
116-
'use_totp' => 'boolean',
117-
'totp_secret' => 'size:16'
114+
'email' => 'sometimes|required|email|unique:users,email,' . $id,
115+
'password' => 'sometimes|required|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})',
116+
'root_admin' => 'sometimes|required|boolean',
117+
'language' => 'sometimes|required|string|min:1|max:5',
118+
'use_totp' => 'sometimes|required|boolean',
119+
'totp_secret' => 'sometimes|required|size:16'
118120
]);
119121

120122
// Run validator, throw catchable and displayable exception if it fails.
@@ -127,7 +129,12 @@ public function update($id, array $data)
127129
$data['password'] = Hash::make($data['password']);
128130
}
129131

130-
return Models\User::findOrFail($id)->update($data);
132+
if (isset($data['password_confirmation'])) {
133+
unset($data['password_confirmation']);
134+
}
135+
136+
$user->fill($data);
137+
$user->save();
131138
}
132139

133140
/**
@@ -144,14 +151,15 @@ public function delete($id)
144151

145152
DB::beginTransaction();
146153

147-
Models\Permission::where('user_id', $id)->delete();
148-
Models\Subuser::where('user_id', $id)->delete();
149-
Models\User::destroy($id);
150-
151154
try {
155+
Models\Permission::where('user_id', $id)->delete();
156+
Models\Subuser::where('user_id', $id)->delete();
157+
Models\User::destroy($id);
158+
152159
DB::commit();
153160
return true;
154161
} catch (\Exception $ex) {
162+
DB::rollBack();
155163
throw $ex;
156164
}
157165
}

0 commit comments

Comments
 (0)