我是Google App Engine的新手。在我閱讀本教程時,我發現我們在php-mysql中做的幾件事情在GAE中不可用。例如在dataStore中,自動遞增功能不可用。另外我對GAE中的會話管理感到困惑。總而言之,我很困惑,無法想象整個事情。Google App Engine的簡單用戶管理示例?
請告訴我一個簡單的用戶管理系統,包含用戶註冊,用戶登錄,用戶註銷,會話(創建,管理,銷燬)與數據存儲。同時請告訴我在哪裏可以得到簡單而有效的例子。
在此先感謝。
我是Google App Engine的新手。在我閱讀本教程時,我發現我們在php-mysql中做的幾件事情在GAE中不可用。例如在dataStore中,自動遞增功能不可用。另外我對GAE中的會話管理感到困惑。總而言之,我很困惑,無法想象整個事情。Google App Engine的簡單用戶管理示例?
請告訴我一個簡單的用戶管理系統,包含用戶註冊,用戶登錄,用戶註銷,會話(創建,管理,銷燬)與數據存儲。同時請告訴我在哪裏可以得到簡單而有效的例子。
在此先感謝。
Django是你最好的選擇 - 根據我指出的版本,auth和sessions應該都是「正常工作」,按照Django文檔。 this article給出了簡單的指示和如何從那裏繼續的例子。
您不會編寫用戶管理和註冊等等,因爲您使用Google自己的身份驗證服務。這些都包含在App Engine文檔中。
我傾向於使用我自己的用戶和會話manangement
對於我的網絡處理,我會附上一個裝飾叫做session
和一個叫authorize
。 session
修飾器將爲每個請求附加一個會話,並且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
雖然我用的Django上AppEngine上,我不會主張忽視appengines內置的身份驗證過的Django。你有什麼理由? (是的,我在外部引擎之外也使用Django) – ironfroggy 2009-06-23 02:24:00
GAE的「內置身份驗證」僅支持Google帳戶,特別是不支持「用戶註冊」,這個問題非常具體要求(這也是爲什麼我甚至不打算建議OpenID這一次 - 最近幾次,我提問者們認爲這顯然是他們想要處理他們自己的用戶註冊的攻擊!)。 – 2009-06-23 02:36:30
用戶可以在首次使用Google(或OpenId)憑據登錄時針對應用程序進行註冊。 – 2009-06-23 11:57:43