Posted: Fri Jun 19, 2009 10:09 pm Post subject: RE:Ruby as CGI Script
Okay, I've run into another problem. (I decided to go with DataMapper btw, thanks for talking me into not using MySQL )
I have some Sinatra... things (Callbacks, hooks, or something?) and I want some to be protected, so that the user logged in needs to be at least a certain rank to view the page. If they aren't, redirect to he home page with a message.
I could do something like this for each, but I was wondering if there was some way to simplify it, make it DRYer.
Ruby:
get '/protected/path'do #session[:uid] is the user's id, nil means not logged in if(session[:uid] == nil)or(User[session[:uid]].level < 3)#User needs to be at least level three to get here
session[:flash] = 'You do not have permission to view this'
redirect '/' end #If we get here, the user is allowed. Continue on with page rendering, whatever. end
Sponsor Sponsor
rdrake
Posted: Sat Jun 20, 2009 1:32 pm Post subject: RE:Ruby as CGI Script
Basically it looks as though you specify user levels, and you can add methods to the base User class in order to perform checks to see if the user is above a certain level.
Their example:
Ruby:
#somewhere in the murky depths of your sinatra app class User
def peasant?
self.permission_level == 0 end
end
DtY
Posted: Sat Jun 20, 2009 3:28 pm Post subject: RE:Ruby as CGI Script
Ah, looks like I might be reworking all my current authentication stuff, since that seems to do everything I've done so far.
Thanks again.
rdrake
Posted: Sat Jun 20, 2009 9:47 pm Post subject: RE:Ruby as CGI Script
If you're ever looking for an extension to do something for Sinatra and Google isn't helping out, search GitHub. I have no idea why, but Ruby coders love Git.