2014-12-21 10:14:58 +00:00
|
|
|
<?php
|
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-04-19 08:52:39 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-08-31 18:59:17 +01:00
|
|
|
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
|
2015-01-01 15:45:04 +00:00
|
|
|
|
2015-08-03 22:32:36 +01: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;
|
2015-08-05 17:18:51 -05:00
|
|
|
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;
|
2015-01-09 09:03:07 +00:00
|
|
|
use PragmaRX\Google2FA\Vendor\Laravel\Facade as Google2FA;
|
2015-01-01 15:45:04 +00:00
|
|
|
|
2015-08-05 17:18:51 -05:00
|
|
|
class UserController extends Controller
|
2014-12-21 10:14:58 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Shows the user view.
|
2014-12-29 23:07:46 +00:00
|
|
|
*
|
2014-12-21 10:14:58 +00:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function showUser()
|
|
|
|
{
|
2015-08-03 22:32:36 +01:00
|
|
|
return View::make('dashboard.user.index')
|
|
|
|
->withPageTitle(trans('dashboard.team.profile').' - '.trans('dashboard.dashboard'));
|
2014-12-21 10:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-21 10:19:18 +00:00
|
|
|
* Updates the current user.
|
2014-12-29 23:07:46 +00:00
|
|
|
*
|
2014-12-21 10:14:58 +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']));
|
2014-12-21 10:14:58 +00:00
|
|
|
|
2015-08-31 19:32:06 +01:00
|
|
|
$enable2FA = (bool) array_pull($userData, 'google2fa');
|
2015-01-09 09:03:07 +00:00
|
|
|
|
|
|
|
// Let's enable/disable auth
|
2015-04-19 08:52:39 +01:00
|
|
|
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();
|
2015-04-19 08:52:39 +01:00
|
|
|
} 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
|
|
|
}
|
2015-01-09 09:03:07 +00:00
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
try {
|
2015-08-31 19:32:06 +01:00
|
|
|
Auth::user()->update($userData);
|
2015-08-03 22:32:36 +01:00
|
|
|
} catch (ValidationException $e) {
|
2015-08-13 22:28:30 +01:00
|
|
|
return Redirect::route('dashboard.user')
|
2015-08-31 19:32:06 +01:00
|
|
|
->withInput($userData)
|
2015-08-03 22:32:36 +01:00
|
|
|
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.team.edit.failure')))
|
|
|
|
->withErrors($e->getMessageBag());
|
2015-01-09 20:21:29 -06:00
|
|
|
}
|
|
|
|
|
2015-08-13 22:28:30 +01:00
|
|
|
return Redirect::route('dashboard.user')
|
2015-08-03 22:32:36 +01:00
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.team.edit.success')));
|
2014-12-21 10:14:58 +00:00
|
|
|
}
|
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));
|
|
|
|
|
2015-08-13 22:28:30 +01:00
|
|
|
return Redirect::route('dashboard.user');
|
2015-01-03 17:51:35 +00:00
|
|
|
}
|
2014-12-21 10:14:58 +00:00
|
|
|
}
|