login hook  Bottom

  • Didn't there used to be a way to hook into a login so you could do some routine every time a person logs in?

    I found login as a hook in the documentation but looking at the Users system there does not seem to be a hook for it. Any ideas how to "hook" a function to a users login?

    thanks!
  • You can use this hook type like that:

    Code

    if (!pnModRegisterHook('zikula', 'login', 'GUI', 'YourModule', 'user', 'yourfunction')) {
            return LogUtil::registerError('Could not register login hook');
        }
    ...
        if (!pnModUnregisterHook('zikula', 'login', 'GUI', 'YourModule', 'user', 'yourfunction')) {
            return LogUtil::registerError('Could not unregister login hook');
        }


    Then you can implement the function in YourModule/pnuser.php or YourModule/pnuser/yourfunction.php like below:

    Code

    /**
     * login hook functionality for hooking into login process
     */

    function YourModule_user_yourfunction($args)
    {
        $dom = ZLanguage::getModuleDomain('YourModule');

        if ((!isset($args['objectid'])) ||
            (!isset($args['extrainfo']))) {
            return LogUtil::registerError(__('Error! Could not do what you wanted. Please check your input.', $dom));
        }

        $userid = $args['objectid'];

        // Example: doing a custom redirect for user 123
        if ($userid == 123) {
            pnRedirect(pnModURL('YourModule', 'user', 'yourotherfunction'));
            pnShutdown();
        }

        // do normal redirect controlled by users module
    }


    --
    Guite | ModuleStudio
  • is there any documentation of all the available hooks?
  • Not for the login-hooks, I was never able to get the information to create even the framework for the docs, so I gave up after awhile. Looks like what we have there may help me finish FirstVisit at least.

    That first code block goes in the pninit.php file correct Axel?



    edited by: HalbrookTech, datetimebrief

    --
    Home Page | Find on Facebook | Follow on Twitter
  • Yes, that is the common use case. In general Zikula allows developers to do that everywhere though.

    --
    Guite | ModuleStudio
  • Then is there also a logout hook?
  • Yes, it works the same way.

    --
    Guite | ModuleStudio
  • I just tried this and it does not seem to be executing my function.

    Code

    function flashchat_user_loginhook($args)
    {
        if ((!isset($args['objectid'])) ||
            (!isset($args['extrainfo']))) {
            return LogUtil::registerError (_MODARGSERROR);
        }

        $userid = $args['objectid'];

        pnModDBInfoLoad('flashchat');

        pnModAPIFunc('flashchat', 'user', 'delchats', array('uid' => $userid));

        // do normal redirect controlled by users module
    }


    And here is the API function

    Code

    function flashchat_userapi_delchats($args)
    {
        $uid = isset($args['uid']) ? $args['uid'] : null;
        if (!($uid) || !is_numeric($uid)) {
            return LogUtil::registerError (_MODARGSERROR);
        }

        // Security check - important to do this as early on as possible to
        // avoid potential security holes or just too much wasted processing
        if (!SecurityUtil::checkPermission('flashchat::', '::', ACCESS_OVERVIEW)){
            return LogUtil::registerPermissionError();
        }
        $pntable = pnDBGetTables();
        $flashchatColumn = $pntable['flashchat_column'];
        $where = "$flashchatColumn[uid] = '" . (int)$uid . "'";

        $res = DBUtil::deleteWhere('flashchat', $where);
        if (!$res) {
            return false;
        }
        return true;
    }
  • Add a

    Code

    die('Test');

    before

    Code

    $userid = $args['objectid'];

    to see if it gets executed.

    --
    Guite | ModuleStudio
  • I did this.
    And no, it did not execute.
  • If you use the pnuserapi.php you have to register the hook for API. If you register the hook for GUI you have to use a pnuser.php function instead. See the third parameters in my first post above.

    --
    Guite | ModuleStudio
  • I have tried it both ways.
  • I don't see anywhere in the Core where a hook is created for login or logout... so I think this doesn't work.

This list is based on users active over the last 60 minutes.