Simple Permissions without Authentication / Authorization
I recently worked on a Rails application where a user needs edit capabilities on a resource, but would like to share a view to this resource with others and not worry about them messing with it. Typically this would entail an Authorization system to provide the proper permissions. Then we’ll need an Authentication system to register a user, and identify them within the system.
Mind you, this is a bit of toy app, and this seemed like a really overblown solution for a simple problem. Lots to configure, and lots of user interaction. In short, way too many moving pieces. Luckily there is a simple solution. Use you some Tokens!
Add private_token and public_token strings via a migration and throw the following in your model:
# my_model.rb
def self.find_by_token(token)
mymodel = MyModel.where('private_token = ? or public_token = ?',
token, token).first || raise(ActiveRecord::RecordNotFound)
@edit_disabled = mymodel.public_token == token
mymodel
end
def can_edit?
@edit_disabled
end
# ...
private
def generate_tokens
self[:public_token] = generate_token
self[:private_token] = generate_token
end
def generate_token
SecureRandom.base64(10).tr('+/=', '-_ ').strip.delete("\n")
end
Now in your controller actions you can assume that the id coming in is now your token, so go ahead and use your new finder to get the trip.
# my_model_controller.rb
def show
@mymodel = MyModel.find_by_token(params[:id])
end
Now these two links point to the same resource, and we can conditionally do things based on the value of @mymodel.can_edit?
http://mysite.com/mymodel/yl45gikaH2sDKA <= public
http://mysite.com/mymodel/2s9GyIrykREPGg < = private
Obviously this is not a hard core security mechanism, but it’s pretty good at providing basic security with very little work.

If you’re building Ruby web apps (Rails, Sinatra, Padrino, …) and you haven’t tried out Heroku, please do so. Immediately. You can run on the base package, which is more than adequate for a toy or even small business app, for free. It’s great for trying things out.





Love it or hate it, pair programming is a large component of many agile development methodologies. I’ve become a firm believer in the benefits of pairing, and very rarely write code nowadays without some degree of collaboration with a second (or third) developer. The benefits have been vast. The code is better thought out because a pairing session always starts with a discussion of the approach to be taken. Fat-fingered mistakes are headed off at the pass because a second set of eyes is closely watching what’s being typed. Less time is wasted checking email, taking calls from the in-laws, and just generally doing things that would annoy the second member of the pair. Above all, it allows developers to analyze and quickly debate the approach being taken, and adjust and improve that approach throughout the development cycle.