首页 > 解决方案 > Requiring Login on CKAN

问题描述

We are looking to require all users log in before using CKAN. Ideally, the way it would work is that a non-authenticated user would automatically be redirected to a custom log in form, which does not expose the rest of the site

We've tried creating a plugin with the below code, while it will get some pages to throw a 403 error, we can not get a redirect to work

import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit


def site_read(context, data_dict=None):

    # Get the user name of the logged-in user.
    user_name = context['user']

    # If they're not a logged in user, don't allow them to see content
    if user_name is None:
        return {'success': False,
                'msg': 'Must be logged in to view site'}
    else:
            flash("You need to login first")
            return redirect('http://www.google.com')

class DrexeluhcPlugin(plugins.SingletonPlugin):

    plugins.implements(plugins.IAuthFunctions)

    def get_auth_functions(self):
        return {'site_read': site_read}

标签: ckan

解决方案


I'm afraid you can't do a redirect in an auth function - you return {'success': True} or {'success': False}. Only a controller/view can initiate a redirect.

Another way to implement a login form would be to do something like ckanext-ldap does to override the template to CKAN's login form, and use parts of IAuthenticator. There are no doubt other examples of using IAuthenticator too.


推荐阅读