Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Ruby as CGI Script
Index -> Programming, Ruby -> Ruby Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DtY




PostPosted: 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 Smile )

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
Sponsor
sponsor
rdrake




PostPosted: Sat Jun 20, 2009 1:32 pm   Post subject: RE:Ruby as CGI Script

There are a ton of Sinatra extensions. For example, there's the Maxjustus's Sinatra Authentication extension.

Documentation is near the bottom of the page.

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




PostPosted: 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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 19 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: