calinrada / PhalconUserPlugin

184 stars 68 forks source link

how to use it? Any demo? #27

Open mikguo opened 9 years ago

mikguo commented 9 years ago

when I insert $di['dispatcher'] = function() use ($di) { $eventsManager = $di->getShared('eventsManager'); $security = new \Phalcon\UserPlugin\Plugin\Security($di); $eventsManager->attach('dispatch', $security);

        $dispatcher = new \Phalcon\Mvc\Dispatcher();
        $dispatcher->setEventsManager($eventsManager);
        return $dispatcher;
    };

to my project, it's report fatal error: Call to a member function hasRememberMe() on a non-object. I need setAuth first? Could you uploads any demo how to use it? Thanks.

mizterp commented 9 years ago

At the moment there is a bug with the "master" repo, so you'll need to grab v2.0 via composer

mikguo commented 9 years ago

sorry, it's still report the error.

2015-09-15 12:38 GMT+08:00 Mike Guo mikguo@gmail.com:

ok, thanks.

2015-09-15 12:34 GMT+08:00 Carl notifications@github.com:

At the moment there is a bug with the "master" repo, so you'll need to grab v2.0 via composer

— Reply to this email directly or view it on GitHub https://github.com/calinrada/PhalconUserPlugin/issues/27#issuecomment-140278118 .

mizterp commented 9 years ago

Could you share your project via github?

My services.php looks something like this:

$di = new FactoryDefault();

//Make config settings available
$di->set('config', $config, true);

/**
 * Plugging the PhalconUserPlugin
 */
$di['dispatcher'] = function() use ($di) {
    $eventsManager = $di->getShared('eventsManager');
    $security = new \Phalcon\UserPlugin\Plugin\Security($di);
    $eventsManager->attach('dispatch', $security);
    $dispatcher = new Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
};
/**
 * Register Auth, ACL and Mail services used by PhalconUserPlugin
 */
$di['auth'] = function(){
    return new \Phalcon\UserPlugin\Auth\Auth();
};
$di['acl'] = function() {
    return new \Phalcon\UserPlugin\Acl\Acl();
};
$di['mail'] = function() {
    return new \Phalcon\UserPlugin\Mail\Mail();
};
mikguo commented 9 years ago

my project uses multiple model, and in Model.php, I insert the code:

public function registerServices(DiInterface $di) { /* * Read configuration / $config = include APP_PATH . "/apps/backend/config/config.php";

/**
 * Setting up the view component
 */
$di['view'] = function () use ($config) {
    $view = new View();
    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ));

            $compiler = $volt->getCompiler();
            $compiler->addExtension(new PhpFunctionExtension());

            return $volt;
        },
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
    ));
    // Disable several levels
    $view->disableLevel(
        array(
            View::LEVEL_LAYOUT      => true,
            View::LEVEL_MAIN_LAYOUT => true
        )
    );
    return $view;
};

/**
 * Database connection is created based in the parameters defined

in the configuration file */ $di['db'] = function () use ($config) { return new DbAdapter($config->database->toArray()); };

/**
 * Plugging the PhalconUserPlugin
 */
$di['dispatcher'] = function() use ($di) {
    $eventsManager = $di->getShared('eventsManager');
    $security = new \Phalcon\UserPlugin\Plugin\Security($di);
    $eventsManager->attach('dispatch', $security);

    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
};

$di['auth'] = function(){
    return new \Phalcon\UserPlugin\Auth\Auth();
};

$di['acl'] = function() {
    return new \Phalcon\UserPlugin\Acl\Acl();
};

$di['mail'] = function() {
    return new \Phalcon\UserPlugin\Mail\Mail();
};

$di->set('config', $config);

}

but when I insert these code, it's report: Fatal error: Call to a member function hasRememberMe() on a non-object in D:\myprojectpath\apps\user\Plugin\Security.php on line 84

2015-09-15 15:20 GMT+08:00 Carl notifications@github.com:

Could you share your project via github?

My services.php looks something like this:

$di = new FactoryDefault();//Make config settings available$di->set('config', $config, true);/* * Plugging the PhalconUserPlugin /$di['dispatcher'] = function() use ($di) { $eventsManager = $di->getShared('eventsManager'); $security = new \Phalcon\UserPlugin\Plugin\Security($di); $eventsManager->attach('dispatch', $security); $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($eventsManager); return $dispatcher;};/* * Register Auth, ACL and Mail services used by PhalconUserPlugin /$di['auth'] = function(){ return new \Phalcon\UserPlugin\Auth\Auth();};$di['acl'] = function() { return new \Phalcon\UserPlugin\Acl\Acl();};$di['mail'] = function() { return new \Phalcon\UserPlugin\Mail\Mail();};

— Reply to this email directly or view it on GitHub https://github.com/calinrada/PhalconUserPlugin/issues/27#issuecomment-140302738 .

mikguo commented 9 years ago

Now I edit the code like:

/**
         * Plugging the PhalconUserPlugin
         */
        $di['auth'] = function(){
            return new \Phalcon\UserPlugin\Auth\Auth();
        };

        $di['acl'] = function() {
            return new \Phalcon\UserPlugin\Acl\Acl();
        };

        $di['mail'] = function() {
            return new \Phalcon\UserPlugin\Mail\Mail();
        };

        $di['dispatcher'] = function() use ($di) {
            $eventsManager = new \Phalcon\Events\Manager();
            $security = new \Phalcon\UserPlugin\Plugin\Security($di);
            $security->setView($di['view'])->setAuth($di['auth']);
            $eventsManager->attach('dispatch', $security);

            $dispatcher = new \Phalcon\Mvc\Dispatcher();
            $dispatcher->setEventsManager($eventsManager);
            return $dispatcher;
        };

the error was fixed. seems like that Security.php lost initialize()?

mizterp commented 9 years ago

Did you remember to configure the 'pup' array in your config?

It should look something like this:

//Example of settings used by PhalconUserPlugin
    'pup' => array(//[P]halcon[U]ser[P]lugin
        'redirect' => array(
            'success' => 'user/profile', //...redirect to this controller/action on success
            'failure' => 'user/login' //... redirect here on auth failure
        ),
        'resources' => array(
            'type' => 'public',
            'resources' => array(
                '*' => array( // All except
                    'user' => array('account', 'profile') //...the actions "account" and "profile" are private
                ),
            )
        ),
    )
mikguo commented 9 years ago

Yes, I have add the config values when start to use it.

mzf commented 8 years ago

Awesome! This thread help me a lot

bnamnguyen commented 8 years ago

$security->setView($di['view'])->setAuth($di['auth']);

This solved my problem, thank you very much.