Google App Engine的简单用户管理示例? [英] Simple User management example for Google App Engine?

查看:95
本文介绍了Google App Engine的简单用户管理示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Google App Engine的新手。在我阅读教程时,我发现我们在php-mysql中做的几件事在GAE中不可用。例如在dataStore中,自动递增功能不可用。另外我对GAE中的会话管理感到困惑。

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



在此先感谢。

解决方案

我倾向于使用我自己的用户和会话管理系统。对于我的web处理程序,我将附加一个名为 session 的装饰器和一个名为 authorize 的装饰器。 会话装饰器将为每个请求附加一个会话,并且授权装饰器将确保用户被授权。



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

因此,例如一个web处理程序可能如下所示:

  class UserProfile(webapp.RequestHandler) :
@session
@authorize
def get(self,user):
#做一些时髦的东西
#会话附加到自我对象上。
someObjectAttachedToSession = self.SessionObj.SomeStuff
self.response.out.write(hello%s%user)

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



装饰者代码如下:

 从模型导入中导入functools 
会话
导入日志

def authorize(redirectTo =/):
def factory(method):
'确保当一个auth cookie提交给有效的请求'
@ functools.wraps(方法)
def包装(self,* args,** kwargs):

#获取会话参数
auth_id = self.request.cookies.get( 'auth_id','')
session_id = self.request.cookies.get('session_id','')

#检查会话数据库
session = Session .GetSession(session_id,auth_id)

如果session是None:
self.redirect(redirectTo)
return
el se:
如果session.settings是None:
self.redirect(redirectTo)
return
$ b username = session.settings.key()。name()

if len(args)> 0:
如果用户名!= args [0]:
#允许用户查看此页面。
self.redirect(redirectTo)
返回

result =方法(self,* args,** kwargs)

返回结果
返回包装
返回工厂

def session(方法):
'确保会话对象(如果存在的话)被附加到请求中'
@functools .wrap(方法)
def包装(self,* args,** kwargs):

#获取会话参数
auth_id = self.request.cookies.get(' auth_id','')
session_id = self.request.cookies.get('session_id','')

#检查会话数据库
session = Session。 GetSession(session_id,auth_id)

如果session为None:
session = Session()
session.session_id = Session.MakeId()
session.auth_token = Session .MakeId()
session.put()

#将会话附加到方法
self.SessionObj = session

#调用处理程序。
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))

返回结果
返回包装

def重定向(method,redirect =/ user /):
'当一个已知的用户登录时,将它们重定向到他们的主页'
@ functools.wraps(method)
def wrapper(self,* args,** kwargs):
try:
如果self.SessionObj不是None:
如果self.SessionObj.settings不是None:
#检查会话是否正确
username = self.SessionObj.settings.key()。name()

self.redirect(重定向+用户名)
返回
除了:
通过
返回方法(self,* args,** kwargs)
返回包装


I am newbie in Google App Engine. While I was going through the tutorial, I found several things that we do in php-mysql is not available in GAE. For example in dataStore auto increment feature is not available. Also I am confused about session management in GAE. Over all I am confused and can not visualize the whole thing.

Please advise me a simple user management system with user registration, user login, user logout, session (create,manage,destroy) with data Store. Also please advise me where I can get simple but effective examples.

Thanks in advance.

解决方案

I tend to use my own user and session manangement

For my web handlers I will attach a decorator called session and one called authorize. The session decorator will attach a session to every request, and the authorize decorator will make sure that the user is authorised.

(A word of caution, the authorize decorator is specific to how I develop my applications - the username being the first parameter in most requests).

So for example a web handler may look like:

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)

In the above code, the session decorator attaches some session stuff that I need based on the cookies that are present on the request. The authorize header will make sure that the user can only access the page if the session is the correct one.

The decorators code are below:

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

这篇关于Google App Engine的简单用户管理示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆