diff options
author | Christopher Allan Webber <cwebber@dustycloud.org> | 2011-04-03 18:34:05 -0500 |
---|---|---|
committer | Christopher Allan Webber <cwebber@dustycloud.org> | 2011-04-03 18:34:05 -0500 |
commit | 58dec5efe56d8affd7aa60f5d01a3c7f3d4fcbb4 (patch) | |
tree | c2397069af5179887db47fa7232b200fac4471ea /mediagoblin/util.py | |
parent | b058cf15f0ef79fcc4d24f2952e5d55ff0be46cc (diff) | |
download | mediagoblin-58dec5efe56d8affd7aa60f5d01a3c7f3d4fcbb4.tar.lz mediagoblin-58dec5efe56d8affd7aa60f5d01a3c7f3d4fcbb4.tar.xz mediagoblin-58dec5efe56d8affd7aa60f5d01a3c7f3d4fcbb4.zip |
Added setup_user_in_request
Diffstat (limited to 'mediagoblin/util.py')
-rw-r--r-- | mediagoblin/util.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 578261b9..5a56d432 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -15,6 +15,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import jinja2 +import mongokit def get_jinja_env(user_template_path=None): if user_template_path: @@ -25,3 +26,27 @@ def get_jinja_env(user_template_path=None): loader = jinja2.PackageLoader('mediagoblin', 'templates') return jinja2.Environment(loader=loader, autoescape=True) + + +def setup_user_in_request(request): + """ + Examine a request and tack on a request.user parameter if that's + appropriate. + """ + if not request.session.has_key('user_id'): + return + + try: + user = request.db.User.one({'_id': request.session['user_id']}) + + if user: + request.user = user + else: + # Something's wrong... this user doesn't exist? Invalidate + # this session. + request.session.invalidate() + + except mongokit.MultipleResultsFound: + # Something's wrong... we shouldn't have multiple users with + # the same user id. Invalidate this session. + request.session.invalidate() |