Google App Engine的简单用户管理示例?
我刚开始接触Google App Engine(GAE),在看教程的时候发现,很多在php-mysql中常用的功能在GAE里并不存在。例如,数据存储中没有自动递增的功能。另外,我对GAE中的会话管理也感到困惑。总的来说,我对整个系统感到迷茫,无法理清思路。
请给我推荐一个简单的用户管理系统,包括用户注册、用户登录、用户登出,以及会话的创建、管理和销毁,使用数据存储来实现。同时,也请告诉我在哪里可以找到简单但有效的示例。
提前谢谢你!
3 个回答
1
你不需要自己写用户管理、注册这些功能,因为你可以直接使用谷歌提供的认证服务。这些内容在App Engine的文档里都有详细说明。
22
我通常会使用自己的用户和会话管理方式。
在我的网页处理程序中,我会添加一个叫做 session
的装饰器和一个叫做 authorize
的装饰器。session
装饰器会在每个请求中附加一个会话,而 authorize
装饰器会确保用户是被授权的。
需要注意的是,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
装饰器会根据请求中存在的 cookies 附加一些我需要的会话信息。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