An Example UserStore

This is the real-life example of an implementation of FSCAuth. This is the MongoDB UserStore used by this blog.

Basically, a fully functional UserStore can be created in about 50 lines. This particular example uses MongoDB. It also overrides UserData to add the MongoDB BsonId attribute. This isn't really required, but it makes things a lot easier.

And here is the code:

public class MongoUserData : UserData{
    [BsonId]
    public ObjectId ID{get;set;}
}

public class MongoUserStore : IUserStore
{
    public MongoUserStore ()
    {
    }
    public UserData GetUser (string username)
    {
        var db=Config.GetDB();
        var u=db.GetCollection<MongoUserData>("users").FindOneAs<MongoUserData>(Query.EQ("Username",username));
        return u;
    }

    public bool UpdateUser (UserData user)
    {
        var userdata=(MongoUserData)user;
        var db=Config.GetDB();
        var u=db.GetCollection<MongoUserData>("users");
        if(u.Save<MongoUserData>(userdata)==null){
            return false;
        }else{
            return true;
        }
    }

    public bool AddUser (UserData user)
    {
        var userdata=(MongoUserData)user;
        var db=Config.GetDB();
        var u=db.GetCollection<MongoUserData>("users");
        if(u.Find(Query.EQ("Username",userdata.Username)).Count()!=0){
            return false;
        }
        u.Insert<MongoUserData>(userdata);
        userdata.UniqueID=userdata.ID.ToString();
        u.Save<MongoUserData>(userdata);
        return true;
    }
    public bool DeleteUser(UserData user){
        var userdata=(MongoUserData)user;
        var db=Config.GetDB();
        var u=db.GetCollection<MongoUserData>("users");
        return u.Remove(Query.EQ("ID",userdata.ID)).Ok;
    }
}

This code is BSD licensed:

Copyright (c) 2010 - 2011 Jordan "Earlz/hckr83" Earls http://lastyearswishes.com All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Posted: 4/23/2011 4:34:08 AM

Umm. Well that wasn't expected

I recently had a bit of a problem when doing a tiny bit of maintenance on my MongoDB database. Luckily, I had backups. Unluckily, they were very old. Luckily again, I had just subscribed to Linode's wonderful backup service. Unluckily, this meant I had a bit more maintenance to do than just copy a set of files.

So now we're back up. And I finally got the feature I've been trying to deploy since last night. Paging! Also, a few minor color differences.

A word of advice: Always do database updates in a staging database to be 100% sure of it's results before doing it on production. And if in doubt even then, take a backup. Hopefully, you won't thank me later

Posted: 4/20/2011 11:20:22 PM

MongoDB and Arch Linux

Note If you can think of some really awesome content to be here(second result in google for "arch linux mongodb", post it in a comment and I'll either post it here or at least link to it

Dear MongoDB, stop sucking so much.

Basically, every once in a while my VPS will be forced to restart. This means MongoDB doesn't get shut down properly.. Which of course means you'll have to run a repair. However, does Arch Linux's package for MongoDB do automatic repairs? Of course not, probably because its a bit unsafe. But worse yet, after doing sudo mongodb --dbpath /.... --repair everything will seem to work fine. So you do the simple

sudo /etc/rc.d/mongodb start

and you see

:: Starting mongodb             [DONE]

as is expected. But then you try to use MongoDB and it doesn't work. Whaiiiiiiii!?

Well, apparently Arch Linux's support for MongoDB also doesn't check any sort of error message. So you'll never see a FAIL unless the mongod file doesn't exist somehow. The reason for this is hidden in /var/log/mongod.

Apparently, when you run mongod as a user it will automatically chown all of the files in the dbpath to the currently running user. That sounds good right? Well, it's not good when you run repair as root, and then try to run mongod as the mongodb user.

Solution:

  • Either sudo in as the mongodb user when doing a repair, or
  • chown the dbpath back to the mongodb user after running a repair.

I prefer the latter, but it's personal choice.

Posted: 3/11/2011 12:15:30 AM