Update user controller

This commit is contained in:
Dane Everitt 2017-01-12 15:40:24 -05:00
parent f292080483
commit e91362eee6
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
9 changed files with 200 additions and 77 deletions

View file

@ -122,6 +122,9 @@ class UserController extends BaseController
{
try {
$user = new UserRepository;
$create = $user->create($request->only([
'email', 'username', 'name_first', 'name_last', 'password', 'root_admin', 'custom_id',
]));
$create = $user->create($request->input('email'), $request->input('password'), $request->input('admin'), $request->input('custom_id'));
return ['id' => $create];
@ -156,7 +159,9 @@ class UserController extends BaseController
{
try {
$user = new UserRepository;
$user->update($id, $request->all());
$user->update($id, $request->only([
'username', 'email', 'name_first', 'name_last', 'password', 'root_admin', 'language',
]));
return Models\User::findOrFail($id);
} catch (DisplayValidationException $ex) {

View file

@ -116,7 +116,13 @@ class UserController extends Controller
{
try {
$user = new UserRepository;
$userid = $user->create($request->input('email'), $request->input('password'));
$userid = $user->create($request->only([
'email',
'password',
'name_first',
'name_last',
'username'
]));
Alert::success('Account has been successfully created.')->flash();
return redirect()->route('admin.users.view', $userid);
@ -132,19 +138,16 @@ class UserController extends Controller
public function updateUser(Request $request, $user)
{
$data = [
'email' => $request->input('email'),
'root_admin' => $request->input('root_admin'),
'password_confirmation' => $request->input('password_confirmation'),
];
if ($request->input('password')) {
$data['password'] = $request->input('password');
}
try {
$repo = new UserRepository;
$repo->update($user, $data);
$repo->update($user, $request->only([
'email',
'password',
'name_first',
'name_last',
'username',
'root_admin',
]));
Alert::success('User account was successfully updated.')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.users.view', $user)->withErrors(json_decode($ex->getMessage()));

View file

@ -37,13 +37,24 @@ use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification;
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, Notifiable;
/**
* The rules for user passwords.
*
* @var string
*/
const PASSWORD_RULES = 'regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})';
/**
* The regex rules for usernames.
*
* @var string
*/
const USERNAME_RULES = 'regex:/^([\w\d\.\-]{1,255})$/';
/**
* The table associated with the model.
*
@ -52,11 +63,11 @@ class User extends Model implements
protected $table = 'users';
/**
* The attributes that are not mass assignable.
* A list of mass-assignable variables.
*
* @var array
* @var [type]
*/
protected $guarded = ['id', 'remeber_token', 'created_at', 'updated_at'];
protected $fillable = ['username', 'email', 'name_first', 'name_last', 'password', 'language', 'use_totp', 'totp_secret', 'gravatar'];
/**
* Cast values to correct type.
@ -66,6 +77,7 @@ class User extends Model implements
protected $casts = [
'root_admin' => 'integer',
'use_totp' => 'integer',
'gravatar' => 'integer',
];
/**
@ -76,12 +88,10 @@ class User extends Model implements
protected $hidden = ['password', 'remember_token', 'totp_secret'];
/**
* The rules for user passwords.
* Determines if a user has permissions.
*
* @var string
* @return bool
*/
const PASSWORD_RULES = 'min:8|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})';
public function permissions()
{
return $this->hasMany(Permission::class);

View file

@ -29,6 +29,7 @@ use DB;
use Auth;
use Hash;
use Carbon;
use Settings;
use Validator;
use Pterodactyl\Models;
use Pterodactyl\Services\UuidService;
@ -52,18 +53,16 @@ class UserRepository
* @param int $token A custom user ID.
* @return bool|int
*/
public function create($email, $password = null, $admin = false, $token = null)
public function create(array $data)
{
$validator = Validator::make([
'email' => $email,
'password' => $password,
'root_admin' => $admin,
'custom_id' => $token,
], [
$validator = Validator::make($data, [
'email' => 'required|email|unique:users,email',
'password' => 'nullable|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})',
'username' => 'required|string|between:1,255|unique:users,username|' . Models\User::USERNAME_RULES,
'name_first' => 'required|string|between:1,255',
'name_last' => 'required|string|between:1,255',
'password' => 'sometimes|nullable|' . Models\User::PASSWORD_RULES,
'root_admin' => 'required|boolean',
'custom_id' => 'nullable|unique:users,id',
'custom_id' => 'sometimes|nullable|unique:users,id',
]);
// Run validator, throw catchable and displayable exception if it fails.
@ -79,26 +78,36 @@ class UserRepository
$uuid = new UuidService;
// Support for API Services
if (! is_null($token)) {
if (isset($data['custom_id']) && ! is_null($data['custom_id'])) {
$user->id = $token;
}
// UUIDs are not mass-fillable.
$user->uuid = $uuid->generate('users', 'uuid');
$user->email = $email;
$user->password = Hash::make((is_null($password)) ? str_random(30) : $password);
$user->language = 'en';
$user->root_admin = ($admin) ? 1 : 0;
$user->fill([
'email' => $data['email'],
'username' => $data['username'],
'name_first' => $data['name_first'],
'name_last' => $data['name_last'],
'password' => Hash::make((empty($data['password'])) ? str_random(30) : $password),
'root_admin' => $data['root_admin'],
'language' => Settings::get('default_language', 'en'),
]);
$user->save();
// Setup a Password Reset to use when they set a password.
$token = str_random(32);
DB::table('password_resets')->insert([
'email' => $user->email,
'token' => $token,
'created_at' => Carbon::now()->toDateTimeString(),
]);
// Only used if no password is provided.
if (empty($data['password'])) {
$token = str_random(32);
DB::table('password_resets')->insert([
'email' => $user->email,
'token' => $token,
'created_at' => Carbon::now()->toDateTimeString(),
]);
$user->notify((new AccountCreated($token)));
$user->notify((new AccountCreated($token)));
}
DB::commit();
@ -122,7 +131,10 @@ class UserRepository
$validator = Validator::make($data, [
'email' => 'sometimes|required|email|unique:users,email,' . $id,
'password' => 'sometimes|required|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})',
'username' => 'sometimes|required|string|between:1,255|unique:users,username,' . $user->id . '|' . Models\User::USERNAME_RULES,
'name_first' => 'sometimes|required|string|between:1,255',
'name_last' => 'sometimes|required|string|between:1,255',
'password' => 'sometimes|nullable|' . Models\User::PASSWORD_RULES,
'root_admin' => 'sometimes|required|boolean',
'language' => 'sometimes|required|string|min:1|max:5',
'use_totp' => 'sometimes|required|boolean',
@ -135,12 +147,15 @@ class UserRepository
throw new DisplayValidationException($validator->errors());
}
if (array_key_exists('password', $data)) {
// The password and root_admin fields are not mass assignable.
if (! empty($data['password'])) {
$data['password'] = Hash::make($data['password']);
} else {
unset($data['password']);
}
if (isset($data['password_confirmation'])) {
unset($data['password_confirmation']);
if (! empty($data['root_admin'])) {
$user->root_admin = $data['root_admin'];
}
$user->fill($data);