Thursday, April 1, 2010

Building your own permission system in ruby on rails.

If you're like me and have a application that's growing into a fairly complex application and certain pages are only accessible to certain users/groups then the best way to keep track of your permissions is to generate a permissions model to store the permissions data. This article is a step by step guide to creating a fully functioning permissions that should suit your applications needs.

Step 1, Create the UserPermission model.

script/generate model UserPermission controller:string action:string id_of_object:integer t.integer person_id:integer

You may or may not want to delete the t.timestamps out of the migration that was created for you in db/migrate (should be your newest migration, eg. 20100401043834_create_user_permissions.rb) as I don't feel timestamps are necessary for this type of data.

In your User model add:

has_many :user_permission

In your UserPermission model add:

belongs_to :person
validates_presence_of :controller
validates_presence_of :action
validates_presence_of :id_of_object
validates_presence_of :person_id


Step 2, Implement your before_filter method
Note you must already have your own method to access the current user to complete this step.
In this code example we access our current user with the @user variable (which in my code this is defined in a before_filter that executes on every page)

In app/controllers/application_controller.rb, write the following. I wrote mine under private.
def require_permission
   p =  = @user.user_permission.first(:conditions => "controller = '%s' and (action = '%s' OR action = '*')" % [params[:controller], params[:action]])
    if params[:id] and p
      unless p.id_of_object == params[:id].to_i or p.id_of_object == 0
        p = false
      end
    end
    unless p
      flash[:notice] = "Permission Denied"
      redirect_to :controller => 'people'
    end
end

Step 3, Implement
Add a before_filter :require_permission anywhere where you want this to execute.

Now lets create some permissions, I do this in my app console with the following code but you may create your own view from here to do this.

script/console production in your app directory to access console for your production environment (remove production for development environment)

Then type this:
p = @user.user_permission.new(:controller => 'posts', :action => '*', :id_of_object => 0); p.save

This will give you access to all objects and all actions for posts. * is a wildcard for :action and 0 is a wildcard for :id_of_objects, you can set this to something specific like :action => 'new' or set the :id_of_object to a specific number.

Anyways hope this helps, if any questions feel free to ask!

Tuesday, August 18, 2009

Music Video: Electric Feel

MGMT - "Electric Feel" Music Video

I thought this was one of the most creative music videos I have seen in a while. What you think? Leave comments please!

Monday, August 3, 2009

Website Development, The Do's and Don'ts.

DO use frameworks.
Frameworks is the serious shit. If you're going into any project, or any task, and you're building it around a framework, you're construction process will always go more smoothly, from CSS frameworks (960.gs, Blueprint), to web application frameworks, such as Django (Python), CakePHP (PHP), Rails (Ruby).
You'll be working with an environment that helps you worry less about structure and standards and focus on the development.

DO write validated HTML.
If you aren't doing this, you'll be shamed upon by anyone looking at your source. You should care because search engines care; however, even writing validated html does not necessarily mean you're writing meaningful html.
DO write meaningful HTML.
Doing <h1><img src="/imgs/mylogo.jpg" /></h1>. Well knock it off. To a computer it means the image link is the header of your page. Instead, do <h1>My Site Name</h1> then add some css style to the h1 tag like, say you wanted to replace that with an image, you could add the attributes: display:none; background:url('/imgs/mylogo.jpg'). If you're writing your html and thinking about the design at the same time, you're simply not doing it right. Instead, just write the content out. Use <h1>s, you may use <div> to split up some sections, or you should use <p> to wrap around your text. Then when you're done with that you add the css attributes to the base tags first. Then follow on with classes for specific detailing. A good example of displaying your page header would be scribefire's homepage.
DO be enthusiastic.

If you want your site to do well, then think about it, be creative, think of different ways to improve your site. Think about your readers wants. Try to get feed back and always challenge yourself. If you wanted to do something in ajax, write down specifically what you're trying to achieve and learn it (Trust me it's not that hard). You're living in an information age, if you feel held back by knowledge, you're not taking advantage.
DON'T get too enthusiastic.
But you can't be too enthusiastic, see when you're researching, you're not really constructing anything. The only way for your site to change, or at least it's core attributes can be your doing. And if you spend too much time researching but not enough time developing, you start to wander too far off and never get anything done due to losing a sense of direction. Try to be focused. Write down a requirement and fulfill it. One requirement at a time. So if you need a comment system but want your users to sign in first. Make the user controller first, and create the log in page. By breaking down the things that you can become more focused on a specific task.
DON'T design sites with flash.
Don't use flash for design purposes. You might think it's cute, or cool. It's not, in fact it's fucking annoying, it wastes my memory, Flash is proprietary and it's support outside of Mac and Windows is pretty shit. You make your site unvisitable from my PHONE. If you have a application that you developed, like a video player or something, then by all means, use it. But if putting flash on your site to represent a menu which doesn't do anything but have a couple hyper links then why are you using flash? Flash is not fast, flash isn't truly re sizable. Flash is one of those things that you should really want to use less of. Adobe hyped you kids up to make you think it was the shit, it really isn't.


Friday, June 12, 2009

Firefox 3.5 is amazing.

I know my title seems like it's lacking creativity, but I was just being honest. I had to go through 5 minutes of instructions on how to install the firefox 3.5 package for Ubuntu but it wasn't too troublesome. You just needed to goto Mozilla's personal repository and then follow the instructions from there on.

After installing you'll notice not much has change. In fact, everything but the new tab button seems pretty much the same.

But under the hood is where the new firefox 3.5 just shines.

No more render hick-ups, no more rough scrolling, everything is just fast.

Web pages render incredibly fast. And from what I've heard, Firefox implements state of the art standards from the world wide web and is really just a showcase of how powerful the open-source community is.

Monday, May 18, 2009

DotA HK logo


So I woke up this morning, and I normally sleep with either my girlfriend in my bed or my laptop in my bed. I can't really say which I prefer sleeping with more, but today I woke up thinking about DotA.HK, now I'm a guy who always has a idea but sometimes never takes the time to fully implement them so I decided to take a look at my past work on various web projects, turns out I had some pretty cool stuff ready made for the DotA.HK logo. Take a look!

Monday, May 4, 2009

Personas for Firefox

Don't really like using themes to change the look of your Firefox?
Want something refreshing? Try Mozilla's new personas!

It's a very cool Firefox add on which allows you to easily change your browser's "clothes".

Monday, April 27, 2009

JGhostInfoceptor project started, and open source.

Yes, I've decided to open-source my GhostInfoceptor and have disbanded the original GhostInfoceptor which was written in Python and renamed it as JGhostInfoceptor written in (yep you guessed it) Java.

So far I'm very pleased with how Java deals with buffers and it's garbage collection. It parses the log file very quickly and I believe it could be quite easy to make this application multi-threaded if I needed to.

Anyways I am using GIT for the distributed version control system as I really like it's philosophy and helps me keep track of everything quite easily.

You're all welcome to fork my repository and start making your improvements to propose and I will gladly take the time to consider third-party improvements.

JGhostInfoceptor Repository link

Sunday, April 19, 2009

Quake is back, play for free!

I just recently discovered Quake Live where you can play Quake straight from your web browser, not only is it a very solid implementation of the game, it's extremely fast and I absolutely have zero problems getting into the game and playing.

I suggest you all give it a try if you were ever into Quake!

Friday, April 17, 2009

DOTA.HK in trouble with Warden, exit Python, enter Java.

Well it's not secret that we've run into problems at dota.hk getting out hosting platform compliant with Warden. In fact none of us at dota.hk are developers for Ghost++, which is okay, Ghost++ itself has a pretty big pool of developers willing to help out. So support for Warden should be in by the end of the month. I was never scared that we would not find a solution, in fact I'm very happy that Blizzard has done something to fight against Maphack once and for all.

I know for fact Blizzard has nothing against bots that only enhance the experience for users on Battle.net, in fact I remember during the Diablo and Starcraft days that Blizzard actually came out with a source code for developers to develop their own chat bots (this was before we even needed cd-keys to have a chat bot on battle.net).

Right before this happened though I was working on an application I wrote in Python that was finished but was very hard to deploy on multiple servers because of my poor application design. Basically I had to edit a lot of variables within the script for each server.
Example:
NTTHK: config_name = 1Last_update
HKNET: config_name = 2Last_update.

Gosh was that annoying. I wasn't really concerned about the application design, just so long as it worked. Well I'm through with that and have come to a conclusion that using Python to write server-side applications is not the greatest idea, well maybe if it was one server, but not if I was writing for 6 servers that also probably needed to be updated from time to time.

Another problem with Python is that there is no decent IDE's out for it, you have no idea how annoying it is keeping track of indentions. This is how I immediately fell in love with Java+Eclipse. Java and Python are both cross platform which is very important for me as a Linux user in a Windows world. However I think the Java virtual machine is much better than Python's interpreter, because it's very hard to write a application which has problems because of how well the debug system in Java is. I don't have to worry about installing the MySQLdb module, SQLite module on every damn server and Java's C++ (OOP) style syntax makes writing huge applications much more readible and forces good programming practice.

Modular programming in Python is also a huge pain in the ass because you need to define the document folder for where you want to import your custom modules from, this varies from server to server and as far as I know there's no way to automatically determine a path. Don't get my wrong, I love python; but I guess all languages have their ups and downs.

So in a way, I'm relieved that our bots aren't functioning correctly at the moment. It gave me some time to look into using another language to write the application I needed to write. It gave me time to just take a break from all this development and just do some research which was a relief. I guess the biggest task that developers must face is staying on top of their game by constantly forcing themselves to learn new things.

So for the time being, I did some further database optimizations to DOTA.HK V2 to extend it's lifespan, because it looks like we're going to have to delay the release of DOTA.HK v3.

Wednesday, June 11, 2008

Kongregate is my time killer.



Got time to burn? Bored? Look no further, I have found our solution! Meet Kongregate, it's a web portal for games, with an achievement system similar to the one on Xbox Live with thousands of different games to play... All for the price of $0.00. What's the catch? Some ads here and there and less time with your friends and family, unless they're hooked on Kongregate too!

Don't forget to tell them fivetwentysix was your referrer! Back to playing some Platform Racing 2. *hehehe.*

Tuesday, May 13, 2008

Mibbit is the answer to keeping IRC popular

I've always been an IRC guy and maybe that makes me a little biased on this topic. But over the years, I come to love IRC more and more each time I use it. It's ability to connect hundreds of people chatting simultaneously is still astonishing. 

It's no surprise that IRC might not appeal to the new day and age with clients like Xchat, Mirc; however Mibbit has done an outstanding job at making IRC very approachable for an IRC newbie.

Not only is Mibbit a very fast IRC client, it's also browser based built with AJAX technologies. That means you can sign in to your Mibbit profile from any computer with a web browser.

The only downside to Mibbit is that it's not a stand alone client. Apart from that Mibbit is as good of a client as it'll get!

Sunday, May 4, 2008

The last.fm radio station




It seems like the world wide web has been socially interactive since we can remember ever using the internet on a daily basis. It started with groups like AOL, communication software like ICQ, online gaming platforms like Battle.net, and web sites like CNET. Seven years ago Yahoo decided to make a legal attempt at bringing commercial music to the web in the form of an online web radio station. Unfortunately this service was filled with advertisements, bad sound quality, bad portability (Uses and Requires closed-source Microsoft technologies). 

A lot of the web has been talking about last.fm and I decided to give it a try just recently, and in response I would like to give it two thumbs up!
It's simple, it's great, it's cross platform, it's free, and it does what it's suppose to do, play music! 

In my opinion, what makes last.fm really stand out is that it has a very noncommercial feel to it, ads are at the bottom of the web-site, the last.fm client which can be compiled for pretty much any platform Win/Linux/Mac has no advertisements on it. 

The standalone client really deserves recognition, it is open-source so that means anyone can look at the code and modify it to their pleasing. You can even turn it into a plug in for your favorite media player. Last.fm is definitely a great tool for all of us to use and I am very thankful.