Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I check if user is logged in at CustomCKFinderAuth.php? #8

Open
darvinsilveira opened this issue Jul 6, 2019 · 8 comments
Open

Comments

@darvinsilveira
Copy link

darvinsilveira commented Jul 6, 2019

I'm trying to implement a CustomCKFinderAuth checking if is an active user, but I'm not finding a way to use Security class inside the CustomCKFinderAuth.php.

I'm using Dependency Injection to try get the user, but I always get the error: "Too few arguments to function App\CustomCKFinderAuth\CustomCKFinderAuth::__construct(), 0 passed..."

@grimmlink
Copy link

Hi,

container is already defined from extension. Here is my Authentication :

<?php

namespace App\Security;

use CKSource\Bundle\CKFinderBundle\Authentication\Authentication as AuthenticationBase;

class CustomCKFinderAuth extends AuthenticationBase
{
    public function authenticate()
    {
        $auth = $this->container->get('security.authorization_checker');
        return $auth->isGranted('IS_AUTHENTICATED_FULLY');
    }
}

@pal-ecl
Copy link

pal-ecl commented Mar 24, 2020

Hi,
I use Symfony 4 and CKFinder 3 for PHP.

I tried grimmlink's Authentication, but it seems that in my case the container has no user, no token provided, although there is a logged in user in my application.

Can someone give me a hint on how to get my logged user in ckfinder Authentication ?

thx

@grimmlink
Copy link

Hi @pal-ecl,

Have you try injection Security service instead of authorization_checker ?
and then $security->isGranted()

@pal-ecl
Copy link

pal-ecl commented Mar 25, 2020

HI @grimmlink,
yes i tried that :

namespace App\CustomCKFinderAuth;

use CKSource\Bundle\CKFinderBundle\Authentication\Authentication as AuthenticationBase;
use Symfony\Component\Security\Core\Security;

class CustomCKFinderAuth extends AuthenticationBase
{
    private $security;

    public function __construct(Security $security){
        $this->security=$security;
    }
    public function authenticate()
    {
...
}

But i have the same error than @envolvelabs :
Too few arguments to function App\CustomCKFinderAuth\CustomCKFinderAuth::__construct(), 0 passed in (...)\var\cache\dev\ContainerNfULVix\getCkfinder_Connector_AuthService.php on line 14 and exactly 1 expected

@sigrtmax
Copy link

I've tired service injection

class CustomCKFinderAuth extends AuthenticationBase
{
    private $authorizationChecker;

    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->authorizationChecker = $authorizationChecker;
    }

    public function authenticate()
    {
        return $this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY');
    }
}

To solve problem in post above, I've added to my service.yaml

ckfinder.connector.auth:
        class: '%ckfinder.connector.auth.class%'
        arguments:
            $authorizationChecker: '@security.authorization_checker'
        public: true
        calls:
            - [setContainer, ['@service_container']]

But it has no result. This way no providing auth token and method call $this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY'); returns false.

@sigrtmax
Copy link

sigrtmax commented Apr 14, 2020

I've found solution that works for me.
In my app I have firewall in security.yaml

firewalls:
        admin:
          pattern:            /(admin)(.*)
          context:            user
          form_login:
            provider:       fos_userbundle
            login_path:     /admin/login
            use_forward:    false
            check_path:     /admin/login_check
            failure_path:   null
          logout:
            path:           /admin/logout
            target:         /admin/login
          anonymous:          true

As we can see pattern for firewall is /admin. But ckfinder's action urls start with /ckfinder. So I edited my configuration

firewalls:
        admin:
-        pattern:            /(admin)(.*)
+        pattern:            /(admin|ckfinder)(.*)
          context:            user
          form_login:
            provider:       fos_userbundle
            login_path:     /admin/login
            use_forward:    false
            check_path:     /admin/login_check
            failure_path:   null
          logout:
            path:           /admin/logout
            target:         /admin/login
          anonymous:          true

After that @grimmlink checking code works fine.

@BoShurik
Copy link
Contributor

You can add admin prefix to ckfinder urls:

# config/routes/ckfinder.yml

ckfinder_connector:
    resource: "@CKSourceCKFinderBundle/Resources/config/routing.yml"
    prefix:   /admin

@pal-ecl
Copy link

pal-ecl commented Apr 29, 2020

@sigrtmax solution works for me, thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants