2009-06-23 72 views
17

我是Google App Engine的新手。在我阅读本教程时,我发现我们在php-mysql中做的几件事情在GAE中不可用。例如在dataStore中,自动递增功能不可用。另外我对GAE中的会话管理感到困惑。总而言之,我很困惑,无法想象整个事情。Google App Engine的简单用户管理示例?

请告诉我一个简单的用户管理系统,包含用户注册,用户登录,用户注销,会话(创建,管理,销毁)与数据存储。同时请告诉我在哪里可以得到简单而有效的例子。

在此先感谢。

回答

6

Django是你最好的选择 - 根据我指出的版本,auth和sessions应该都是“正常工作”,按照Django文档。 this article给出了简单的指示和如何从那里继续的例子。

对于Django会话,请参见here;为Django验证,here

+1

虽然我用的Django上AppEngine上,我不会主张忽视appengines内置的身份验证过的Django。你有什么理由? (是的,我在外部引擎之外也使用Django) – ironfroggy 2009-06-23 02:24:00

+1

GAE的“内置身份验证”仅支持Google帐户,特别是不支持“用户注册”,这个问题非常具体要求(这也是为什么我甚至不打算建议OpenID这一次 - 最近几次,我提问者们认为这显然是他们想要处理他们自己的用户注册的攻击!)。 – 2009-06-23 02:36:30

+0

用户可以在首次使用Google(或OpenId)凭据登录时针对应用程序进行注册。 – 2009-06-23 11:57:43

1

您不会编写用户管理和注册等等,因为您使用Google自己的身份验证服务。这些都包含在App Engine文档中。

22

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

对于我的网络处理,我会附上一个装饰叫做session和一个叫authorizesession修饰器将为每个请求附加一个会话,并且authorize修饰器将确保用户被授权。

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

因此,例如,网络处理程序可能看起来像:

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装饰重视基础是存在于该请求的饼干,我需要一些东西会议。如果会话是正确的,则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