2005-06-18 13:46:03 +00:00
|
|
|
class NoteController < ApplicationController
|
2005-08-08 01:54:05 +00:00
|
|
|
|
|
|
|
|
model :user
|
This changeset introduces some integrated web service type features that take advantage
of the Rails 1.1 responds_to functionality. It also lays a foundation for future API
enhancements.
Basically, if you request the /projects, contexts/ or notes/ URLs with a client that specifies that it wants XML, Tracks will return XML. See DHH on the Accept header (http://www.loudthinking.com/arc/000572.html).
But there's a wrinkle. The controller actions mapped to these URLs are protected by an authentication filter. In normal use, Tracks redirects an unauthenticated user to the login screen for session-based authentication.
I've added a secondary authentication check that looks for a valid username and password coming from HTTP_BASIC authentication.
To test out the new functionality, try this:
curl -H 'Accept: application/xml' --basic --user YOUR_TRACKS_USERNAME:YOUR_TRACKS_PASSWORD http://localhost:3000/projects/
curl -H 'Accept: application/xml' --basic --user YOUR_TRACKS_USERNAME:YOUR_TRACKS_PASSWORD http://localhost:3000/contexts/
curl -H 'Accept: application/xml' --basic --user YOUR_TRACKS_USERNAME:YOUR_TRACKS_PASSWORD http://localhost:3000/notes/
HTTP_BASIC sends passwords in plain text, so the use of https is encouraged.
I haven't tested this on a shared host yet, but Coda Hale, whose simple_http_auth inspired this solution and provided some copy and paste code for it (thanks, Coda!), has some notes about how to make it work in his plugin readme (http://svn.codahale.com/simple_http_auth/README). To wit, putting the following in .htaccess:
RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
My thinking on this architecture is as follows:
1) Follow the spirit of responds_to and DRY to leverage existing controller code for API functionality
2) Get away from using the user token for API interactions. Let's keep it for feeds, so it's basically a "lite" form of security for read-only feeds.
3) Keep Tracks in shape to adopt the simply_restful plugin being developed alongside Rails Edge
There's no real new functionality in this release that the existing API didn't provide (except for seeing your notes as XML, and somehow I don't think people are clamoring for that), but this work is an important step to being able to implement the types of API features people have been asking for.
While I was at it, I did some refactoring to the login_controller for readability and style.
Finally, I replaced the activity indicator graphic to work with the new navigation background color.
git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@251 a4c988fc-2ded-0310-b66e-134b36920a42
2006-06-04 07:07:42 +00:00
|
|
|
prepend_before_filter :login_required
|
2005-08-08 01:54:05 +00:00
|
|
|
|
2005-06-18 13:46:03 +00:00
|
|
|
layout "standard"
|
2005-08-08 01:54:05 +00:00
|
|
|
|
2005-06-18 13:46:03 +00:00
|
|
|
def index
|
2005-08-08 01:54:05 +00:00
|
|
|
@all_notes = @user.notes
|
2005-06-18 13:46:03 +00:00
|
|
|
@page_title = "TRACKS::All notes"
|
This changeset introduces some integrated web service type features that take advantage
of the Rails 1.1 responds_to functionality. It also lays a foundation for future API
enhancements.
Basically, if you request the /projects, contexts/ or notes/ URLs with a client that specifies that it wants XML, Tracks will return XML. See DHH on the Accept header (http://www.loudthinking.com/arc/000572.html).
But there's a wrinkle. The controller actions mapped to these URLs are protected by an authentication filter. In normal use, Tracks redirects an unauthenticated user to the login screen for session-based authentication.
I've added a secondary authentication check that looks for a valid username and password coming from HTTP_BASIC authentication.
To test out the new functionality, try this:
curl -H 'Accept: application/xml' --basic --user YOUR_TRACKS_USERNAME:YOUR_TRACKS_PASSWORD http://localhost:3000/projects/
curl -H 'Accept: application/xml' --basic --user YOUR_TRACKS_USERNAME:YOUR_TRACKS_PASSWORD http://localhost:3000/contexts/
curl -H 'Accept: application/xml' --basic --user YOUR_TRACKS_USERNAME:YOUR_TRACKS_PASSWORD http://localhost:3000/notes/
HTTP_BASIC sends passwords in plain text, so the use of https is encouraged.
I haven't tested this on a shared host yet, but Coda Hale, whose simple_http_auth inspired this solution and provided some copy and paste code for it (thanks, Coda!), has some notes about how to make it work in his plugin readme (http://svn.codahale.com/simple_http_auth/README). To wit, putting the following in .htaccess:
RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
My thinking on this architecture is as follows:
1) Follow the spirit of responds_to and DRY to leverage existing controller code for API functionality
2) Get away from using the user token for API interactions. Let's keep it for feeds, so it's basically a "lite" form of security for read-only feeds.
3) Keep Tracks in shape to adopt the simply_restful plugin being developed alongside Rails Edge
There's no real new functionality in this release that the existing API didn't provide (except for seeing your notes as XML, and somehow I don't think people are clamoring for that), but this work is an important step to being able to implement the types of API features people have been asking for.
While I was at it, I did some refactoring to the login_controller for readability and style.
Finally, I replaced the activity indicator graphic to work with the new navigation background color.
git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@251 a4c988fc-2ded-0310-b66e-134b36920a42
2006-06-04 07:07:42 +00:00
|
|
|
respond_to do |wants|
|
|
|
|
|
wants.html
|
2006-06-10 06:05:01 +00:00
|
|
|
wants.xml { render :xml => @all_notes.to_xml( :except => :user_id ) }
|
This changeset introduces some integrated web service type features that take advantage
of the Rails 1.1 responds_to functionality. It also lays a foundation for future API
enhancements.
Basically, if you request the /projects, contexts/ or notes/ URLs with a client that specifies that it wants XML, Tracks will return XML. See DHH on the Accept header (http://www.loudthinking.com/arc/000572.html).
But there's a wrinkle. The controller actions mapped to these URLs are protected by an authentication filter. In normal use, Tracks redirects an unauthenticated user to the login screen for session-based authentication.
I've added a secondary authentication check that looks for a valid username and password coming from HTTP_BASIC authentication.
To test out the new functionality, try this:
curl -H 'Accept: application/xml' --basic --user YOUR_TRACKS_USERNAME:YOUR_TRACKS_PASSWORD http://localhost:3000/projects/
curl -H 'Accept: application/xml' --basic --user YOUR_TRACKS_USERNAME:YOUR_TRACKS_PASSWORD http://localhost:3000/contexts/
curl -H 'Accept: application/xml' --basic --user YOUR_TRACKS_USERNAME:YOUR_TRACKS_PASSWORD http://localhost:3000/notes/
HTTP_BASIC sends passwords in plain text, so the use of https is encouraged.
I haven't tested this on a shared host yet, but Coda Hale, whose simple_http_auth inspired this solution and provided some copy and paste code for it (thanks, Coda!), has some notes about how to make it work in his plugin readme (http://svn.codahale.com/simple_http_auth/README). To wit, putting the following in .htaccess:
RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
My thinking on this architecture is as follows:
1) Follow the spirit of responds_to and DRY to leverage existing controller code for API functionality
2) Get away from using the user token for API interactions. Let's keep it for feeds, so it's basically a "lite" form of security for read-only feeds.
3) Keep Tracks in shape to adopt the simply_restful plugin being developed alongside Rails Edge
There's no real new functionality in this release that the existing API didn't provide (except for seeing your notes as XML, and somehow I don't think people are clamoring for that), but this work is an important step to being able to implement the types of API features people have been asking for.
While I was at it, I did some refactoring to the login_controller for readability and style.
Finally, I replaced the activity indicator graphic to work with the new navigation background color.
git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@251 a4c988fc-2ded-0310-b66e-134b36920a42
2006-06-04 07:07:42 +00:00
|
|
|
end
|
2005-06-18 13:46:03 +00:00
|
|
|
end
|
2005-08-08 01:54:05 +00:00
|
|
|
|
2005-06-18 13:46:03 +00:00
|
|
|
def show
|
2005-08-08 01:54:05 +00:00
|
|
|
@note = check_user_return_note
|
2005-06-18 13:46:03 +00:00
|
|
|
@page_title = "TRACKS::Note " + @note.id.to_s
|
|
|
|
|
end
|
2005-08-08 01:54:05 +00:00
|
|
|
|
2005-06-18 13:46:03 +00:00
|
|
|
# Add a new note to this project
|
|
|
|
|
#
|
2005-08-08 01:54:05 +00:00
|
|
|
def add
|
|
|
|
|
note = @user.notes.build
|
2006-05-13 13:32:39 +00:00
|
|
|
note.attributes = params["new_note"]
|
2005-06-18 13:46:03 +00:00
|
|
|
|
|
|
|
|
if note.save
|
2006-11-05 10:41:59 +00:00
|
|
|
render :partial => 'notes_summary', :object => note
|
2005-06-18 13:46:03 +00:00
|
|
|
else
|
2006-11-05 10:41:59 +00:00
|
|
|
render :text => ''
|
2005-06-18 13:46:03 +00:00
|
|
|
end
|
|
|
|
|
end
|
2005-08-08 01:54:05 +00:00
|
|
|
|
|
|
|
|
def delete
|
|
|
|
|
note = check_user_return_note
|
2005-06-18 13:46:03 +00:00
|
|
|
if note.destroy
|
2006-11-05 10:41:59 +00:00
|
|
|
render :text => ''
|
2005-06-18 13:46:03 +00:00
|
|
|
else
|
2006-11-05 10:41:59 +00:00
|
|
|
flash[:warning] = "Couldn't delete note \"#{note.id.to_s}\""
|
|
|
|
|
render :text => ''
|
2005-06-18 13:46:03 +00:00
|
|
|
end
|
|
|
|
|
end
|
2005-08-08 01:54:05 +00:00
|
|
|
|
|
|
|
|
def update
|
|
|
|
|
note = check_user_return_note
|
2006-05-13 13:32:39 +00:00
|
|
|
note.attributes = params["note"]
|
2005-06-18 13:46:03 +00:00
|
|
|
if note.save
|
2006-11-05 10:41:59 +00:00
|
|
|
render :partial => 'notes', :object => note
|
2005-06-18 13:46:03 +00:00
|
|
|
else
|
2006-11-05 10:47:51 +00:00
|
|
|
flash[:warning] = "Couldn't update note \"#{note.id.to_s}\""
|
2006-11-05 10:41:59 +00:00
|
|
|
render :text => ''
|
2005-06-18 13:46:03 +00:00
|
|
|
end
|
|
|
|
|
end
|
2005-08-08 01:54:05 +00:00
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
|
|
def check_user_return_note
|
2006-05-13 13:32:39 +00:00
|
|
|
note = Note.find_by_id( params['id'] )
|
2006-04-08 17:46:41 +00:00
|
|
|
if @user == note.user
|
2005-08-08 01:54:05 +00:00
|
|
|
return note
|
|
|
|
|
else
|
2006-11-05 10:41:59 +00:00
|
|
|
render :text => ''
|
2005-08-08 01:54:05 +00:00
|
|
|
end
|
|
|
|
|
end
|
2005-06-18 13:46:03 +00:00
|
|
|
end
|