Cachet/app/Http/Controllers/Dashboard/UserController.php

89 lines
2.7 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
2015-01-01 15:45:04 +00:00
use AltThree\Validator\ValidationException;
2016-08-03 12:26:23 +01:00
use CachetHQ\Cachet\Bus\Events\User\UserDisabledTwoAuthEvent;
use CachetHQ\Cachet\Bus\Events\User\UserEnabledTwoAuthEvent;
use CachetHQ\Cachet\Bus\Events\User\UserRegeneratedApiTokenEvent;
2015-01-03 17:51:35 +00:00
use CachetHQ\Cachet\Models\User;
2015-01-02 12:05:50 +00:00
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Routing\Controller;
2015-01-01 15:45:04 +00:00
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use PragmaRX\Google2FA\Vendor\Laravel\Facade as Google2FA;
2015-01-01 15:45:04 +00:00
class UserController extends Controller
{
/**
* Shows the user view.
2014-12-29 23:07:46 +00:00
*
* @return \Illuminate\View\View
*/
public function showUser()
{
return View::make('dashboard.user.index')
->withPageTitle(trans('dashboard.team.profile').' - '.trans('dashboard.dashboard'));
}
/**
2014-12-21 10:19:18 +00:00
* Updates the current user.
2014-12-29 23:07:46 +00:00
*
* @return \Illuminate\View\View
*/
public function postUser()
{
2015-11-07 17:09:09 +00:00
$userData = array_filter(Binput::only(['username', 'email', 'password', 'google2fa']));
2015-08-31 19:32:06 +01:00
$enable2FA = (bool) array_pull($userData, 'google2fa');
// Let's enable/disable auth
if ($enable2FA && !Auth::user()->hasTwoFactor) {
2016-08-03 12:26:23 +01:00
event(new UserEnabledTwoAuthEvent(Auth::user()));
2015-08-31 19:32:06 +01:00
$userData['google_2fa_secret'] = Google2FA::generateSecretKey();
} elseif (!$enable2FA) {
2016-08-03 12:26:23 +01:00
event(new UserDisabledTwoAuthEvent(Auth::user()));
2015-08-31 19:32:06 +01:00
$userData['google_2fa_secret'] = '';
2015-01-09 14:21:53 -06:00
}
try {
2015-08-31 19:32:06 +01:00
Auth::user()->update($userData);
} catch (ValidationException $e) {
return Redirect::route('dashboard.user')
2015-08-31 19:32:06 +01:00
->withInput($userData)
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.team.edit.failure')))
->withErrors($e->getMessageBag());
}
return Redirect::route('dashboard.user')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.team.edit.success')));
}
2015-01-03 17:51:35 +00:00
/**
* Regenerates the users API key.
2015-12-22 12:14:47 +00:00
*
* @param \CachetHQ\Cachet\Models\User $user
2015-01-03 17:51:35 +00:00
*
* @return \Illuminate\View\View
*/
public function regenerateApiKey(User $user)
{
$user->api_key = User::generateApiKey();
$user->save();
2016-08-03 12:26:23 +01:00
event(new UserRegeneratedApiTokenEvent($user));
return Redirect::route('dashboard.user');
2015-01-03 17:51:35 +00:00
}
}