<?php
namespace Platform\SecurityBundle\Controller\Dashboard;
use Cms\CoreBundle\Model\Scenes\DashboardScenes\DocumentScene;
use Cms\CoreBundle\Service\Aws\S3Wrapper;
use Cms\CoreBundle\Util\Controller;
use Cms\FileBundle\Service\BlitlineWrapper;
use Cms\ModuleBundle\Controller\ContentController;
use Cms\Modules\PeopleBundle\Entity\Profile\ProfileProxy;
use Platform\SecurityBundle\Entity\Identity\Account;
use Platform\SecurityBundle\Form\Type\AccountType;
use Platform\SecurityBundle\Form\Type\ChangePasswordType;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Class ProfileController
* @package Platform\SecurityBundle\Controller\Dashboard
*/
class ProfileController extends Controller
{
const ROUTES__LANDING = 'platform.security.dashboard.profile.landing';
const ROUTES__VIEW = 'platform.security.dashboard.profile.view';
const ROUTES__EDIT = 'platform.security.dashboard.profile.edit';
const ROUTES__CREDENTIALS = 'platform.security.dashboard.profile.credentials';
const ROUTES__DELETE_CREDENTIAL = 'platform.security.dashboard.profile.credential_delete';
const ROUTES__CREATE_CREDENTIAL = 'platform.security.dashboard.profile.credential_create';
const ROUTES__REGISTER_CREDENTIAL = 'platform.security.dashboard.profile.credential_register';
const ROUTES__CHANGE_PASSWORD = 'platform.security.dashboard.profile.change.password';
const ROUTES__CHANGE_PASSWORD_FORCED = 'platform.security.dashboard.profile.change.password.forced';
const ROUTES__AVATAR = 'platform.security.dashboard.profile.avatar';
const ROUTES__AVATAR_CROP = 'platform.security.dashboard.profile.avatar_crop';
const ROUTES__LIST = 'platform.security.dashboard.profile.list';
const ROUTES__LIST_LAZY = 'platform.security.dashboard.profile.list_lazy';
/**
* TODO: this needs moved to a more proper controller...
*
* Quick controller to handle Settings tab redirection based on permissions.
*
* @return RedirectResponse
*
* @Route(
* "/redir",
* name = ProfileController::ROUTES__LANDING
* )
*/
public function landingAction()
{
// check special cases
switch (true) {
// see if they have permission to manage accounts and security stuff
case $this->isGranted('campussuite.platform.security.accounts.manage'):
return $this->redirectToRoute('campussuite.platform.security.dashboard.accounts.list');
}
// by default, redirect to the "my accounts" view as everyone has this
return $this->redirectToRoute(self::ROUTES__VIEW);
}
/**
* Renders 'My Account' page.
*
* @return DocumentScene
*
* @Route("/", name = ProfileController::ROUTES__VIEW)
*/
public function viewAction()
{
return $this->view(
array(
'account' => $this->getGlobalContext()->getEffectiveAccount(),
'profile' => $this->getGlobalContext()->getEffectiveAccount()->getSystemProfile(),
)
);
}
/**
* @return DocumentScene
* @throws \Exception
*
* @Route(
* "/profiles",
* name = ProfileController::ROUTES__LIST
* )
*/
public function listAction()
{
/** @var ProfileProxy[] $proxies */
$proxies = $this->getEntityManager()->getRepository(ProfileProxy::class)->findByAccount(
$this->getUser()
);
return $this->view(array(
'proxies' => $proxies,
'redirectTo' => $this->generateUrl(self::ROUTES__LIST),
'routes' => array(
'content' => ContentController::routing(),
),
));
}
/**
* Renders 'Edit Account' page.
*
* @param Request $request
* @return DocumentScene
*
* @Route(
* "/edit",
* name = ProfileController::ROUTES__EDIT,
* )
*/
public function editAction(Request $request)
{
$account = $this->getGlobalContext()->getEffectiveAccount();
$form = $this->createForm(
AccountType::class,
$account,
[]
);
if ($request->isMethod('POST')) {
/** @var Account $account */
$account = $form->handleRequest($request)->getData();
$this->getEntityManager()->save($account);
// record log
$this->getActivityLogger()->createLog($account);
return $this->redirectToRoute(self::ROUTES__VIEW);
}
return $this->view(
array(
'form' => $form->createView(),
'account' => $account,
'profile' => $account->getSystemProfile(),
)
);
}
/**
* Allows user to change password.
*
* @param Request $request
* @return DocumentScene|RedirectResponse
*
* @Route(
* "/change-password",
* name = ProfileController::ROUTES__CHANGE_PASSWORD,
* )
*
* @Route(
* "/change-password-forced",
* name = ProfileController::ROUTES__CHANGE_PASSWORD_FORCED,
* )
*/
public function changePasswordAction(Request $request)
{
$account = $this->getGlobalContext()->getEffectiveAccount();
$form = $this->createForm(
ChangePasswordType::class,
[],
array(
'account' => $account,
)
);
if ($request->isMethod('POST')) {
$data = $form->handleRequest($request)->getData();
if ($form->isValid()) {
$this->getEntityManager()->save($account->setPasswordRaw($data['password']));
// record log
$this->getActivityLogger()->createLog($account);
return $this->redirectToRoute(self::ROUTES__VIEW);
}
}
return $this->view(
array(
'form' => $form->createView(),
'account' => $account,
)
);
}
/**
* @return S3Wrapper|object
*/
private function getS3Wrapper(): S3Wrapper
{
return $this->get(__METHOD__);
}
/**
* @return BlitlineWrapper|object
*/
private function getBlitlineWrapper(): BlitlineWrapper
{
return $this->get(__METHOD__);
}
/**
* @param Request $request
* @return DocumentScene
* @throws \Exception
*
* @Route(
* "/profile/avatar/crop",
* name = ProfileController::ROUTES__AVATAR_CROP
* )
*/
public function avatarCropAction(Request $request)
{
if ($request->query->count() > 0) {
return $this->redirectToRoute(self::ROUTES__AVATAR_CROP);
}
$account = $this->getGlobalContext()->getEffectiveAccount();
$key = $this->getS3Wrapper()->entityKey($account, '/avatar/original');
if ($this->getS3Wrapper()->exists(S3Wrapper::BUCKETS__STORAGE, $key) === false) {
throw new \Exception();
}
if ($request->isMethod('POST')) {
$cropping = $request->request->all();
$blitline = $this->getBlitlineWrapper();
$funcs = [];
$funcs[] = $blitline->cropResize(
$this->getS3Wrapper()->entityKey($account, '/avatar/feature'),
$cropping['x'],
$cropping['y'],
$cropping['width'],
$cropping['height'],
164,
164,
);
$funcs[] = $blitline->cropResize(
$this->getS3Wrapper()->entityKey($account, '/avatar/thumb'),
$cropping['x'],
$cropping['y'],
$cropping['width'],
$cropping['height'],
36,
36,
);
$blitline->job(
$this->getS3Wrapper()->url(S3Wrapper::BUCKETS__STORAGE, $key),
$funcs
);
$account->getSystemProfile()->setAvatar(true);
$this->getEntityManager()->save($account);
// record log
$this->getActivityLogger()->createLog($account);
return $this->redirectToRoute(self::ROUTES__VIEW);
}
return $this->view(
array(
'account' => $account,
'image' => $this->getS3Wrapper()->url(S3Wrapper::BUCKETS__STORAGE, $key),
)
);
}
/**
* @return DocumentScene
* @throws \Exception
* @Route("/profile/avatar",name = ProfileController::ROUTES__AVATAR)
*/
public function avatarAction()
{
$account = $this->getGlobalContext()->getEffectiveAccount();
$fields = $this->getS3Wrapper()->formInputs(S3Wrapper::BUCKETS__STORAGE, array(
'success_action_redirect' => $this->generateUrl(
self::ROUTES__AVATAR_CROP,
[],
UrlGeneratorInterface::ABSOLUTE_URL
),
'key' => ltrim($this->getS3Wrapper()->entityKey($account, '/avatar/original'), '/'),
'Content-Type' => '^',
));
$action = $this->getS3Wrapper()->formAction(S3Wrapper::BUCKETS__STORAGE);
$form = $this->getFormFactory()->createNamedBuilder('');
foreach ($fields as $name => $value) {
$form->add($name, HiddenType::class, array(
'data' => $value,
));
}
$form
->add('file', FileType::class, [])
->setAction($action);
$form = $form
->getForm();
return $this->view(
array(
'form' => $form->createView(),
)
);
}
}