Improved user management in Admin CP

Fixes a few bugs from PR#17
This commit is contained in:
Dane Everitt 2016-01-02 15:08:33 -05:00
parent 9d6563f8de
commit f467c3f0eb
5 changed files with 85 additions and 61 deletions

View file

@ -4,6 +4,7 @@ namespace Pterodactyl\Http\Controllers\Admin;
use Alert; use Alert;
use Mail; use Mail;
use Log;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Pterodactyl\Repositories\UserRepository; use Pterodactyl\Repositories\UserRepository;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
@ -40,7 +41,14 @@ class AccountsController extends Controller
public function getView(Request $request, $id) public function getView(Request $request, $id)
{ {
return view('admin.accounts.view', ['user' => User::findOrFail($id), 'servers' => Server::where('owner', $id)->first()]); return view('admin.accounts.view', [
'user' => User::findOrFail($id),
'servers' => Server::select('servers.*', 'nodes.name as nodeName', 'locations.long as location')
->join('nodes', 'servers.node', '=', 'nodes.id')
->join('locations', 'nodes.location', '=', 'locations.id')
->where('active', 1)
->get(),
]);
} }
public function getDelete(Request $request, $id) public function getDelete(Request $request, $id)
@ -55,22 +63,23 @@ class AccountsController extends Controller
public function postNew(Request $request) public function postNew(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'username' => 'required|min:4|unique:users,username',
'email' => 'required|email|unique:users,email', 'email' => 'required|email|unique:users,email',
'password' => 'required|confirmed|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})', 'password' => 'required|confirmed|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})'
'password_confirmation' => 'required'
]); ]);
try { try {
$user = new UserRepository; $user = new UserRepository;
$userid = $user->create($request->input('username'), $request->input('email'), $request->input('password')); $userid = $user->create($request->input('username'), $request->input('email'), $request->input('password'));
if (!$userid) {
throw new \Exception('Unable to create user, response was not an integer.');
}
Alert::success('Account has been successfully created.')->flash(); Alert::success('Account has been successfully created.')->flash();
return redirect()->route('admin.accounts.view', ['id' => $userid]); return redirect()->route('admin.accounts.view', ['id' => $userid]);
} catch (\Exception $e) { } catch (\Exception $e) {
Alert::danger('An error occured while attempting to add a new user. Please check the logs or try again.')->flash(); Log::error($e);
Alert::danger('An error occured while attempting to add a new user. ' . $e->getMessage())->flash();
return redirect()->route('admin.accounts.new'); return redirect()->route('admin.accounts.new');
} }
@ -81,38 +90,40 @@ class AccountsController extends Controller
$this->validate($request, [ $this->validate($request, [
'email' => 'required|email|unique:users,email,'.$request->input('user'), 'email' => 'required|email|unique:users,email,'.$request->input('user'),
'root_admin' => 'required', 'root_admin' => 'required',
'password' => 'required_with:password_confirmation', 'password' => 'required_with:password_confirmation|confirmed',
'password_confirmation' => 'required_with:password' 'password_confirmation' => 'required_with:password'
]); ]);
try { try {
$users = new UserRepository; $users = new UserRepository;
$user = []; $user = [
'email' => $request->input('email'),
$user['email'] = $request->input('email'); 'root_admin' => $request->input('root_admin')
$user['root_admin'] = $request->input('root_admin'); ];
if(!empty($request->input('password'))) { if(!empty($request->input('password'))) {
$user['password'] = $request->input('password'); $user['password'] = $request->input('password');
} }
$users->update($request->input('user'), $user); if(!$users->update($request->input('user'), $user)) {
throw new \Exception('Unable to update user, response was not valid.');
}
if($request->input('email_user')) {
Mail::send('emails.new_password', ['user' => User::findOrFail($request->input('user')), 'password' => $request->input('password')], function($message) use ($request) {
$message->to($request->input('email'))->subject('Pterodactyl - Admin Reset Password');
});
}
Alert::success('User account was successfully updated.')->flash();
return redirect()->route('admin.accounts.view', ['id' => $request->input('user')]);
} catch (\Exception $e) { } catch (\Exception $e) {
Alert::danger('An error occured while attempting to update a user. Please check the logs or try again.')->flash(); Log::error($e);
Alert::danger('An error occured while attempting to update this user. ' . $e->getMessage())->flash();
return redirect()->route('admin.accounts.view', ['id' => $request->input('user')]); return redirect()->route('admin.accounts.view', ['id' => $request->input('user')]);
} }
if($request->input('email_user')) {
Mail::send('emails.new_password', ['user' => User::findOrFail($request->input('user')), 'password' => $request->input('password')], function($message) use ($request) {
$message->to($request->input('email'))->subject('Pterodactyl - Admin Reset Password');
});
}
Alert::success('A user was successfully updated.')->flash();
return redirect()->route('admin.accounts.view', ['id' => $request->input('user')]);
} }
} }

View file

@ -18,59 +18,47 @@ class UserRepository
/** /**
* Creates a user on the panel. Returns the created user's ID. * Creates a user on the panel. Returns the created user's ID.
* *
* @param string $username
* @param string $email * @param string $email
* @param string $password An unhashed version of the user's password. * @param string $password An unhashed version of the user's password.
* @return integer * @return bool|integer
*/ */
public function create($username, $email, $password) public function create($email, $password)
{ {
$user = new User; $user = new User;
$uuid = new UuidService; $uuid = new UuidService;
$user->uuid = $uuid->generate('users', 'uuid'); $user->uuid = $uuid->generate('users', 'uuid');
$user->username = $username;
$user->email = $email; $user->email = $email;
$user->password = Hash::make($password); $user->password = Hash::make($password);
$user->save(); return ($user->save()) ? $user->id : false;
return $user->id;
} }
/** /**
* Updates a user on the panel. Returns true if the update was successful. * Updates a user on the panel.
* *
* @param string $username * @param integer $id
* @param string $email * @param array $user An array of columns and their associated values to update for the user.
* @param string $password An unhashed version of the user's password.
* @return boolean * @return boolean
*/ */
public function update($id, $user) public function update($id, array $user)
{ {
if(array_key_exists('password', $user)) { if(array_key_exists('password', $user)) {
$user['password'] = Hash::make($user['password']); $user['password'] = Hash::make($user['password']);
} }
User::where('id', $id)->update($user); return User::find($id)->update($user);
return true;
} }
/** /**
* Deletes a user on the panel. Returns true if the deletion was successful. * Deletes a user on the panel, returns the number of records deleted.
* *
* @param string $username * @param integer $id
* @param string $email * @return integer
* @param string $password An unhashed version of the user's password.
* @return boolean
*/ */
public function delete($id) public function delete($id)
{ {
User::destroy($id); return User::destroy($id);
return true;
} }
} }

View file

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class RemoveUsername extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('username');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->string('username')->after('uuid');
});
}
}

View file

@ -24,12 +24,6 @@
<h3>Create New Account</h3><hr /> <h3>Create New Account</h3><hr />
<form action="new" method="post"> <form action="new" method="post">
<fieldset> <fieldset>
<div class="form-group">
<label for="username" class="control-label">Username</label>
<div>
<input type="text" autocomplete="off" name="username" class="form-control" />
</div>
</div>
<div class="form-group"> <div class="form-group">
<label for="email" class="control-label">Email</label> <label for="email" class="control-label">Email</label>
<div> <div>

View file

@ -9,7 +9,7 @@
<ul class="breadcrumb"> <ul class="breadcrumb">
<li><a href="/admin">Admin Controls</a></li> <li><a href="/admin">Admin Controls</a></li>
<li><a href="/admin/accounts">Accounts</a></li> <li><a href="/admin/accounts">Accounts</a></li>
<li class="active">{{ $user->id }}</li> <li class="active">{{ $user->email }}</li>
</ul> </ul>
@if (count($errors) > 0) @if (count($errors) > 0)
<div class="alert alert-danger"> <div class="alert alert-danger">
@ -109,10 +109,10 @@
<tbody> <tbody>
@foreach($servers as $server) @foreach($servers as $server)
<tr> <tr>
<td><a href="/server/{{ $server->uuidShort }}"><i class="fa fa-tachometer"></i></a></td> <td><a href="/server/{{ $server->uuidShort }}/"><i class="fa fa-tachometer"></i></a></td>
<td><a href="/admin/server/view/{{ $server->id }}">{{ $server->name }}</a></td> <td><a href="/admin/servers/view/{{ $server->id }}">{{ $server->name }}</a></td>
<td>{{ $server->node_name }}</td> <td>{{ $server->nodeName }}</td>
<td>{{ $server->server_ip }}:{{ $server->server_port }}</td> <td><code>{{ $server->ip }}:{{ $server->port }}</code></td>
<td>@if($server->active)<span class="label label-success">Enabled</span>@else<span class="label label-danger">Disabled</span>@endif</td> <td>@if($server->active)<span class="label label-success">Enabled</span>@else<span class="label label-danger">Disabled</span>@endif</td>
</td> </td>
@endforeach @endforeach