本文整理汇总了PHP中Symfony\Component\Security\Core\SecurityContext::isGranted方法的典型用法代码示例。如果您正苦于以下问题:PHP SecurityContext::isGranted方法的具体用法?PHP SecurityContext::isGranted怎么用?PHP SecurityContext::isGranted使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Security\Core\SecurityContext
的用法示例。
在下文中一共展示了SecurityContext::isGranted方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onFilterController
public function onFilterController(FilterControllerEvent $event)
{
list($object, $method) = $event->getController();
// the controller could be a proxy
$className = ClassUtils::getClass($object);
$reflectionClass = new \ReflectionClass($className);
$reflectionMethod = $reflectionClass->getMethod($method);
$allControllerAnnotations = $this->annotationReader->getClassAnnotations($reflectionClass);
$allMethodAnnotations = $this->annotationReader->getMethodAnnotations($reflectionMethod);
$guardAnnotationsFilter = function ($annotation) {
return $annotation instanceof Guard;
};
$controllerGuardAnnotations = array_filter($allControllerAnnotations, $guardAnnotationsFilter);
$methodGuardAnnotations = array_filter($allMethodAnnotations, $guardAnnotationsFilter);
$guardAnnotations = array_merge($controllerGuardAnnotations, $methodGuardAnnotations);
$permissions = [];
foreach ($guardAnnotations as $guardAnnotation) {
$value = $guardAnnotation->value;
if (!is_array($value)) {
$value = [$value];
}
$permissions = array_merge($value, $permissions);
}
$permissions = array_unique($permissions);
if (!empty($permissions) && !$this->security->isGranted($permissions)) {
$e = new PermissionRequiredException();
$e->setRequiredPermissions($permissions)->setCurrentPermissions($this->security->getToken()->getUser()->getPermissions());
throw $e;
}
}
示例2: avoidIncorrectVoting
/**
* Listener for comments' votes persistence to avoid voting for own comments
* and multiple voting for comments
*
* @param VotePersistEvent $event
* @return void
*/
public function avoidIncorrectVoting(VotePersistEvent $event)
{
try {
if (!$this->context->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)) {
throw new \Exception('Avoid voting if user is not authenticated');
}
/** @var $vote SignedVoteInterface */
$vote = $event->getVote();
/** @var $user User */
$user = $this->context->getToken()->getUser();
if ($vote->getVoter() !== $user) {
throw new \Exception('Attempt to vote for different user');
}
if ($vote->getComment()->getAuthor() === $user) {
throw new \Exception('Attempt to vote for own comment');
}
$existingVote = $this->voteManager->findVoteBy(array('comment' => $vote->getComment(), 'voter' => $user));
if ($existingVote) {
throw new \Exception('Attempt to vote multiple times for same comment');
}
} catch (\Exception $e) {
$event->abortPersistence();
$event->stopPropagation();
}
}
示例3: contentBlock
/**
* @param string $blockName
* @param array $options
* @param string $default
* @return string
*/
public function contentBlock($blockName, $options = array(), $default = null)
{
$em = $this->doctrine->getManager();
$repository = $em->getRepository('GlavwebContentBlockBundle:ContentBlock');
$contentBlock = $repository->findOneByName($blockName);
$tag = isset($options['tag']) ? $options['tag'] : 'div';
$attr = isset($options['attr']) ? $options['attr'] : array();
if (isset($options['class'])) {
$attr['class'] = $options['class'];
}
if (isset($options['href'])) {
$attr['href'] = $options['href'];
}
if (!$contentBlock) {
$contentBlock = new ContentBlock();
$contentBlock->setName($blockName);
$contentBlock->setBody($default ? $default : $blockName);
$em->persist($contentBlock);
$em->flush();
}
$contentEditable = '';
$dataBlockName = '';
$isEditable = $this->request && $this->request->get('contenteditable') && $this->securityContext->isGranted('ROLE_ADMIN');
if ($isEditable) {
$contentEditable = ' contenteditable="true"';
$dataBlockName = ' data-block-name="' . $blockName . '"';
$attr['class'] = isset($attr['class']) ? $attr['class'] . ' js-content-block' : 'js-content-block';
}
$attrParts = array();
foreach ($attr as $attrName => $value) {
$attrParts[] = sprintf('%s="%s"', $attrName, $value);
}
return '<' . $tag . ' ' . implode(' ', $attrParts) . ' ' . $contentEditable . $dataBlockName . '>' . $contentBlock->getBody() . '</' . $tag . '>';
}
示例4: onSecurityInteractiveLogin
/**
* Do the magic.
*
* @param InteractiveLoginEvent $event
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
if ($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
// user has just logged in
}
if ($this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
// user has logged in using remember_me cookie
}
// do some other magic here
$session = $event->getRequest()->getSession();
$referrer = $session->get('referrer');
if (empty($referrer)) {
$referrer = $event->getRequest()->getUri();
}
$user = $event->getAuthenticationToken()->getUser();
$log = new Log();
$log->setIdUsuario($user);
$log->setDtAcao(new \DateTime());
$log->setNmTabela('acesso');
$log->setCsAcao('ACE');
$log->setTeIpOrigem($event->getRequest()->getClientIp());
$log->setNmScript($referrer);
// Registra login
$this->em->persist($log);
$this->em->flush();
// TODO: Redireciona para útima página visitada
}
示例5: getUserPermissions
/**
* Gets permissions of the given user
*
* @param User $user
* @param Criteria|null $filters
*
* @return array
*/
public function getUserPermissions(User $user, Criteria $filters = null)
{
$entityAclExtension = $this->aclSelector->select($user);
$resources = array_map(function (AclClassInfo $class) use($entityAclExtension) {
return ['type' => $entityAclExtension->getExtensionKey(), 'resource' => $class->getClassName()];
}, $entityAclExtension->getClasses());
if ($filters) {
$collection = new ArrayCollection($resources);
$resources = $collection->matching($filters)->toArray();
}
$result = [];
$originalToken = $this->impersonateUser($user);
try {
foreach ($resources as $resource) {
$oid = new ObjectIdentity($resource['type'], $resource['resource']);
$permissions = [];
foreach ($entityAclExtension->getAllowedPermissions($oid) as $permission) {
if ($this->securityContext->isGranted($permission, $oid)) {
$permissions[] = $permission;
}
}
$result[] = array_merge($resource, ['permissions' => $permissions]);
}
$this->undoImpersonation($originalToken);
} catch (\Exception $e) {
$this->undoImpersonation($originalToken);
throw $e;
}
return $result;
}
示例6: build
/**
* Construit le menu supplémentaire de la gestion des services du serveur
*
* @param SidebarItem $sidebar Sidebar d'origine à completer
*/
public function build(SidebarItem $sidebar)
{
if (!$this->context->isGranted('ROLE_SUPER_ADMIN')) {
return;
}
$server = $sidebar->addChild('olix_server', array('label' => 'Gestion du serveur', 'icon' => 'fa fa-server fa-fw'));
$server->addChild('olix_server_monit', array('label' => 'Gestion des services', 'icon' => 'fa fa-circle fa-fw', 'route' => 'olix_server_monit'));
$server->addChild('olix_server_collectd', array('label' => 'Monitoring', 'icon' => 'fa fa-area-chart fa-fw', 'route' => 'olix_server_collectd'));
}
示例7: buildForm
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('grid', 'enhavo_grid', array('label' => 'form.label.content', 'translation_domain' => 'EnhavoAppBundle'));
if ($this->securityContext->isGranted('WORKFLOW_ACTIVE', $this->dataClass)) {
$entityName = array();
$entityName[0] = $this->dataClass;
$builder->add('workflow_status', 'enhavo_workflow_status', array('label' => 'workflow.form.label.next_state', 'translation_domain' => 'EnhavoWorkflowBundle', 'attr' => $entityName));
}
}
示例8: onSecurityInteractiveLogin
/**
* Do the magic.
*
* @param InteractiveLoginEvent $event
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
if ($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
$user = $this->securityContext->getToken()->getUser();
if ($user instanceof User) {
$user->setLastLoginAt(new DateTime());
$this->em->persist($user);
$this->em->flush($user);
}
}
}
示例9: setDefaultOptions
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$choices = [];
if ($this->context->isGranted(User::ROLE_SUPER_ADMIN)) {
$choices = $this->repository->findAll();
} else {
$groups = $this->groupResolver->getAccessibleGroupsId();
$choices = $this->repository->findByGroups($groups);
}
$resolver->setDefaults(array('label' => 'game.selectMachine', 'class' => 'DPMachineBundle:Machine', 'choices' => $choices));
}
示例10: checkAction
/**
* @Route("/check", name="check_oauth")
* Cette action vérifie que l'utilisateur a remplis son inscription et que le compte est actif.
*/
public function checkAction()
{
$this->securityContext = $this->get('security.context');
$this->token = $this->securityContext->getToken();
if (!$this->securityContext->isGranted('ROLE_ORGA')) {
// Si l'utilisateur ne s'est pas enregistré
return $this->redirect($this->generateUrl("register_oauth"));
} else {
return $this->redirect($this->generateUrl("base_accueil"));
}
}
示例11: checkAnyRole
/**
* @param array $roles
* @return bool
*/
public function checkAnyRole(array $roles)
{
if ($this->securityContext->isGranted('ROLE_SUPER_ADMIN')) {
return true;
}
foreach ($roles as $role) {
if ($this->securityContext->isGranted($role)) {
return true;
}
}
return false;
}
示例12: onSlugSecurityEvent
/**
*
*/
public function onSlugSecurityEvent()
{
$node = $this->request->attributes->get('_nodeTranslation')->getNode();
/* @var SecurityContextInterface $securityContext */
if (false === $this->securityContext->isGranted(PermissionMap::PERMISSION_VIEW, $node)) {
throw new AccessDeniedException('You do not have sufficient rights to access this page.');
}
$locale = $this->request->attributes->get('_locale');
$preview = $this->request->attributes->get('preview');
$nodeMenu = new NodeMenu($this->em, $this->securityContext, $this->acl, $locale, $node, PermissionMap::PERMISSION_VIEW, $preview);
$this->request->attributes->set('_nodeMenu', $nodeMenu);
}
示例13: createUserMenu
/**
* @param Request $request
*
* @return \Knp\Menu\ItemInterface
*/
public function createUserMenu(Request $request)
{
$menu = $this->factory->createItem('user');
if (false === $this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
$menu->addChild('login', ['route' => 'fos_user_security_login', 'label' => $this->translator->trans('login', [], 'menu')]);
} else {
$user = $this->securityContext->getToken()->getUser();
$currentUserMenu = $menu->addChild('current_user', ['label' => $user->getUsername()]);
$currentUserMenu->addChild('profile', ['route' => 'fos_user_profile_show', 'label' => '.icon-user ' . $this->translator->trans('profile', [], 'menu')]);
$currentUserMenu->addChild('logout', ['route' => 'fos_user_security_logout', 'label' => '.icon-off ' . $this->translator->trans('logout', [], 'menu')]);
}
return $menu;
}
示例14: onSecurityInteractiveLogin
/**
* Do the magic.
*
* @param InteractiveLoginEvent $event
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
if ($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
// user has just logged in
}
if ($this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
// user has logged in using remember_me cookie
}
// do some other magic here
$user = $event->getAuthenticationToken()->getUser();
// ...
$this->getUserService()->markLoginInfo();
}
示例15: createMainMenu
/**
* @TODO: Move below to config
*
* @param Request $request
*
* @return \Knp\Menu\ItemInterface
*/
public function createMainMenu(Request $request)
{
$menu = $this->factory->createItem('root');
$menu->setChildrenAttributes(array('class' => 'navbar-nav nav'));
$menu->addChild('nav.home', array('route' => 'quickstart_app_homepage'));
if ($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
$menu->addChild($this->securityContext->getToken()->getUser()->getEmail(), array('route' => 'quickstart_app_account'));
$menu->addChild('nav.logout', array('route' => 'fos_user_security_logout'));
} else {
$menu->addChild('nav.register', array('route' => 'fos_user_registration_register'));
$menu->addChild('nav.login', array('route' => 'fos_user_security_login'));
}
return $menu;
}