【问题标题】:Simple User management example for Google App Engine?Google App Engine 的简单用户管理示例?
【发布时间】:2010-11-05 01:01:42
【问题描述】:

我是 Google App Engine 的新手。在阅读本教程时,我发现我们在 php-mysql 中所做的一些事情在 GAE 中不可用。例如,在 dataStore 中,自动增量功能不可用。我也对 GAE 中的会话管理感到困惑。总之,我很困惑,无法想象整个事情。

请告诉我一个简单的用户管理系统,包括用户注册、用户登录、用户注销、会话(创建、管理、销毁)与数据存储。另外请告诉我在哪里可以获得简单但有效的示例。

提前致谢。

【问题讨论】:

    标签: php python google-app-engine


    【解决方案1】:

    我倾向于使用自己的用户和会话管理

    对于我的 Web 处理程序,我将附加一个名为 session 的装饰器和一个名为 authorize 的装饰器。 session 装饰器将为每个请求附加一个会话,authorize 装饰器将确保用户被授权。

    (请注意,授权装饰器特定于我如何开发我的应用程序 - 用户名是大多数请求中的第一个参数)。

    例如,一个 web 处理程序可能看起来像:

    class UserProfile(webapp.RequestHandler):
      @session
      @authorize
      def get(self, user):
         # Do some funky stuff
         # The session is attached to the self object.
         someObjectAttachedToSession = self.SessionObj.SomeStuff
         self.response.out.write("hello %s" % user)
    

    在上面的代码中,session 装饰器根据请求中存在的 cookie 附加了一些我需要的会话内容。 authorize 标头将确保用户只有在会话正确的情况下才能访问该页面。

    装饰器代码如下:

    import functools
    from model import Session
    import logging
    
    def authorize(redirectTo = "/"):
        def factory(method):
            'Ensures that when an auth cookie is presented to the request that is is valid'
            @functools.wraps(method)
            def wrapper(self, *args, **kwargs):
    
                #Get the session parameters
                auth_id = self.request.cookies.get('auth_id', '')
                session_id = self.request.cookies.get('session_id', '')
    
                #Check the db for the session
                session = Session.GetSession(session_id, auth_id)           
    
                if session is None:
                    self.redirect(redirectTo)
                    return
                else:
                    if session.settings is None:
                        self.redirect(redirectTo)
                        return
    
                    username = session.settings.key().name()
    
                    if len(args) > 0:               
                        if username != args[0]:
                            # The user is allowed to view this page.
                            self.redirect(redirectTo)
                            return
    
                result = method(self, *args, **kwargs)
    
                return result
            return wrapper
        return factory
    
    def session(method):
        'Ensures that the sessions object (if it exists) is attached to the request.'
        @functools.wraps(method)
        def wrapper(self, *args, **kwargs):
    
            #Get the session parameters
            auth_id = self.request.cookies.get('auth_id', '')
            session_id = self.request.cookies.get('session_id', '')
    
            #Check the db for the session
            session = Session.GetSession(session_id, auth_id)           
    
            if session is None:
                session = Session()
                session.session_id = Session.MakeId()
                session.auth_token = Session.MakeId()
                session.put()
    
            # Attach the session to the method
            self.SessionObj = session           
    
            #Call the handler.          
            result = method(self, *args, **kwargs)
    
            self.response.headers.add_header('Set-Cookie', 'auth_id=%s; path=/; HttpOnly' % str(session.auth_token))
            self.response.headers.add_header('Set-Cookie', 'session_id=%s; path=/; HttpOnly' % str(session.session_id))
    
            return result
        return wrapper
    
    def redirect(method, redirect = "/user/"):
        'When a known user is logged in redirect them to their home page'
        @functools.wraps(method)
        def wrapper(self, *args, **kwargs):
            try:    
                if self.SessionObj is not None:
                    if self.SessionObj.settings is not None:
                        # Check that the session is correct
                        username = self.SessionObj.settings.key().name()
    
                        self.redirect(redirect + username)
                        return
            except:
                pass
            return method(self, *args, **kwargs)
        return wrapper
    

    【讨论】:

    • 非常感谢!这说得通。但是,如果我必须为了自己的用户管理而集成 django,我整天都会感到困扰。据我所知,webapp 只支持谷歌用户和各种支持。对此有任何帮助或建议吗?我可以不使用 Django 就简单地做到这一点吗?
    • 上面的代码不依赖于谷歌用户,所以你失去了一些开发的便利,但你得到了更多的控制——以上都不需要 Django...——我所有的用户管理都不需要Google 用户 API(用于 twollo.com、ff.amplifeeder.com 等)。我目前只使用 Django 进行模板渲染,所以对 Django 也没有要求。
    • 这仍然是客户用户管理的首选方式吗? Session - 对象是如何实现的,特别是 MakeId() 函数?
    【解决方案2】:

    您无需编写用户管理和注册等所有内容,因为您使用的是 Google 自己的身份验证服务。这都包含在 App Engine 文档中。

    【讨论】:

    • 那么,我的用户必须使用谷歌用户名吗?他们不能只注册到我的网站吗?那么,用户注册会发生什么?
    【解决方案3】:

    Django 是你最好的选择——按照我指出的版本,auth 和 session 都应该按照 Django 文档“正常工作”。 this article 提供了简单的说明和如何从那里开始的示例。

    对于 Django 会话,请参阅here;对于 Django 身份验证,here.

    【讨论】:

    • 虽然我使用过 Django-on-AppEngine,但我不主张忽略 django 上的内置 auth appengines。你这样做有什么理由吗? (是的,我也在 appengine 之外使用 Django)
    • GAE 的“内置身份验证”仅支持 Google 帐户,尤其不支持“用户注册”,这个问题非常具体地要求(这也是我这次什至不费心建议 OpenID 的原因 - - 最近几次我这样做,提问者显然认为这是对他们处理自己用户注册的愿望的攻击!-)。
    • 用户可以在第一次使用 Google(或 OpenId)凭据登录时针对该应用进行注册。
    猜你喜欢
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2010-12-31
    相关资源
    最近更新 更多