All pastes #476931 Raw Edit

Untitled

public text v1 · immutable
·published 2007-05-08 11:50 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using Db4objects.Db4o;
using System.Web;
using System.Web.SessionState;
using System.Net;

using mfa;

public class Backend
{
    public static string file = "mfa.db4o";
    static IObjectContainer db;

    public static IObjectContainer DB
    {
        get
        {
            if (db == null)
                db = Db4oFactory.OpenFile(file);

            if (db == null) System.Diagnostics.Debug.Assert(false);
            return db;
        }
    }

    static Backend()
    {
        Db4oFactory.Configure().GenerateUUIDs(int.MaxValue);
        //Db4oFactory.Configure().ObjectClass(typeof(Game)).GenerateUUIDs(true);
        //Db4oFactory.Configure().ObjectClass(typeof(User)).GenerateUUIDs(true);
    }


    public static long getID(Object o)
    {
        return DB.Ext().GetObjectInfo(o).GetUUID().GetLongPart();
    }

    public static Object get(long uuid)
    {
        return DB.Ext().GetByUUID(new Db4objects.Db4o.Ext.Db4oUUID(uuid, db.Ext().Identity().GetSignature()));
    }

    public static T get<T>(long uuid)
    {
        return (T)get(uuid);
    }

    public static Village getVillage(HttpContext context)
    {
        long village_id = Int64.Parse(context.Request.QueryString["village_id"]);
        return Backend.get<Village>(village_id);
    }

    public static mfa.User authUser(HttpContext context)
    { 
        HttpSessionState sess = context.Session;
        mfa.User authed = null;

        string auth_username = (string)sess["username_authed"];
        String username = context.Request.QueryString["username"];
        String password = context.Request.QueryString["password"];

        if (auth_username != null)
        {
            IList<mfa.User> usr = db.Query<mfa.User>(delegate(mfa.User u)
            {
                return username == auth_username;
            });

            if (usr.Count > 0)
                authed = usr[0];
        }

        if (authed != null)
            return authed;

        if (username != null && password != null)
        {
            IList<mfa.User> usr = db.Query<mfa.User>(delegate(mfa.User u)
            {
                return username == u.Name && password == u.Password;
            });

            if (usr.Count > 0)
                authed = usr[0];
        }

        if (authed != null)
        {
            sess["username_authed"] = authed.Name;
            return authed;
        }

        return null;
    }

}